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