Update V8 to version 4.6.52.
[chromium-blink-merge.git] / ui / base / win / shell.cc
blob2a5bd29798da5b9ec878477e1007f07a87eb5c08
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.
5 #include "ui/base/win/shell.h"
7 #include <dwmapi.h>
8 #include <shlobj.h> // Must be before propkey.
9 #include <propkey.h>
10 #include <shellapi.h>
12 #include "base/command_line.h"
13 #include "base/debug/alias.h"
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/native_library.h"
17 #include "base/strings/string_util.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "base/win/metro.h"
20 #include "base/win/scoped_comptr.h"
21 #include "base/win/win_util.h"
22 #include "base/win/windows_version.h"
23 #include "ui/base/ui_base_switches.h"
25 namespace ui {
26 namespace win {
28 namespace {
30 // Default ShellExecuteEx flags used with the "openas" verb.
32 // SEE_MASK_NOASYNC is specified so that ShellExecuteEx can be invoked from a
33 // thread whose message loop may not wait around long enough for the
34 // asynchronous tasks initiated by ShellExecuteEx to complete. Using this flag
35 // causes ShellExecuteEx() to block until these tasks complete.
36 const DWORD kDefaultOpenAsFlags = SEE_MASK_NOASYNC;
38 // Default ShellExecuteEx flags used with the "explore", "open" or default verb.
40 // See kDefaultOpenFlags for description SEE_MASK_NOASYNC flag.
41 // SEE_MASK_FLAG_NO_UI is used to suppress any error message boxes that might be
42 // displayed if there is an error in opening the file. Failure in invoking the
43 // "open" actions result in invocation of the "saveas" verb, making the error
44 // dialog superfluous.
45 const DWORD kDefaultOpenFlags = SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI;
47 // Invokes ShellExecuteExW() with the given parameters.
48 DWORD InvokeShellExecute(const base::string16 path,
49 const base::string16 working_directory,
50 const base::string16 args,
51 const base::string16 verb,
52 DWORD mask) {
53 base::ThreadRestrictions::AssertIOAllowed();
54 SHELLEXECUTEINFO sei = {sizeof(sei)};
55 sei.fMask = mask;
56 sei.nShow = SW_SHOWNORMAL;
57 sei.lpVerb = (verb.empty() ? nullptr : verb.c_str());
58 sei.lpFile = path.c_str();
59 sei.lpDirectory =
60 (working_directory.empty() ? nullptr : working_directory.c_str());
61 sei.lpParameters = (args.empty() ? nullptr : args.c_str());
62 return ::ShellExecuteExW(&sei) ? ERROR_SUCCESS : ::GetLastError();
65 } // namespace
67 bool OpenAnyViaShell(const base::string16& full_path,
68 const base::string16& directory,
69 const base::string16& args,
70 DWORD mask) {
71 DWORD open_result =
72 InvokeShellExecute(full_path, directory, args, base::string16(), mask);
73 if (open_result == ERROR_SUCCESS)
74 return true;
75 // Show the Windows "Open With" dialog box to ask the user to pick an app to
76 // open the file with. Note that we are not forwarding |args| for the "openas"
77 // call since the target application is nolonger known at this point.
78 if (open_result == ERROR_NO_ASSOCIATION)
79 return InvokeShellExecute(full_path, directory, base::string16(), L"openas",
80 kDefaultOpenAsFlags) == ERROR_SUCCESS;
81 return false;
84 bool OpenFileViaShell(const base::FilePath& full_path) {
85 return OpenAnyViaShell(full_path.value(), full_path.DirName().value(),
86 base::string16(), kDefaultOpenFlags);
89 bool OpenFolderViaShell(const base::FilePath& full_path) {
90 // The "explore" verb causes the folder at |full_path| to be displayed in a
91 // file browser. This will fail if |full_path| is not a directory. The
92 // resulting error does not cause UI due to the SEE_MASK_FLAG_NO_UI flag in
93 // kDefaultOpenFlags.
94 return InvokeShellExecute(full_path.value(), full_path.value(),
95 base::string16(), L"explore",
96 kDefaultOpenFlags) == ERROR_SUCCESS;
99 bool PreventWindowFromPinning(HWND hwnd) {
100 // This functionality is only available on Win7+. It also doesn't make sense
101 // to do this for Chrome Metro.
102 if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
103 base::win::IsMetroProcess())
104 return false;
105 base::win::ScopedComPtr<IPropertyStore> pps;
106 HRESULT result = SHGetPropertyStoreForWindow(
107 hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive()));
108 if (FAILED(result))
109 return false;
111 return base::win::SetBooleanValueForPropertyStore(
112 pps.get(), PKEY_AppUserModel_PreventPinning, true);
115 // TODO(calamity): investigate moving this out of the UI thread as COM
116 // operations may spawn nested message loops which can cause issues.
117 void SetAppDetailsForWindow(const base::string16& app_id,
118 const base::string16& app_icon,
119 const base::string16& relaunch_command,
120 const base::string16& relaunch_display_name,
121 HWND hwnd) {
122 // This functionality is only available on Win7+. It also doesn't make sense
123 // to do this for Chrome Metro.
124 if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
125 base::win::IsMetroProcess())
126 return;
127 base::win::ScopedComPtr<IPropertyStore> pps;
128 HRESULT result = SHGetPropertyStoreForWindow(
129 hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive()));
130 if (S_OK == result) {
131 if (!app_id.empty())
132 base::win::SetAppIdForPropertyStore(pps.get(), app_id.c_str());
133 if (!app_icon.empty()) {
134 base::win::SetStringValueForPropertyStore(
135 pps.get(), PKEY_AppUserModel_RelaunchIconResource, app_icon.c_str());
137 if (!relaunch_command.empty()) {
138 base::win::SetStringValueForPropertyStore(
139 pps.get(), PKEY_AppUserModel_RelaunchCommand,
140 relaunch_command.c_str());
142 if (!relaunch_display_name.empty()) {
143 base::win::SetStringValueForPropertyStore(
144 pps.get(), PKEY_AppUserModel_RelaunchDisplayNameResource,
145 relaunch_display_name.c_str());
150 void SetAppIdForWindow(const base::string16& app_id, HWND hwnd) {
151 SetAppDetailsForWindow(app_id,
152 base::string16(),
153 base::string16(),
154 base::string16(),
155 hwnd);
158 void SetAppIconForWindow(const base::string16& app_icon, HWND hwnd) {
159 SetAppDetailsForWindow(base::string16(),
160 app_icon,
161 base::string16(),
162 base::string16(),
163 hwnd);
166 void SetRelaunchDetailsForWindow(const base::string16& relaunch_command,
167 const base::string16& display_name,
168 HWND hwnd) {
169 SetAppDetailsForWindow(base::string16(),
170 base::string16(),
171 relaunch_command,
172 display_name,
173 hwnd);
176 bool IsAeroGlassEnabled() {
177 // For testing in Win8 (where it is not possible to disable composition) the
178 // user can specify this command line switch to mimic the behavior. In this
179 // mode, cross-HWND transparency is not supported and various types of
180 // widgets fallback to more simplified rendering behavior.
181 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
182 switches::kDisableDwmComposition))
183 return false;
185 // Technically Aero glass works in Vista but we want to put XP and Vista
186 // at the same feature level. See bug 426573.
187 if (base::win::GetVersion() < base::win::VERSION_WIN7)
188 return false;
189 // If composition is not enabled, we behave like on XP.
190 BOOL enabled = FALSE;
191 return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled;
194 } // namespace win
195 } // namespace ui