[email protected] | 5a20b19 | 2012-03-01 06:01:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 5 | #include "chrome/browser/shell_integration.h" |
| 6 | |
[email protected] | c5efb95 | 2011-11-01 01:28:36 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 8 | #include "base/command_line.h" |
| 9 | #include "base/file_util.h" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | e450fa6 | 2011-02-01 12:52:56 | [diff] [blame] | 13 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 14 | #include "chrome/common/chrome_paths.h" |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 15 | #include "chrome/common/chrome_switches.h" |
[email protected] | e450fa6 | 2011-02-01 12:52:56 | [diff] [blame] | 16 | #include "chrome/common/pref_names.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 19 | using content::BrowserThread; |
| 20 | |
[email protected] | 5e3e0963 | 2011-07-18 02:25:31 | [diff] [blame] | 21 | bool ShellIntegration::CanSetAsDefaultProtocolClient() { |
[email protected] | a01481b | 2011-07-15 04:30:02 | [diff] [blame] | 22 | // Allowed as long as the browser can become the operating system default |
| 23 | // browser. |
| 24 | return CanSetAsDefaultBrowser(); |
| 25 | } |
| 26 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 27 | ShellIntegration::ShortcutInfo::ShortcutInfo() |
[email protected] | 5a20b19 | 2012-03-01 06:01:57 | [diff] [blame] | 28 | : is_platform_app(false), |
| 29 | create_on_desktop(false), |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 30 | create_in_applications_menu(false), |
| 31 | create_in_quick_launch_bar(false) { |
| 32 | } |
| 33 | |
| 34 | ShellIntegration::ShortcutInfo::~ShortcutInfo() {} |
| 35 | |
[email protected] | 9561bc91 | 2012-03-07 02:41:16 | [diff] [blame^] | 36 | static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL; |
| 37 | |
| 38 | // static |
| 39 | void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) { |
| 40 | gAppModeInfo = info; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() { |
| 45 | return gAppModeInfo; |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | bool ShellIntegration::IsRunningInAppMode() { |
| 50 | return gAppModeInfo != NULL; |
| 51 | } |
| 52 | |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 53 | // static |
| 54 | CommandLine ShellIntegration::CommandLineArgsForLauncher( |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 55 | const GURL& url, |
[email protected] | 01ed196 | 2011-03-04 19:03:13 | [diff] [blame] | 56 | const std::string& extension_app_id) { |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 57 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 58 | CommandLine new_cmd_line(CommandLine::NO_PROGRAM); |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 59 | |
| 60 | // Use the same UserDataDir for new launches that we currently have set. |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 61 | FilePath user_data_dir = cmd_line.GetSwitchValuePath(switches::kUserDataDir); |
| 62 | if (!user_data_dir.empty()) { |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 63 | // Make sure user_data_dir is an absolute path. |
| 64 | if (file_util::AbsolutePath(&user_data_dir) && |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 65 | file_util::PathExists(user_data_dir)) { |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 66 | new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir); |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
[email protected] | 660e428f | 2010-08-04 01:23:00 | [diff] [blame] | 70 | #if defined(OS_CHROMEOS) |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 71 | FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile); |
| 72 | if (!profile.empty()) |
| 73 | new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile); |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | // If |extension_app_id| is present, we use the kAppId switch rather than |
| 77 | // the kApp switch (the launch url will be read from the extension app |
| 78 | // during launch. |
[email protected] | ec5b50d | 2010-10-09 16:35:18 | [diff] [blame] | 79 | if (!extension_app_id.empty()) { |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 80 | new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id); |
[email protected] | 64048bd | 2010-03-08 23:28:58 | [diff] [blame] | 81 | } else { |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 82 | // Use '--app=url' instead of just 'url' to launch the browser with minimal |
| 83 | // chrome. |
| 84 | // Note: Do not change this flag! Old Gears shortcuts will break if you do! |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 85 | new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec()); |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 86 | } |
[email protected] | b1039293 | 2011-03-08 21:28:14 | [diff] [blame] | 87 | return new_cmd_line; |
[email protected] | 28375ae | 2010-02-05 04:45:50 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 5a20b19 | 2012-03-01 06:01:57 | [diff] [blame] | 90 | |
| 91 | // static |
| 92 | CommandLine ShellIntegration::CommandLineArgsForPlatformApp( |
| 93 | const std::string& extension_app_id, |
| 94 | const FilePath& user_data_dir, |
| 95 | const FilePath& extension_path) { |
| 96 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 97 | CommandLine new_cmd_line(CommandLine::NO_PROGRAM); |
| 98 | |
| 99 | DCHECK(!extension_app_id.empty()); |
| 100 | DCHECK(!user_data_dir.empty()); |
| 101 | DCHECK(!extension_path.empty()); |
| 102 | |
| 103 | // Convert path to absolute and ensure it exists. |
| 104 | FilePath absolute_user_data_dir(user_data_dir); |
| 105 | DCHECK(file_util::AbsolutePath(&absolute_user_data_dir) && |
| 106 | file_util::PathExists(absolute_user_data_dir)); |
| 107 | new_cmd_line.AppendSwitchPath(switches::kUserDataDir, absolute_user_data_dir); |
| 108 | |
| 109 | #if defined(OS_CHROMEOS) |
| 110 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 111 | FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile); |
| 112 | if (!profile.empty()) |
| 113 | new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile); |
| 114 | #endif |
| 115 | |
| 116 | new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id); |
| 117 | |
| 118 | // Convert path to absolute and ensure it exists. |
| 119 | FilePath absolute_extension_path(extension_path); |
| 120 | DCHECK(file_util::AbsolutePath(&absolute_extension_path) && |
| 121 | file_util::PathExists(absolute_extension_path)); |
| 122 | // TODO(sail): Use a different flag that doesn't imply Location::LOAD for the |
| 123 | // extension. |
| 124 | new_cmd_line.AppendSwitchPath(switches::kLoadExtension, |
| 125 | absolute_extension_path); |
| 126 | |
| 127 | return new_cmd_line; |
| 128 | } |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 130 | // ShellIntegration::DefaultWebClientWorker |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 131 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 133 | ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker( |
| 134 | DefaultWebClientObserver* observer) |
[email protected] | 0d3dc8e2 | 2009-11-03 02:27:01 | [diff] [blame] | 135 | : observer_(observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 138 | void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() { |
| 139 | if (observer_) { |
| 140 | observer_->SetDefaultWebClientUIState(STATE_PROCESSING); |
| 141 | BrowserThread::PostTask( |
| 142 | BrowserThread::FILE, FROM_HERE, |
[email protected] | c5efb95 | 2011-11-01 01:28:36 | [diff] [blame] | 143 | base::Bind( |
| 144 | &DefaultWebClientWorker::ExecuteCheckIsDefault, this)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() { |
| 149 | if (observer_) { |
| 150 | observer_->SetDefaultWebClientUIState(STATE_PROCESSING); |
| 151 | } |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 152 | BrowserThread::PostTask( |
| 153 | BrowserThread::FILE, FROM_HERE, |
[email protected] | c5efb95 | 2011-11-01 01:28:36 | [diff] [blame] | 154 | base::Bind( |
| 155 | &DefaultWebClientWorker::ExecuteSetAsDefault, this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 158 | void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 159 | // Our associated view has gone away, so we shouldn't call back to it if |
| 160 | // our worker thread returns after the view is dead. |
[email protected] | bcb999d | 2011-05-31 15:11:35 | [diff] [blame] | 161 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 162 | observer_ = NULL; |
| 163 | } |
| 164 | |
| 165 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 166 | // DefaultWebClientWorker, private: |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 167 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 168 | void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() { |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 169 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 170 | DefaultWebClientState state = CheckIsDefault(); |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 171 | BrowserThread::PostTask( |
| 172 | BrowserThread::UI, FROM_HERE, |
[email protected] | c5efb95 | 2011-11-01 01:28:36 | [diff] [blame] | 173 | base::Bind( |
| 174 | &DefaultWebClientWorker::CompleteCheckIsDefault, this, state)); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 177 | void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault( |
| 178 | DefaultWebClientState state) { |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 179 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame] | 180 | UpdateUI(state); |
[email protected] | 97e2e3f | 2011-07-22 10:21:25 | [diff] [blame] | 181 | // The worker has finished everything it needs to do, so free the observer |
| 182 | // if we own it. |
| 183 | if (observer_ && observer_->IsOwnedByWorker()) { |
| 184 | delete observer_; |
| 185 | observer_ = NULL; |
| 186 | } |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 189 | void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault() { |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 190 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 191 | SetAsDefault(); |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 192 | BrowserThread::PostTask( |
| 193 | BrowserThread::UI, FROM_HERE, |
[email protected] | c5efb95 | 2011-11-01 01:28:36 | [diff] [blame] | 194 | base::Bind( |
| 195 | &DefaultWebClientWorker::CompleteSetAsDefault, this)); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 198 | void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault() { |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 199 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 200 | // Set as default completed, check again to make sure it stuck... |
| 201 | StartCheckIsDefault(); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 204 | void ShellIntegration::DefaultWebClientWorker::UpdateUI( |
| 205 | DefaultWebClientState state) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 206 | if (observer_) { |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame] | 207 | switch (state) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 208 | case NOT_DEFAULT_WEB_CLIENT: |
| 209 | observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame] | 210 | break; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 211 | case IS_DEFAULT_WEB_CLIENT: |
| 212 | observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame] | 213 | break; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 214 | case UNKNOWN_DEFAULT_WEB_CLIENT: |
| 215 | observer_->SetDefaultWebClientUIState(STATE_UNKNOWN); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame] | 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 220 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | } |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 222 | |
| 223 | /////////////////////////////////////////////////////////////////////////////// |
| 224 | // ShellIntegration::DefaultBrowserWorker |
| 225 | // |
| 226 | |
| 227 | ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker( |
| 228 | DefaultWebClientObserver* observer) |
| 229 | : DefaultWebClientWorker(observer) { |
| 230 | } |
| 231 | |
| 232 | /////////////////////////////////////////////////////////////////////////////// |
| 233 | // DefaultBrowserWorker, private: |
| 234 | |
| 235 | ShellIntegration::DefaultWebClientState |
| 236 | ShellIntegration::DefaultBrowserWorker::CheckIsDefault() { |
| 237 | return ShellIntegration::IsDefaultBrowser(); |
| 238 | } |
| 239 | |
| 240 | void ShellIntegration::DefaultBrowserWorker::SetAsDefault() { |
| 241 | ShellIntegration::SetAsDefaultBrowser(); |
| 242 | } |
| 243 | |
| 244 | /////////////////////////////////////////////////////////////////////////////// |
| 245 | // ShellIntegration::DefaultProtocolClientWorker |
| 246 | // |
| 247 | |
| 248 | ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker( |
| 249 | DefaultWebClientObserver* observer, const std::string& protocol) |
| 250 | : DefaultWebClientWorker(observer), |
| 251 | protocol_(protocol) { |
| 252 | } |
| 253 | |
| 254 | /////////////////////////////////////////////////////////////////////////////// |
| 255 | // DefaultProtocolClientWorker, private: |
| 256 | |
| 257 | ShellIntegration::DefaultWebClientState |
| 258 | ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() { |
| 259 | return ShellIntegration::IsDefaultProtocolClient(protocol_); |
| 260 | } |
| 261 | |
| 262 | void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault() { |
| 263 | ShellIntegration::SetAsDefaultProtocolClient(protocol_); |
| 264 | } |