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