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/file_util.h"
10 #include "base/path_service.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "chrome/browser/policy/policy_path_parser.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/browser_thread.h"
21 #if defined(OS_CHROMEOS)
22 #include "chromeos/chromeos_switches.h"
26 #include "chrome/common/chrome_version_info.h"
27 #include "grit/chromium_strings.h"
28 #include "ui/base/l10n/l10n_util.h"
31 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 ShellIntegration::ShortcutInfo::ShortcutInfo()
41 : is_platform_app(false) {
44 ShellIntegration::ShortcutInfo::~ShortcutInfo() {}
46 ShellIntegration::ShortcutLocations::ShortcutLocations()
48 applications_menu_location(APP_MENU_LOCATION_NONE
),
49 in_quick_launch_bar(false)
56 static const struct ShellIntegration::AppModeInfo
* gAppModeInfo
= NULL
;
59 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo
* info
) {
64 const struct ShellIntegration::AppModeInfo
* ShellIntegration::AppModeInfo() {
69 bool ShellIntegration::IsRunningInAppMode() {
70 return gAppModeInfo
!= NULL
;
74 CommandLine
ShellIntegration::CommandLineArgsForLauncher(
76 const std::string
& extension_app_id
,
77 const base::FilePath
& profile_path
) {
78 base::ThreadRestrictions::AssertIOAllowed();
79 CommandLine
new_cmd_line(CommandLine::NO_PROGRAM
);
82 extension_app_id
.empty() ? base::FilePath() : profile_path
,
85 // If |extension_app_id| is present, we use the kAppId switch rather than
86 // the kApp switch (the launch url will be read from the extension app
88 if (!extension_app_id
.empty()) {
89 new_cmd_line
.AppendSwitchASCII(switches::kAppId
, extension_app_id
);
91 // Use '--app=url' instead of just 'url' to launch the browser with minimal
93 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
94 new_cmd_line
.AppendSwitchASCII(switches::kApp
, url
.spec());
100 void ShellIntegration::AppendProfileArgs(
101 const base::FilePath
& profile_path
,
102 CommandLine
* command_line
) {
103 DCHECK(command_line
);
104 const CommandLine
& cmd_line
= *CommandLine::ForCurrentProcess();
106 // Use the same UserDataDir for new launches that we currently have set.
107 base::FilePath user_data_dir
=
108 cmd_line
.GetSwitchValuePath(switches::kUserDataDir
);
109 #if defined(OS_MACOSX) || defined(OS_WIN)
110 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
112 if (!user_data_dir
.empty()) {
113 // Make sure user_data_dir is an absolute path.
114 user_data_dir
= base::MakeAbsoluteFilePath(user_data_dir
);
115 if (!user_data_dir
.empty() && base::PathExists(user_data_dir
))
116 command_line
->AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
119 #if defined(OS_CHROMEOS)
120 base::FilePath profile
= cmd_line
.GetSwitchValuePath(
121 chromeos::switches::kLoginProfile
);
122 if (!profile
.empty())
123 command_line
->AppendSwitchPath(chromeos::switches::kLoginProfile
, profile
);
125 if (!profile_path
.empty())
126 command_line
->AppendSwitchPath(switches::kProfileDirectory
,
127 profile_path
.BaseName());
133 base::string16
ShellIntegration::GetAppShortcutsSubdirName() {
134 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY
)
135 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY
);
136 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME
);
140 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
145 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
146 const std::string
& protocol
) {
151 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
155 bool ShellIntegration::DefaultWebClientObserver::
156 IsInteractiveSetDefaultPermitted() {
160 ///////////////////////////////////////////////////////////////////////////////
161 // ShellIntegration::DefaultWebClientWorker
164 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
165 DefaultWebClientObserver
* observer
)
166 : observer_(observer
) {
169 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
171 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
172 BrowserThread::PostTask(
173 BrowserThread::FILE, FROM_HERE
,
175 &DefaultWebClientWorker::ExecuteCheckIsDefault
, this));
179 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
180 bool interactive_permitted
= false;
182 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
183 interactive_permitted
= observer_
->IsInteractiveSetDefaultPermitted();
185 BrowserThread::PostTask(
186 BrowserThread::FILE, FROM_HERE
,
187 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault
, this,
188 interactive_permitted
));
191 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
192 // Our associated view has gone away, so we shouldn't call back to it if
193 // our worker thread returns after the view is dead.
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
198 ///////////////////////////////////////////////////////////////////////////////
199 // DefaultWebClientWorker, private:
201 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
203 DefaultWebClientState state
= CheckIsDefault();
204 BrowserThread::PostTask(
205 BrowserThread::UI
, FROM_HERE
,
207 &DefaultWebClientWorker::CompleteCheckIsDefault
, this, state
));
210 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
211 DefaultWebClientState state
) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
214 // The worker has finished everything it needs to do, so free the observer
216 if (observer_
&& observer_
->IsOwnedByWorker()) {
222 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
223 bool interactive_permitted
) {
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
226 bool result
= SetAsDefault(interactive_permitted
);
227 BrowserThread::PostTask(
228 BrowserThread::UI
, FROM_HERE
,
229 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault
, this, result
));
232 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
235 // First tell the observer what the SetAsDefault call has returned.
237 observer_
->OnSetAsDefaultConcluded(succeeded
);
238 // Set as default completed, check again to make sure it stuck...
239 StartCheckIsDefault();
242 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
243 DefaultWebClientState state
) {
247 observer_
->SetDefaultWebClientUIState(STATE_NOT_DEFAULT
);
250 observer_
->SetDefaultWebClientUIState(STATE_IS_DEFAULT
);
252 case UNKNOWN_DEFAULT
:
253 observer_
->SetDefaultWebClientUIState(STATE_UNKNOWN
);
261 ///////////////////////////////////////////////////////////////////////////////
262 // ShellIntegration::DefaultBrowserWorker
265 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
266 DefaultWebClientObserver
* observer
)
267 : DefaultWebClientWorker(observer
) {
270 ///////////////////////////////////////////////////////////////////////////////
271 // DefaultBrowserWorker, private:
273 ShellIntegration::DefaultWebClientState
274 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
275 return ShellIntegration::GetDefaultBrowser();
278 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
279 bool interactive_permitted
) {
281 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
282 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
283 result
= ShellIntegration::SetAsDefaultBrowser();
285 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
286 if (interactive_permitted
)
287 result
= ShellIntegration::SetAsDefaultBrowserInteractive();
296 ///////////////////////////////////////////////////////////////////////////////
297 // ShellIntegration::DefaultProtocolClientWorker
300 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
301 DefaultWebClientObserver
* observer
, const std::string
& protocol
)
302 : DefaultWebClientWorker(observer
),
303 protocol_(protocol
) {
306 ///////////////////////////////////////////////////////////////////////////////
307 // DefaultProtocolClientWorker, private:
309 ShellIntegration::DefaultWebClientState
310 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
311 return ShellIntegration::IsDefaultProtocolClient(protocol_
);
314 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
315 bool interactive_permitted
) {
317 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
318 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED
:
321 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
322 result
= ShellIntegration::SetAsDefaultProtocolClient(protocol_
);
324 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
325 if (interactive_permitted
) {
326 result
= ShellIntegration::SetAsDefaultProtocolClientInteractive(