Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: ui/base/win/shell.cc

Issue 352393002: Be explicit about target type in platform_util::OpenItem() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with trunk Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ui/base/win/shell.h ('K') | « ui/base/win/shell.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/win/shell.h" 5 #include "ui/base/win/shell.h"
6 6
7 #include <dwmapi.h> 7 #include <dwmapi.h>
8 #include <shlobj.h> // Must be before propkey. 8 #include <shlobj.h> // Must be before propkey.
9 #include <propkey.h> 9 #include <propkey.h>
10 #include <shellapi.h> 10 #include <shellapi.h>
11 11
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/debug/alias.h" 13 #include "base/debug/alias.h"
14 #include "base/files/file.h"
14 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
15 #include "base/native_library.h" 16 #include "base/native_library.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/threading/thread_restrictions.h"
17 #include "base/win/metro.h" 19 #include "base/win/metro.h"
18 #include "base/win/scoped_comptr.h" 20 #include "base/win/scoped_comptr.h"
19 #include "base/win/win_util.h" 21 #include "base/win/win_util.h"
20 #include "base/win/windows_version.h" 22 #include "base/win/windows_version.h"
21 #include "ui/base/ui_base_switches.h" 23 #include "ui/base/ui_base_switches.h"
22 24
23 namespace ui { 25 namespace ui {
24 namespace win { 26 namespace win {
25 27
26 namespace { 28 namespace {
27 29
28 // Show the Windows "Open With" dialog box to ask the user to pick an app to 30 // Default ShellExecuteEx flags used with the "openas" verb.
29 // open the file with. 31 //
30 bool OpenItemWithExternalApp(const base::string16& full_path) { 32 // SEE_MASK_NOASYNC is specified so that ShellExecuteEx can be invoked from a
31 SHELLEXECUTEINFO sei = { sizeof(sei) }; 33 // thread whose message loop may not wait around long enough for the
32 sei.fMask = SEE_MASK_FLAG_DDEWAIT; 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;
37
38 // Default ShellExecuteEx flags used with the "explore", "open" or default verb.
39 //
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;
46
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;
33 sei.nShow = SW_SHOWNORMAL; 56 sei.nShow = SW_SHOWNORMAL;
34 sei.lpVerb = L"openas"; 57 sei.lpVerb = (verb.empty() ? NULL : verb.c_str());
sky 2015/03/06 18:21:33 nullptr evey where?
asanka 2015/03/06 21:09:40 Done.
35 sei.lpFile = full_path.c_str(); 58 sei.lpFile = path.c_str();
36 return (TRUE == ::ShellExecuteExW(&sei)); 59 sei.lpDirectory =
60 (working_directory.empty() ? NULL : working_directory.c_str());
61 sei.lpParameters = (args.empty() ? NULL : args.c_str());
62 return ::ShellExecuteExW(&sei) ? ERROR_SUCCESS : ::GetLastError();
37 } 63 }
38 64
39 } // namespace 65 } // namespace
40 66
41 bool OpenAnyViaShell(const base::string16& full_path, 67 bool OpenAnyViaShell(const base::string16& full_path,
42 const base::string16& directory, 68 const base::string16& directory,
43 const base::string16& args, 69 const base::string16& args,
44 DWORD mask) { 70 DWORD mask) {
45 SHELLEXECUTEINFO sei = { sizeof(sei) }; 71 DWORD open_result =
46 sei.fMask = mask; 72 InvokeShellExecute(full_path, directory, args, base::string16(), mask);
47 sei.nShow = SW_SHOWNORMAL; 73 if (open_result == ERROR_SUCCESS)
48 sei.lpFile = full_path.c_str();
49 sei.lpDirectory = directory.c_str();
50 if (!args.empty())
51 sei.lpParameters = args.c_str();
52
53 if (::ShellExecuteExW(&sei))
54 return true; 74 return true;
55 if (::GetLastError() == ERROR_NO_ASSOCIATION) 75 // Show the Windows "Open With" dialog box to ask the user to pick an app to
56 return OpenItemWithExternalApp(full_path); 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;
57 return false; 81 return false;
58 } 82 }
59 83
60 bool OpenItemViaShell(const base::FilePath& full_path) { 84 bool OpenFileViaShell(const base::FilePath& full_path) {
61 return OpenAnyViaShell(full_path.value(), full_path.DirName().value(), 85 return OpenAnyViaShell(full_path.value(), full_path.DirName().value(),
62 base::string16(), 0); 86 base::string16(), kDefaultOpenFlags);
87 }
88
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;
63 } 97 }
64 98
65 bool PreventWindowFromPinning(HWND hwnd) { 99 bool PreventWindowFromPinning(HWND hwnd) {
66 // This functionality is only available on Win7+. It also doesn't make sense 100 // This functionality is only available on Win7+. It also doesn't make sense
67 // to do this for Chrome Metro. 101 // to do this for Chrome Metro.
68 if (base::win::GetVersion() < base::win::VERSION_WIN7 || 102 if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
69 base::win::IsMetroProcess()) 103 base::win::IsMetroProcess())
70 return false; 104 return false;
71 base::win::ScopedComPtr<IPropertyStore> pps; 105 base::win::ScopedComPtr<IPropertyStore> pps;
72 HRESULT result = SHGetPropertyStoreForWindow( 106 HRESULT result = SHGetPropertyStoreForWindow(
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // at the same feature level. See bug 426573. 186 // at the same feature level. See bug 426573.
153 if (base::win::GetVersion() < base::win::VERSION_WIN7) 187 if (base::win::GetVersion() < base::win::VERSION_WIN7)
154 return false; 188 return false;
155 // If composition is not enabled, we behave like on XP. 189 // If composition is not enabled, we behave like on XP.
156 BOOL enabled = FALSE; 190 BOOL enabled = FALSE;
157 return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled; 191 return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled;
158 } 192 }
159 193
160 } // namespace win 194 } // namespace win
161 } // namespace ui 195 } // namespace ui
OLDNEW
« ui/base/win/shell.h ('K') | « ui/base/win/shell.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698