shell_integration.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall// Use of this source code is governed by a BSD-style license that can be
3511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall// found in the LICENSE file.
4511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
5511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chrome/browser/shell_integration.h"
6511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
7511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/bind.h"
8511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/command_line.h"
9511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/file_util.h"
10511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/path_service.h"
11511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/prefs/pref_service.h"
12511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/strings/string_util.h"
13511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/strings/utf_string_conversions.h"
14511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "base/threading/thread_restrictions.h"
15511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chrome/browser/policy/policy_path_parser.h"
16511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chrome/common/chrome_paths.h"
17511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chrome/common/chrome_switches.h"
18511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chrome/common/pref_names.h"
19511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "content/public/browser/browser_thread.h"
20511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
21511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#if defined(OS_CHROMEOS)
22511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include "chromeos/chromeos_switches.h"
23511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#endif
24511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
25511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallusing content::BrowserThread;
26511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
27511eca30a483e912c274e1d8ba3a0f8f081e2227JP AbgrallShellIntegration::DefaultWebClientSetPermission
28511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall    ShellIntegration::CanSetAsDefaultProtocolClient() {
29511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall  // Allowed as long as the browser can become the operating system default
30511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall  // browser.
31511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall  return CanSetAsDefaultBrowser();
32511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall}
33511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
34511eca30a483e912c274e1d8ba3a0f8f081e2227JP AbgrallShellIntegration::ShortcutInfo::ShortcutInfo()
35511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall    : is_platform_app(false) {
36511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall}
37511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
38511eca30a483e912c274e1d8ba3a0f8f081e2227JP AbgrallShellIntegration::ShortcutInfo::~ShortcutInfo() {}
39511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
40511eca30a483e912c274e1d8ba3a0f8f081e2227JP AbgrallShellIntegration::ShortcutLocations::ShortcutLocations()
41511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall    : on_desktop(false),
42511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall      in_applications_menu(false),
43511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall      in_quick_launch_bar(false),
44511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall      hidden(false) {
45511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall}
46511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
47511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
48511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall
49// static
50void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
51  gAppModeInfo = info;
52}
53
54// static
55const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
56  return gAppModeInfo;
57}
58
59// static
60bool ShellIntegration::IsRunningInAppMode() {
61  return gAppModeInfo != NULL;
62}
63
64// static
65CommandLine ShellIntegration::CommandLineArgsForLauncher(
66    const GURL& url,
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);
72
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);
78#endif
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);
84  }
85
86#if defined(OS_CHROMEOS)
87  base::FilePath profile = cmd_line.GetSwitchValuePath(
88      chromeos::switches::kLoginProfile);
89  if (!profile.empty())
90    new_cmd_line.AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
91#else
92  if (!profile_path.empty() && !extension_app_id.empty())
93    new_cmd_line.AppendSwitchPath(switches::kProfileDirectory,
94                                  profile_path.BaseName());
95#endif
96
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
99  // during launch.
100  if (!extension_app_id.empty()) {
101    new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
102  } else {
103    // Use '--app=url' instead of just 'url' to launch the browser with minimal
104    // chrome.
105    // Note: Do not change this flag!  Old Gears shortcuts will break if you do!
106    new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
107  }
108  return new_cmd_line;
109}
110
111#if !defined(OS_WIN)
112// static
113bool ShellIntegration::SetAsDefaultBrowserInteractive() {
114  return false;
115}
116
117// static
118bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
119    const std::string& protocol) {
120  return false;
121}
122#endif
123
124bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
125  return false;
126}
127
128bool ShellIntegration::DefaultWebClientObserver::
129    IsInteractiveSetDefaultPermitted() {
130  return false;
131}
132
133///////////////////////////////////////////////////////////////////////////////
134// ShellIntegration::DefaultWebClientWorker
135//
136
137ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
138    DefaultWebClientObserver* observer)
139    : observer_(observer) {
140}
141
142void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
143  if (observer_) {
144    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
145    BrowserThread::PostTask(
146        BrowserThread::FILE, FROM_HERE,
147        base::Bind(
148            &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
149  }
150}
151
152void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
153  bool interactive_permitted = false;
154  if (observer_) {
155    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
156    interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
157  }
158  BrowserThread::PostTask(
159      BrowserThread::FILE, FROM_HERE,
160      base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
161                 interactive_permitted));
162}
163
164void 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));
168  observer_ = NULL;
169}
170
171///////////////////////////////////////////////////////////////////////////////
172// DefaultWebClientWorker, private:
173
174void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
175  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
176  DefaultWebClientState state = CheckIsDefault();
177  BrowserThread::PostTask(
178      BrowserThread::UI, FROM_HERE,
179      base::Bind(
180          &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
181}
182
183void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
184    DefaultWebClientState state) {
185  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
186  UpdateUI(state);
187  // The worker has finished everything it needs to do, so free the observer
188  // if we own it.
189  if (observer_ && observer_->IsOwnedByWorker()) {
190    delete observer_;
191    observer_ = NULL;
192  }
193}
194
195void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
196    bool interactive_permitted) {
197  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
198
199  bool result = SetAsDefault(interactive_permitted);
200  BrowserThread::PostTask(
201      BrowserThread::UI, FROM_HERE,
202      base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
203}
204
205void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
206    bool succeeded) {
207  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
208  // First tell the observer what the SetAsDefault call has returned.
209  if (observer_)
210    observer_->OnSetAsDefaultConcluded(succeeded);
211  // Set as default completed, check again to make sure it stuck...
212  StartCheckIsDefault();
213}
214
215void ShellIntegration::DefaultWebClientWorker::UpdateUI(
216    DefaultWebClientState state) {
217  if (observer_) {
218    switch (state) {
219      case NOT_DEFAULT:
220        observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
221        break;
222      case IS_DEFAULT:
223        observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
224        break;
225      case UNKNOWN_DEFAULT:
226        observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
227        break;
228      default:
229        break;
230    }
231  }
232}
233
234///////////////////////////////////////////////////////////////////////////////
235// ShellIntegration::DefaultBrowserWorker
236//
237
238ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
239    DefaultWebClientObserver* observer)
240    : DefaultWebClientWorker(observer) {
241}
242
243///////////////////////////////////////////////////////////////////////////////
244// DefaultBrowserWorker, private:
245
246ShellIntegration::DefaultWebClientState
247ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
248  return ShellIntegration::GetDefaultBrowser();
249}
250
251bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
252    bool interactive_permitted) {
253  bool result = false;
254  switch (ShellIntegration::CanSetAsDefaultBrowser()) {
255    case ShellIntegration::SET_DEFAULT_UNATTENDED:
256      result = ShellIntegration::SetAsDefaultBrowser();
257      break;
258    case ShellIntegration::SET_DEFAULT_INTERACTIVE:
259      if (interactive_permitted)
260        result = ShellIntegration::SetAsDefaultBrowserInteractive();
261      break;
262    default:
263      NOTREACHED();
264  }
265
266  return result;
267}
268
269///////////////////////////////////////////////////////////////////////////////
270// ShellIntegration::DefaultProtocolClientWorker
271//
272
273ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
274    DefaultWebClientObserver* observer, const std::string& protocol)
275    : DefaultWebClientWorker(observer),
276      protocol_(protocol) {
277}
278
279///////////////////////////////////////////////////////////////////////////////
280// DefaultProtocolClientWorker, private:
281
282ShellIntegration::DefaultWebClientState
283ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
284  return ShellIntegration::IsDefaultProtocolClient(protocol_);
285}
286
287bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
288    bool interactive_permitted) {
289  bool result = false;
290  switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
291    case ShellIntegration::SET_DEFAULT_UNATTENDED:
292      result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
293      break;
294    case ShellIntegration::SET_DEFAULT_INTERACTIVE:
295      if (interactive_permitted) {
296        result = ShellIntegration::SetAsDefaultProtocolClientInteractive(
297            protocol_);
298      }
299      break;
300    default:
301      NOTREACHED();
302  }
303
304  return result;
305}
306