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 <shellapi.h> | |
8 #include <shlwapi.h> | 9 #include <shlwapi.h> |
9 #include <shobjidl.h> | 10 #include <shobjidl.h> |
10 #include <propkey.h> // Needs to come after shobjidl.h. | 11 #include <propkey.h> // Needs to come after shobjidl.h. |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
14 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/metrics/field_trial.h" | |
19 #include "base/metrics/histogram_macros.h" | |
17 #include "base/path_service.h" | 20 #include "base/path_service.h" |
18 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
22 #include "base/win/registry.h" | 25 #include "base/win/registry.h" |
23 #include "base/win/scoped_comptr.h" | 26 #include "base/win/scoped_comptr.h" |
24 #include "base/win/scoped_propvariant.h" | 27 #include "base/win/scoped_propvariant.h" |
25 #include "base/win/shortcut.h" | 28 #include "base/win/shortcut.h" |
26 #include "base/win/windows_version.h" | 29 #include "base/win/windows_version.h" |
27 #include "chrome/browser/policy/policy_path_parser.h" | 30 #include "chrome/browser/policy/policy_path_parser.h" |
31 #include "chrome/browser/ui/startup/startup_browser_creator.h" | |
28 #include "chrome/browser/web_applications/web_app.h" | 32 #include "chrome/browser/web_applications/web_app.h" |
29 #include "chrome/common/chrome_constants.h" | 33 #include "chrome/common/chrome_constants.h" |
30 #include "chrome/common/chrome_paths_internal.h" | 34 #include "chrome/common/chrome_paths_internal.h" |
31 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
32 #include "chrome/installer/setup/setup_util.h" | 36 #include "chrome/installer/setup/setup_util.h" |
33 #include "chrome/installer/util/browser_distribution.h" | 37 #include "chrome/installer/util/browser_distribution.h" |
34 #include "chrome/installer/util/create_reg_key_work_item.h" | 38 #include "chrome/installer/util/create_reg_key_work_item.h" |
35 #include "chrome/installer/util/install_util.h" | 39 #include "chrome/installer/util/install_util.h" |
36 #include "chrome/installer/util/set_reg_value_work_item.h" | 40 #include "chrome/installer/util/set_reg_value_work_item.h" |
37 #include "chrome/installer/util/shell_util.h" | 41 #include "chrome/installer/util/shell_util.h" |
38 #include "chrome/installer/util/util_constants.h" | 42 #include "chrome/installer/util/util_constants.h" |
39 #include "chrome/installer/util/work_item.h" | 43 #include "chrome/installer/util/work_item.h" |
40 #include "chrome/installer/util/work_item_list.h" | 44 #include "chrome/installer/util/work_item_list.h" |
45 #include "components/variations/variations_associated_data.h" | |
41 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/browser_thread.h" |
42 | 47 |
43 using content::BrowserThread; | 48 using content::BrowserThread; |
44 | 49 |
45 namespace { | 50 namespace { |
46 | 51 |
47 const wchar_t kAppListAppNameSuffix[] = L"AppList"; | 52 const wchar_t kAppListAppNameSuffix[] = L"AppList"; |
53 const wchar_t kSetDefaultBrowserHelpUrl[] = | |
54 L"https://support.google.com/chrome?p=default_browser"; | |
55 | |
56 const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault"; | |
57 const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default"; | |
58 const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default"; | |
48 | 59 |
49 // Helper function for ShellIntegration::GetAppId to generates profile id | 60 // Helper function for ShellIntegration::GetAppId to generates profile id |
50 // from profile path. "profile_id" is composed of sanitized basenames of | 61 // from profile path. "profile_id" is composed of sanitized basenames of |
51 // user data dir and profile dir joined by a ".". | 62 // user data dir and profile dir joined by a ".". |
52 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { | 63 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { |
53 // Return empty string if profile_path is empty | 64 // Return empty string if profile_path is empty |
54 if (profile_path.empty()) | 65 if (profile_path.empty()) |
55 return base::string16(); | 66 return base::string16(); |
56 | 67 |
57 base::FilePath default_user_data_dir; | 68 base::FilePath default_user_data_dir; |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 case ShellUtil::NOT_DEFAULT: | 244 case ShellUtil::NOT_DEFAULT: |
234 return ShellIntegration::NOT_DEFAULT; | 245 return ShellIntegration::NOT_DEFAULT; |
235 case ShellUtil::IS_DEFAULT: | 246 case ShellUtil::IS_DEFAULT: |
236 return ShellIntegration::IS_DEFAULT; | 247 return ShellIntegration::IS_DEFAULT; |
237 default: | 248 default: |
238 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 249 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
239 return ShellIntegration::UNKNOWN_DEFAULT; | 250 return ShellIntegration::UNKNOWN_DEFAULT; |
240 } | 251 } |
241 } | 252 } |
242 | 253 |
254 // Resets the default browser choice for the current user. | |
255 void ResetDefaultBrowser() { | |
256 static const wchar_t* kUrlAssociationKeyFormats[] = { | |
grt (UTC plus 2)
2015/09/22 18:21:47
static const wchar_t* const
Patrick Monette
2015/09/23 22:40:44
Done.
| |
257 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" | |
258 L"%ls\\UserChoice", | |
259 L"SOFTWARE\\Microsoft\\Windows\\Roaming\\OpenWith\\UrlAssociations\\" | |
260 L"%ls\\UserChoice"}; | |
261 static const wchar_t* kProtocols[] = {L"http", L"https"}; | |
grt (UTC plus 2)
2015/09/22 18:21:47
static const wchar_t* const
Patrick Monette
2015/09/23 22:40:44
Done.
| |
262 | |
263 for (const wchar_t* format : kUrlAssociationKeyFormats) { | |
264 for (const wchar_t* protocol : kProtocols) { | |
265 base::win::RegKey registry_key( | |
266 HKEY_CURRENT_USER, base::StringPrintf(format, protocol).c_str(), | |
267 KEY_WRITE); | |
grt (UTC plus 2)
2015/09/22 18:21:47
KEY_WRITE -> KEY_SET_VALUE
Patrick Monette
2015/09/23 22:40:44
Done.
| |
268 registry_key.DeleteValue(L"Hash"); | |
269 } | |
270 } | |
271 } | |
272 | |
273 // Returns true if the AsyncSetAsDefault field trial is activated. | |
274 bool IsAsyncSetAsDefaultEnabled() { | |
275 using base::CommandLine; | |
276 | |
277 const std::string group_name = | |
278 base::FieldTrialList::FindFullName("AsyncSetAsDefault"); | |
279 if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableAsyncSetAsDefault)) | |
280 return false; | |
281 if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableAsyncSetAsDefault)) | |
282 return true; | |
283 | |
284 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); | |
285 } | |
286 | |
243 } // namespace | 287 } // namespace |
244 | 288 |
289 // static | |
290 bool ShellIntegration::IsSetAsDefaultAsynchronous() { | |
291 return base::win::GetVersion() >= base::win::VERSION_WIN10 && | |
292 IsAsyncSetAsDefaultEnabled(); | |
293 } | |
294 | |
295 void ShellIntegration::SetAsDefaultBrowserAsynchronous() { | |
296 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN10); | |
grt (UTC plus 2)
2015/09/22 18:21:47
DCHECK_GE
Patrick Monette
2015/09/23 22:40:44
Done.
| |
297 | |
298 ResetDefaultBrowser(); | |
299 ShellExecute(0, 0, kSetDefaultBrowserHelpUrl, 0, 0, SW_SHOW); | |
grt (UTC plus 2)
2015/09/22 18:21:47
does something like this work?
CommandLine cmd(L"o
Patrick Monette
2015/09/23 22:40:44
Done.
| |
300 } | |
301 | |
245 ShellIntegration::DefaultWebClientSetPermission | 302 ShellIntegration::DefaultWebClientSetPermission |
246 ShellIntegration::CanSetAsDefaultBrowser() { | 303 ShellIntegration::CanSetAsDefaultBrowser() { |
247 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 304 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
248 if (distribution->GetDefaultBrowserControlPolicy() != | 305 if (distribution->GetDefaultBrowserControlPolicy() != |
249 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 306 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
250 return SET_DEFAULT_NOT_ALLOWED; | 307 return SET_DEFAULT_NOT_ALLOWED; |
251 | 308 |
252 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 309 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
253 return SET_DEFAULT_UNATTENDED; | 310 return SET_DEFAULT_UNATTENDED; |
311 else if (IsSetAsDefaultAsynchronous()) | |
312 return SET_DEFAULT_ASYNCHRONOUS; | |
254 else | 313 else |
255 return SET_DEFAULT_INTERACTIVE; | 314 return SET_DEFAULT_INTERACTIVE; |
256 } | 315 } |
257 | 316 |
258 bool ShellIntegration::SetAsDefaultBrowser() { | 317 bool ShellIntegration::SetAsDefaultBrowser() { |
259 base::FilePath chrome_exe; | 318 base::FilePath chrome_exe; |
260 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 319 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
261 LOG(ERROR) << "Error getting app exe path"; | 320 LOG(ERROR) << "Error getting app exe path"; |
262 return false; | 321 return false; |
263 } | 322 } |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 } | 640 } |
582 | 641 |
583 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + | 642 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + |
584 installer::kLnkExt); | 643 installer::kLnkExt); |
585 if (base::PathExists(shortcut)) | 644 if (base::PathExists(shortcut)) |
586 return shortcut; | 645 return shortcut; |
587 } | 646 } |
588 | 647 |
589 return base::FilePath(); | 648 return base::FilePath(); |
590 } | 649 } |
650 | |
651 void ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() { | |
652 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
653 | |
654 if (IsSetAsDefaultAsynchronous()) { | |
655 StartupBrowserCreator::AddUrlFilter( | |
656 GURL(kSetDefaultBrowserHelpUrl), | |
657 base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, true)); | |
658 | |
659 // Remember the start time. | |
660 start_time_ = base::TimeTicks::Now(); | |
661 | |
662 // Start the timer. | |
663 if (!async_timer_) { | |
664 async_timer_.reset(new base::OneShotTimer<DefaultWebClientWorker>()); | |
665 } | |
666 | |
667 base::TimeDelta timer_duration = base::TimeDelta::FromMinutes(2); | |
668 std::string value = variations::GetVariationParamValue( | |
669 kAsyncSetAsDefaultExperimentName, "TimerDuration"); | |
670 if (!value.empty()) { | |
671 timer_duration = base::TimeDelta::FromSeconds(std::atoi(value.c_str())); | |
672 } | |
673 | |
674 async_timer_->Start( | |
675 FROM_HERE, timer_duration, | |
676 base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, false)); | |
677 } | |
678 } | |
679 | |
680 bool ShellIntegration::DefaultBrowserWorker::UninitializeSetAsDefault( | |
681 bool succeeded) { | |
682 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
683 | |
684 if (IsSetAsDefaultAsynchronous()) { | |
685 GURL default_browser_help_url(kSetDefaultBrowserHelpUrl); | |
686 if (!async_timer_) { | |
687 // If the timer isnt set, that means it has already fired and | |
688 // CompleteSetAsDefault should not be called again. | |
689 DCHECK(!StartupBrowserCreator::UrlFilterExists(default_browser_help_url)); | |
690 return false; | |
691 } | |
692 | |
693 // Record the duration from starting to set as default browser to now. | |
694 UMA_HISTOGRAM_TIMES("AsyncSetAsDefault.Duration", | |
695 base::TimeTicks::Now() - start_time_); | |
696 if (succeeded) { | |
Alexei Svitkine (slow)
2015/09/22 15:20:43
Nit: No {}'s for 1-line bodies. Same for code in t
Patrick Monette
2015/09/22 17:11:09
Done.
| |
697 UMA_HISTOGRAM_COUNTS("AsyncSetAsDefault.Success", 1); | |
Alexei Svitkine (slow)
2015/09/22 15:20:43
For, for a boolean histogram, you should use UMA_H
Patrick Monette
2015/09/22 17:11:09
Done.
The reasoning was that we already have the t
| |
698 } | |
699 | |
700 // Destroy timer. | |
701 async_timer_.reset(); | |
702 | |
703 // Remove filter if it exists. | |
704 if (StartupBrowserCreator::UrlFilterExists(default_browser_help_url)) | |
705 StartupBrowserCreator::RemoveUrlFilter(default_browser_help_url); | |
706 } | |
707 return true; | |
708 } | |
OLD | NEW |