1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/shell_integration.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "chrome/browser/policy/policy_path_parser.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "components/version_info/version_info.h"
18 #include "content/public/browser/browser_thread.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/chromeos_switches.h"
25 #include "chrome/common/channel_info.h"
26 #include "chrome/grit/chromium_strings.h"
27 #include "ui/base/l10n/l10n_util.h"
30 using content::BrowserThread
;
32 ShellIntegration::DefaultWebClientSetPermission
33 ShellIntegration::CanSetAsDefaultProtocolClient() {
34 // Allowed as long as the browser can become the operating system default
36 return CanSetAsDefaultBrowser();
39 static const struct ShellIntegration::AppModeInfo
* gAppModeInfo
= NULL
;
42 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo
* info
) {
47 const struct ShellIntegration::AppModeInfo
* ShellIntegration::AppModeInfo() {
52 bool ShellIntegration::IsRunningInAppMode() {
53 return gAppModeInfo
!= NULL
;
57 base::CommandLine
ShellIntegration::CommandLineArgsForLauncher(
59 const std::string
& extension_app_id
,
60 const base::FilePath
& profile_path
) {
61 base::ThreadRestrictions::AssertIOAllowed();
62 base::CommandLine
new_cmd_line(base::CommandLine::NO_PROGRAM
);
65 extension_app_id
.empty() ? base::FilePath() : profile_path
,
68 // If |extension_app_id| is present, we use the kAppId switch rather than
69 // the kApp switch (the launch url will be read from the extension app
71 if (!extension_app_id
.empty()) {
72 new_cmd_line
.AppendSwitchASCII(switches::kAppId
, extension_app_id
);
74 // Use '--app=url' instead of just 'url' to launch the browser with minimal
76 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
77 new_cmd_line
.AppendSwitchASCII(switches::kApp
, url
.spec());
83 void ShellIntegration::AppendProfileArgs(const base::FilePath
& profile_path
,
84 base::CommandLine
* command_line
) {
86 const base::CommandLine
& cmd_line
= *base::CommandLine::ForCurrentProcess();
88 // Use the same UserDataDir for new launches that we currently have set.
89 base::FilePath user_data_dir
=
90 cmd_line
.GetSwitchValuePath(switches::kUserDataDir
);
91 #if defined(OS_MACOSX) || defined(OS_WIN)
92 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
94 if (!user_data_dir
.empty()) {
95 // Make sure user_data_dir is an absolute path.
96 user_data_dir
= base::MakeAbsoluteFilePath(user_data_dir
);
97 if (!user_data_dir
.empty() && base::PathExists(user_data_dir
))
98 command_line
->AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
101 #if defined(OS_CHROMEOS)
102 base::FilePath profile
= cmd_line
.GetSwitchValuePath(
103 chromeos::switches::kLoginProfile
);
104 if (!profile
.empty())
105 command_line
->AppendSwitchPath(chromeos::switches::kLoginProfile
, profile
);
107 if (!profile_path
.empty())
108 command_line
->AppendSwitchPath(switches::kProfileDirectory
,
109 profile_path
.BaseName());
115 base::string16
ShellIntegration::GetAppShortcutsSubdirName() {
116 if (chrome::GetChannel() == version_info::Channel::CANARY
)
117 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY
);
118 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME
);
122 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
127 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
128 const std::string
& protocol
) {
133 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
137 bool ShellIntegration::DefaultWebClientObserver::
138 IsInteractiveSetDefaultPermitted() {
142 ///////////////////////////////////////////////////////////////////////////////
143 // ShellIntegration::DefaultWebClientWorker
146 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
147 DefaultWebClientObserver
* observer
)
148 : observer_(observer
) {
151 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
153 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
154 BrowserThread::PostTask(
155 BrowserThread::FILE, FROM_HERE
,
157 &DefaultWebClientWorker::ExecuteCheckIsDefault
, this));
161 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
162 bool interactive_permitted
= false;
164 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
165 interactive_permitted
= observer_
->IsInteractiveSetDefaultPermitted();
167 BrowserThread::PostTask(
168 BrowserThread::FILE, FROM_HERE
,
169 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault
, this,
170 interactive_permitted
));
173 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
174 // Our associated view has gone away, so we shouldn't call back to it if
175 // our worker thread returns after the view is dead.
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
180 ///////////////////////////////////////////////////////////////////////////////
181 // DefaultWebClientWorker, private:
183 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
185 DefaultWebClientState state
= CheckIsDefault();
186 BrowserThread::PostTask(
187 BrowserThread::UI
, FROM_HERE
,
189 &DefaultWebClientWorker::CompleteCheckIsDefault
, this, state
));
192 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
193 DefaultWebClientState state
) {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
196 // The worker has finished everything it needs to do, so free the observer
198 if (observer_
&& observer_
->IsOwnedByWorker()) {
204 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
205 bool interactive_permitted
) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
208 bool result
= SetAsDefault(interactive_permitted
);
209 BrowserThread::PostTask(
210 BrowserThread::UI
, FROM_HERE
,
211 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault
, this, result
));
214 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
217 // First tell the observer what the SetAsDefault call has returned.
219 observer_
->OnSetAsDefaultConcluded(succeeded
);
220 // Set as default completed, check again to make sure it stuck...
221 StartCheckIsDefault();
224 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
225 DefaultWebClientState state
) {
229 observer_
->SetDefaultWebClientUIState(STATE_NOT_DEFAULT
);
232 observer_
->SetDefaultWebClientUIState(STATE_IS_DEFAULT
);
234 case UNKNOWN_DEFAULT
:
235 observer_
->SetDefaultWebClientUIState(STATE_UNKNOWN
);
243 ///////////////////////////////////////////////////////////////////////////////
244 // ShellIntegration::DefaultBrowserWorker
247 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
248 DefaultWebClientObserver
* observer
)
249 : DefaultWebClientWorker(observer
) {
252 ///////////////////////////////////////////////////////////////////////////////
253 // DefaultBrowserWorker, private:
255 ShellIntegration::DefaultWebClientState
256 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
257 return ShellIntegration::GetDefaultBrowser();
260 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
261 bool interactive_permitted
) {
263 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
264 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
265 result
= ShellIntegration::SetAsDefaultBrowser();
267 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
268 if (interactive_permitted
)
269 result
= ShellIntegration::SetAsDefaultBrowserInteractive();
278 ///////////////////////////////////////////////////////////////////////////////
279 // ShellIntegration::DefaultProtocolClientWorker
282 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
283 DefaultWebClientObserver
* observer
, const std::string
& protocol
)
284 : DefaultWebClientWorker(observer
),
285 protocol_(protocol
) {
288 ///////////////////////////////////////////////////////////////////////////////
289 // DefaultProtocolClientWorker, private:
291 ShellIntegration::DefaultWebClientState
292 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
293 return ShellIntegration::IsDefaultProtocolClient(protocol_
);
296 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
297 bool interactive_permitted
) {
299 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
300 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED
:
303 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
304 result
= ShellIntegration::SetAsDefaultProtocolClient(protocol_
);
306 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
307 if (interactive_permitted
) {
308 result
= ShellIntegration::SetAsDefaultProtocolClientInteractive(