| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/message_loop.h" |
| 8 #include <shlobj.h> | 8 #include "base/thread.h" |
| 9 #include <shobjidl.h> | 9 #include "chrome/browser/browser_process.h" |
| 10 | 10 |
| 11 #include "app/win_util.h" | 11 /////////////////////////////////////////////////////////////////////////////// |
| 12 #include "base/command_line.h" | 12 // ShellIntegration::DefaultBrowserWorker |
| 13 #include "base/file_util.h" | 13 // |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/path_service.h" | |
| 16 #include "base/registry.h" | |
| 17 #include "base/string_util.h" | |
| 18 #include "base/task.h" | |
| 19 #include "base/win_util.h" | |
| 20 #include "chrome/common/chrome_constants.h" | |
| 21 #include "chrome/installer/util/browser_distribution.h" | |
| 22 #include "chrome/installer/util/create_reg_key_work_item.h" | |
| 23 #include "chrome/installer/util/set_reg_value_work_item.h" | |
| 24 #include "chrome/installer/util/shell_util.h" | |
| 25 #include "chrome/installer/util/util_constants.h" | |
| 26 #include "chrome/installer/util/work_item.h" | |
| 27 #include "chrome/installer/util/work_item_list.h" | |
| 28 | 14 |
| 29 bool ShellIntegration::SetAsDefaultBrowser() { | 15 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker( |
| 30 std::wstring chrome_exe; | 16 DefaultBrowserObserver* observer) |
| 31 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 17 : observer_(observer), |
| 32 LOG(ERROR) << "Error getting app exe path"; | 18 ui_loop_(MessageLoop::current()), |
| 33 return false; | 19 file_loop_(g_browser_process->file_thread()->message_loop()) { |
| 34 } | |
| 35 | |
| 36 // From UI currently we only allow setting default browser for current user. | |
| 37 if (!ShellUtil::MakeChromeDefault(ShellUtil::CURRENT_USER, | |
| 38 chrome_exe, true)) { | |
| 39 LOG(ERROR) << "Chrome could not be set as default browser."; | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 43 LOG(INFO) << "Chrome registered as default browser."; | |
| 44 return true; | |
| 45 } | 20 } |
| 46 | 21 |
| 47 bool ShellIntegration::IsDefaultBrowser() { | 22 void ShellIntegration::DefaultBrowserWorker::StartCheckDefaultBrowser() { |
| 48 // First determine the app path. If we can't determine what that is, we have | 23 observer_->SetDefaultBrowserUIState(STATE_PROCESSING); |
| 49 // bigger fish to fry... | 24 file_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 50 std::wstring app_path; | 25 &DefaultBrowserWorker::ExecuteCheckDefaultBrowser)); |
| 51 if (!PathService::Get(base::FILE_EXE, &app_path)) { | |
| 52 LOG(ERROR) << "Error getting app exe path"; | |
| 53 return false; | |
| 54 } | |
| 55 // When we check for default browser we don't necessarily want to count file | |
| 56 // type handlers and icons as having changed the default browser status, | |
| 57 // since the user may have changed their shell settings to cause HTML files | |
| 58 // to open with a text editor for example. We also don't want to aggressively | |
| 59 // claim FTP, since the user may have a separate FTP client. It is an open | |
| 60 // question as to how to "heal" these settings. Perhaps the user should just | |
| 61 // re-run the installer or run with the --set-default-browser command line | |
| 62 // flag. There is doubtless some other key we can hook into to cause "Repair" | |
| 63 // to show up in Add/Remove programs for us. | |
| 64 const std::wstring kChromeProtocols[] = {L"http", L"https"}; | |
| 65 | |
| 66 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { | |
| 67 IApplicationAssociationRegistration* pAAR; | |
| 68 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, | |
| 69 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), | |
| 70 (void**)&pAAR); | |
| 71 if (!SUCCEEDED(hr)) | |
| 72 return false; | |
| 73 | |
| 74 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 75 std::wstring app_name = dist->GetApplicationName(); | |
| 76 std::wstring app_name_with_suffix; | |
| 77 ShellUtil::GetUserSpecificDefaultBrowserSuffix(&app_name_with_suffix); | |
| 78 app_name_with_suffix = app_name + app_name_with_suffix; | |
| 79 for (int i = 0; i < _countof(kChromeProtocols); i++) { | |
| 80 BOOL result = TRUE; | |
| 81 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL, | |
| 82 AL_EFFECTIVE, app_name_with_suffix.c_str(), &result); | |
| 83 if (!SUCCEEDED(hr) || (result == FALSE)) { | |
| 84 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), | |
| 85 AT_URLPROTOCOL, AL_EFFECTIVE, app_name.c_str(), &result); | |
| 86 if (!SUCCEEDED(hr) || (result == FALSE)) { | |
| 87 pAAR->Release(); | |
| 88 return false; | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 pAAR->Release(); | |
| 93 } else { | |
| 94 std::wstring short_app_path; | |
| 95 GetShortPathName(app_path.c_str(), WriteInto(&short_app_path, MAX_PATH), | |
| 96 MAX_PATH); | |
| 97 | |
| 98 // open command for protocol associations | |
| 99 for (int i = 0; i < _countof(kChromeProtocols); i++) { | |
| 100 // Check in HKEY_CLASSES_ROOT that is the result of merge between | |
| 101 // HKLM and HKCU | |
| 102 HKEY root_key = HKEY_CLASSES_ROOT; | |
| 103 // Check <protocol>\shell\open\command | |
| 104 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); | |
| 105 RegKey key(root_key, key_path.c_str(), KEY_READ); | |
| 106 std::wstring value; | |
| 107 if (!key.Valid() || !key.ReadValue(L"", &value)) | |
| 108 return false; | |
| 109 // Need to normalize path in case it's been munged. | |
| 110 CommandLine command_line(L""); | |
| 111 command_line.ParseFromString(value); | |
| 112 std::wstring short_path; | |
| 113 GetShortPathName(command_line.program().c_str(), | |
| 114 WriteInto(&short_path, MAX_PATH), MAX_PATH); | |
| 115 if ((short_path.size() != short_app_path.size()) || | |
| 116 (!std::equal(short_path.begin(), | |
| 117 short_path.end(), | |
| 118 short_app_path.begin(), | |
| 119 CaseInsensitiveCompare<wchar_t>()))) | |
| 120 return false; | |
| 121 } | |
| 122 } | |
| 123 return true; | |
| 124 } | 26 } |
| 125 | 27 |
| 126 // There is no reliable way to say which browser is default on a machine (each | 28 void ShellIntegration::DefaultBrowserWorker::StartSetAsDefaultBrowser() { |
| 127 // browser can have some of the protocols/shortcuts). So we look for only HTTP | 29 observer_->SetDefaultBrowserUIState(STATE_PROCESSING); |
| 128 // protocol handler. Even this handler is located at different places in | 30 file_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 129 // registry on XP and Vista: | 31 &DefaultBrowserWorker::ExecuteSetAsDefaultBrowser)); |
| 130 // - HKCR\http\shell\open\command (XP) | 32 } |
| 131 // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ | 33 |
| 132 // http\UserChoice (Vista) | 34 void ShellIntegration::DefaultBrowserWorker::ObserverDestroyed() { |
| 133 // This method checks if Firefox is defualt browser by checking these | 35 // Our associated view has gone away, so we shouldn't call back to it if |
| 134 // locations and returns true if Firefox traces are found there. In case of | 36 // our worker thread returns after the view is dead. |
| 135 // error (or if Firefox is not found)it returns the default value which | 37 observer_ = NULL; |
| 136 // is false. | 38 } |
| 137 bool ShellIntegration::IsFirefoxDefaultBrowser() { | 39 |
| 138 bool ff_default = false; | 40 /////////////////////////////////////////////////////////////////////////////// |
| 139 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { | 41 // DefaultBrowserWorker, private: |
| 140 std::wstring app_cmd; | 42 |
| 141 RegKey key(HKEY_CURRENT_USER, ShellUtil::kRegVistaUrlPrefs, KEY_READ); | 43 void ShellIntegration::DefaultBrowserWorker::ExecuteCheckDefaultBrowser() { |
| 142 if (key.Valid() && key.ReadValue(L"Progid", &app_cmd) && | 44 DCHECK(MessageLoop::current() == file_loop_); |
| 143 app_cmd == L"FirefoxURL") | 45 bool is_default = ShellIntegration::IsDefaultBrowser(); |
| 144 ff_default = true; | 46 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 145 } else { | 47 &DefaultBrowserWorker::CompleteCheckDefaultBrowser, is_default)); |
| 146 std::wstring key_path(L"http"); | 48 } |
| 147 key_path.append(ShellUtil::kRegShellOpen); | 49 |
| 148 RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); | 50 void ShellIntegration::DefaultBrowserWorker::CompleteCheckDefaultBrowser( |
| 149 std::wstring app_cmd; | 51 bool is_default) { |
| 150 if (key.Valid() && key.ReadValue(L"", &app_cmd) && | 52 DCHECK(MessageLoop::current() == ui_loop_); |
| 151 std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox")) | 53 UpdateUI(is_default); |
| 152 ff_default = true; | 54 } |
| 55 |
| 56 void ShellIntegration::DefaultBrowserWorker::ExecuteSetAsDefaultBrowser() { |
| 57 DCHECK(MessageLoop::current() == file_loop_); |
| 58 ShellIntegration::SetAsDefaultBrowser(); |
| 59 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 60 &DefaultBrowserWorker::CompleteSetAsDefaultBrowser)); |
| 61 } |
| 62 |
| 63 void ShellIntegration::DefaultBrowserWorker::CompleteSetAsDefaultBrowser() { |
| 64 DCHECK(MessageLoop::current() == ui_loop_); |
| 65 if (observer_) { |
| 66 // Set as default completed, check again to make sure it stuck... |
| 67 StartCheckDefaultBrowser(); |
| 153 } | 68 } |
| 154 return ff_default; | |
| 155 } | 69 } |
| 70 |
| 71 void ShellIntegration::DefaultBrowserWorker::UpdateUI(bool is_default) { |
| 72 if (observer_) { |
| 73 DefaultBrowserUIState state = |
| 74 is_default ? STATE_DEFAULT : STATE_NOT_DEFAULT; |
| 75 observer_->SetDefaultBrowserUIState(state); |
| 76 } |
| 77 } |
| OLD | NEW |