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 const CommandLine
& cmd_line
= *CommandLine::ForCurrentProcess();
80 CommandLine
new_cmd_line(CommandLine::NO_PROGRAM
);
82 // Use the same UserDataDir for new launches that we currently have set.
83 base::FilePath user_data_dir
=
84 cmd_line
.GetSwitchValuePath(switches::kUserDataDir
);
85 #if defined(OS_MACOSX) || defined(OS_WIN)
86 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
88 if (!user_data_dir
.empty()) {
89 // Make sure user_data_dir is an absolute path.
90 user_data_dir
= base::MakeAbsoluteFilePath(user_data_dir
);
91 if (!user_data_dir
.empty() && base::PathExists(user_data_dir
))
92 new_cmd_line
.AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
95 #if defined(OS_CHROMEOS)
96 base::FilePath profile
= cmd_line
.GetSwitchValuePath(
97 chromeos::switches::kLoginProfile
);
99 new_cmd_line
.AppendSwitchPath(chromeos::switches::kLoginProfile
, profile
);
101 if (!profile_path
.empty() && !extension_app_id
.empty())
102 new_cmd_line
.AppendSwitchPath(switches::kProfileDirectory
,
103 profile_path
.BaseName());
106 // If |extension_app_id| is present, we use the kAppId switch rather than
107 // the kApp switch (the launch url will be read from the extension app
109 if (!extension_app_id
.empty()) {
110 new_cmd_line
.AppendSwitchASCII(switches::kAppId
, extension_app_id
);
112 // Use '--app=url' instead of just 'url' to launch the browser with minimal
114 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
115 new_cmd_line
.AppendSwitchASCII(switches::kApp
, url
.spec());
122 base::string16
ShellIntegration::GetAppShortcutsSubdirName() {
123 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY
)
124 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY
);
125 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME
);
129 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
134 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
135 const std::string
& protocol
) {
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(