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 "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ref_counted.h" | |
19 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
20 #include "base/path_service.h" | 21 #include "base/path_service.h" |
21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
25 #include "base/timer/timer.h" | |
24 #include "base/win/registry.h" | 26 #include "base/win/registry.h" |
25 #include "base/win/scoped_comptr.h" | 27 #include "base/win/scoped_comptr.h" |
26 #include "base/win/scoped_propvariant.h" | 28 #include "base/win/scoped_propvariant.h" |
27 #include "base/win/shortcut.h" | 29 #include "base/win/shortcut.h" |
28 #include "base/win/windows_version.h" | 30 #include "base/win/windows_version.h" |
29 #include "chrome/browser/policy/policy_path_parser.h" | 31 #include "chrome/browser/policy/policy_path_parser.h" |
30 #include "chrome/browser/web_applications/web_app.h" | 32 #include "chrome/browser/web_applications/web_app.h" |
31 #include "chrome/common/chrome_constants.h" | 33 #include "chrome/common/chrome_constants.h" |
32 #include "chrome/common/chrome_paths_internal.h" | 34 #include "chrome/common/chrome_paths_internal.h" |
33 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 case ShellUtil::NOT_DEFAULT: | 210 case ShellUtil::NOT_DEFAULT: |
209 return DefaultWebClientState::NOT_DEFAULT; | 211 return DefaultWebClientState::NOT_DEFAULT; |
210 case ShellUtil::IS_DEFAULT: | 212 case ShellUtil::IS_DEFAULT: |
211 return DefaultWebClientState::IS_DEFAULT; | 213 return DefaultWebClientState::IS_DEFAULT; |
212 default: | 214 default: |
213 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 215 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
214 return DefaultWebClientState::UNKNOWN_DEFAULT; | 216 return DefaultWebClientState::UNKNOWN_DEFAULT; |
215 } | 217 } |
216 } | 218 } |
217 | 219 |
220 bool IsSetAsDefaultUsingSystemSettings() { | |
221 return base::win::GetVersion() >= base::win::VERSION_WIN10; | |
222 } | |
223 | |
224 // There is no way to make sure the user is done with the system settings, but a | |
225 // signal that the interaction is finished is needed for UMA. A timer of 2 | |
226 // minutes is used as a substitute. The registry values for default browser is | |
227 // also monitored (http and https association) to signal the end of the | |
228 // interaction early when it is clear that the user made a choice. | |
229 // | |
230 // This helper class manages both the timer and the registry watchers and make | |
grt (UTC plus 2)
2016/03/29 16:45:03
nit: make -> makes
Patrick Monette
2016/03/29 17:43:08
Done.
| |
231 // sure the callback for the end of the settings interaction is only run once. | |
232 class OpenSystemSettingsHelper | |
233 : public base::RefCounted<OpenSystemSettingsHelper> { | |
234 public: | |
235 OpenSystemSettingsHelper() {} | |
236 | |
237 // Initializes the timer and the registry watcher. | |
238 void Initialize( | |
239 base::Callback<void(bool)> conclude_settings_interaction_callback) { | |
240 const wchar_t url_association_format[] = | |
241 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" | |
242 L"%ls\\UserChoice"; | |
243 http_registry_key_ = CreateRegistryKeyWatcher( | |
244 base::StringPrintf(url_association_format, L"http").c_str(), | |
245 conclude_settings_interaction_callback); | |
246 https_registry_key_ = CreateRegistryKeyWatcher( | |
247 base::StringPrintf(url_association_format, L"https").c_str(), | |
248 conclude_settings_interaction_callback); | |
249 | |
250 timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(2), | |
251 base::Bind(conclude_settings_interaction_callback, false)); | |
252 } | |
253 | |
254 // Either the timer or the registry watcher signaled the end of the | |
255 // interaction. Make sure the other signal is ignored. | |
256 void Clear() { | |
257 run_once_ = false; | |
258 timer_.Reset(); | |
259 http_registry_key_.reset(); | |
260 https_registry_key_.reset(); | |
261 } | |
262 | |
263 void IncrementRegistryWatcherCount() { registry_watcher_count_++; } | |
264 | |
265 const bool run_once() { return run_once_; } | |
266 const int get_registry_watcher_count() { return registry_watcher_count_; } | |
267 | |
268 private: | |
269 scoped_ptr<base::win::RegKey> CreateRegistryKeyWatcher( | |
270 const wchar_t* registry_value, | |
271 base::Callback<void(bool)> conclude_settings_interaction_callback) { | |
272 scoped_ptr<base::win::RegKey> reg_key( | |
273 new base::win::RegKey(HKEY_CURRENT_USER, registry_value, KEY_READ)); | |
274 | |
275 if (reg_key && reg_key->Valid()) { | |
grt (UTC plus 2)
2016/03/29 16:45:03
no need to test reg_key alone since it will always
Patrick Monette
2016/03/29 17:43:08
Done.
| |
276 reg_key->StartWatching( | |
277 base::Bind(conclude_settings_interaction_callback, true)); | |
278 } | |
279 return reg_key; | |
280 } | |
281 | |
282 base::OneShotTimer timer_; | |
283 scoped_ptr<base::win::RegKey> http_registry_key_; | |
284 scoped_ptr<base::win::RegKey> https_registry_key_; | |
285 | |
286 // Used to make sure only one of the timer or the registry watcher's signal is | |
287 // taken in consideration. | |
288 bool run_once_ = true; | |
289 | |
290 // The number of time a registry key watcher fired. | |
291 int registry_watcher_count_ = 0; | |
292 | |
293 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper); | |
294 }; | |
295 | |
296 void ConcludeOpenSystemSettingsToDefaultBrowser( | |
grt (UTC plus 2)
2016/03/29 16:45:03
this function is tightly coupled to the helper. ca
Patrick Monette
2016/03/29 17:43:08
Done.
| |
297 scoped_refptr<OpenSystemSettingsHelper> helper, | |
298 base::Callback<void()> on_finished_callback, | |
grt (UTC plus 2)
2016/03/29 16:45:03
const base::Closure&
Patrick Monette
2016/03/29 17:43:08
Done.
| |
299 bool is_registry_watcher) { | |
300 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
301 DCHECK(helper); | |
302 | |
303 // It is possible to get here if only one of the http/https url association | |
304 // was changed, which is not enough for GetDefaultBrowser() to return | |
305 // IS_DEFAULT. | |
306 if (is_registry_watcher) { | |
307 helper->IncrementRegistryWatcherCount(); | |
308 if (helper->get_registry_watcher_count() != 2) | |
309 return; | |
310 } | |
311 | |
312 if (!helper->run_once()) | |
313 return; | |
314 helper->Clear(); | |
315 | |
316 on_finished_callback.Run(); | |
317 } | |
318 | |
218 } // namespace | 319 } // namespace |
219 | 320 |
220 bool SetAsDefaultBrowser() { | 321 bool SetAsDefaultBrowser() { |
221 base::FilePath chrome_exe; | 322 base::FilePath chrome_exe; |
222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 323 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
223 LOG(ERROR) << "Error getting app exe path"; | 324 LOG(ERROR) << "Error getting app exe path"; |
224 return false; | 325 return false; |
225 } | 326 } |
226 | 327 |
227 // From UI currently we only allow setting default browser for current user. | 328 // From UI currently we only allow setting default browser for current user. |
(...skipping 18 matching lines...) Expand all Loading... | |
246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 347 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { | 348 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { |
248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; | 349 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
249 return false; | 350 return false; |
250 } | 351 } |
251 | 352 |
252 VLOG(1) << "Set-default-browser Windows UI completed."; | 353 VLOG(1) << "Set-default-browser Windows UI completed."; |
253 return true; | 354 return true; |
254 } | 355 } |
255 | 356 |
357 void SetAsDefaultBrowserUsingSystemSettings( | |
358 base::Closure on_finished_callback) { | |
359 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
360 | |
361 scoped_refptr<OpenSystemSettingsHelper> helper(new OpenSystemSettingsHelper); | |
362 helper->Initialize(base::Bind(&ConcludeOpenSystemSettingsToDefaultBrowser, | |
363 helper, on_finished_callback)); | |
364 | |
365 base::FilePath chrome_exe; | |
366 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
367 NOTREACHED() << "Error getting app exe path"; | |
368 return; | |
369 } | |
370 | |
371 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
372 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe); | |
373 } | |
374 | |
256 bool SetAsDefaultProtocolClient(const std::string& protocol) { | 375 bool SetAsDefaultProtocolClient(const std::string& protocol) { |
257 if (protocol.empty()) | 376 if (protocol.empty()) |
258 return false; | 377 return false; |
259 | 378 |
260 base::FilePath chrome_exe; | 379 base::FilePath chrome_exe; |
261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 380 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
262 LOG(ERROR) << "Error getting app exe path"; | 381 LOG(ERROR) << "Error getting app exe path"; |
263 return false; | 382 return false; |
264 } | 383 } |
265 | 384 |
(...skipping 29 matching lines...) Expand all Loading... | |
295 return true; | 414 return true; |
296 } | 415 } |
297 | 416 |
298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { | 417 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { |
299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 418 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
300 if (distribution->GetDefaultBrowserControlPolicy() != | 419 if (distribution->GetDefaultBrowserControlPolicy() != |
301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 420 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
302 return SET_DEFAULT_NOT_ALLOWED; | 421 return SET_DEFAULT_NOT_ALLOWED; |
303 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 422 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
304 return SET_DEFAULT_UNATTENDED; | 423 return SET_DEFAULT_UNATTENDED; |
424 if (IsSetAsDefaultUsingSystemSettings()) | |
425 return SET_DEFAULT_OPEN_SETTINGS; | |
305 return SET_DEFAULT_INTERACTIVE; | 426 return SET_DEFAULT_INTERACTIVE; |
306 } | 427 } |
307 | 428 |
308 bool IsElevationNeededForSettingDefaultProtocolClient() { | 429 bool IsElevationNeededForSettingDefaultProtocolClient() { |
309 return base::win::GetVersion() < base::win::VERSION_WIN8; | 430 return base::win::GetVersion() < base::win::VERSION_WIN8; |
310 } | 431 } |
311 | 432 |
312 base::string16 GetApplicationNameForProtocol(const GURL& url) { | 433 base::string16 GetApplicationNameForProtocol(const GURL& url) { |
313 // Windows 8 or above requires a new protocol association query. | 434 // Windows 8 or above requires a new protocol association query. |
314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 435 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
535 | 656 |
536 shortcut = programs_folder.Append(shortcut_name); | 657 shortcut = programs_folder.Append(shortcut_name); |
537 if (base::PathExists(shortcut)) | 658 if (base::PathExists(shortcut)) |
538 return shortcut; | 659 return shortcut; |
539 } | 660 } |
540 | 661 |
541 return base::FilePath(); | 662 return base::FilePath(); |
542 } | 663 } |
543 | 664 |
544 } // namespace shell_integration | 665 } // namespace shell_integration |
OLD | NEW |