| 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_win.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" |
| 17 #include "base/callback.h" |
| 15 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 16 #include "base/files/file_enumerator.h" | 19 #include "base/files/file_enumerator.h" |
| 17 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
| 18 #include "base/macros.h" | 21 #include "base/macros.h" |
| 22 #include "base/memory/weak_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
| 24 #include "base/metrics/histogram_macros.h" |
| 20 #include "base/path_service.h" | 25 #include "base/path_service.h" |
| 21 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
| 29 #include "base/time/time.h" |
| 30 #include "base/timer/timer.h" |
| 24 #include "base/win/registry.h" | 31 #include "base/win/registry.h" |
| 25 #include "base/win/scoped_comptr.h" | 32 #include "base/win/scoped_comptr.h" |
| 26 #include "base/win/scoped_propvariant.h" | 33 #include "base/win/scoped_propvariant.h" |
| 27 #include "base/win/shortcut.h" | 34 #include "base/win/shortcut.h" |
| 28 #include "base/win/windows_version.h" | 35 #include "base/win/windows_version.h" |
| 29 #include "chrome/browser/policy/policy_path_parser.h" | 36 #include "chrome/browser/policy/policy_path_parser.h" |
| 37 #include "chrome/browser/shell_integration.h" |
| 30 #include "chrome/browser/web_applications/web_app.h" | 38 #include "chrome/browser/web_applications/web_app.h" |
| 31 #include "chrome/common/chrome_constants.h" | 39 #include "chrome/common/chrome_constants.h" |
| 32 #include "chrome/common/chrome_paths_internal.h" | 40 #include "chrome/common/chrome_paths_internal.h" |
| 33 #include "chrome/common/chrome_switches.h" | 41 #include "chrome/common/chrome_switches.h" |
| 34 #include "chrome/installer/setup/setup_util.h" | 42 #include "chrome/installer/setup/setup_util.h" |
| 35 #include "chrome/installer/util/browser_distribution.h" | 43 #include "chrome/installer/util/browser_distribution.h" |
| 36 #include "chrome/installer/util/create_reg_key_work_item.h" | 44 #include "chrome/installer/util/create_reg_key_work_item.h" |
| 37 #include "chrome/installer/util/install_util.h" | 45 #include "chrome/installer/util/install_util.h" |
| 38 #include "chrome/installer/util/set_reg_value_work_item.h" | 46 #include "chrome/installer/util/set_reg_value_work_item.h" |
| 39 #include "chrome/installer/util/shell_util.h" | 47 #include "chrome/installer/util/shell_util.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 case ShellUtil::NOT_DEFAULT: | 216 case ShellUtil::NOT_DEFAULT: |
| 209 return DefaultWebClientState::NOT_DEFAULT; | 217 return DefaultWebClientState::NOT_DEFAULT; |
| 210 case ShellUtil::IS_DEFAULT: | 218 case ShellUtil::IS_DEFAULT: |
| 211 return DefaultWebClientState::IS_DEFAULT; | 219 return DefaultWebClientState::IS_DEFAULT; |
| 212 default: | 220 default: |
| 213 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 221 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
| 214 return DefaultWebClientState::UNKNOWN_DEFAULT; | 222 return DefaultWebClientState::UNKNOWN_DEFAULT; |
| 215 } | 223 } |
| 216 } | 224 } |
| 217 | 225 |
| 226 // There is no way to make sure the user is done with the system settings, but a |
| 227 // signal that the interaction is finished is needed for UMA. A timer of 2 |
| 228 // minutes is used as a substitute. The registry keys for the protocol |
| 229 // association with an app are also monitored to signal the end of the |
| 230 // interaction early when it is clear that the user made a choice (e.g. http |
| 231 // and https for default browser). |
| 232 // |
| 233 // This helper class manages both the timer and the registry watchers and makes |
| 234 // sure the callback for the end of the settings interaction is only run once. |
| 235 // This class also manages its own lifetime. |
| 236 class OpenSystemSettingsHelper { |
| 237 public: |
| 238 // Begin the monitoring and will call |on_finished_callback| when done. |
| 239 // Takes in a null-terminated array of |protocols| whose registry keys must be |
| 240 // watched. |
| 241 static void Begin(const wchar_t* const protocols[], |
| 242 const base::Closure& on_finished_callback) { |
| 243 new OpenSystemSettingsHelper(protocols, on_finished_callback); |
| 244 } |
| 245 |
| 246 private: |
| 247 // The reason the settings interaction concluded. Do not modify the ordering |
| 248 // because it is used for UMA. |
| 249 enum ConcludeReason { REGISTRY_WATCHER, TIMEOUT, NUM_CONCLUDE_REASON_TYPES }; |
| 250 |
| 251 OpenSystemSettingsHelper(const wchar_t* const protocols[], |
| 252 const base::Closure& on_finished_callback) |
| 253 : on_finished_callback_(on_finished_callback), weak_ptr_factory_(this) { |
| 254 static const wchar_t kUrlAssociationFormat[] = |
| 255 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" |
| 256 L"%ls\\UserChoice"; |
| 257 |
| 258 // Remember the start time. |
| 259 start_time_ = base::TimeTicks::Now(); |
| 260 |
| 261 for (const wchar_t* const* scan = &protocols[0]; *scan != nullptr; ++scan) { |
| 262 AddRegistryKeyWatcher( |
| 263 base::StringPrintf(kUrlAssociationFormat, *scan).c_str()); |
| 264 } |
| 265 // Only the watchers that were succesfully initialized are counted. |
| 266 registry_watcher_count_ = registry_key_watchers_.size(); |
| 267 |
| 268 timer_.Start( |
| 269 FROM_HERE, base::TimeDelta::FromMinutes(2), |
| 270 base::Bind(&OpenSystemSettingsHelper::ConcludeInteraction, |
| 271 weak_ptr_factory_.GetWeakPtr(), ConcludeReason::TIMEOUT)); |
| 272 } |
| 273 |
| 274 // Called when a change is detected on one of the registry keys being watched. |
| 275 // Note: All types of modification to the registry key will trigger this |
| 276 // function even if the value change is the only one that matters. This |
| 277 // is good enough for now. |
| 278 void OnRegistryKeyChanged() { |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 280 |
| 281 // Make sure all the registry watchers have fired. |
| 282 if (--registry_watcher_count_ == 0) { |
| 283 UMA_HISTOGRAM_MEDIUM_TIMES( |
| 284 "DefaultBrowser.SettingsInteraction.RegistryWatcherDuration", |
| 285 base::TimeTicks::Now() - start_time_); |
| 286 |
| 287 ConcludeInteraction(ConcludeReason::REGISTRY_WATCHER); |
| 288 } |
| 289 } |
| 290 |
| 291 // Ends the monitoring with the system settings. Will call |
| 292 // |on_finished_callback_| and then dispose of this class instance to make |
| 293 // sure the callback won't get called subsequently. |
| 294 void ConcludeInteraction(ConcludeReason conclude_reason) { |
| 295 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 296 |
| 297 UMA_HISTOGRAM_ENUMERATION( |
| 298 "DefaultBrowser.SettingsInteraction.ConcludeReason", conclude_reason, |
| 299 NUM_CONCLUDE_REASON_TYPES); |
| 300 on_finished_callback_.Run(); |
| 301 delete this; |
| 302 } |
| 303 |
| 304 // Helper function to create a registry watcher for a given |key_path|. Do |
| 305 // nothing on initialization failure. |
| 306 void AddRegistryKeyWatcher(const wchar_t* key_path) { |
| 307 auto reg_key = make_scoped_ptr( |
| 308 new base::win::RegKey(HKEY_CURRENT_USER, key_path, KEY_NOTIFY)); |
| 309 |
| 310 if (reg_key->Valid() && |
| 311 reg_key->StartWatching( |
| 312 base::Bind(&OpenSystemSettingsHelper::OnRegistryKeyChanged, |
| 313 weak_ptr_factory_.GetWeakPtr()))) { |
| 314 registry_key_watchers_.push_back(std::move(reg_key)); |
| 315 } |
| 316 } |
| 317 |
| 318 // The function to call when the interaction with the system settings is |
| 319 // finished. |
| 320 base::Closure on_finished_callback_; |
| 321 |
| 322 // The number of time the registry key watchers must fire. |
| 323 int registry_watcher_count_ = 0; |
| 324 |
| 325 // There can be multiple registry key watchers as some settings modify |
| 326 // multiple protocol associations. e.g. Changing the default browser modifies |
| 327 // the http and https associations. |
| 328 std::vector<scoped_ptr<base::win::RegKey>> registry_key_watchers_; |
| 329 |
| 330 base::OneShotTimer timer_; |
| 331 |
| 332 // Records the time it takes for the final registry watcher to get signaled. |
| 333 base::TimeTicks start_time_; |
| 334 |
| 335 // Weak ptrs are used to bind this class to the callbacks of the timer and the |
| 336 // registry watcher. This makes it possible to self-delete after one of the |
| 337 // callbacks is executed to cancel the remaining ones. |
| 338 base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_; |
| 339 |
| 340 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper); |
| 341 }; |
| 342 |
| 218 } // namespace | 343 } // namespace |
| 219 | 344 |
| 220 bool SetAsDefaultBrowser() { | 345 bool SetAsDefaultBrowser() { |
| 221 base::FilePath chrome_exe; | 346 base::FilePath chrome_exe; |
| 222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 347 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 223 LOG(ERROR) << "Error getting app exe path"; | 348 LOG(ERROR) << "Error getting app exe path"; |
| 224 return false; | 349 return false; |
| 225 } | 350 } |
| 226 | 351 |
| 227 // From UI currently we only allow setting default browser for current user. | 352 // From UI currently we only allow setting default browser for current user. |
| 228 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 353 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 229 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, | 354 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, |
| 230 true /* elevate_if_not_admin */)) { | 355 true /* elevate_if_not_admin */)) { |
| 231 LOG(ERROR) << "Chrome could not be set as default browser."; | 356 LOG(ERROR) << "Chrome could not be set as default browser."; |
| 232 return false; | 357 return false; |
| 233 } | 358 } |
| 234 | 359 |
| 235 VLOG(1) << "Chrome registered as default browser."; | 360 VLOG(1) << "Chrome registered as default browser."; |
| 236 return true; | 361 return true; |
| 237 } | 362 } |
| 238 | 363 |
| 239 bool SetAsDefaultBrowserInteractive() { | |
| 240 base::FilePath chrome_exe; | |
| 241 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
| 242 NOTREACHED() << "Error getting app exe path"; | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { | |
| 248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; | |
| 249 return false; | |
| 250 } | |
| 251 | |
| 252 VLOG(1) << "Set-default-browser Windows UI completed."; | |
| 253 return true; | |
| 254 } | |
| 255 | |
| 256 bool SetAsDefaultProtocolClient(const std::string& protocol) { | 364 bool SetAsDefaultProtocolClient(const std::string& protocol) { |
| 257 if (protocol.empty()) | 365 if (protocol.empty()) |
| 258 return false; | 366 return false; |
| 259 | 367 |
| 260 base::FilePath chrome_exe; | 368 base::FilePath chrome_exe; |
| 261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 369 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 262 LOG(ERROR) << "Error getting app exe path"; | 370 LOG(ERROR) << "Error getting app exe path"; |
| 263 return false; | 371 return false; |
| 264 } | 372 } |
| 265 | 373 |
| 266 base::string16 wprotocol(base::UTF8ToUTF16(protocol)); | 374 base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
| 267 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 375 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 268 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe, | 376 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe, |
| 269 wprotocol)) { | 377 wprotocol)) { |
| 270 LOG(ERROR) << "Chrome could not be set as default handler for " | 378 LOG(ERROR) << "Chrome could not be set as default handler for " |
| 271 << protocol << "."; | 379 << protocol << "."; |
| 272 return false; | 380 return false; |
| 273 } | 381 } |
| 274 | 382 |
| 275 VLOG(1) << "Chrome registered as default handler for " << protocol << "."; | 383 VLOG(1) << "Chrome registered as default handler for " << protocol << "."; |
| 276 return true; | 384 return true; |
| 277 } | 385 } |
| 278 | 386 |
| 279 bool SetAsDefaultProtocolClientInteractive(const std::string& protocol) { | 387 DefaultWebClientSetPermission GetDefaultWebClientSetPermission() { |
| 280 base::FilePath chrome_exe; | |
| 281 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
| 282 NOTREACHED() << "Error getting app exe path"; | |
| 283 return false; | |
| 284 } | |
| 285 | |
| 286 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 287 base::string16 wprotocol(base::UTF8ToUTF16(protocol)); | |
| 288 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe, | |
| 289 wprotocol)) { | |
| 290 LOG(ERROR) << "Failed to launch the set-default-client Windows UI."; | |
| 291 return false; | |
| 292 } | |
| 293 | |
| 294 VLOG(1) << "Set-default-client Windows UI completed."; | |
| 295 return true; | |
| 296 } | |
| 297 | |
| 298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { | |
| 299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 388 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 300 if (distribution->GetDefaultBrowserControlPolicy() != | 389 if (distribution->GetDefaultBrowserControlPolicy() != |
| 301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 390 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
| 302 return SET_DEFAULT_NOT_ALLOWED; | 391 return SET_DEFAULT_NOT_ALLOWED; |
| 303 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 392 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 304 return SET_DEFAULT_UNATTENDED; | 393 return SET_DEFAULT_UNATTENDED; |
| 394 // Windows 8 and 10 both introduced a new way to set the default web client |
| 395 // which require user interaction. |
| 305 return SET_DEFAULT_INTERACTIVE; | 396 return SET_DEFAULT_INTERACTIVE; |
| 306 } | 397 } |
| 307 | 398 |
| 308 bool IsElevationNeededForSettingDefaultProtocolClient() { | 399 bool IsElevationNeededForSettingDefaultProtocolClient() { |
| 309 return base::win::GetVersion() < base::win::VERSION_WIN8; | 400 return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 310 } | 401 } |
| 311 | 402 |
| 312 base::string16 GetApplicationNameForProtocol(const GURL& url) { | 403 base::string16 GetApplicationNameForProtocol(const GURL& url) { |
| 313 // Windows 8 or above requires a new protocol association query. | 404 // Windows 8 or above requires a new protocol association query. |
| 314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 405 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 } | 625 } |
| 535 | 626 |
| 536 shortcut = programs_folder.Append(shortcut_name); | 627 shortcut = programs_folder.Append(shortcut_name); |
| 537 if (base::PathExists(shortcut)) | 628 if (base::PathExists(shortcut)) |
| 538 return shortcut; | 629 return shortcut; |
| 539 } | 630 } |
| 540 | 631 |
| 541 return base::FilePath(); | 632 return base::FilePath(); |
| 542 } | 633 } |
| 543 | 634 |
| 635 namespace win { |
| 636 |
| 637 bool SetAsDefaultBrowserUsingIntentPicker() { |
| 638 base::FilePath chrome_exe; |
| 639 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 640 NOTREACHED() << "Error getting app exe path"; |
| 641 return false; |
| 642 } |
| 643 |
| 644 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 645 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { |
| 646 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
| 647 return false; |
| 648 } |
| 649 |
| 650 VLOG(1) << "Set-default-browser Windows UI completed."; |
| 651 return true; |
| 652 } |
| 653 |
| 654 void SetAsDefaultBrowserUsingSystemSettings( |
| 655 const base::Closure& on_finished_callback) { |
| 656 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 657 |
| 658 base::FilePath chrome_exe; |
| 659 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 660 NOTREACHED() << "Error getting app exe path"; |
| 661 on_finished_callback.Run(); |
| 662 return; |
| 663 } |
| 664 |
| 665 // The helper manages its own lifetime. |
| 666 static const wchar_t* const kProtocols[] = {L"http", L"https", nullptr}; |
| 667 OpenSystemSettingsHelper::Begin(kProtocols, on_finished_callback); |
| 668 |
| 669 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 670 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe); |
| 671 } |
| 672 |
| 673 bool SetAsDefaultProtocolClientUsingIntentPicker(const std::string& protocol) { |
| 674 base::FilePath chrome_exe; |
| 675 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 676 NOTREACHED() << "Error getting app exe path"; |
| 677 return false; |
| 678 } |
| 679 |
| 680 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 681 base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
| 682 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe, |
| 683 wprotocol)) { |
| 684 LOG(ERROR) << "Failed to launch the set-default-client Windows UI."; |
| 685 return false; |
| 686 } |
| 687 |
| 688 VLOG(1) << "Set-default-client Windows UI completed."; |
| 689 return true; |
| 690 } |
| 691 |
| 692 } // namespace win |
| 693 |
| 544 } // namespace shell_integration | 694 } // namespace shell_integration |
| OLD | NEW |