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