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"
25 using content::BrowserThread;
27 ShellIntegration::DefaultWebClientSetPermission
28 ShellIntegration::CanSetAsDefaultProtocolClient() {
29 // Allowed as long as the browser can become the operating system default
31 return CanSetAsDefaultBrowser();
34 ShellIntegration::ShortcutInfo::ShortcutInfo()
35 : is_platform_app(false) {
38 ShellIntegration::ShortcutInfo::~ShortcutInfo() {}
40 ShellIntegration::ShortcutLocations::ShortcutLocations()
42 in_applications_menu(false),
43 in_quick_launch_bar(false),
47 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
50 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
55 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
60 bool ShellIntegration::IsRunningInAppMode() {
61 return gAppModeInfo != NULL;
65 CommandLine ShellIntegration::CommandLineArgsForLauncher(
67 const std::string& extension_app_id,
68 const base::FilePath& profile_path) {
69 base::ThreadRestrictions::AssertIOAllowed();
70 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
71 CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
73 // Use the same UserDataDir for new launches that we currently have set.
74 base::FilePath user_data_dir =
75 cmd_line.GetSwitchValuePath(switches::kUserDataDir);
76 #if defined(OS_MACOSX) || defined(OS_WIN)
77 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
79 if (!user_data_dir.empty()) {
80 // Make sure user_data_dir is an absolute path.
81 user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
82 if (!user_data_dir.empty() && base::PathExists(user_data_dir))
83 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
86 #if defined(OS_CHROMEOS)
87 base::FilePath profile = cmd_line.GetSwitchValuePath(
88 chromeos::switches::kLoginProfile);
90 new_cmd_line.AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
92 if (!profile_path.empty() && !extension_app_id.empty())
93 new_cmd_line.AppendSwitchPath(switches::kProfileDirectory,
94 profile_path.BaseName());
97 // If |extension_app_id| is present, we use the kAppId switch rather than
98 // the kApp switch (the launch url will be read from the extension app
100 if (!extension_app_id.empty()) {
101 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
103 // Use '--app=url' instead of just 'url' to launch the browser with minimal
105 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
106 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
113 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
118 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
119 const std::string& protocol) {
124 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
128 bool ShellIntegration::DefaultWebClientObserver::
129 IsInteractiveSetDefaultPermitted() {
133 ///////////////////////////////////////////////////////////////////////////////
134 // ShellIntegration::DefaultWebClientWorker
137 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
138 DefaultWebClientObserver* observer)
139 : observer_(observer) {
142 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
144 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
145 BrowserThread::PostTask(
146 BrowserThread::FILE, FROM_HERE,
148 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
152 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
153 bool interactive_permitted = false;
155 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
156 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
158 BrowserThread::PostTask(
159 BrowserThread::FILE, FROM_HERE,
160 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
161 interactive_permitted));
164 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
165 // Our associated view has gone away, so we shouldn't call back to it if
166 // our worker thread returns after the view is dead.
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
171 ///////////////////////////////////////////////////////////////////////////////
172 // DefaultWebClientWorker, private:
174 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
176 DefaultWebClientState state = CheckIsDefault();
177 BrowserThread::PostTask(
178 BrowserThread::UI, FROM_HERE,
180 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
183 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
184 DefaultWebClientState state) {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 // The worker has finished everything it needs to do, so free the observer
189 if (observer_ && observer_->IsOwnedByWorker()) {
195 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
196 bool interactive_permitted) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
199 bool result = SetAsDefault(interactive_permitted);
200 BrowserThread::PostTask(
201 BrowserThread::UI, FROM_HERE,
202 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
205 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
208 // First tell the observer what the SetAsDefault call has returned.
210 observer_->OnSetAsDefaultConcluded(succeeded);
211 // Set as default completed, check again to make sure it stuck...
212 StartCheckIsDefault();
215 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
216 DefaultWebClientState state) {
220 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
223 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
225 case UNKNOWN_DEFAULT:
226 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
234 ///////////////////////////////////////////////////////////////////////////////
235 // ShellIntegration::DefaultBrowserWorker
238 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
239 DefaultWebClientObserver* observer)
240 : DefaultWebClientWorker(observer) {
243 ///////////////////////////////////////////////////////////////////////////////
244 // DefaultBrowserWorker, private:
246 ShellIntegration::DefaultWebClientState
247 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
248 return ShellIntegration::GetDefaultBrowser();
251 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
252 bool interactive_permitted) {
254 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
255 case ShellIntegration::SET_DEFAULT_UNATTENDED:
256 result = ShellIntegration::SetAsDefaultBrowser();
258 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
259 if (interactive_permitted)
260 result = ShellIntegration::SetAsDefaultBrowserInteractive();
269 ///////////////////////////////////////////////////////////////////////////////
270 // ShellIntegration::DefaultProtocolClientWorker
273 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
274 DefaultWebClientObserver* observer, const std::string& protocol)
275 : DefaultWebClientWorker(observer),
276 protocol_(protocol) {
279 ///////////////////////////////////////////////////////////////////////////////
280 // DefaultProtocolClientWorker, private:
282 ShellIntegration::DefaultWebClientState
283 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
284 return ShellIntegration::IsDefaultProtocolClient(protocol_);
287 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
288 bool interactive_permitted) {
290 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
291 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED:
294 case ShellIntegration::SET_DEFAULT_UNATTENDED:
295 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
297 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
298 if (interactive_permitted) {
299 result = ShellIntegration::SetAsDefaultProtocolClientInteractive(