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