blob: aef0d041ff689e6dc3ba8f668c14829eff030731 [file] [log] [blame]
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +00001/*
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.org3d34f662013-06-04 18:51:23 +000013#include "webrtc/modules/desktop_capture/win/desktop.h"
14
15namespace webrtc {
16
17ScopedThreadDesktop::ScopedThreadDesktop()
18 : initial_(Desktop::GetThreadDesktop()) {
19}
20
21ScopedThreadDesktop::~ScopedThreadDesktop() {
22 Revert();
23}
24
25bool 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
33void ScopedThreadDesktop::Revert() {
34 if (assigned_.get() != NULL) {
35 initial_->SetThreadDesktop();
36 assigned_.reset();
37 }
38}
39
40bool ScopedThreadDesktop::SetThreadDesktop(Desktop* desktop) {
41 Revert();
42
kwiberg2bb3afa2016-03-16 15:58:08 -070043 std::unique_ptr<Desktop> scoped_desktop(desktop);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000044
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