OLD | NEW |
---|---|
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 "chrome/browser/shell_integration.h" | 5 #include "chrome/browser/shell_integration.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <shlwapi.h> | 8 #include <shlwapi.h> |
9 #include <shobjidl.h> | 9 #include <shobjidl.h> |
10 #include <propkey.h> // Needs to come after shobjidl.h. | 10 #include <propkey.h> // Needs to come after shobjidl.h. |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 #include <stdint.h> | 12 #include <stdint.h> |
13 | 13 |
14 #include <vector> | |
15 | |
14 #include "base/bind.h" | 16 #include "base/bind.h" |
15 #include "base/command_line.h" | 17 #include "base/command_line.h" |
16 #include "base/files/file_enumerator.h" | 18 #include "base/files/file_enumerator.h" |
17 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
18 #include "base/macros.h" | 20 #include "base/macros.h" |
21 #include "base/memory/ptr_util.h" | |
22 #include "base/memory/weak_ptr.h" | |
19 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
20 #include "base/path_service.h" | 24 #include "base/path_service.h" |
21 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
22 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
23 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
28 #include "base/timer/timer.h" | |
24 #include "base/win/registry.h" | 29 #include "base/win/registry.h" |
25 #include "base/win/scoped_comptr.h" | 30 #include "base/win/scoped_comptr.h" |
26 #include "base/win/scoped_propvariant.h" | 31 #include "base/win/scoped_propvariant.h" |
27 #include "base/win/shortcut.h" | 32 #include "base/win/shortcut.h" |
28 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
29 #include "chrome/browser/policy/policy_path_parser.h" | 34 #include "chrome/browser/policy/policy_path_parser.h" |
30 #include "chrome/browser/web_applications/web_app.h" | 35 #include "chrome/browser/web_applications/web_app.h" |
31 #include "chrome/common/chrome_constants.h" | 36 #include "chrome/common/chrome_constants.h" |
32 #include "chrome/common/chrome_paths_internal.h" | 37 #include "chrome/common/chrome_paths_internal.h" |
33 #include "chrome/common/chrome_switches.h" | 38 #include "chrome/common/chrome_switches.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 case ShellUtil::NOT_DEFAULT: | 213 case ShellUtil::NOT_DEFAULT: |
209 return DefaultWebClientState::NOT_DEFAULT; | 214 return DefaultWebClientState::NOT_DEFAULT; |
210 case ShellUtil::IS_DEFAULT: | 215 case ShellUtil::IS_DEFAULT: |
211 return DefaultWebClientState::IS_DEFAULT; | 216 return DefaultWebClientState::IS_DEFAULT; |
212 default: | 217 default: |
213 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 218 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
214 return DefaultWebClientState::UNKNOWN_DEFAULT; | 219 return DefaultWebClientState::UNKNOWN_DEFAULT; |
215 } | 220 } |
216 } | 221 } |
217 | 222 |
223 bool IsSetAsDefaultUsingSystemSettings() { | |
224 return base::win::GetVersion() >= base::win::VERSION_WIN10; | |
225 } | |
226 | |
227 // There is no way to make sure the user is done with the system settings, but a | |
228 // signal that the interaction is finished is needed for UMA. A timer of 2 | |
229 // minutes is used as a substitute. The registry keys for the protocol | |
230 // association with an app are also monitored to signal the end of the | |
231 // interaction early when it is clear that the user made a choice (e.g. http | |
232 // and https for default browser). | |
233 // | |
234 // This helper class manages both the timer and the registry watchers and makes | |
235 // sure the callback for the end of the settings interaction is only run once. | |
236 // This class also manages its own lifetime. | |
237 class OpenSystemSettingsHelper { | |
238 public: | |
239 // Begin the monitoring and will call |on_finished_callback| when done. | |
240 // Takes in a null-terminated array of |protocols| whose registry keys must be | |
241 // watched. | |
242 static void Begin(const wchar_t* const protocols[], | |
243 const base::Closure& on_finished_callback) { | |
244 new OpenSystemSettingsHelper(protocols, on_finished_callback); | |
245 } | |
246 | |
247 private: | |
248 OpenSystemSettingsHelper(const wchar_t* const protocols[], | |
249 const base::Closure& on_finished_callback) | |
250 : on_finished_callback_(on_finished_callback), weak_ptr_factory_(this) { | |
251 static const wchar_t kUrlAssociationFormat[] = | |
252 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" | |
253 L"%ls\\UserChoice"; | |
254 | |
255 int i = 0; | |
grt (UTC plus 2)
2016/03/31 19:08:12
you could get rid of |i| by scanning the array:
Patrick Monette
2016/04/05 20:04:55
Done.
| |
256 while (protocols[i] != nullptr) { | |
257 AddRegistryKeyWatcher( | |
258 base::StringPrintf(kUrlAssociationFormat, protocols[i]).c_str()); | |
259 i++; | |
260 } | |
261 // Only the watchers that were succesfully initialized are counted. | |
262 registry_watcher_count_ = registry_key_watchers_.size(); | |
263 | |
264 timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(2), | |
265 base::Bind(&OpenSystemSettingsHelper::ConcludeInteraction, | |
266 weak_ptr_factory_.GetWeakPtr())); | |
267 } | |
268 | |
269 // Called when a change is detected on one of the registry keys being watched. | |
270 // Note: All types of modification to the registry key will trigger this | |
271 // function even if the value change is the only one that matters. This | |
272 // is good enough for now. | |
273 void OnRegistryKeyChanged() { | |
274 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
275 | |
276 // Make sure all the registry watchers have fired. | |
277 if (--registry_watcher_count_ == 0) | |
278 ConcludeInteraction(); | |
279 } | |
280 | |
281 // Ends the monitoring with the system settings. Will call | |
282 // |on_finished_callback_| and then dispose of this class instance to make | |
283 // sure the callback won't get called subsequently. | |
284 void ConcludeInteraction() { | |
285 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
286 | |
grt (UTC plus 2)
2016/03/31 19:08:12
how about UMA here so we know if the interaction i
Patrick Monette
2016/04/05 20:04:55
Done.
| |
287 on_finished_callback_.Run(); | |
288 delete this; | |
289 } | |
290 | |
291 // Helper function to create a registry watcher for a given |key_path|. Do | |
292 // nothing on initialization failure. | |
293 void AddRegistryKeyWatcher(const wchar_t* key_path) { | |
294 auto reg_key = make_scoped_ptr( | |
295 new base::win::RegKey(HKEY_CURRENT_USER, key_path, KEY_NOTIFY)); | |
296 | |
297 if (reg_key->Valid() && | |
298 reg_key->StartWatching( | |
299 base::Bind(&OpenSystemSettingsHelper::OnRegistryKeyChanged, | |
300 weak_ptr_factory_.GetWeakPtr()))) { | |
301 registry_key_watchers_.push_back(std::move(reg_key)); | |
302 } | |
303 } | |
304 | |
305 // The function to call when the interaction with the system settings is | |
306 // finished. | |
307 base::Closure on_finished_callback_; | |
308 | |
309 // The number of time the registry key watchers must fire. | |
310 int registry_watcher_count_ = 0; | |
311 | |
312 // There can be multiple registry key watchers as some settings modify | |
313 // multiple protocol associations. e.g. Changing the default browser modifies | |
314 // the http and https associations. | |
315 std::vector<scoped_ptr<base::win::RegKey>> registry_key_watchers_; | |
316 | |
317 base::OneShotTimer timer_; | |
318 | |
319 // Weak ptrs are used to bind this class to the callbacks of the timer and the | |
320 // registry watcher. This makes it possible to self-delete after one of the | |
321 // callbacks is executed to cancel the remaining ones. | |
322 base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_; | |
323 | |
324 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper); | |
325 }; | |
326 | |
218 } // namespace | 327 } // namespace |
219 | 328 |
220 bool SetAsDefaultBrowser() { | 329 bool SetAsDefaultBrowser() { |
221 base::FilePath chrome_exe; | 330 base::FilePath chrome_exe; |
222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 331 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
223 LOG(ERROR) << "Error getting app exe path"; | 332 LOG(ERROR) << "Error getting app exe path"; |
224 return false; | 333 return false; |
225 } | 334 } |
226 | 335 |
227 // From UI currently we only allow setting default browser for current user. | 336 // From UI currently we only allow setting default browser for current user. |
(...skipping 18 matching lines...) Expand all Loading... | |
246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 355 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { | 356 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { |
248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; | 357 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
249 return false; | 358 return false; |
250 } | 359 } |
251 | 360 |
252 VLOG(1) << "Set-default-browser Windows UI completed."; | 361 VLOG(1) << "Set-default-browser Windows UI completed."; |
253 return true; | 362 return true; |
254 } | 363 } |
255 | 364 |
365 void SetAsDefaultBrowserUsingSystemSettings( | |
366 const base::Closure& on_finished_callback) { | |
367 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
368 | |
369 // The helper manages its own lifetime. | |
370 static const wchar_t* const kProtocols[] = {L"http", L"https", nullptr}; | |
371 OpenSystemSettingsHelper::Begin(kProtocols, on_finished_callback); | |
372 | |
373 base::FilePath chrome_exe; | |
374 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
375 NOTREACHED() << "Error getting app exe path"; | |
376 return; | |
grt (UTC plus 2)
2016/03/31 19:08:12
wdyt of running the callback before returning here
Patrick Monette
2016/04/05 20:04:55
Done. The logging is consistent with the other Set
| |
377 } | |
378 | |
379 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
380 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe); | |
381 } | |
382 | |
256 bool SetAsDefaultProtocolClient(const std::string& protocol) { | 383 bool SetAsDefaultProtocolClient(const std::string& protocol) { |
257 if (protocol.empty()) | 384 if (protocol.empty()) |
258 return false; | 385 return false; |
259 | 386 |
260 base::FilePath chrome_exe; | 387 base::FilePath chrome_exe; |
261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 388 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
262 LOG(ERROR) << "Error getting app exe path"; | 389 LOG(ERROR) << "Error getting app exe path"; |
263 return false; | 390 return false; |
264 } | 391 } |
265 | 392 |
(...skipping 29 matching lines...) Expand all Loading... | |
295 return true; | 422 return true; |
296 } | 423 } |
297 | 424 |
298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { | 425 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { |
299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 426 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
300 if (distribution->GetDefaultBrowserControlPolicy() != | 427 if (distribution->GetDefaultBrowserControlPolicy() != |
301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 428 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
302 return SET_DEFAULT_NOT_ALLOWED; | 429 return SET_DEFAULT_NOT_ALLOWED; |
303 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 430 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
304 return SET_DEFAULT_UNATTENDED; | 431 return SET_DEFAULT_UNATTENDED; |
432 if (IsSetAsDefaultUsingSystemSettings()) | |
433 return SET_DEFAULT_OPEN_SETTINGS; | |
305 return SET_DEFAULT_INTERACTIVE; | 434 return SET_DEFAULT_INTERACTIVE; |
306 } | 435 } |
307 | 436 |
308 bool IsElevationNeededForSettingDefaultProtocolClient() { | 437 bool IsElevationNeededForSettingDefaultProtocolClient() { |
309 return base::win::GetVersion() < base::win::VERSION_WIN8; | 438 return base::win::GetVersion() < base::win::VERSION_WIN8; |
310 } | 439 } |
311 | 440 |
312 base::string16 GetApplicationNameForProtocol(const GURL& url) { | 441 base::string16 GetApplicationNameForProtocol(const GURL& url) { |
313 // Windows 8 or above requires a new protocol association query. | 442 // Windows 8 or above requires a new protocol association query. |
314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 443 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 | 664 |
536 shortcut = programs_folder.Append(shortcut_name); | 665 shortcut = programs_folder.Append(shortcut_name); |
537 if (base::PathExists(shortcut)) | 666 if (base::PathExists(shortcut)) |
538 return shortcut; | 667 return shortcut; |
539 } | 668 } |
540 | 669 |
541 return base::FilePath(); | 670 return base::FilePath(); |
542 } | 671 } |
543 | 672 |
544 } // namespace shell_integration | 673 } // namespace shell_integration |
OLD | NEW |