blob: e3eae8167adfe1434dc27ba4912aa93a41719dfa [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]9561bc912012-03-07 02:41:1636static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
37
38// static
39void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
40 gAppModeInfo = info;
41}
42
43// static
44const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
45 return gAppModeInfo;
46}
47
48// static
49bool ShellIntegration::IsRunningInAppMode() {
50 return gAppModeInfo != NULL;
51}
52
[email protected]b10392932011-03-08 21:28:1453// static
54CommandLine ShellIntegration::CommandLineArgsForLauncher(
[email protected]2f1c09d2011-01-14 14:58:1455 const GURL& url,
[email protected]01ed1962011-03-04 19:03:1356 const std::string& extension_app_id) {
[email protected]b10392932011-03-08 21:28:1457 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
58 CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
[email protected]28375ae2010-02-05 04:45:5059
60 // Use the same UserDataDir for new launches that we currently have set.
[email protected]b10392932011-03-08 21:28:1461 FilePath user_data_dir = cmd_line.GetSwitchValuePath(switches::kUserDataDir);
62 if (!user_data_dir.empty()) {
[email protected]28375ae2010-02-05 04:45:5063 // Make sure user_data_dir is an absolute path.
64 if (file_util::AbsolutePath(&user_data_dir) &&
[email protected]63597e4e2010-07-08 17:49:0565 file_util::PathExists(user_data_dir)) {
[email protected]b10392932011-03-08 21:28:1466 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[email protected]28375ae2010-02-05 04:45:5067 }
68 }
69
[email protected]660e428f2010-08-04 01:23:0070#if defined(OS_CHROMEOS)
[email protected]b10392932011-03-08 21:28:1471 FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile);
72 if (!profile.empty())
73 new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile);
[email protected]28375ae2010-02-05 04:45:5074#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]ec5b50d2010-10-09 16:35:1879 if (!extension_app_id.empty()) {
[email protected]b10392932011-03-08 21:28:1480 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
[email protected]64048bd2010-03-08 23:28:5881 } else {
[email protected]28375ae2010-02-05 04:45:5082 // 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]b10392932011-03-08 21:28:1485 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
[email protected]28375ae2010-02-05 04:45:5086 }
[email protected]b10392932011-03-08 21:28:1487 return new_cmd_line;
[email protected]28375ae2010-02-05 04:45:5088}
89
[email protected]5a20b192012-03-01 06:01:5790
91// static
92CommandLine 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]d24c4012009-07-28 01:57:31129///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:02130// ShellIntegration::DefaultWebClientWorker
[email protected]d24c4012009-07-28 01:57:31131//
initial.commit09911bf2008-07-26 23:55:29132
[email protected]4468a5b2011-05-26 07:48:02133ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
134 DefaultWebClientObserver* observer)
[email protected]0d3dc8e22009-11-03 02:27:01135 : observer_(observer) {
initial.commit09911bf2008-07-26 23:55:29136}
137
[email protected]4468a5b2011-05-26 07:48:02138void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
139 if (observer_) {
140 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
141 BrowserThread::PostTask(
142 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36143 base::Bind(
144 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
[email protected]4468a5b2011-05-26 07:48:02145 }
146}
147
148void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
149 if (observer_) {
150 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
151 }
[email protected]0c7d74f2010-10-11 11:55:26152 BrowserThread::PostTask(
153 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36154 base::Bind(
155 &DefaultWebClientWorker::ExecuteSetAsDefault, this));
initial.commit09911bf2008-07-26 23:55:29156}
157
[email protected]4468a5b2011-05-26 07:48:02158void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
[email protected]d24c4012009-07-28 01:57:31159 // 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]bcb999d2011-05-31 15:11:35161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d24c4012009-07-28 01:57:31162 observer_ = NULL;
163}
164
165///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:02166// DefaultWebClientWorker, private:
[email protected]d24c4012009-07-28 01:57:31167
[email protected]4468a5b2011-05-26 07:48:02168void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02170 DefaultWebClientState state = CheckIsDefault();
[email protected]0c7d74f2010-10-11 11:55:26171 BrowserThread::PostTask(
172 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36173 base::Bind(
174 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
[email protected]d24c4012009-07-28 01:57:31175}
176
[email protected]4468a5b2011-05-26 07:48:02177void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
178 DefaultWebClientState state) {
[email protected]0c7d74f2010-10-11 11:55:26179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]264f74d12009-09-04 23:39:58180 UpdateUI(state);
[email protected]97e2e3f2011-07-22 10:21:25181 // 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]d24c4012009-07-28 01:57:31187}
188
[email protected]4468a5b2011-05-26 07:48:02189void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02191 SetAsDefault();
[email protected]0c7d74f2010-10-11 11:55:26192 BrowserThread::PostTask(
193 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36194 base::Bind(
195 &DefaultWebClientWorker::CompleteSetAsDefault, this));
[email protected]d24c4012009-07-28 01:57:31196}
197
[email protected]4468a5b2011-05-26 07:48:02198void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4468a5b2011-05-26 07:48:02200 // Set as default completed, check again to make sure it stuck...
201 StartCheckIsDefault();
[email protected]d24c4012009-07-28 01:57:31202}
203
[email protected]4468a5b2011-05-26 07:48:02204void ShellIntegration::DefaultWebClientWorker::UpdateUI(
205 DefaultWebClientState state) {
[email protected]d24c4012009-07-28 01:57:31206 if (observer_) {
[email protected]264f74d12009-09-04 23:39:58207 switch (state) {
[email protected]4468a5b2011-05-26 07:48:02208 case NOT_DEFAULT_WEB_CLIENT:
209 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58210 break;
[email protected]4468a5b2011-05-26 07:48:02211 case IS_DEFAULT_WEB_CLIENT:
212 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58213 break;
[email protected]4468a5b2011-05-26 07:48:02214 case UNKNOWN_DEFAULT_WEB_CLIENT:
215 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
[email protected]264f74d12009-09-04 23:39:58216 break;
217 default:
218 break;
219 }
[email protected]d24c4012009-07-28 01:57:31220 }
initial.commit09911bf2008-07-26 23:55:29221}
[email protected]4468a5b2011-05-26 07:48:02222
223///////////////////////////////////////////////////////////////////////////////
224// ShellIntegration::DefaultBrowserWorker
225//
226
227ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
228 DefaultWebClientObserver* observer)
229 : DefaultWebClientWorker(observer) {
230}
231
232///////////////////////////////////////////////////////////////////////////////
233// DefaultBrowserWorker, private:
234
235ShellIntegration::DefaultWebClientState
236ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
237 return ShellIntegration::IsDefaultBrowser();
238}
239
240void ShellIntegration::DefaultBrowserWorker::SetAsDefault() {
241 ShellIntegration::SetAsDefaultBrowser();
242}
243
244///////////////////////////////////////////////////////////////////////////////
245// ShellIntegration::DefaultProtocolClientWorker
246//
247
248ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
249 DefaultWebClientObserver* observer, const std::string& protocol)
250 : DefaultWebClientWorker(observer),
251 protocol_(protocol) {
252}
253
254///////////////////////////////////////////////////////////////////////////////
255// DefaultProtocolClientWorker, private:
256
257ShellIntegration::DefaultWebClientState
258ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
259 return ShellIntegration::IsDefaultProtocolClient(protocol_);
260}
261
262void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault() {
263 ShellIntegration::SetAsDefaultProtocolClient(protocol_);
264}