OLD | NEW |
---|---|
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> |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 if (protocol.empty()) | |
292 return false; | |
293 | |
294 FilePath chrome_exe; | |
295 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | |
296 LOG(ERROR) << "Error getting app exe path"; | |
297 return false; | |
298 } | |
299 | |
300 std::wstring wprotocol = UTF8ToWide(protocol); | |
301 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
302 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(), | |
303 wprotocol)) { | |
304 LOG(ERROR) << "Chrome could not be set as default handler for " | |
305 << protocol << "."; | |
306 return false; | |
307 } | |
308 | |
309 VLOG(1) << "Chrome registered as default handler for " << protocol << "."; | |
310 return true; | |
311 } | |
312 | |
313 ShellIntegration::DefaultWebClientState ShellIntegration::IsDefaultBrowser() { | |
291 // First determine the app path. If we can't determine what that is, we have | 314 // First determine the app path. If we can't determine what that is, we have |
292 // bigger fish to fry... | 315 // bigger fish to fry... |
293 FilePath app_path; | 316 FilePath app_path; |
294 if (!PathService::Get(base::FILE_EXE, &app_path)) { | 317 if (!PathService::Get(base::FILE_EXE, &app_path)) { |
295 LOG(ERROR) << "Error getting app exe path"; | 318 LOG(ERROR) << "Error getting app exe path"; |
296 return UNKNOWN_DEFAULT_BROWSER; | 319 return UNKNOWN_DEFAULT_WEB_CLIENT; |
297 } | 320 } |
298 // When we check for default browser we don't necessarily want to count file | 321 // 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, | 322 // 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 | 323 // 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 | 324 // 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 | 325 // 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 | 326 // 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 | 327 // 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" | 328 // flag. There is doubtless some other key we can hook into to cause "Repair" |
306 // to show up in Add/Remove programs for us. | 329 // to show up in Add/Remove programs for us. |
307 const std::wstring kChromeProtocols[] = {L"http", L"https"}; | 330 const std::wstring kChromeProtocols[] = {L"http", L"https"}; |
308 | 331 |
309 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | 332 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
310 IApplicationAssociationRegistration* pAAR; | 333 IApplicationAssociationRegistration* pAAR; |
311 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, | 334 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, |
312 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), | 335 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), |
313 (void**)&pAAR); | 336 (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 pAAR->Release(); |
332 return NOT_DEFAULT_BROWSER; | 355 return NOT_DEFAULT_WEB_CLIENT; |
333 } | 356 } |
334 } | 357 } |
335 pAAR->Release(); | 358 pAAR->Release(); |
336 } else { | 359 } else { |
337 std::wstring short_app_path; | 360 std::wstring short_app_path; |
338 GetShortPathName(app_path.value().c_str(), | 361 GetShortPathName(app_path.value().c_str(), |
339 WriteInto(&short_app_path, MAX_PATH), | 362 WriteInto(&short_app_path, MAX_PATH), |
340 MAX_PATH); | 363 MAX_PATH); |
341 | 364 |
342 // open command for protocol associations | 365 // open command for protocol associations |
343 for (int i = 0; i < _countof(kChromeProtocols); i++) { | 366 for (int i = 0; i < _countof(kChromeProtocols); i++) { |
344 // Check in HKEY_CLASSES_ROOT that is the result of merge between | 367 // Check in HKEY_CLASSES_ROOT that is the result of merge between |
345 // HKLM and HKCU | 368 // HKLM and HKCU |
346 HKEY root_key = HKEY_CLASSES_ROOT; | 369 HKEY root_key = HKEY_CLASSES_ROOT; |
347 // Check <protocol>\shell\open\command | 370 // Check <protocol>\shell\open\command |
348 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); | 371 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); |
349 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); | 372 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); |
350 std::wstring value; | 373 std::wstring value; |
351 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) | 374 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) |
352 return NOT_DEFAULT_BROWSER; | 375 return NOT_DEFAULT_WEB_CLIENT; |
353 // Need to normalize path in case it's been munged. | 376 // Need to normalize path in case it's been munged. |
354 CommandLine command_line = CommandLine::FromString(value); | 377 CommandLine command_line = CommandLine::FromString(value); |
355 std::wstring short_path; | 378 std::wstring short_path; |
356 GetShortPathName(command_line.GetProgram().value().c_str(), | 379 GetShortPathName(command_line.GetProgram().value().c_str(), |
357 WriteInto(&short_path, MAX_PATH), MAX_PATH); | 380 WriteInto(&short_path, MAX_PATH), MAX_PATH); |
358 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) | 381 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) |
359 return NOT_DEFAULT_BROWSER; | 382 return NOT_DEFAULT_WEB_CLIENT; |
360 } | 383 } |
361 } | 384 } |
362 return IS_DEFAULT_BROWSER; | 385 return IS_DEFAULT_WEB_CLIENT; |
386 } | |
387 | |
388 ShellIntegration::DefaultWebClientState | |
389 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) { | |
390 if (protocol.empty()) | |
391 return UNKNOWN_DEFAULT_WEB_CLIENT; | |
392 | |
393 // Determine the app path. If we can't determine what that is, we have | |
394 // bigger fish to fry... | |
395 FilePath app_path; | |
396 if (!PathService::Get(base::FILE_EXE, &app_path)) { | |
397 LOG(ERROR) << "Error getting app exe path"; | |
398 return UNKNOWN_DEFAULT_WEB_CLIENT; | |
399 } | |
400 | |
401 std::wstring wprotocol = UTF8ToWide(protocol); | |
402 | |
403 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | |
404 IApplicationAssociationRegistration* pAAR; | |
xiyuan
2011/05/24 17:55:20
suggest to use base::win::ScopedComPtr<...> so tha
benwells
2011/05/25 08:07:19
Done (also for IsDefaultBrowser earlier).
| |
405 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, | |
406 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), | |
407 (void**)&pAAR); | |
408 if (!SUCCEEDED(hr)) | |
409 return NOT_DEFAULT_WEB_CLIENT; | |
410 | |
411 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
412 std::wstring app_name = dist->GetApplicationName(); | |
413 // If a user specific default browser entry exists, we check for that | |
414 // app name being default. If not, then default browser is just called | |
415 // Google Chrome or Chromium so we do not append suffix to app name. | |
416 std::wstring suffix; | |
417 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) | |
418 app_name += suffix; | |
419 | |
420 BOOL result = TRUE; | |
421 hr = pAAR->QueryAppIsDefault(wprotocol.c_str(), AT_URLPROTOCOL, | |
422 AL_EFFECTIVE, app_name.c_str(), &result); | |
423 if (!SUCCEEDED(hr) || result == FALSE) { | |
424 pAAR->Release(); | |
425 return NOT_DEFAULT_WEB_CLIENT; | |
426 } | |
427 pAAR->Release(); | |
428 } else { | |
429 std::wstring short_app_path; | |
430 GetShortPathName(app_path.value().c_str(), | |
grt (UTC plus 2)
2011/05/24 20:27:53
This has a variety of failure modes. Check the re
benwells
2011/05/25 08:07:19
See later...
| |
431 WriteInto(&short_app_path, MAX_PATH), | |
432 MAX_PATH); | |
433 | |
434 // open command for protocol associations | |
435 // Check in HKEY_CLASSES_ROOT that is the result of merge between | |
436 // HKLM and HKCU | |
437 HKEY root_key = HKEY_CLASSES_ROOT; | |
438 // Check <protocol>\shell\open\command | |
439 std::wstring key_path(wprotocol + ShellUtil::kRegShellOpen); | |
440 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); | |
441 std::wstring value; | |
442 if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) | |
443 return NOT_DEFAULT_WEB_CLIENT; | |
444 // Need to normalize path in case it's been munged. | |
445 CommandLine command_line = CommandLine::FromString(value); | |
446 std::wstring short_path; | |
447 GetShortPathName(command_line.GetProgram().value().c_str(), | |
grt (UTC plus 2)
2011/05/24 20:27:53
Same comment as above.
benwells
2011/05/25 08:07:19
See later ...
| |
448 WriteInto(&short_path, MAX_PATH), MAX_PATH); | |
449 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) | |
grt (UTC plus 2)
2011/05/24 20:27:53
Ah, I see now that this code is using string compa
benwells
2011/05/25 08:07:19
The ProgramCompare looks very useful here but when
grt (UTC plus 2)
2011/05/25 14:35:45
If you choose to defer using the same mechanism as
benwells
2011/05/26 00:23:04
Done (and same change was made in IsDefaultBrowser
| |
450 return NOT_DEFAULT_WEB_CLIENT; | |
451 } | |
452 return IS_DEFAULT_WEB_CLIENT; | |
363 } | 453 } |
364 | 454 |
365 // There is no reliable way to say which browser is default on a machine (each | 455 // 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 | 456 // 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 | 457 // protocol handler. Even this handler is located at different places in |
368 // registry on XP and Vista: | 458 // registry on XP and Vista: |
369 // - HKCR\http\shell\open\command (XP) | 459 // - HKCR\http\shell\open\command (XP) |
370 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ | 460 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ |
371 // http\UserChoice (Vista) | 461 // http\UserChoice (Vista) |
372 // This method checks if Firefox is defualt browser by checking these | 462 // This method checks if Firefox is defualt browser by checking these |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 profile_path); | 504 profile_path); |
415 } | 505 } |
416 | 506 |
417 void ShellIntegration::MigrateChromiumShortcuts() { | 507 void ShellIntegration::MigrateChromiumShortcuts() { |
418 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 508 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
419 return; | 509 return; |
420 | 510 |
421 BrowserThread::PostTask( | 511 BrowserThread::PostTask( |
422 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask()); | 512 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask()); |
423 } | 513 } |
OLD | NEW |