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 "content/public/browser/browser_thread.h"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/chromeos_switches.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/grit/chromium_strings.h"
26 #include "ui/base/l10n/l10n_util.h"
29 using content::BrowserThread
;
31 ShellIntegration::DefaultWebClientSetPermission
32 ShellIntegration::CanSetAsDefaultProtocolClient() {
33 // Allowed as long as the browser can become the operating system default
35 return CanSetAsDefaultBrowser();
38 static const struct ShellIntegration::AppModeInfo
* gAppModeInfo
= NULL
;
41 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo
* info
) {
46 const struct ShellIntegration::AppModeInfo
* ShellIntegration::AppModeInfo() {
51 bool ShellIntegration::IsRunningInAppMode() {
52 return gAppModeInfo
!= NULL
;
56 base::CommandLine
ShellIntegration::CommandLineArgsForLauncher(
58 const std::string
& extension_app_id
,
59 const base::FilePath
& profile_path
) {
60 base::ThreadRestrictions::AssertIOAllowed();
61 base::CommandLine
new_cmd_line(base::CommandLine::NO_PROGRAM
);
64 extension_app_id
.empty() ? base::FilePath() : profile_path
,
67 // If |extension_app_id| is present, we use the kAppId switch rather than
68 // the kApp switch (the launch url will be read from the extension app
70 if (!extension_app_id
.empty()) {
71 new_cmd_line
.AppendSwitchASCII(switches::kAppId
, extension_app_id
);
73 // Use '--app=url' instead of just 'url' to launch the browser with minimal
75 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
76 new_cmd_line
.AppendSwitchASCII(switches::kApp
, url
.spec());
82 void ShellIntegration::AppendProfileArgs(const base::FilePath
& profile_path
,
83 base::CommandLine
* command_line
) {
85 const base::CommandLine
& cmd_line
= *base::CommandLine::ForCurrentProcess();
87 // Use the same UserDataDir for new launches that we currently have set.
88 base::FilePath user_data_dir
=
89 cmd_line
.GetSwitchValuePath(switches::kUserDataDir
);
90 #if defined(OS_MACOSX) || defined(OS_WIN)
91 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
93 if (!user_data_dir
.empty()) {
94 // Make sure user_data_dir is an absolute path.
95 user_data_dir
= base::MakeAbsoluteFilePath(user_data_dir
);
96 if (!user_data_dir
.empty() && base::PathExists(user_data_dir
))
97 command_line
->AppendSwitchPath(switches::kUserDataDir
, user_data_dir
);
100 #if defined(OS_CHROMEOS)
101 base::FilePath profile
= cmd_line
.GetSwitchValuePath(
102 chromeos::switches::kLoginProfile
);
103 if (!profile
.empty())
104 command_line
->AppendSwitchPath(chromeos::switches::kLoginProfile
, profile
);
106 if (!profile_path
.empty())
107 command_line
->AppendSwitchPath(switches::kProfileDirectory
,
108 profile_path
.BaseName());
114 base::string16
ShellIntegration::GetAppShortcutsSubdirName() {
115 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY
)
116 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY
);
117 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME
);
121 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
126 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
127 const std::string
& protocol
) {
132 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
136 bool ShellIntegration::DefaultWebClientObserver::
137 IsInteractiveSetDefaultPermitted() {
141 ///////////////////////////////////////////////////////////////////////////////
142 // ShellIntegration::DefaultWebClientWorker
145 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
146 DefaultWebClientObserver
* observer
)
147 : observer_(observer
) {
150 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
152 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
153 BrowserThread::PostTask(
154 BrowserThread::FILE, FROM_HERE
,
156 &DefaultWebClientWorker::ExecuteCheckIsDefault
, this));
160 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
161 bool interactive_permitted
= false;
163 observer_
->SetDefaultWebClientUIState(STATE_PROCESSING
);
164 interactive_permitted
= observer_
->IsInteractiveSetDefaultPermitted();
166 BrowserThread::PostTask(
167 BrowserThread::FILE, FROM_HERE
,
168 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault
, this,
169 interactive_permitted
));
172 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
173 // Our associated view has gone away, so we shouldn't call back to it if
174 // our worker thread returns after the view is dead.
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
179 ///////////////////////////////////////////////////////////////////////////////
180 // DefaultWebClientWorker, private:
182 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
184 DefaultWebClientState state
= CheckIsDefault();
185 BrowserThread::PostTask(
186 BrowserThread::UI
, FROM_HERE
,
188 &DefaultWebClientWorker::CompleteCheckIsDefault
, this, state
));
191 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
192 DefaultWebClientState state
) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
195 // The worker has finished everything it needs to do, so free the observer
197 if (observer_
&& observer_
->IsOwnedByWorker()) {
203 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
204 bool interactive_permitted
) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
207 bool result
= SetAsDefault(interactive_permitted
);
208 BrowserThread::PostTask(
209 BrowserThread::UI
, FROM_HERE
,
210 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault
, this, result
));
213 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
216 // First tell the observer what the SetAsDefault call has returned.
218 observer_
->OnSetAsDefaultConcluded(succeeded
);
219 // Set as default completed, check again to make sure it stuck...
220 StartCheckIsDefault();
223 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
224 DefaultWebClientState state
) {
228 observer_
->SetDefaultWebClientUIState(STATE_NOT_DEFAULT
);
231 observer_
->SetDefaultWebClientUIState(STATE_IS_DEFAULT
);
233 case UNKNOWN_DEFAULT
:
234 observer_
->SetDefaultWebClientUIState(STATE_UNKNOWN
);
242 ///////////////////////////////////////////////////////////////////////////////
243 // ShellIntegration::DefaultBrowserWorker
246 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
247 DefaultWebClientObserver
* observer
)
248 : DefaultWebClientWorker(observer
) {
251 ///////////////////////////////////////////////////////////////////////////////
252 // DefaultBrowserWorker, private:
254 ShellIntegration::DefaultWebClientState
255 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
256 return ShellIntegration::GetDefaultBrowser();
259 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
260 bool interactive_permitted
) {
262 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
263 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
264 result
= ShellIntegration::SetAsDefaultBrowser();
266 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
267 if (interactive_permitted
)
268 result
= ShellIntegration::SetAsDefaultBrowserInteractive();
277 ///////////////////////////////////////////////////////////////////////////////
278 // ShellIntegration::DefaultProtocolClientWorker
281 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
282 DefaultWebClientObserver
* observer
, const std::string
& protocol
)
283 : DefaultWebClientWorker(observer
),
284 protocol_(protocol
) {
287 ///////////////////////////////////////////////////////////////////////////////
288 // DefaultProtocolClientWorker, private:
290 ShellIntegration::DefaultWebClientState
291 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
292 return ShellIntegration::IsDefaultProtocolClient(protocol_
);
295 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
296 bool interactive_permitted
) {
298 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
299 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED
:
302 case ShellIntegration::SET_DEFAULT_UNATTENDED
:
303 result
= ShellIntegration::SetAsDefaultProtocolClient(protocol_
);
305 case ShellIntegration::SET_DEFAULT_INTERACTIVE
:
306 if (interactive_permitted
) {
307 result
= ShellIntegration::SetAsDefaultProtocolClientInteractive(