| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/shell_integration.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/prefs/pref_service.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "chrome/browser/policy/policy_path_parser.h" | |
| 15 #include "chrome/common/chrome_paths.h" | |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 | |
| 20 using content::BrowserThread; | |
| 21 | |
| 22 ShellIntegration::DefaultWebClientSetPermission | |
| 23 ShellIntegration::CanSetAsDefaultProtocolClient() { | |
| 24 // Allowed as long as the browser can become the operating system default | |
| 25 // browser. | |
| 26 return CanSetAsDefaultBrowser(); | |
| 27 } | |
| 28 | |
| 29 ShellIntegration::ShortcutInfo::ShortcutInfo() | |
| 30 : is_platform_app(false), | |
| 31 create_on_desktop(false), | |
| 32 create_in_applications_menu(false), | |
| 33 create_in_quick_launch_bar(false) { | |
| 34 } | |
| 35 | |
| 36 ShellIntegration::ShortcutInfo::~ShortcutInfo() {} | |
| 37 | |
| 38 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL; | |
| 39 | |
| 40 // static | |
| 41 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) { | |
| 42 gAppModeInfo = info; | |
| 43 } | |
| 44 | |
| 45 // static | |
| 46 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() { | |
| 47 return gAppModeInfo; | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 bool ShellIntegration::IsRunningInAppMode() { | |
| 52 return gAppModeInfo != NULL; | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 CommandLine ShellIntegration::CommandLineArgsForLauncher( | |
| 57 const GURL& url, | |
| 58 const std::string& extension_app_id, | |
| 59 const base::FilePath& profile_path) { | |
| 60 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); | |
| 61 CommandLine new_cmd_line(CommandLine::NO_PROGRAM); | |
| 62 | |
| 63 // Use the same UserDataDir for new launches that we currently have set. | |
| 64 base::FilePath user_data_dir = | |
| 65 cmd_line.GetSwitchValuePath(switches::kUserDataDir); | |
| 66 #if defined(OS_MACOSX) || defined(OS_WIN) | |
| 67 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir); | |
| 68 #endif | |
| 69 if (!user_data_dir.empty()) { | |
| 70 // Make sure user_data_dir is an absolute path. | |
| 71 if (file_util::AbsolutePath(&user_data_dir) && | |
| 72 file_util::PathExists(user_data_dir)) { | |
| 73 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 #if defined(OS_CHROMEOS) | |
| 78 base::FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile); | |
| 79 if (!profile.empty()) | |
| 80 new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile); | |
| 81 #else | |
| 82 if (!profile_path.empty() && !extension_app_id.empty()) | |
| 83 new_cmd_line.AppendSwitchPath(switches::kProfileDirectory, | |
| 84 profile_path.BaseName()); | |
| 85 #endif | |
| 86 | |
| 87 // If |extension_app_id| is present, we use the kAppId switch rather than | |
| 88 // the kApp switch (the launch url will be read from the extension app | |
| 89 // during launch. | |
| 90 if (!extension_app_id.empty()) { | |
| 91 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id); | |
| 92 } else { | |
| 93 // Use '--app=url' instead of just 'url' to launch the browser with minimal | |
| 94 // chrome. | |
| 95 // Note: Do not change this flag! Old Gears shortcuts will break if you do! | |
| 96 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec()); | |
| 97 } | |
| 98 return new_cmd_line; | |
| 99 } | |
| 100 | |
| 101 #if !defined(OS_WIN) | |
| 102 // static | |
| 103 bool ShellIntegration::SetAsDefaultBrowserInteractive() { | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 bool ShellIntegration::SetAsDefaultProtocolClientInteractive( | |
| 109 const std::string& protocol) { | |
| 110 return false; | |
| 111 } | |
| 112 #endif | |
| 113 | |
| 114 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() { | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 bool ShellIntegration::DefaultWebClientObserver:: | |
| 119 IsInteractiveSetDefaultPermitted() { | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 /////////////////////////////////////////////////////////////////////////////// | |
| 124 // ShellIntegration::DefaultWebClientWorker | |
| 125 // | |
| 126 | |
| 127 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker( | |
| 128 DefaultWebClientObserver* observer) | |
| 129 : observer_(observer) { | |
| 130 } | |
| 131 | |
| 132 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() { | |
| 133 if (observer_) { | |
| 134 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); | |
| 135 BrowserThread::PostTask( | |
| 136 BrowserThread::FILE, FROM_HERE, | |
| 137 base::Bind( | |
| 138 &DefaultWebClientWorker::ExecuteCheckIsDefault, this)); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() { | |
| 143 bool interactive_permitted = false; | |
| 144 if (observer_) { | |
| 145 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); | |
| 146 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted(); | |
| 147 } | |
| 148 BrowserThread::PostTask( | |
| 149 BrowserThread::FILE, FROM_HERE, | |
| 150 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this, | |
| 151 interactive_permitted)); | |
| 152 } | |
| 153 | |
| 154 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() { | |
| 155 // Our associated view has gone away, so we shouldn't call back to it if | |
| 156 // our worker thread returns after the view is dead. | |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 158 observer_ = NULL; | |
| 159 } | |
| 160 | |
| 161 /////////////////////////////////////////////////////////////////////////////// | |
| 162 // DefaultWebClientWorker, private: | |
| 163 | |
| 164 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() { | |
| 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 166 DefaultWebClientState state = CheckIsDefault(); | |
| 167 BrowserThread::PostTask( | |
| 168 BrowserThread::UI, FROM_HERE, | |
| 169 base::Bind( | |
| 170 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state)); | |
| 171 } | |
| 172 | |
| 173 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault( | |
| 174 DefaultWebClientState state) { | |
| 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 176 UpdateUI(state); | |
| 177 // The worker has finished everything it needs to do, so free the observer | |
| 178 // if we own it. | |
| 179 if (observer_ && observer_->IsOwnedByWorker()) { | |
| 180 delete observer_; | |
| 181 observer_ = NULL; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault( | |
| 186 bool interactive_permitted) { | |
| 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 188 | |
| 189 bool result = SetAsDefault(interactive_permitted); | |
| 190 BrowserThread::PostTask( | |
| 191 BrowserThread::UI, FROM_HERE, | |
| 192 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result)); | |
| 193 } | |
| 194 | |
| 195 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault( | |
| 196 bool succeeded) { | |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 198 // First tell the observer what the SetAsDefault call has returned. | |
| 199 if (observer_) | |
| 200 observer_->OnSetAsDefaultConcluded(succeeded); | |
| 201 // Set as default completed, check again to make sure it stuck... | |
| 202 StartCheckIsDefault(); | |
| 203 } | |
| 204 | |
| 205 void ShellIntegration::DefaultWebClientWorker::UpdateUI( | |
| 206 DefaultWebClientState state) { | |
| 207 if (observer_) { | |
| 208 switch (state) { | |
| 209 case NOT_DEFAULT: | |
| 210 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT); | |
| 211 break; | |
| 212 case IS_DEFAULT: | |
| 213 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT); | |
| 214 break; | |
| 215 case UNKNOWN_DEFAULT: | |
| 216 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN); | |
| 217 break; | |
| 218 default: | |
| 219 break; | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 /////////////////////////////////////////////////////////////////////////////// | |
| 225 // ShellIntegration::DefaultBrowserWorker | |
| 226 // | |
| 227 | |
| 228 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker( | |
| 229 DefaultWebClientObserver* observer) | |
| 230 : DefaultWebClientWorker(observer) { | |
| 231 } | |
| 232 | |
| 233 /////////////////////////////////////////////////////////////////////////////// | |
| 234 // DefaultBrowserWorker, private: | |
| 235 | |
| 236 ShellIntegration::DefaultWebClientState | |
| 237 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() { | |
| 238 return ShellIntegration::GetDefaultBrowser(); | |
| 239 } | |
| 240 | |
| 241 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault( | |
| 242 bool interactive_permitted) { | |
| 243 bool result = false; | |
| 244 switch (ShellIntegration::CanSetAsDefaultBrowser()) { | |
| 245 case ShellIntegration::SET_DEFAULT_UNATTENDED: | |
| 246 result = ShellIntegration::SetAsDefaultBrowser(); | |
| 247 break; | |
| 248 case ShellIntegration::SET_DEFAULT_INTERACTIVE: | |
| 249 if (interactive_permitted) | |
| 250 result = ShellIntegration::SetAsDefaultBrowserInteractive(); | |
| 251 break; | |
| 252 default: | |
| 253 NOTREACHED(); | |
| 254 } | |
| 255 | |
| 256 return result; | |
| 257 } | |
| 258 | |
| 259 /////////////////////////////////////////////////////////////////////////////// | |
| 260 // ShellIntegration::DefaultProtocolClientWorker | |
| 261 // | |
| 262 | |
| 263 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker( | |
| 264 DefaultWebClientObserver* observer, const std::string& protocol) | |
| 265 : DefaultWebClientWorker(observer), | |
| 266 protocol_(protocol) { | |
| 267 } | |
| 268 | |
| 269 /////////////////////////////////////////////////////////////////////////////// | |
| 270 // DefaultProtocolClientWorker, private: | |
| 271 | |
| 272 ShellIntegration::DefaultWebClientState | |
| 273 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() { | |
| 274 return ShellIntegration::IsDefaultProtocolClient(protocol_); | |
| 275 } | |
| 276 | |
| 277 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault( | |
| 278 bool interactive_permitted) { | |
| 279 bool result = false; | |
| 280 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) { | |
| 281 case ShellIntegration::SET_DEFAULT_UNATTENDED: | |
| 282 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_); | |
| 283 break; | |
| 284 case ShellIntegration::SET_DEFAULT_INTERACTIVE: | |
| 285 if (interactive_permitted) { | |
| 286 result = ShellIntegration::SetAsDefaultProtocolClientInteractive( | |
| 287 protocol_); | |
| 288 } | |
| 289 break; | |
| 290 default: | |
| 291 NOTREACHED(); | |
| 292 } | |
| 293 | |
| 294 return result; | |
| 295 } | |
| OLD | NEW |