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
;
33 ShellIntegration::DefaultWebClientSetPermission
34 ShellIntegration::CanSetAsDefaultProtocolClient() {
35 // Allowed as long as the browser can become the operating system default
37 return CanSetAsDefaultBrowser();
40 static const struct ShellIntegration::AppModeInfo
* gAppModeInfo
= NULL
;
43 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo
* info
) {
48 const struct ShellIntegration::AppModeInfo
* ShellIntegration::AppModeInfo() {
53 bool ShellIntegration::IsRunningInAppMode() {
54 return gAppModeInfo
!= NULL
;
58 base::CommandLine
ShellIntegration::CommandLineArgsForLauncher(
60 const std::string
& extension_app_id
,
61 const base::FilePath
& profile_path
) {
62 base::ThreadRestrictions::AssertIOAllowed();
63 base::CommandLine
new_cmd_line(base::CommandLine::NO_PROGRAM
);
66 extension_app_id
.empty() ? base::FilePath() : profile_path
,
69 // If |extension_app_id| is present, we use the kAppId switch rather than
70 // the kApp switch (the launch url will be read from the extension app
72 if (!extension_app_id
.empty()) {
73 new_cmd_line
.AppendSwitchASCII(switches::kAppId
, extension_app_id
);
75 // Use '--app=url' instead of just 'url' to launch the browser with minimal
77 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
78 new_cmd_line
.AppendSwitchASCII(switches::kApp
, url
.spec());
84 void ShellIntegration::AppendProfileArgs(const base::FilePath
& profile_path
,
85 base::CommandLine
* command_line
) {
87 const base::CommandLine
& cmd_line
= *base::CommandLine::ForCurrentProcess();
89 // Use the same UserDataDir for new launches that we currently have set.
90 base::FilePath user_data_dir
=
91 cmd_line
.GetSwitchValuePath(switches::kUserDataDir
);
92 #if defined(OS_MACOSX) || defined(OS_WIN)
93 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
95 if (!user_data_dir
.empty()) {
96 // Make sure user_data_dir is an absolute path.
97 user_data_dir
= base::MakeAbsoluteFilePath(user_data_dir
);
98 if (!user_data_dir
.empty() && base::PathExists(user_data_dir
))
99 command_line
->AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
102 #if defined(OS_CHROMEOS)
103 base::FilePath profile
= cmd_line
.GetSwitchValuePath(
104 chromeos::switches::kLoginProfile
);
105 if (!profile
.empty())
106 command_line
->AppendSwitchPath(chromeos::switches::kLoginProfile
, profile
);
108 if (!profile_path
.empty())
109 command_line
->AppendSwitchPath(switches::kProfileDirectory
,
110 profile_path
.BaseName());
116 base::string16
ShellIntegration::GetAppShortcutsSubdirName() {
117 if (chrome::GetChannel() == version_info::Channel::CANARY
)
118 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY
);
119 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME
);
123 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
128 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
129 const std::string
& protocol
) {
134 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() {
138 #endif // !defined(OS_WIN)
140 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
144 bool ShellIntegration::DefaultWebClientObserver::
145 IsInteractiveSetDefaultPermitted() {
149 ///////////////////////////////////////////////////////////////////////////////
150 // ShellIntegration::DefaultWebClientWorker
153 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
154 DefaultWebClientObserver
* observer
)
155 : observer_(observer
) {
158 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
160 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
161 BrowserThread::PostTask(
162 BrowserThread::FILE, FROM_HERE
,
164 &DefaultWebClientWorker::ExecuteCheckIsDefault
, this));
168 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
169 bool interactive_permitted
= false;
171 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
172 interactive_permitted
= observer_
->IsInteractiveSetDefaultPermitted();
174 BrowserThread::PostTask(
175 BrowserThread::FILE, FROM_HERE
,
176 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault
, this,
177 interactive_permitted
));
180 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
181 // Our associated view has gone away, so we shouldn't call back to it if
182 // our worker thread returns after the view is dead.
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
187 ///////////////////////////////////////////////////////////////////////////////
188 // DefaultWebClientWorker, private:
190 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
192 DefaultWebClientState state
= CheckIsDefault();
193 BrowserThread::PostTask(
194 BrowserThread::UI
, FROM_HERE
,
196 &DefaultWebClientWorker::CompleteCheckIsDefault
, this, state
));
199 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
200 DefaultWebClientState state
) {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
203 // The worker has finished everything it needs to do, so free the observer
205 if (observer_
&& observer_
->IsOwnedByWorker()) {
211 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
212 bool interactive_permitted
) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
215 bool result
= SetAsDefault(interactive_permitted
);
216 BrowserThread::PostTask(
217 BrowserThread::UI
, FROM_HERE
,
218 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault
, this, result
));
221 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
224 // First tell the observer what the SetAsDefault call has returned.
226 observer_
->OnSetAsDefaultConcluded(succeeded
);
227 // Set as default completed, check again to make sure it stuck...
228 StartCheckIsDefault();
231 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
232 DefaultWebClientState state
) {
236 observer_
->SetDefaultWebClientUIState(STATE_NOT_DEFAULT
);
239 observer_
->SetDefaultWebClientUIState(STATE_IS_DEFAULT
);
241 case UNKNOWN_DEFAULT
:
242 observer_
->SetDefaultWebClientUIState(STATE_UNKNOWN
);
250 ///////////////////////////////////////////////////////////////////////////////
251 // ShellIntegration::DefaultBrowserWorker
254 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
255 DefaultWebClientObserver
* observer
)
256 : DefaultWebClientWorker(observer
) {
259 ///////////////////////////////////////////////////////////////////////////////
260 // DefaultBrowserWorker, private:
262 ShellIntegration::DefaultWebClientState
263 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
264 return ShellIntegration::GetDefaultBrowser();
267 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
268 bool interactive_permitted
) {
270 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
271 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
272 result
= ShellIntegration::SetAsDefaultBrowser();
274 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
275 if (interactive_permitted
)
276 result
= ShellIntegration::SetAsDefaultBrowserInteractive();
285 ///////////////////////////////////////////////////////////////////////////////
286 // ShellIntegration::DefaultProtocolClientWorker
289 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
290 DefaultWebClientObserver
* observer
, const std::string
& protocol
)
291 : DefaultWebClientWorker(observer
),
292 protocol_(protocol
) {
295 ///////////////////////////////////////////////////////////////////////////////
296 // DefaultProtocolClientWorker, private:
298 ShellIntegration::DefaultWebClientState
299 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
300 return ShellIntegration::IsDefaultProtocolClient(protocol_
);
303 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
304 bool interactive_permitted
) {
306 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
307 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED
:
310 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
311 result
= ShellIntegration::SetAsDefaultProtocolClient(protocol_
);
313 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
314 if (interactive_permitted
) {
315 result
= ShellIntegration::SetAsDefaultProtocolClientInteractive(