blob: fe900e4f87acc1089c46c42e1bbe88dc3af7a397 [file] [log] [blame]
[email protected]e450fa62011-02-01 12:52:561// Copyright (c) 2011 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()
28 : create_on_desktop(false),
29 create_in_applications_menu(false),
30 create_in_quick_launch_bar(false) {
31}
32
33ShellIntegration::ShortcutInfo::~ShortcutInfo() {}
34
[email protected]b10392932011-03-08 21:28:1435// static
36CommandLine ShellIntegration::CommandLineArgsForLauncher(
[email protected]2f1c09d2011-01-14 14:58:1437 const GURL& url,
[email protected]01ed1962011-03-04 19:03:1338 const std::string& extension_app_id) {
[email protected]b10392932011-03-08 21:28:1439 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
40 CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
[email protected]28375ae2010-02-05 04:45:5041
42 // Use the same UserDataDir for new launches that we currently have set.
[email protected]b10392932011-03-08 21:28:1443 FilePath user_data_dir = cmd_line.GetSwitchValuePath(switches::kUserDataDir);
44 if (!user_data_dir.empty()) {
[email protected]28375ae2010-02-05 04:45:5045 // Make sure user_data_dir is an absolute path.
46 if (file_util::AbsolutePath(&user_data_dir) &&
[email protected]63597e4e2010-07-08 17:49:0547 file_util::PathExists(user_data_dir)) {
[email protected]b10392932011-03-08 21:28:1448 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
[email protected]28375ae2010-02-05 04:45:5049 }
50 }
51
[email protected]660e428f2010-08-04 01:23:0052#if defined(OS_CHROMEOS)
[email protected]b10392932011-03-08 21:28:1453 FilePath profile = cmd_line.GetSwitchValuePath(switches::kLoginProfile);
54 if (!profile.empty())
55 new_cmd_line.AppendSwitchPath(switches::kLoginProfile, profile);
[email protected]28375ae2010-02-05 04:45:5056#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]ec5b50d2010-10-09 16:35:1861 if (!extension_app_id.empty()) {
[email protected]b10392932011-03-08 21:28:1462 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
[email protected]64048bd2010-03-08 23:28:5863 } else {
[email protected]28375ae2010-02-05 04:45:5064 // 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]b10392932011-03-08 21:28:1467 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
[email protected]28375ae2010-02-05 04:45:5068 }
[email protected]b10392932011-03-08 21:28:1469 return new_cmd_line;
[email protected]28375ae2010-02-05 04:45:5070}
71
[email protected]d24c4012009-07-28 01:57:3172///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:0273// ShellIntegration::DefaultWebClientWorker
[email protected]d24c4012009-07-28 01:57:3174//
initial.commit09911bf2008-07-26 23:55:2975
[email protected]4468a5b2011-05-26 07:48:0276ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
77 DefaultWebClientObserver* observer)
[email protected]0d3dc8e22009-11-03 02:27:0178 : observer_(observer) {
initial.commit09911bf2008-07-26 23:55:2979}
80
[email protected]4468a5b2011-05-26 07:48:0281void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
82 if (observer_) {
83 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
84 BrowserThread::PostTask(
85 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:3686 base::Bind(
87 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
[email protected]4468a5b2011-05-26 07:48:0288 }
89}
90
91void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
92 if (observer_) {
93 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
94 }
[email protected]0c7d74f2010-10-11 11:55:2695 BrowserThread::PostTask(
96 BrowserThread::FILE, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:3697 base::Bind(
98 &DefaultWebClientWorker::ExecuteSetAsDefault, this));
initial.commit09911bf2008-07-26 23:55:2999}
100
[email protected]4468a5b2011-05-26 07:48:02101void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
[email protected]d24c4012009-07-28 01:57:31102 // 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]bcb999d2011-05-31 15:11:35104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d24c4012009-07-28 01:57:31105 observer_ = NULL;
106}
107
108///////////////////////////////////////////////////////////////////////////////
[email protected]4468a5b2011-05-26 07:48:02109// DefaultWebClientWorker, private:
[email protected]d24c4012009-07-28 01:57:31110
[email protected]4468a5b2011-05-26 07:48:02111void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02113 DefaultWebClientState state = CheckIsDefault();
[email protected]0c7d74f2010-10-11 11:55:26114 BrowserThread::PostTask(
115 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36116 base::Bind(
117 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
[email protected]d24c4012009-07-28 01:57:31118}
119
[email protected]4468a5b2011-05-26 07:48:02120void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
121 DefaultWebClientState state) {
[email protected]0c7d74f2010-10-11 11:55:26122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]264f74d12009-09-04 23:39:58123 UpdateUI(state);
[email protected]97e2e3f2011-07-22 10:21:25124 // 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]d24c4012009-07-28 01:57:31130}
131
[email protected]4468a5b2011-05-26 07:48:02132void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]4468a5b2011-05-26 07:48:02134 SetAsDefault();
[email protected]0c7d74f2010-10-11 11:55:26135 BrowserThread::PostTask(
136 BrowserThread::UI, FROM_HERE,
[email protected]c5efb952011-11-01 01:28:36137 base::Bind(
138 &DefaultWebClientWorker::CompleteSetAsDefault, this));
[email protected]d24c4012009-07-28 01:57:31139}
140
[email protected]4468a5b2011-05-26 07:48:02141void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault() {
[email protected]0c7d74f2010-10-11 11:55:26142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4468a5b2011-05-26 07:48:02143 // Set as default completed, check again to make sure it stuck...
144 StartCheckIsDefault();
[email protected]d24c4012009-07-28 01:57:31145}
146
[email protected]4468a5b2011-05-26 07:48:02147void ShellIntegration::DefaultWebClientWorker::UpdateUI(
148 DefaultWebClientState state) {
[email protected]d24c4012009-07-28 01:57:31149 if (observer_) {
[email protected]264f74d12009-09-04 23:39:58150 switch (state) {
[email protected]4468a5b2011-05-26 07:48:02151 case NOT_DEFAULT_WEB_CLIENT:
152 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58153 break;
[email protected]4468a5b2011-05-26 07:48:02154 case IS_DEFAULT_WEB_CLIENT:
155 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
[email protected]264f74d12009-09-04 23:39:58156 break;
[email protected]4468a5b2011-05-26 07:48:02157 case UNKNOWN_DEFAULT_WEB_CLIENT:
158 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
[email protected]264f74d12009-09-04 23:39:58159 break;
160 default:
161 break;
162 }
[email protected]d24c4012009-07-28 01:57:31163 }
initial.commit09911bf2008-07-26 23:55:29164}
[email protected]4468a5b2011-05-26 07:48:02165
166///////////////////////////////////////////////////////////////////////////////
167// ShellIntegration::DefaultBrowserWorker
168//
169
170ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
171 DefaultWebClientObserver* observer)
172 : DefaultWebClientWorker(observer) {
173}
174
175///////////////////////////////////////////////////////////////////////////////
176// DefaultBrowserWorker, private:
177
178ShellIntegration::DefaultWebClientState
179ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
180 return ShellIntegration::IsDefaultBrowser();
181}
182
183void ShellIntegration::DefaultBrowserWorker::SetAsDefault() {
184 ShellIntegration::SetAsDefaultBrowser();
185}
186
187///////////////////////////////////////////////////////////////////////////////
188// ShellIntegration::DefaultProtocolClientWorker
189//
190
191ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
192 DefaultWebClientObserver* observer, const std::string& protocol)
193 : DefaultWebClientWorker(observer),
194 protocol_(protocol) {
195}
196
197///////////////////////////////////////////////////////////////////////////////
198// DefaultProtocolClientWorker, private:
199
200ShellIntegration::DefaultWebClientState
201ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
202 return ShellIntegration::IsDefaultProtocolClient(protocol_);
203}
204
205void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault() {
206 ShellIntegration::SetAsDefaultProtocolClient(protocol_);
207}