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 #include "modules/desktop_capture/win/scoped_thread_desktop.h"
12 
13 #include "modules/desktop_capture/win/desktop.h"
14 
15 namespace webrtc {
16 
ScopedThreadDesktop()17 ScopedThreadDesktop::ScopedThreadDesktop()
18     : initial_(Desktop::GetThreadDesktop()) {}
19 
~ScopedThreadDesktop()20 ScopedThreadDesktop::~ScopedThreadDesktop() {
21   Revert();
22 }
23 
IsSame(const Desktop & desktop)24 bool ScopedThreadDesktop::IsSame(const Desktop& desktop) {
25   if (assigned_.get() != NULL) {
26     return assigned_->IsSame(desktop);
27   } else {
28     return initial_->IsSame(desktop);
29   }
30 }
31 
Revert()32 void ScopedThreadDesktop::Revert() {
33   if (assigned_.get() != NULL) {
34     initial_->SetThreadDesktop();
35     assigned_.reset();
36   }
37 }
38 
SetThreadDesktop(Desktop * desktop)39 bool ScopedThreadDesktop::SetThreadDesktop(Desktop* desktop) {
40   Revert();
41 
42   std::unique_ptr<Desktop> scoped_desktop(desktop);
43 
44   if (initial_->IsSame(*desktop))
45     return true;
46 
47   if (!desktop->SetThreadDesktop())
48     return false;
49 
50   assigned_.reset(scoped_desktop.release());
51   return true;
52 }
53 
54 }  // namespace webrtc
55