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

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: 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.
Mark Mentovai 2011/05/23 00:23:52 I haven’t reviewed this file.
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
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, 280 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
281 chrome_exe.value(), true)) { 281 chrome_exe.value(), true)) {
282 LOG(ERROR) << "Chrome could not be set as default browser."; 282 LOG(ERROR) << "Chrome could not be set as default browser.";
283 return false; 283 return false;
284 } 284 }
285 285
286 VLOG(1) << "Chrome registered as default browser."; 286 VLOG(1) << "Chrome registered as default browser.";
287 return true; 287 return true;
288 } 288 }
289 289
290 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { 290 bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
291 FilePath chrome_exe;
292 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
293 LOG(ERROR) << "Error getting app exe path";
294 return false;
295 }
296
297 std::wstring wprotocol = UTF8ToWide(protocol);
298 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
299 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
300 wprotocol)) {
301 LOG(ERROR) << "Chrome could not be set as default handler for "
302 << protocol << ".";
303 return false;
304 }
305
306 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
307 return true;
308 }
309
310 ShellIntegration::DefaultClientAppState ShellIntegration::IsDefaultBrowser() {
291 // First determine the app path. If we can't determine what that is, we have 311 // First determine the app path. If we can't determine what that is, we have
292 // bigger fish to fry... 312 // bigger fish to fry...
293 FilePath app_path; 313 FilePath app_path;
294 if (!PathService::Get(base::FILE_EXE, &app_path)) { 314 if (!PathService::Get(base::FILE_EXE, &app_path)) {
295 LOG(ERROR) << "Error getting app exe path"; 315 LOG(ERROR) << "Error getting app exe path";
296 return UNKNOWN_DEFAULT_BROWSER; 316 return UNKNOWN_DEFAULT_CLIENT_APP;
297 } 317 }
298 // When we check for default browser we don't necessarily want to count file 318 // 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, 319 // 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 320 // 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 321 // 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 322 // 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 323 // 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 324 // 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" 325 // flag. There is doubtless some other key we can hook into to cause "Repair"
306 // to show up in Add/Remove programs for us. 326 // to show up in Add/Remove programs for us.
307 const std::wstring kChromeProtocols[] = {L"http", L"https"}; 327 const std::wstring kChromeProtocols[] = {L"http", L"https"};
308 328
309 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 329 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
310 IApplicationAssociationRegistration* pAAR; 330 IApplicationAssociationRegistration* pAAR;
311 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, 331 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
312 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), 332 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration),
313 (void**)&pAAR); 333 (void**)&pAAR);
314 if (!SUCCEEDED(hr)) 334 if (!SUCCEEDED(hr))
315 return NOT_DEFAULT_BROWSER; 335 return NOT_DEFAULT_CLIENT_APP;
316 336
317 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 337 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
318 std::wstring app_name = dist->GetApplicationName(); 338 std::wstring app_name = dist->GetApplicationName();
319 // If a user specific default browser entry exists, we check for that 339 // 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 340 // 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. 341 // Google Chrome or Chromium so we do not append suffix to app name.
322 std::wstring suffix; 342 std::wstring suffix;
323 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) 343 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
324 app_name += suffix; 344 app_name += suffix;
325 345
326 for (int i = 0; i < _countof(kChromeProtocols); i++) { 346 for (int i = 0; i < _countof(kChromeProtocols); i++) {
327 BOOL result = TRUE; 347 BOOL result = TRUE;
328 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL, 348 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
329 AL_EFFECTIVE, app_name.c_str(), &result); 349 AL_EFFECTIVE, app_name.c_str(), &result);
330 if (!SUCCEEDED(hr) || result == FALSE) { 350 if (!SUCCEEDED(hr) || result == FALSE) {
331 pAAR->Release(); 351 pAAR->Release();
332 return NOT_DEFAULT_BROWSER; 352 return NOT_DEFAULT_CLIENT_APP;
333 } 353 }
334 } 354 }
335 pAAR->Release(); 355 pAAR->Release();
336 } else { 356 } else {
337 std::wstring short_app_path; 357 std::wstring short_app_path;
338 GetShortPathName(app_path.value().c_str(), 358 GetShortPathName(app_path.value().c_str(),
339 WriteInto(&short_app_path, MAX_PATH), 359 WriteInto(&short_app_path, MAX_PATH),
340 MAX_PATH); 360 MAX_PATH);
341 361
342 // open command for protocol associations 362 // open command for protocol associations
343 for (int i = 0; i < _countof(kChromeProtocols); i++) { 363 for (int i = 0; i < _countof(kChromeProtocols); i++) {
344 // Check in HKEY_CLASSES_ROOT that is the result of merge between 364 // Check in HKEY_CLASSES_ROOT that is the result of merge between
345 // HKLM and HKCU 365 // HKLM and HKCU
346 HKEY root_key = HKEY_CLASSES_ROOT; 366 HKEY root_key = HKEY_CLASSES_ROOT;
347 // Check <protocol>\shell\open\command 367 // Check <protocol>\shell\open\command
348 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); 368 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen);
349 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); 369 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ);
350 std::wstring value; 370 std::wstring value;
351 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) 371 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS))
352 return NOT_DEFAULT_BROWSER; 372 return NOT_DEFAULT_CLIENT_APP;
353 // Need to normalize path in case it's been munged. 373 // Need to normalize path in case it's been munged.
354 CommandLine command_line = CommandLine::FromString(value); 374 CommandLine command_line = CommandLine::FromString(value);
355 std::wstring short_path; 375 std::wstring short_path;
356 GetShortPathName(command_line.GetProgram().value().c_str(), 376 GetShortPathName(command_line.GetProgram().value().c_str(),
357 WriteInto(&short_path, MAX_PATH), MAX_PATH); 377 WriteInto(&short_path, MAX_PATH), MAX_PATH);
358 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) 378 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
359 return NOT_DEFAULT_BROWSER; 379 return NOT_DEFAULT_CLIENT_APP;
360 } 380 }
361 } 381 }
362 return IS_DEFAULT_BROWSER; 382 return IS_DEFAULT_CLIENT_APP;
383 }
384
385 ShellIntegration::DefaultClientAppState
386 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
387 // First determine the app path. If we can't determine what that is, we have
388 // bigger fish to fry...
389 FilePath app_path;
390 if (!PathService::Get(base::FILE_EXE, &app_path)) {
391 LOG(ERROR) << "Error getting app exe path";
392 return UNKNOWN_DEFAULT_CLIENT_APP;
393 }
394
395 std::wstring wprotocol = UTF8ToWide(protocol);
396
397 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
398 IApplicationAssociationRegistration* pAAR;
399 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
400 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration),
401 (void**)&pAAR);
402 if (!SUCCEEDED(hr))
403 return NOT_DEFAULT_CLIENT_APP;
404
405 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
406 std::wstring app_name = dist->GetApplicationName();
407 // If a user specific default browser entry exists, we check for that
408 // app name being default. If not, then default browser is just called
409 // Google Chrome or Chromium so we do not append suffix to app name.
410 std::wstring suffix;
411 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
412 app_name += suffix;
413
414 BOOL result = TRUE;
415 hr = pAAR->QueryAppIsDefault(wprotocol.c_str(), AT_URLPROTOCOL,
416 AL_EFFECTIVE, app_name.c_str(), &result);
417 if (!SUCCEEDED(hr) || result == FALSE) {
418 pAAR->Release();
419 return NOT_DEFAULT_CLIENT_APP;
420 }
421 pAAR->Release();
422 } else {
423 std::wstring short_app_path;
424 GetShortPathName(app_path.value().c_str(),
425 WriteInto(&short_app_path, MAX_PATH),
426 MAX_PATH);
427
428 // open command for protocol associations
429 // Check in HKEY_CLASSES_ROOT that is the result of merge between
430 // HKLM and HKCU
431 HKEY root_key = HKEY_CLASSES_ROOT;
432 // Check <protocol>\shell\open\command
433 std::wstring key_path(wprotocol + ShellUtil::kRegShellOpen);
434 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ);
435 std::wstring value;
436 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS))
437 return NOT_DEFAULT_CLIENT_APP;
438 // Need to normalize path in case it's been munged.
439 CommandLine command_line = CommandLine::FromString(value);
440 std::wstring short_path;
441 GetShortPathName(command_line.GetProgram().value().c_str(),
442 WriteInto(&short_path, MAX_PATH), MAX_PATH);
443 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
444 return NOT_DEFAULT_CLIENT_APP;
445 }
446 return IS_DEFAULT_CLIENT_APP;
363 } 447 }
364 448
365 // There is no reliable way to say which browser is default on a machine (each 449 // 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 450 // 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 451 // protocol handler. Even this handler is located at different places in
368 // registry on XP and Vista: 452 // registry on XP and Vista:
369 // - HKCR\http\shell\open\command (XP) 453 // - HKCR\http\shell\open\command (XP)
370 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ 454 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
371 // http\UserChoice (Vista) 455 // http\UserChoice (Vista)
372 // This method checks if Firefox is defualt browser by checking these 456 // 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); 498 profile_path);
415 } 499 }
416 500
417 void ShellIntegration::MigrateChromiumShortcuts() { 501 void ShellIntegration::MigrateChromiumShortcuts() {
418 if (base::win::GetVersion() < base::win::VERSION_WIN7) 502 if (base::win::GetVersion() < base::win::VERSION_WIN7)
419 return; 503 return;
420 504
421 BrowserThread::PostTask( 505 BrowserThread::PostTask(
422 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask()); 506 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask());
423 } 507 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698