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

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: Address TOCTOU races on Windows. 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
« chrome/browser/platform_util_mac.mm ('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 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 if (!verb.empty())
35 sei.lpFile = full_path.c_str(); 58 sei.lpVerb = verb.c_str();
36 return (TRUE == ::ShellExecuteExW(&sei)); 59 sei.lpFile = path.c_str();
60 if (!directory.empty())
61 sei.lpDirectory = directory.c_str();
62 if (!args.empty())
63 sei.lpParameters = args.c_str();
64 return ::ShellExecuteExW(&sei) ? ERROR_SUCCESS : ::GetLastError();
37 } 65 }
38 66
39 } // namespace 67 } // namespace
40 68
41 bool OpenAnyViaShell(const base::string16& full_path, 69 bool OpenAnyViaShell(const base::string16& full_path,
42 const base::string16& directory, 70 const base::string16& directory,
43 const base::string16& args, 71 const base::string16& args,
44 DWORD mask) { 72 DWORD mask) {
45 SHELLEXECUTEINFO sei = { sizeof(sei) }; 73 DWORD open_result =
46 sei.fMask = mask; 74 InvokeShellExecute(full_path, directory, args, base::string16(), mask);
47 sei.nShow = SW_SHOWNORMAL; 75 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; 76 return true;
55 if (::GetLastError() == ERROR_NO_ASSOCIATION) 77 // Show the Windows "Open With" dialog box to ask the user to pick an app to
56 return OpenItemWithExternalApp(full_path); 78 // open the file with. Note that we are not forwarding |args| for the "openas"
79 // call since the target application is nolonger known at this point.
80 if (open_result == ERROR_NO_ASSOCIATION)
81 return InvokeShellExecute(full_path, directory, base::string16(), L"openas",
82 kDefaultOpenAsFlags) == ERROR_SUCCESS;
57 return false; 83 return false;
58 } 84 }
59 85
60 bool OpenItemViaShell(const base::FilePath& full_path) { 86 bool OpenFileViaShell(const base::FilePath& full_path) {
61 return OpenAnyViaShell(full_path.value(), full_path.DirName().value(), 87 return OpenAnyViaShell(full_path.value(), full_path.DirName().value(),
62 base::string16(), 0); 88 base::string16(), kDefaultOpenFlags);
89 }
90
91 bool OpenFolderViaShell(const base::FilePath& full_path) {
92 // Only invoke the "explore" verb. This will fail if |full_path| is not a
93 // directory. The resulting error does not cause UI due to the
94 // SEE_MASK_FLAG_NO_UI flag in kDefaultOpenFlags.
95 return InvokeShellExecute(full_path.value(), full_path.value(),
96 base::string16(), L"explore",
97 kDefaultOpenFlags) == ERROR_SUCCESS;
63 } 98 }
64 99
65 bool PreventWindowFromPinning(HWND hwnd) { 100 bool PreventWindowFromPinning(HWND hwnd) {
66 // This functionality is only available on Win7+. It also doesn't make sense 101 // This functionality is only available on Win7+. It also doesn't make sense
67 // to do this for Chrome Metro. 102 // to do this for Chrome Metro.
68 if (base::win::GetVersion() < base::win::VERSION_WIN7 || 103 if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
69 base::win::IsMetroProcess()) 104 base::win::IsMetroProcess())
70 return false; 105 return false;
71 base::win::ScopedComPtr<IPropertyStore> pps; 106 base::win::ScopedComPtr<IPropertyStore> pps;
72 HRESULT result = SHGetPropertyStoreForWindow( 107 HRESULT result = SHGetPropertyStoreForWindow(
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // at the same feature level. See bug 426573. 187 // at the same feature level. See bug 426573.
153 if (base::win::GetVersion() < base::win::VERSION_WIN7) 188 if (base::win::GetVersion() < base::win::VERSION_WIN7)
154 return false; 189 return false;
155 // If composition is not enabled, we behave like on XP. 190 // If composition is not enabled, we behave like on XP.
156 BOOL enabled = FALSE; 191 BOOL enabled = FALSE;
157 return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled; 192 return SUCCEEDED(DwmIsCompositionEnabled(&enabled)) && enabled;
158 } 193 }
159 194
160 } // namespace win 195 } // namespace win
161 } // namespace ui 196 } // namespace ui
OLDNEW
« chrome/browser/platform_util_mac.mm ('K') | « ui/base/win/shell.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698