blob: c7362d69020eb153a871f827e24c58a55e3eefb3 [file] [log] [blame]
[email protected]5a20b192012-03-01 06:01:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]4a0765a2009-05-08 23:12:255#include "chrome/browser/shell_integration.h"
6
[email protected]c5efb952011-11-01 01:28:367#include "base/bind.h"
[email protected]28375ae2010-02-05 04:45:508#include "base/command_line.h"
9#include "base/file_util.h"
[email protected]b96aa932009-08-12 21:34:4910#include "base/path_service.h"
[email protected]5d91c9e2010-07-28 17:25:2811#include "base/string_util.h"
[email protected]64048bd2010-03-08 23:28:5812#include "base/utf_string_conversions.h"
[email protected]e450fa62011-02-01 12:52:5613#include "chrome/browser/prefs/pref_service.h"
[email protected]b96aa932009-08-12 21:34:4914#include "chrome/common/chrome_paths.h"
[email protected]28375ae2010-02-05 04:45:5015#include "chrome/common/chrome_switches.h"
[email protected]e450fa62011-02-01 12:52:5616#include "chrome/common/pref_names.h"
[email protected]c38831a12011-10-28 12:44:4917#include "content/public/browser/browser_thread.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]631bb742011-11-02 11:29:3919using content::BrowserThread;
20
[email protected]5e3e09632011-07-18 02:25:3121bool ShellIntegration::CanSetAsDefaultProtocolClient() {
[email protected]a01481b2011-07-15 04:30:0222 // Allowed as long as the browser can become the operating system default
23 // browser.
24 return CanSetAsDefaultBrowser();
25}
26
[email protected]93aa89c72010-10-20 21:32:0427ShellIntegration::ShortcutInfo::ShortcutInfo()
[email protected]5a20b192012-03-01 06:01:5728 : is_platform_app(false),
29 create_on_desktop(false),
[email protected]93aa89c72010-10-20 21:32:0430 create_in_applications_menu(false),
31 create_in_quick_launch_bar(false) {
32}
33
34ShellIntegration::ShortcutInfo::~ShortcutInfo() {}
35
[email protected]b10392932011-03-08 21:28:1436// static
37CommandLine ShellIntegration::CommandLineArgsForLauncher(
[email protected]2f1c09d2011-01-14 14:58:1438 const GURL& url,
[email protected]01ed1962011-03-04 19:03:1339 const std::string& extension_app_id) {
[email protected]b10392932011-03-08 21:28:1440 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
41 CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
[email protected]28375ae2010-02-05 04:45:5042
43 // Use the same UserDataDir for new launches that we currently have set.
[email protected]b10392932011-03-08 21:28:1444 FilePath user_data_dir = cmd_line.GetSwitchValuePath(switches::kUserDataDir);
45 if (!user_data_dir.empty()) {
[email protected]28375ae2010-02-05 04:45:5046 // Make sure user_data_dir is an absolute path.
47 if (file_util::AbsolutePath(&user_data_dir) &&
[email protected]63597e4e2010-07-08 17:49:0548 file_util::PathExists(user_data_dir)) {
[email protected]b10392932011-03-08 21:28:1449 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[email protected]28375ae2010-02-05 04:45:5050 }
51 }
52
[email protected]660e428f2010-08-04 01:23:0053#if defined(OS_CHROMEOS)
[email protected]b10392932011-03-08 21:28:1454 FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile);
55 if (!profile.empty())
56 new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile);
[email protected]28375ae2010-02-05 04:45:5057#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]ec5b50d2010-10-09 16:35:1862 if (!extension_app_id.empty()) {
[email protected]b10392932011-03-08 21:28:1463 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
[email protected]64048bd2010-03-08 23:28:5864 } else {
[email protected]28375ae2010-02-05 04:45:5065 // 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]b10392932011-03-08 21:28:1468 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
[email protected]28375ae2010-02-05 04:45:5069 }
[email protected]b10392932011-03-08 21:28:1470 return new_cmd_line;
[email protected]28375ae2010-02-05 04:45:5071}
72
[email protected]5a20b192012-03-01 06:01:5773
74// static
75CommandLine 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]d24c4012009-07-28 01:57:31112///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:02113// ShellIntegration::DefaultWebClientWorker
[email protected]d24c4012009-07-28 01:57:31114//
initial.commit09911bf2008-07-26 23:55:29115
[email protected]4468a5b2011-05-26 07:48:02116ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
117 DefaultWebClientObserver* observer)
[email protected]0d3dc8e22009-11-03 02:27:01118 : observer_(observer) {
initial.commit09911bf2008-07-26 23:55:29119}
120
[email protected]4468a5b2011-05-26 07:48:02121void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
122 if (observer_) {
123 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
124 BrowserThread::PostTask(
125 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36126 base::Bind(
127 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
[email protected]4468a5b2011-05-26 07:48:02128 }
129}
130
131void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
132 if (observer_) {
133 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
134 }
[email protected]0c7d74f2010-10-11 11:55:26135 BrowserThread::PostTask(
136 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36137 base::Bind(
138 &DefaultWebClientWorker::ExecuteSetAsDefault, this));
initial.commit09911bf2008-07-26 23:55:29139}
140
[email protected]4468a5b2011-05-26 07:48:02141void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
[email protected]d24c4012009-07-28 01:57:31142 // 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]bcb999d2011-05-31 15:11:35144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d24c4012009-07-28 01:57:31145 observer_ = NULL;
146}
147
148///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:02149// DefaultWebClientWorker, private:
[email protected]d24c4012009-07-28 01:57:31150
[email protected]4468a5b2011-05-26 07:48:02151void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02153 DefaultWebClientState state = CheckIsDefault();
[email protected]0c7d74f2010-10-11 11:55:26154 BrowserThread::PostTask(
155 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36156 base::Bind(
157 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
[email protected]d24c4012009-07-28 01:57:31158}
159
[email protected]4468a5b2011-05-26 07:48:02160void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
161 DefaultWebClientState state) {
[email protected]0c7d74f2010-10-11 11:55:26162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]264f74d12009-09-04 23:39:58163 UpdateUI(state);
[email protected]97e2e3f2011-07-22 10:21:25164 // 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]d24c4012009-07-28 01:57:31170}
171
[email protected]4468a5b2011-05-26 07:48:02172void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02174 SetAsDefault();
[email protected]0c7d74f2010-10-11 11:55:26175 BrowserThread::PostTask(
176 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36177 base::Bind(
178 &DefaultWebClientWorker::CompleteSetAsDefault, this));
[email protected]d24c4012009-07-28 01:57:31179}
180
[email protected]4468a5b2011-05-26 07:48:02181void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4468a5b2011-05-26 07:48:02183 // Set as default completed, check again to make sure it stuck...
184 StartCheckIsDefault();
[email protected]d24c4012009-07-28 01:57:31185}
186
[email protected]4468a5b2011-05-26 07:48:02187void ShellIntegration::DefaultWebClientWorker::UpdateUI(
188 DefaultWebClientState state) {
[email protected]d24c4012009-07-28 01:57:31189 if (observer_) {
[email protected]264f74d12009-09-04 23:39:58190 switch (state) {
[email protected]4468a5b2011-05-26 07:48:02191 case NOT_DEFAULT_WEB_CLIENT:
192 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58193 break;
[email protected]4468a5b2011-05-26 07:48:02194 case IS_DEFAULT_WEB_CLIENT:
195 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58196 break;
[email protected]4468a5b2011-05-26 07:48:02197 case UNKNOWN_DEFAULT_WEB_CLIENT:
198 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
[email protected]264f74d12009-09-04 23:39:58199 break;
200 default:
201 break;
202 }
[email protected]d24c4012009-07-28 01:57:31203 }
initial.commit09911bf2008-07-26 23:55:29204}
[email protected]4468a5b2011-05-26 07:48:02205
206///////////////////////////////////////////////////////////////////////////////
207// ShellIntegration::DefaultBrowserWorker
208//
209
210ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
211 DefaultWebClientObserver* observer)
212 : DefaultWebClientWorker(observer) {
213}
214
215///////////////////////////////////////////////////////////////////////////////
216// DefaultBrowserWorker, private:
217
218ShellIntegration::DefaultWebClientState
219ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
220 return ShellIntegration::IsDefaultBrowser();
221}
222
223void ShellIntegration::DefaultBrowserWorker::SetAsDefault() {
224 ShellIntegration::SetAsDefaultBrowser();
225}
226
227///////////////////////////////////////////////////////////////////////////////
228// ShellIntegration::DefaultProtocolClientWorker
229//
230
231ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
232 DefaultWebClientObserver* observer, const std::string& protocol)
233 : DefaultWebClientWorker(observer),
234 protocol_(protocol) {
235}
236
237///////////////////////////////////////////////////////////////////////////////
238// DefaultProtocolClientWorker, private:
239
240ShellIntegration::DefaultWebClientState
241ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
242 return ShellIntegration::IsDefaultProtocolClient(protocol_);
243}
244
245void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault() {
246 ShellIntegration::SetAsDefaultProtocolClient(protocol_);
247}