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

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

Issue 6961013: Allow chrome to become the os default handler for arbitrary protocols on mac/win. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More changes for comments; fix for a bug introduced with DefaultWebClientWorker refactoring Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <shobjidl.h> 8 #include <shobjidl.h>
9 #include <propkey.h> 9 #include <propkey.h>
10 #include <propvarutil.h> 10 #include <propvarutil.h>
11 11
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/file_util.h" 13 #include "base/file_util.h"
14 #include "base/message_loop.h" 14 #include "base/message_loop.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/task.h" 17 #include "base/task.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "base/win/registry.h" 19 #include "base/win/registry.h"
20 #include "base/win/scoped_comptr.h" 20 #include "base/win/scoped_comptr.h"
21 #include "base/win/windows_version.h" 21 #include "base/win/windows_version.h"
22 #include "chrome/browser/web_applications/web_app.h" 22 #include "chrome/browser/web_applications/web_app.h"
23 #include "chrome/common/chrome_constants.h" 23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_paths.h" 24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_paths_internal.h" 25 #include "chrome/common/chrome_paths_internal.h"
26 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/installer/setup/setup_util.h"
27 #include "chrome/installer/util/browser_distribution.h" 28 #include "chrome/installer/util/browser_distribution.h"
28 #include "chrome/installer/util/create_reg_key_work_item.h" 29 #include "chrome/installer/util/create_reg_key_work_item.h"
29 #include "chrome/installer/util/set_reg_value_work_item.h" 30 #include "chrome/installer/util/set_reg_value_work_item.h"
30 #include "chrome/installer/util/shell_util.h" 31 #include "chrome/installer/util/shell_util.h"
31 #include "chrome/installer/util/util_constants.h" 32 #include "chrome/installer/util/util_constants.h"
32 #include "chrome/installer/util/work_item.h" 33 #include "chrome/installer/util/work_item.h"
33 #include "chrome/installer/util/work_item_list.h" 34 #include "chrome/installer/util/work_item_list.h"
34 #include "content/browser/browser_thread.h" 35 #include "content/browser/browser_thread.h"
35 36
36 namespace { 37 namespace {
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, 281 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
281 chrome_exe.value(), true)) { 282 chrome_exe.value(), true)) {
282 LOG(ERROR) << "Chrome could not be set as default browser."; 283 LOG(ERROR) << "Chrome could not be set as default browser.";
283 return false; 284 return false;
284 } 285 }
285 286
286 VLOG(1) << "Chrome registered as default browser."; 287 VLOG(1) << "Chrome registered as default browser.";
287 return true; 288 return true;
288 } 289 }
289 290
290 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { 291 bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
292 if (protocol.empty())
293 return false;
294
295 FilePath chrome_exe;
296 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
297 LOG(ERROR) << "Error getting app exe path";
298 return false;
299 }
300
301 std::wstring wprotocol = UTF8ToWide(protocol);
302 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
303 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
304 wprotocol)) {
305 LOG(ERROR) << "Chrome could not be set as default handler for "
306 << protocol << ".";
307 return false;
308 }
309
310 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
311 return true;
312 }
313
314 ShellIntegration::DefaultWebClientState ShellIntegration::IsDefaultBrowser() {
291 // First determine the app path. If we can't determine what that is, we have 315 // First determine the app path. If we can't determine what that is, we have
292 // bigger fish to fry... 316 // bigger fish to fry...
293 FilePath app_path; 317 FilePath app_path;
294 if (!PathService::Get(base::FILE_EXE, &app_path)) { 318 if (!PathService::Get(base::FILE_EXE, &app_path)) {
295 LOG(ERROR) << "Error getting app exe path"; 319 LOG(ERROR) << "Error getting app exe path";
296 return UNKNOWN_DEFAULT_BROWSER; 320 return UNKNOWN_DEFAULT_WEB_CLIENT;
297 } 321 }
298 // When we check for default browser we don't necessarily want to count file 322 // When we check for default browser we don't necessarily want to count file
299 // type handlers and icons as having changed the default browser status, 323 // type handlers and icons as having changed the default browser status,
300 // since the user may have changed their shell settings to cause HTML files 324 // since the user may have changed their shell settings to cause HTML files
301 // to open with a text editor for example. We also don't want to aggressively 325 // to open with a text editor for example. We also don't want to aggressively
302 // claim FTP, since the user may have a separate FTP client. It is an open 326 // claim FTP, since the user may have a separate FTP client. It is an open
303 // question as to how to "heal" these settings. Perhaps the user should just 327 // question as to how to "heal" these settings. Perhaps the user should just
304 // re-run the installer or run with the --set-default-browser command line 328 // re-run the installer or run with the --set-default-browser command line
305 // flag. There is doubtless some other key we can hook into to cause "Repair" 329 // flag. There is doubtless some other key we can hook into to cause "Repair"
306 // to show up in Add/Remove programs for us. 330 // to show up in Add/Remove programs for us.
307 const std::wstring kChromeProtocols[] = {L"http", L"https"}; 331 const std::wstring kChromeProtocols[] = {L"http", L"https"};
308 332
309 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 333 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
310 IApplicationAssociationRegistration* pAAR; 334 base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR;
311 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, 335 HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration,
312 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), 336 NULL, CLSCTX_INPROC);
313 (void**)&pAAR);
314 if (!SUCCEEDED(hr)) 337 if (!SUCCEEDED(hr))
315 return NOT_DEFAULT_BROWSER; 338 return NOT_DEFAULT_WEB_CLIENT;
316 339
317 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 340 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
318 std::wstring app_name = dist->GetApplicationName(); 341 std::wstring app_name = dist->GetApplicationName();
319 // If a user specific default browser entry exists, we check for that 342 // If a user specific default browser entry exists, we check for that
320 // app name being default. If not, then default browser is just called 343 // app name being default. If not, then default browser is just called
321 // Google Chrome or Chromium so we do not append suffix to app name. 344 // Google Chrome or Chromium so we do not append suffix to app name.
322 std::wstring suffix; 345 std::wstring suffix;
323 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) 346 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
324 app_name += suffix; 347 app_name += suffix;
325 348
326 for (int i = 0; i < _countof(kChromeProtocols); i++) { 349 for (int i = 0; i < _countof(kChromeProtocols); i++) {
327 BOOL result = TRUE; 350 BOOL result = TRUE;
328 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL, 351 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
329 AL_EFFECTIVE, app_name.c_str(), &result); 352 AL_EFFECTIVE, app_name.c_str(), &result);
330 if (!SUCCEEDED(hr) || result == FALSE) { 353 if (!SUCCEEDED(hr) || result == FALSE) {
331 pAAR->Release(); 354 return NOT_DEFAULT_WEB_CLIENT;
332 return NOT_DEFAULT_BROWSER;
333 } 355 }
334 } 356 }
335 pAAR->Release();
336 } else { 357 } else {
337 std::wstring short_app_path; 358 std::wstring short_app_path;
338 GetShortPathName(app_path.value().c_str(), 359 DWORD get_path_result = GetShortPathName(app_path.value().c_str(),
339 WriteInto(&short_app_path, MAX_PATH), 360 WriteInto(&short_app_path, MAX_PATH), MAX_PATH);
340 MAX_PATH); 361 if (!get_path_result || get_path_result > MAX_PATH) {
362 LOG(ERROR) << "GetShortPathName error in IsDefaultBrowser.";
363 return UNKNOWN_DEFAULT_WEB_CLIENT;
364 }
341 365
342 // open command for protocol associations 366 // open command for protocol associations
343 for (int i = 0; i < _countof(kChromeProtocols); i++) { 367 for (int i = 0; i < _countof(kChromeProtocols); i++) {
344 // Check in HKEY_CLASSES_ROOT that is the result of merge between 368 // Check in HKEY_CLASSES_ROOT that is the result of merge between
345 // HKLM and HKCU 369 // HKLM and HKCU
346 HKEY root_key = HKEY_CLASSES_ROOT; 370 HKEY root_key = HKEY_CLASSES_ROOT;
347 // Check <protocol>\shell\open\command 371 // Check <protocol>\shell\open\command
348 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); 372 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen);
349 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); 373 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ);
350 std::wstring value; 374 std::wstring value;
351 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) 375 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS))
352 return NOT_DEFAULT_BROWSER; 376 return NOT_DEFAULT_WEB_CLIENT;
353 // Need to normalize path in case it's been munged. 377 // Need to normalize path in case it's been munged.
354 CommandLine command_line = CommandLine::FromString(value); 378 CommandLine command_line = CommandLine::FromString(value);
355 std::wstring short_path; 379 std::wstring short_path;
356 GetShortPathName(command_line.GetProgram().value().c_str(), 380 get_path_result = GetShortPathName(
357 WriteInto(&short_path, MAX_PATH), MAX_PATH); 381 command_line.GetProgram().value().c_str(),
382 WriteInto(&short_path, MAX_PATH), MAX_PATH);
383 if (!get_path_result || get_path_result > MAX_PATH) {
384 LOG(ERROR) << "GetShortPathName error in IsDefaultBrowser.";
385 return UNKNOWN_DEFAULT_WEB_CLIENT;
386 }
358 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) 387 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
359 return NOT_DEFAULT_BROWSER; 388 return NOT_DEFAULT_WEB_CLIENT;
360 } 389 }
361 } 390 }
362 return IS_DEFAULT_BROWSER; 391 return IS_DEFAULT_WEB_CLIENT;
392 }
393
394 ShellIntegration::DefaultWebClientState
395 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
396 if (protocol.empty())
397 return UNKNOWN_DEFAULT_WEB_CLIENT;
398
399 // Determine the app path. If we can't determine what that is, we have
400 // bigger fish to fry...
401 FilePath app_path;
402 if (!PathService::Get(base::FILE_EXE, &app_path)) {
403 LOG(ERROR) << "Error getting app exe path";
404 return UNKNOWN_DEFAULT_WEB_CLIENT;
405 }
406
407 std::wstring wprotocol = UTF8ToWide(protocol);
408
409 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
410 base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR;
411 HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration,
412 NULL, CLSCTX_INPROC);
413 if (!SUCCEEDED(hr))
414 return NOT_DEFAULT_WEB_CLIENT;
415
416 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
417 std::wstring app_name = dist->GetApplicationName();
418 // If a user specific default browser entry exists, we check for that
419 // app name being default. If not, then default browser is just called
420 // Google Chrome or Chromium so we do not append suffix to app name.
421 std::wstring suffix;
422 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
423 app_name += suffix;
424
425 BOOL result = TRUE;
426 hr = pAAR->QueryAppIsDefault(wprotocol.c_str(), AT_URLPROTOCOL,
427 AL_EFFECTIVE, app_name.c_str(), &result);
428 if (!SUCCEEDED(hr) || result == FALSE) {
429 return NOT_DEFAULT_WEB_CLIENT;
430 }
431 } else {
432 std::wstring short_app_path;
433 DWORD get_path_result = GetShortPathName(app_path.value().c_str(),
434 WriteInto(&short_app_path, MAX_PATH), MAX_PATH);
435 if (!get_path_result || get_path_result > MAX_PATH) {
436 LOG(ERROR) << "GetShortPathName error in IsDefaultProtocolClient.";
437 return UNKNOWN_DEFAULT_WEB_CLIENT;
438 }
439
440 // open command for protocol associations
441 // Check in HKEY_CLASSES_ROOT that is the result of merge between
442 // HKLM and HKCU
443 HKEY root_key = HKEY_CLASSES_ROOT;
444 // Check <protocol>\shell\open\command
445 std::wstring key_path(wprotocol + ShellUtil::kRegShellOpen);
446 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ);
447 std::wstring value;
448 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS))
449 return NOT_DEFAULT_WEB_CLIENT;
450 // Need to normalize path in case it's been munged.
451 CommandLine command_line = CommandLine::FromString(value);
452 std::wstring short_path;
453 get_path_result = GetShortPathName(
454 command_line.GetProgram().value().c_str(),
455 WriteInto(&short_path, MAX_PATH), MAX_PATH);
456 if (!get_path_result || get_path_result > MAX_PATH) {
457 LOG(ERROR) << "GetShortPathName error in IsDefaultProtocolClient.";
458 return UNKNOWN_DEFAULT_WEB_CLIENT;
459 }
460 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
461 return NOT_DEFAULT_WEB_CLIENT;
462 }
463 return IS_DEFAULT_WEB_CLIENT;
363 } 464 }
364 465
365 // There is no reliable way to say which browser is default on a machine (each 466 // There is no reliable way to say which browser is default on a machine (each
366 // browser can have some of the protocols/shortcuts). So we look for only HTTP 467 // browser can have some of the protocols/shortcuts). So we look for only HTTP
367 // protocol handler. Even this handler is located at different places in 468 // protocol handler. Even this handler is located at different places in
368 // registry on XP and Vista: 469 // registry on XP and Vista:
369 // - HKCR\http\shell\open\command (XP) 470 // - HKCR\http\shell\open\command (XP)
370 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ 471 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
371 // http\UserChoice (Vista) 472 // http\UserChoice (Vista)
372 // This method checks if Firefox is defualt browser by checking these 473 // This method checks if Firefox is defualt browser by checking these
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 profile_path); 515 profile_path);
415 } 516 }
416 517
417 void ShellIntegration::MigrateChromiumShortcuts() { 518 void ShellIntegration::MigrateChromiumShortcuts() {
418 if (base::win::GetVersion() < base::win::VERSION_WIN7) 519 if (base::win::GetVersion() < base::win::VERSION_WIN7)
419 return; 520 return;
420 521
421 BrowserThread::PostTask( 522 BrowserThread::PostTask(
422 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask()); 523 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask());
423 } 524 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698