Chromium Code Reviews| 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/weak_ptr.h" | |
| 19 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
| 20 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 21 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/timer/timer.h" | |
| 24 #include "base/win/registry.h" | 28 #include "base/win/registry.h" |
| 25 #include "base/win/scoped_comptr.h" | 29 #include "base/win/scoped_comptr.h" |
| 26 #include "base/win/scoped_propvariant.h" | 30 #include "base/win/scoped_propvariant.h" |
| 27 #include "base/win/shortcut.h" | 31 #include "base/win/shortcut.h" |
| 28 #include "base/win/windows_version.h" | 32 #include "base/win/windows_version.h" |
| 29 #include "chrome/browser/policy/policy_path_parser.h" | 33 #include "chrome/browser/policy/policy_path_parser.h" |
| 30 #include "chrome/browser/web_applications/web_app.h" | 34 #include "chrome/browser/web_applications/web_app.h" |
| 31 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
| 32 #include "chrome/common/chrome_paths_internal.h" | 36 #include "chrome/common/chrome_paths_internal.h" |
| 33 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 case ShellUtil::NOT_DEFAULT: | 212 case ShellUtil::NOT_DEFAULT: |
| 209 return DefaultWebClientState::NOT_DEFAULT; | 213 return DefaultWebClientState::NOT_DEFAULT; |
| 210 case ShellUtil::IS_DEFAULT: | 214 case ShellUtil::IS_DEFAULT: |
| 211 return DefaultWebClientState::IS_DEFAULT; | 215 return DefaultWebClientState::IS_DEFAULT; |
| 212 default: | 216 default: |
| 213 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 217 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
| 214 return DefaultWebClientState::UNKNOWN_DEFAULT; | 218 return DefaultWebClientState::UNKNOWN_DEFAULT; |
| 215 } | 219 } |
| 216 } | 220 } |
| 217 | 221 |
| 222 bool IsSetAsDefaultUsingSystemSettings() { | |
| 223 return base::win::GetVersion() >= base::win::VERSION_WIN10; | |
| 224 } | |
| 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 values 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 OpenSystemSettingsHelper(const std::vector<const wchar_t*>& protocols, | |
| 239 const base::Closure& on_finished_callback) | |
| 240 : on_finished_callback_(on_finished_callback), | |
| 241 registry_watcher_count_(protocols.size()), | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
should this count be the # of protocols provided,
Patrick Monette
2016/03/31 18:26:03
Okay I changed it to the # of watchers successfull
| |
| 242 weak_ptr_factory_(this) { | |
| 243 static const wchar_t kUrlAssociationFormat[] = | |
| 244 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" | |
| 245 L"%ls\\UserChoice"; | |
| 246 | |
| 247 registry_key_watchers_.reserve(registry_watcher_count_); | |
| 248 for (const wchar_t* protocol : protocols) { | |
| 249 registry_key_watchers_.push_back(CreateRegistryKeyWatcher( | |
| 250 base::StringPrintf(kUrlAssociationFormat, protocol).c_str())); | |
| 251 } | |
| 252 | |
| 253 timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(2), | |
| 254 base::Bind(&OpenSystemSettingsHelper:: | |
| 255 ConcludeOpenSystemSettingsToDefaultBrowser, | |
| 256 weak_ptr_factory_.GetWeakPtr())); | |
| 257 } | |
| 258 | |
| 259 private: | |
| 260 void OnRegistryValueChanged() { | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: OnRegistryKeyChanged since it monitors a key
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 261 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 262 | |
| 263 // Make sure all the registry watchers fired. | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: "have fired"
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 264 registry_watcher_count_--; | |
| 265 | |
| 266 if (registry_watcher_count_ == 0) | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: if (--registry_watcher_count_ == 0)
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 267 ConcludeOpenSystemSettingsToDefaultBrowser(); | |
| 268 } | |
| 269 | |
| 270 void ConcludeOpenSystemSettingsToDefaultBrowser() { | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: add a doc comment
grt (UTC plus 2)
2016/03/31 13:23:37
since this will be re-used for other protocols, co
Patrick Monette
2016/03/31 18:26:03
Done.
Patrick Monette
2016/03/31 18:26:04
Done.
| |
| 271 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 272 | |
| 273 on_finished_callback_.Run(); | |
| 274 delete this; | |
| 275 } | |
| 276 | |
| 277 scoped_ptr<base::win::RegKey> CreateRegistryKeyWatcher( | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: add a doc comment
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 278 const wchar_t* registry_value) { | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: key_path since it's not naming a value in the
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 279 scoped_ptr<base::win::RegKey> reg_key( | |
| 280 new base::win::RegKey(HKEY_CURRENT_USER, registry_value, KEY_READ)); | |
| 281 | |
| 282 if (reg_key->Valid()) { | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
only return the key if it's really being watched?
Patrick Monette
2016/03/31 18:26:03
I changed it to AddRegistryKeyWatcher() so the vec
| |
| 283 reg_key->StartWatching( | |
| 284 base::Bind(&OpenSystemSettingsHelper::OnRegistryValueChanged, | |
| 285 weak_ptr_factory_.GetWeakPtr())); | |
| 286 } | |
| 287 return reg_key; | |
| 288 } | |
| 289 | |
| 290 // The function to call when the interaction with the system settings is | |
| 291 // finished. | |
| 292 base::Closure on_finished_callback_; | |
| 293 | |
| 294 // The number of time the registry key watchers must fire. | |
| 295 int registry_watcher_count_; | |
| 296 | |
| 297 // There can be multiple registry key watchers as some settings modify | |
| 298 // multiple protocol associations. e.g. Changing the default browser modify | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
nit: "modifies"
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 299 // the http and https associations. | |
| 300 std::vector<scoped_ptr<base::win::RegKey>> registry_key_watchers_; | |
| 301 | |
| 302 base::OneShotTimer timer_; | |
| 303 | |
| 304 // Weak ptrs are used to bind this class to the callbacks of the timer and the | |
| 305 // registry watcher. This makes it possible to self-delete after one of the | |
| 306 // callbacks is executed to cancel the remaining ones. | |
| 307 base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_; | |
| 308 | |
| 309 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper); | |
| 310 }; | |
| 311 | |
| 218 } // namespace | 312 } // namespace |
| 219 | 313 |
| 220 bool SetAsDefaultBrowser() { | 314 bool SetAsDefaultBrowser() { |
| 221 base::FilePath chrome_exe; | 315 base::FilePath chrome_exe; |
| 222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 316 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 223 LOG(ERROR) << "Error getting app exe path"; | 317 LOG(ERROR) << "Error getting app exe path"; |
| 224 return false; | 318 return false; |
| 225 } | 319 } |
| 226 | 320 |
| 227 // From UI currently we only allow setting default browser for current user. | 321 // From UI currently we only allow setting default browser for current user. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 340 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { | 341 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { |
| 248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; | 342 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
| 249 return false; | 343 return false; |
| 250 } | 344 } |
| 251 | 345 |
| 252 VLOG(1) << "Set-default-browser Windows UI completed."; | 346 VLOG(1) << "Set-default-browser Windows UI completed."; |
| 253 return true; | 347 return true; |
| 254 } | 348 } |
| 255 | 349 |
| 350 void SetAsDefaultBrowserUsingSystemSettings( | |
| 351 const base::Closure& on_finished_callback) { | |
| 352 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 353 | |
| 354 // The helper manages its own lifetime. | |
| 355 new OpenSystemSettingsHelper({L"http", L"https"}, on_finished_callback); | |
|
grt (UTC plus 2)
2016/03/31 13:23:37
note: uniform initialization is banned (http://chr
grt (UTC plus 2)
2016/03/31 13:23:37
rather than the naked new, i suggest a static func
Patrick Monette
2016/03/31 18:26:03
Done.
Patrick Monette
2016/03/31 18:26:03
Done.
| |
| 356 | |
| 357 base::FilePath chrome_exe; | |
| 358 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
| 359 NOTREACHED() << "Error getting app exe path"; | |
| 360 return; | |
| 361 } | |
| 362 | |
| 363 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 364 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe); | |
| 365 } | |
| 366 | |
| 256 bool SetAsDefaultProtocolClient(const std::string& protocol) { | 367 bool SetAsDefaultProtocolClient(const std::string& protocol) { |
| 257 if (protocol.empty()) | 368 if (protocol.empty()) |
| 258 return false; | 369 return false; |
| 259 | 370 |
| 260 base::FilePath chrome_exe; | 371 base::FilePath chrome_exe; |
| 261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 372 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 262 LOG(ERROR) << "Error getting app exe path"; | 373 LOG(ERROR) << "Error getting app exe path"; |
| 263 return false; | 374 return false; |
| 264 } | 375 } |
| 265 | 376 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 295 return true; | 406 return true; |
| 296 } | 407 } |
| 297 | 408 |
| 298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { | 409 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { |
| 299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 410 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 300 if (distribution->GetDefaultBrowserControlPolicy() != | 411 if (distribution->GetDefaultBrowserControlPolicy() != |
| 301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 412 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
| 302 return SET_DEFAULT_NOT_ALLOWED; | 413 return SET_DEFAULT_NOT_ALLOWED; |
| 303 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 414 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 304 return SET_DEFAULT_UNATTENDED; | 415 return SET_DEFAULT_UNATTENDED; |
| 416 if (IsSetAsDefaultUsingSystemSettings()) | |
| 417 return SET_DEFAULT_OPEN_SETTINGS; | |
| 305 return SET_DEFAULT_INTERACTIVE; | 418 return SET_DEFAULT_INTERACTIVE; |
| 306 } | 419 } |
| 307 | 420 |
| 308 bool IsElevationNeededForSettingDefaultProtocolClient() { | 421 bool IsElevationNeededForSettingDefaultProtocolClient() { |
| 309 return base::win::GetVersion() < base::win::VERSION_WIN8; | 422 return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 310 } | 423 } |
| 311 | 424 |
| 312 base::string16 GetApplicationNameForProtocol(const GURL& url) { | 425 base::string16 GetApplicationNameForProtocol(const GURL& url) { |
| 313 // Windows 8 or above requires a new protocol association query. | 426 // Windows 8 or above requires a new protocol association query. |
| 314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 427 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 | 648 |
| 536 shortcut = programs_folder.Append(shortcut_name); | 649 shortcut = programs_folder.Append(shortcut_name); |
| 537 if (base::PathExists(shortcut)) | 650 if (base::PathExists(shortcut)) |
| 538 return shortcut; | 651 return shortcut; |
| 539 } | 652 } |
| 540 | 653 |
| 541 return base::FilePath(); | 654 return base::FilePath(); |
| 542 } | 655 } |
| 543 | 656 |
| 544 } // namespace shell_integration | 657 } // namespace shell_integration |
| OLD | NEW |