OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/capturer/win/desktop.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace remoting { | |
12 | |
13 Desktop::Desktop(HDESK desktop, bool own) : desktop_(desktop), own_(own) { | |
14 } | |
15 | |
16 Desktop::~Desktop() { | |
17 if (own_ && desktop_ != NULL) { | |
18 if (!::CloseDesktop(desktop_)) { | |
19 LOG_GETLASTERROR(ERROR) | |
20 << "Failed to close the owned desktop handle"; | |
21 } | |
22 } | |
23 } | |
24 | |
25 bool Desktop::GetName(string16* desktop_name_out) const { | |
26 if (desktop_ == NULL) | |
27 return false; | |
28 | |
29 DWORD length; | |
30 CHECK(!GetUserObjectInformationW(desktop_, UOI_NAME, NULL, 0, &length)); | |
31 CHECK(GetLastError() == ERROR_INSUFFICIENT_BUFFER); | |
32 | |
33 length /= sizeof(char16); | |
34 std::vector<char16> buffer(length); | |
35 if (!GetUserObjectInformationW(desktop_, UOI_NAME, &buffer[0], | |
36 length * sizeof(char16), &length)) { | |
37 LOG_GETLASTERROR(ERROR) | |
38 << "Failed to query the desktop name"; | |
39 return false; | |
40 } | |
41 | |
42 desktop_name_out->assign(&buffer[0], length / sizeof(char16)); | |
43 return true; | |
44 } | |
45 | |
46 bool Desktop::IsSame(const Desktop& other) const { | |
47 string16 name; | |
48 if (!GetName(&name)) | |
49 return false; | |
50 | |
51 string16 other_name; | |
52 if (!other.GetName(&other_name)) | |
53 return false; | |
54 | |
55 return name == other_name; | |
56 } | |
57 | |
58 bool Desktop::SetThreadDesktop() const { | |
59 if (!::SetThreadDesktop(desktop_)) { | |
60 LOG_GETLASTERROR(ERROR) | |
61 << "Failed to assign the desktop to the current thread"; | |
62 return false; | |
63 } | |
64 | |
65 return true; | |
66 } | |
67 | |
68 scoped_ptr<Desktop> Desktop::GetDesktop(const wchar_t* desktop_name) { | |
69 ACCESS_MASK desired_access = | |
70 DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | | |
71 DESKTOP_HOOKCONTROL | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS | | |
72 DESKTOP_SWITCHDESKTOP | GENERIC_WRITE; | |
73 HDESK desktop = OpenDesktop(desktop_name, 0, FALSE, desired_access); | |
74 if (desktop == NULL) { | |
75 LOG_GETLASTERROR(ERROR) | |
76 << "Failed to open the desktop '" << desktop_name << "'"; | |
77 return scoped_ptr<Desktop>(); | |
78 } | |
79 | |
80 return scoped_ptr<Desktop>(new Desktop(desktop, true)); | |
81 } | |
82 | |
83 scoped_ptr<Desktop> Desktop::GetInputDesktop() { | |
84 HDESK desktop = OpenInputDesktop( | |
85 0, FALSE, GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE); | |
86 if (desktop == NULL) | |
87 return scoped_ptr<Desktop>(); | |
88 | |
89 return scoped_ptr<Desktop>(new Desktop(desktop, true)); | |
90 } | |
91 | |
92 scoped_ptr<Desktop> Desktop::GetThreadDesktop() { | |
93 HDESK desktop = ::GetThreadDesktop(GetCurrentThreadId()); | |
94 if (desktop == NULL) { | |
95 LOG_GETLASTERROR(ERROR) | |
96 << "Failed to retrieve the handle of the desktop assigned to " | |
97 "the current thread"; | |
98 return scoped_ptr<Desktop>(); | |
99 } | |
100 | |
101 return scoped_ptr<Desktop>(new Desktop(desktop, false)); | |
102 } | |
103 | |
104 } // namespace remoting | |
OLD | NEW |