Added GetState, GetManagedProperties, CreateNetwork methods to WiFiService.
[chromium-blink-merge.git] / sandbox / win / src / window.cc
blobd8de967272661360a9309ee6bf571c4bfed4b47d
1 // Copyright (c) 2011 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.
5 #include "sandbox/win/src/window.h"
7 #include <aclapi.h>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
12 namespace {
14 // Gets the security attributes of a window object referenced by |handle|. The
15 // lpSecurityDescriptor member of the SECURITY_ATTRIBUTES parameter returned
16 // must be freed using LocalFree by the caller.
17 bool GetSecurityAttributes(HANDLE handle, SECURITY_ATTRIBUTES* attributes) {
18 attributes->bInheritHandle = FALSE;
19 attributes->nLength = sizeof(SECURITY_ATTRIBUTES);
21 PACL dacl = NULL;
22 DWORD result = ::GetSecurityInfo(handle, SE_WINDOW_OBJECT,
23 DACL_SECURITY_INFORMATION, NULL, NULL, &dacl,
24 NULL, &attributes->lpSecurityDescriptor);
25 if (ERROR_SUCCESS == result)
26 return true;
28 return false;
33 namespace sandbox {
35 ResultCode CreateAltWindowStation(HWINSTA* winsta) {
36 // Get the security attributes from the current window station; we will
37 // use this as the base security attributes for the new window station.
38 SECURITY_ATTRIBUTES attributes = {0};
39 if (!GetSecurityAttributes(::GetProcessWindowStation(), &attributes)) {
40 return SBOX_ERROR_CANNOT_CREATE_WINSTATION;
43 // Create the window station using NULL for the name to ask the os to
44 // generate it.
45 // TODO(nsylvain): don't ask for WINSTA_ALL_ACCESS if we don't need to.
46 *winsta = ::CreateWindowStationW(NULL, 0, WINSTA_ALL_ACCESS, &attributes);
47 LocalFree(attributes.lpSecurityDescriptor);
49 if (*winsta)
50 return SBOX_ALL_OK;
52 return SBOX_ERROR_CANNOT_CREATE_WINSTATION;
55 ResultCode CreateAltDesktop(HWINSTA winsta, HDESK* desktop) {
56 std::wstring desktop_name = L"sbox_alternate_desktop_";
58 // Append the current PID to the desktop name.
59 wchar_t buffer[16];
60 _snwprintf_s(buffer, sizeof(buffer) / sizeof(wchar_t), L"0x%X",
61 ::GetCurrentProcessId());
62 desktop_name += buffer;
64 // Get the security attributes from the current desktop, we will use this as
65 // the base security attributes for the new desktop.
66 SECURITY_ATTRIBUTES attributes = {0};
67 if (!GetSecurityAttributes(GetThreadDesktop(GetCurrentThreadId()),
68 &attributes)) {
69 return SBOX_ERROR_CANNOT_CREATE_DESKTOP;
72 // Back up the current window station, in case we need to switch it.
73 HWINSTA current_winsta = ::GetProcessWindowStation();
75 if (winsta) {
76 // We need to switch to the alternate window station before creating the
77 // desktop.
78 if (!::SetProcessWindowStation(winsta)) {
79 ::LocalFree(attributes.lpSecurityDescriptor);
80 return SBOX_ERROR_CANNOT_CREATE_DESKTOP;
84 // Create the destkop.
85 // TODO(nsylvain): don't ask for GENERIC_ALL if we don't need to.
86 *desktop = ::CreateDesktop(desktop_name.c_str(), NULL, NULL, 0, GENERIC_ALL,
87 &attributes);
88 ::LocalFree(attributes.lpSecurityDescriptor);
90 if (winsta) {
91 // Revert to the right window station.
92 if (!::SetProcessWindowStation(current_winsta)) {
93 return SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION;
97 if (*desktop)
98 return SBOX_ALL_OK;
100 return SBOX_ERROR_CANNOT_CREATE_DESKTOP;
103 std::wstring GetWindowObjectName(HANDLE handle) {
104 // Get the size of the name.
105 DWORD size = 0;
106 ::GetUserObjectInformation(handle, UOI_NAME, NULL, 0, &size);
108 if (!size) {
109 NOTREACHED();
110 return std::wstring();
113 // Create the buffer that will hold the name.
114 scoped_ptr<wchar_t[]> name_buffer(new wchar_t[size]);
116 // Query the name of the object.
117 if (!::GetUserObjectInformation(handle, UOI_NAME, name_buffer.get(), size,
118 &size)) {
119 NOTREACHED();
120 return std::wstring();
123 return std::wstring(name_buffer.get());
126 std::wstring GetFullDesktopName(HWINSTA winsta, HDESK desktop) {
127 if (!desktop) {
128 NOTREACHED();
129 return std::wstring();
132 std::wstring name;
133 if (winsta) {
134 name = GetWindowObjectName(winsta);
135 name += L'\\';
138 name += GetWindowObjectName(desktop);
139 return name;
142 } // namespace sandbox