xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/scoped_thread_desktop.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_WIN_SCOPED_THREAD_DESKTOP_H_
12 #define MODULES_DESKTOP_CAPTURE_WIN_SCOPED_THREAD_DESKTOP_H_
13 
14 #include <windows.h>
15 
16 #include <memory>
17 
18 #include "rtc_base/system/rtc_export.h"
19 
20 namespace webrtc {
21 
22 class Desktop;
23 
24 class RTC_EXPORT ScopedThreadDesktop {
25  public:
26   ScopedThreadDesktop();
27   ~ScopedThreadDesktop();
28 
29   ScopedThreadDesktop(const ScopedThreadDesktop&) = delete;
30   ScopedThreadDesktop& operator=(const ScopedThreadDesktop&) = delete;
31 
32   // Returns true if `desktop` has the same desktop name as the currently
33   // assigned desktop (if assigned) or as the initial desktop (if not assigned).
34   // Returns false in any other case including failing Win32 APIs and
35   // uninitialized desktop handles.
36   bool IsSame(const Desktop& desktop);
37 
38   // Reverts the calling thread to use the initial desktop.
39   void Revert();
40 
41   // Assigns `desktop` to be the calling thread. Returns true if the thread has
42   // been switched to `desktop` successfully. Takes ownership of `desktop`.
43   bool SetThreadDesktop(Desktop* desktop);
44 
45  private:
46   // The desktop handle assigned to the calling thread by Set
47   std::unique_ptr<Desktop> assigned_;
48 
49   // The desktop handle assigned to the calling thread at creation.
50   std::unique_ptr<Desktop> initial_;
51 };
52 
53 }  // namespace webrtc
54 
55 #endif  // MODULES_DESKTOP_CAPTURE_WIN_SCOPED_THREAD_DESKTOP_H_
56