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

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: Addressed 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 <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/ptr_util.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"
30 #include "chrome/browser/web_applications/web_app.h" 37 #include "chrome/browser/web_applications/web_app.h"
31 #include "chrome/common/chrome_constants.h" 38 #include "chrome/common/chrome_constants.h"
32 #include "chrome/common/chrome_paths_internal.h" 39 #include "chrome/common/chrome_paths_internal.h"
33 #include "chrome/common/chrome_switches.h" 40 #include "chrome/common/chrome_switches.h"
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 case ShellUtil::NOT_DEFAULT: 215 case ShellUtil::NOT_DEFAULT:
209 return DefaultWebClientState::NOT_DEFAULT; 216 return DefaultWebClientState::NOT_DEFAULT;
210 case ShellUtil::IS_DEFAULT: 217 case ShellUtil::IS_DEFAULT:
211 return DefaultWebClientState::IS_DEFAULT; 218 return DefaultWebClientState::IS_DEFAULT;
212 default: 219 default:
213 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); 220 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
214 return DefaultWebClientState::UNKNOWN_DEFAULT; 221 return DefaultWebClientState::UNKNOWN_DEFAULT;
215 } 222 }
216 } 223 }
217 224
225 bool IsSetAsDefaultUsingSystemSettings() {
226 return base::win::GetVersion() >= base::win::VERSION_WIN10;
227 }
228
229 // There is no way to make sure the user is done with the system settings, but a
230 // signal that the interaction is finished is needed for UMA. A timer of 2
231 // minutes is used as a substitute. The registry keys for the protocol
232 // association with an app are also monitored to signal the end of the
233 // interaction early when it is clear that the user made a choice (e.g. http
234 // and https for default browser).
235 //
236 // This helper class manages both the timer and the registry watchers and makes
237 // sure the callback for the end of the settings interaction is only run once.
238 // This class also manages its own lifetime.
239 class OpenSystemSettingsHelper {
240 public:
241 // Begin the monitoring and will call |on_finished_callback| when done.
242 // Takes in a null-terminated array of |protocols| whose registry keys must be
243 // watched.
244 static void Begin(const wchar_t* const protocols[],
245 const base::Closure& on_finished_callback) {
246 new OpenSystemSettingsHelper(protocols, on_finished_callback);
247 }
248
249 private:
250 // The reason the settings interaction concluded. Do not modify the ordering
251 // because it is used for UMA.
252 enum ConcludeReason { REGISTRY_WATCHER, TIMEOUT, NUM_CONCLUDE_REASON_TYPES };
253
254 OpenSystemSettingsHelper(const wchar_t* const protocols[],
255 const base::Closure& on_finished_callback)
256 : on_finished_callback_(on_finished_callback), weak_ptr_factory_(this) {
257 static const wchar_t kUrlAssociationFormat[] =
258 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
259 L"%ls\\UserChoice";
260
261 // Remember the start time.
262 start_time_ = base::TimeTicks::Now();
263
264 for (const wchar_t* const* scan = &protocols[0]; *scan != nullptr; ++scan) {
265 AddRegistryKeyWatcher(
266 base::StringPrintf(kUrlAssociationFormat, *scan).c_str());
267 }
268 // Only the watchers that were succesfully initialized are counted.
269 registry_watcher_count_ = registry_key_watchers_.size();
270
271 timer_.Start(
272 FROM_HERE, base::TimeDelta::FromMinutes(2),
273 base::Bind(&OpenSystemSettingsHelper::ConcludeInteraction,
274 weak_ptr_factory_.GetWeakPtr(), ConcludeReason::TIMEOUT));
275 }
276
277 // Called when a change is detected on one of the registry keys being watched.
278 // Note: All types of modification to the registry key will trigger this
279 // function even if the value change is the only one that matters. This
280 // is good enough for now.
281 void OnRegistryKeyChanged() {
282 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
283
284 // Make sure all the registry watchers have fired.
285 if (--registry_watcher_count_ == 0) {
286 UMA_HISTOGRAM_MEDIUM_TIMES(
287 "DefaultBrowser.SettingsInteraction.RegistryWatcherDuration",
288 base::TimeTicks::Now() - start_time_);
289
290 ConcludeInteraction(ConcludeReason::REGISTRY_WATCHER);
291 }
292 }
293
294 // Ends the monitoring with the system settings. Will call
295 // |on_finished_callback_| and then dispose of this class instance to make
296 // sure the callback won't get called subsequently.
297 void ConcludeInteraction(ConcludeReason conclude_reason) {
298 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
299
300 UMA_HISTOGRAM_ENUMERATION(
301 "DefaultBrowser.SettingsInteraction.ConcludeReason", conclude_reason,
302 NUM_CONCLUDE_REASON_TYPES);
303 on_finished_callback_.Run();
304 delete this;
305 }
306
307 // Helper function to create a registry watcher for a given |key_path|. Do
308 // nothing on initialization failure.
309 void AddRegistryKeyWatcher(const wchar_t* key_path) {
310 auto reg_key = make_scoped_ptr(
311 new base::win::RegKey(HKEY_CURRENT_USER, key_path, KEY_NOTIFY));
312
313 if (reg_key->Valid() &&
314 reg_key->StartWatching(
315 base::Bind(&OpenSystemSettingsHelper::OnRegistryKeyChanged,
316 weak_ptr_factory_.GetWeakPtr()))) {
317 registry_key_watchers_.push_back(std::move(reg_key));
318 }
319 }
320
321 // The function to call when the interaction with the system settings is
322 // finished.
323 base::Closure on_finished_callback_;
324
325 // The number of time the registry key watchers must fire.
326 int registry_watcher_count_ = 0;
327
328 // There can be multiple registry key watchers as some settings modify
329 // multiple protocol associations. e.g. Changing the default browser modifies
330 // the http and https associations.
331 std::vector<scoped_ptr<base::win::RegKey>> registry_key_watchers_;
332
333 base::OneShotTimer timer_;
334
335 // Records the time it takes for the final registry watcher to get signaled.
336 base::TimeTicks start_time_;
337
338 // Weak ptrs are used to bind this class to the callbacks of the timer and the
339 // registry watcher. This makes it possible to self-delete after one of the
340 // callbacks is executed to cancel the remaining ones.
341 base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_;
342
343 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper);
344 };
345
218 } // namespace 346 } // namespace
219 347
220 bool SetAsDefaultBrowser() { 348 bool SetAsDefaultBrowser() {
221 base::FilePath chrome_exe; 349 base::FilePath chrome_exe;
222 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 350 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
223 LOG(ERROR) << "Error getting app exe path"; 351 LOG(ERROR) << "Error getting app exe path";
224 return false; 352 return false;
225 } 353 }
226 354
227 // From UI currently we only allow setting default browser for current user. 355 // From UI currently we only allow setting default browser for current user.
(...skipping 18 matching lines...) Expand all
246 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 374 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
247 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { 375 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) {
248 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; 376 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
249 return false; 377 return false;
250 } 378 }
251 379
252 VLOG(1) << "Set-default-browser Windows UI completed."; 380 VLOG(1) << "Set-default-browser Windows UI completed.";
253 return true; 381 return true;
254 } 382 }
255 383
384 void SetAsDefaultBrowserUsingSystemSettings(
385 const base::Closure& on_finished_callback) {
386 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
387
388 base::FilePath chrome_exe;
389 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
390 NOTREACHED() << "Error getting app exe path";
391 on_finished_callback.Run();
392 return;
393 }
394
395 // The helper manages its own lifetime.
396 static const wchar_t* const kProtocols[] = {L"http", L"https", nullptr};
397 OpenSystemSettingsHelper::Begin(kProtocols, on_finished_callback);
398
399 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
400 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe);
401 }
402
256 bool SetAsDefaultProtocolClient(const std::string& protocol) { 403 bool SetAsDefaultProtocolClient(const std::string& protocol) {
257 if (protocol.empty()) 404 if (protocol.empty())
258 return false; 405 return false;
259 406
260 base::FilePath chrome_exe; 407 base::FilePath chrome_exe;
261 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 408 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
262 LOG(ERROR) << "Error getting app exe path"; 409 LOG(ERROR) << "Error getting app exe path";
263 return false; 410 return false;
264 } 411 }
265 412
(...skipping 29 matching lines...) Expand all
295 return true; 442 return true;
296 } 443 }
297 444
298 DefaultWebClientSetPermission CanSetAsDefaultBrowser() { 445 DefaultWebClientSetPermission CanSetAsDefaultBrowser() {
299 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 446 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
300 if (distribution->GetDefaultBrowserControlPolicy() != 447 if (distribution->GetDefaultBrowserControlPolicy() !=
301 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) 448 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
302 return SET_DEFAULT_NOT_ALLOWED; 449 return SET_DEFAULT_NOT_ALLOWED;
303 if (ShellUtil::CanMakeChromeDefaultUnattended()) 450 if (ShellUtil::CanMakeChromeDefaultUnattended())
304 return SET_DEFAULT_UNATTENDED; 451 return SET_DEFAULT_UNATTENDED;
452 if (IsSetAsDefaultUsingSystemSettings())
453 return SET_DEFAULT_OPEN_SETTINGS;
305 return SET_DEFAULT_INTERACTIVE; 454 return SET_DEFAULT_INTERACTIVE;
306 } 455 }
307 456
308 bool IsElevationNeededForSettingDefaultProtocolClient() { 457 bool IsElevationNeededForSettingDefaultProtocolClient() {
309 return base::win::GetVersion() < base::win::VERSION_WIN8; 458 return base::win::GetVersion() < base::win::VERSION_WIN8;
310 } 459 }
311 460
312 base::string16 GetApplicationNameForProtocol(const GURL& url) { 461 base::string16 GetApplicationNameForProtocol(const GURL& url) {
313 // Windows 8 or above requires a new protocol association query. 462 // Windows 8 or above requires a new protocol association query.
314 if (base::win::GetVersion() >= base::win::VERSION_WIN8) 463 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 684
536 shortcut = programs_folder.Append(shortcut_name); 685 shortcut = programs_folder.Append(shortcut_name);
537 if (base::PathExists(shortcut)) 686 if (base::PathExists(shortcut))
538 return shortcut; 687 return shortcut;
539 } 688 }
540 689
541 return base::FilePath(); 690 return base::FilePath();
542 } 691 }
543 692
544 } // namespace shell_integration 693 } // namespace shell_integration
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698