Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: chrome/browser/shell_integration_win.cc

Issue 1832853003: DefaultBrowserWorker now fully supports opening the settings for Win10 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 makes
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(const base::Closure& on_finished_callback) {
grt (UTC plus 2) 2016/03/30 15:55:09 is there a reason not to move this into the ctor a
Patrick Monette 2016/03/30 22:21:55 No reason. Changed it!
239 const wchar_t url_association_format[] =
grt (UTC plus 2) 2016/03/30 15:55:10 nit: static const wchar_t kUrlAssociationFormat[]
Patrick Monette 2016/03/30 22:21:55 Done.
240 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
241 L"%ls\\UserChoice";
242
243 base::Callback<void(bool)> conclude_settings_interaction_callback =
244 base::Bind(&OpenSystemSettingsHelper::
245 ConcludeOpenSystemSettingsToDefaultBrowser,
246 this, on_finished_callback);
grt (UTC plus 2) 2016/03/30 15:55:10 what's the advantage of binding on_finished_callba
Patrick Monette 2016/03/30 22:21:55 Done.
247
248 http_registry_key_ = CreateRegistryKeyWatcher(
249 base::StringPrintf(url_association_format, L"http").c_str(),
250 conclude_settings_interaction_callback);
251 https_registry_key_ = CreateRegistryKeyWatcher(
252 base::StringPrintf(url_association_format, L"https").c_str(),
253 conclude_settings_interaction_callback);
254
255 timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(2),
256 base::Bind(conclude_settings_interaction_callback, false));
257 }
258
259 // Either the timer or the registry watcher signaled the end of the
260 // interaction. Make sure the other signal is ignored.
261 void Clear() {
262 run_once_ = false;
263 timer_.Reset();
264 http_registry_key_.reset();
265 https_registry_key_.reset();
266 }
267
268 private:
269 void ConcludeOpenSystemSettingsToDefaultBrowser(
270 const base::Closure& on_finished_callback,
271 bool is_registry_watcher) {
grt (UTC plus 2) 2016/03/30 15:55:09 i think the code would be a little more readable i
Patrick Monette 2016/03/30 22:21:55 Done for "a reg key was modified" and kept Conclud
272 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
273
274 // It is possible to get here if only one of the http/https url association
275 // was changed, which is not enough for GetDefaultBrowser() to return
276 // IS_DEFAULT.
277 if (is_registry_watcher) {
278 registry_watcher_count_++;
279 if (registry_watcher_count_ != 2)
280 return;
281 }
282
283 if (!run_once_)
grt (UTC plus 2) 2016/03/30 15:55:10 you can do away with |run_once_| if you keep on_fi
Patrick Monette 2016/03/30 22:21:55 Done. The naked new in SetAsDefaultBrowserUsingSys
284 return;
285 Clear();
286
287 on_finished_callback.Run();
288 }
289
290 scoped_ptr<base::win::RegKey> CreateRegistryKeyWatcher(
grt (UTC plus 2) 2016/03/30 15:55:09 wdyt of getting rid of the scoped_ptrs and changin
Patrick Monette 2016/03/30 22:21:55 Kept this as-is to use with the new vector of scop
291 const wchar_t* registry_value,
292 const base::Callback<void(bool)>&
293 conclude_settings_interaction_callback) {
294 scoped_ptr<base::win::RegKey> reg_key(
295 new base::win::RegKey(HKEY_CURRENT_USER, registry_value, KEY_READ));
296
297 if (reg_key->Valid()) {
298 reg_key->StartWatching(
299 base::Bind(conclude_settings_interaction_callback, true));
300 }
301 return reg_key;
302 }
303
304 base::OneShotTimer timer_;
305 scoped_ptr<base::win::RegKey> http_registry_key_;
306 scoped_ptr<base::win::RegKey> https_registry_key_;
307
308 // Used to make sure only one of the timer or the registry watcher's signal is
309 // taken in consideration.
310 bool run_once_ = true;
311
312 // The number of time a registry key watcher fired.
313 int registry_watcher_count_ = 0;
314
315 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper);
316 };
317
218 } // namespace 318 } // namespace
219 319
220 bool SetAsDefaultBrowser() { 320 bool SetAsDefaultBrowser() {
221 base::FilePath chrome_exe; 321 base::FilePath chrome_exe;
222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 322 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
223 LOG(ERROR) << "Error getting app exe path"; 323 LOG(ERROR) << "Error getting app exe path";
224 return false; 324 return false;
225 } 325 }
226 326
227 // From UI currently we only allow setting default browser for current user. 327 // From UI currently we only allow setting default browser for current user.
(...skipping 18 matching lines...) Expand all
246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 346 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { 347 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) {
248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; 348 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
249 return false; 349 return false;
250 } 350 }
251 351
252 VLOG(1) << "Set-default-browser Windows UI completed."; 352 VLOG(1) << "Set-default-browser Windows UI completed.";
253 return true; 353 return true;
254 } 354 }
255 355
356 void SetAsDefaultBrowserUsingSystemSettings(
357 const base::Closure& on_finished_callback) {
358 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
359
360 scoped_refptr<OpenSystemSettingsHelper> helper(new OpenSystemSettingsHelper);
361 helper->Initialize(on_finished_callback);
362
363 base::FilePath chrome_exe;
364 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
365 NOTREACHED() << "Error getting app exe path";
366 return;
367 }
368
369 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
370 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe);
371 }
372
256 bool SetAsDefaultProtocolClient(const std::string& protocol) { 373 bool SetAsDefaultProtocolClient(const std::string& protocol) {
257 if (protocol.empty()) 374 if (protocol.empty())
258 return false; 375 return false;
259 376
260 base::FilePath chrome_exe; 377 base::FilePath chrome_exe;
261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 378 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
262 LOG(ERROR) << "Error getting app exe path"; 379 LOG(ERROR) << "Error getting app exe path";
263 return false; 380 return false;
264 } 381 }
265 382
(...skipping 29 matching lines...) Expand all
295 return true; 412 return true;
296 } 413 }
297 414
298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { 415 DefaultWebClientSetPermission CanSetAsDefaultBrowser() {
299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 416 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
300 if (distribution->GetDefaultBrowserControlPolicy() != 417 if (distribution->GetDefaultBrowserControlPolicy() !=
301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) 418 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
302 return SET_DEFAULT_NOT_ALLOWED; 419 return SET_DEFAULT_NOT_ALLOWED;
303 if (ShellUtil::CanMakeChromeDefaultUnattended()) 420 if (ShellUtil::CanMakeChromeDefaultUnattended())
304 return SET_DEFAULT_UNATTENDED; 421 return SET_DEFAULT_UNATTENDED;
422 if (IsSetAsDefaultUsingSystemSettings())
423 return SET_DEFAULT_OPEN_SETTINGS;
305 return SET_DEFAULT_INTERACTIVE; 424 return SET_DEFAULT_INTERACTIVE;
306 } 425 }
307 426
308 bool IsElevationNeededForSettingDefaultProtocolClient() { 427 bool IsElevationNeededForSettingDefaultProtocolClient() {
309 return base::win::GetVersion() < base::win::VERSION_WIN8; 428 return base::win::GetVersion() < base::win::VERSION_WIN8;
310 } 429 }
311 430
312 base::string16 GetApplicationNameForProtocol(const GURL& url) { 431 base::string16 GetApplicationNameForProtocol(const GURL& url) {
313 // Windows 8 or above requires a new protocol association query. 432 // Windows 8 or above requires a new protocol association query.
314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) 433 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 654
536 shortcut = programs_folder.Append(shortcut_name); 655 shortcut = programs_folder.Append(shortcut_name);
537 if (base::PathExists(shortcut)) 656 if (base::PathExists(shortcut))
538 return shortcut; 657 return shortcut;
539 } 658 }
540 659
541 return base::FilePath(); 660 return base::FilePath();
542 } 661 }
543 662
544 } // namespace shell_integration 663 } // namespace shell_integration
OLDNEW
« chrome/browser/shell_integration.h ('K') | « chrome/browser/shell_integration.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698