blob: 43b6dbc0f7b12996f3932b5154e54771661f3e3d [file] [log] [blame]
[email protected]5a20b192012-03-01 06:01:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]4a0765a2009-05-08 23:12:255#include "chrome/browser/shell_integration.h"
6
[email protected]c5efb952011-11-01 01:28:367#include "base/bind.h"
[email protected]28375ae2010-02-05 04:45:508#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:049#include "base/files/file_util.h"
pmonette5207d8952016-03-22 15:53:0810#include "base/metrics/histogram.h"
[email protected]24a555b62013-06-10 22:01:1711#include "base/strings/string_util.h"
pmonette9c1457f2015-11-19 20:29:3112#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0813#include "base/strings/utf_string_conversions.h"
[email protected]124901f52013-03-27 14:42:1014#include "base/threading/thread_restrictions.h"
avi664c07b2015-12-26 02:18:3115#include "build/build_config.h"
[email protected]ba26a442013-02-20 15:32:5516#include "chrome/browser/policy/policy_path_parser.h"
[email protected]b96aa932009-08-12 21:34:4917#include "chrome/common/chrome_paths.h"
[email protected]28375ae2010-02-05 04:45:5018#include "chrome/common/chrome_switches.h"
brettwb1fc1b82016-02-02 00:19:0819#include "components/prefs/pref_service.h"
sdefresne9fb67692015-08-03 18:48:2220#include "components/version_info/version_info.h"
[email protected]c38831a12011-10-28 12:44:4921#include "content/public/browser/browser_thread.h"
initial.commit09911bf2008-07-26 23:55:2922
[email protected]931d1042013-04-05 17:50:4423#if defined(OS_CHROMEOS)
24#include "chromeos/chromeos_switches.h"
25#endif
26
[email protected]2f5bc322013-12-04 08:58:1527#if !defined(OS_WIN)
sdefresne9fb67692015-08-03 18:48:2228#include "chrome/common/channel_info.h"
[email protected]af39f002014-08-22 10:18:1829#include "chrome/grit/chromium_strings.h"
[email protected]2f5bc322013-12-04 08:58:1530#include "ui/base/l10n/l10n_util.h"
31#endif
32
[email protected]631bb742011-11-02 11:29:3933using content::BrowserThread;
34
pmonette9fa59e882016-02-10 00:12:1935namespace shell_integration {
36
pmonette034a03d92015-10-02 21:04:2737namespace {
38
pmonette9fa59e882016-02-10 00:12:1939const struct AppModeInfo* gAppModeInfo = nullptr;
pmonette034a03d92015-10-02 21:04:2740
41} // namespace
42
43#if !defined(OS_WIN)
pmonette9fa59e882016-02-10 00:12:1944bool SetAsDefaultBrowserInteractive() {
pmonette034a03d92015-10-02 21:04:2745 return false;
46}
47
pmonette9fa59e882016-02-10 00:12:1948bool SetAsDefaultProtocolClientInteractive(const std::string& protocol) {
pmonette034a03d92015-10-02 21:04:2749 return false;
50}
51#endif // !defined(OS_WIN)
52
pmonette9fa59e882016-02-10 00:12:1953DefaultWebClientSetPermission CanSetAsDefaultProtocolClient() {
[email protected]a01481b2011-07-15 04:30:0254 // Allowed as long as the browser can become the operating system default
55 // browser.
pmonette5207d8952016-03-22 15:53:0856 return CanSetAsDefaultBrowser();
[email protected]a01481b2011-07-15 04:30:0257}
58
pmonette034a03d92015-10-02 21:04:2759#if !defined(OS_WIN)
pmonette9fa59e882016-02-10 00:12:1960bool IsElevationNeededForSettingDefaultProtocolClient() {
pmonette034a03d92015-10-02 21:04:2761 return false;
62}
63#endif // !defined(OS_WIN)
[email protected]9561bc912012-03-07 02:41:1664
pmonette9fa59e882016-02-10 00:12:1965void SetAppModeInfo(const struct AppModeInfo* info) {
[email protected]9561bc912012-03-07 02:41:1666 gAppModeInfo = info;
67}
68
pmonette9fa59e882016-02-10 00:12:1969const struct AppModeInfo* AppModeInfo() {
[email protected]9561bc912012-03-07 02:41:1670 return gAppModeInfo;
71}
72
pmonette9fa59e882016-02-10 00:12:1973bool IsRunningInAppMode() {
[email protected]9561bc912012-03-07 02:41:1674 return gAppModeInfo != NULL;
75}
76
pmonette9fa59e882016-02-10 00:12:1977base::CommandLine CommandLineArgsForLauncher(
[email protected]2f1c09d2011-01-14 14:58:1478 const GURL& url,
[email protected]5c93a0c12012-05-02 19:45:2479 const std::string& extension_app_id,
[email protected]650b2d52013-02-10 03:41:4580 const base::FilePath& profile_path) {
[email protected]124901f52013-03-27 14:42:1081 base::ThreadRestrictions::AssertIOAllowed();
avi556c05022014-12-22 23:31:4382 base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);
[email protected]28375ae2010-02-05 04:45:5083
[email protected]fb06aea82014-02-26 15:48:4784 AppendProfileArgs(
85 extension_app_id.empty() ? base::FilePath() : profile_path,
86 &new_cmd_line);
[email protected]28375ae2010-02-05 04:45:5087
88 // If |extension_app_id| is present, we use the kAppId switch rather than
89 // the kApp switch (the launch url will be read from the extension app
90 // during launch.
[email protected]ec5b50d2010-10-09 16:35:1891 if (!extension_app_id.empty()) {
[email protected]b10392932011-03-08 21:28:1492 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
[email protected]64048bd2010-03-08 23:28:5893 } else {
[email protected]28375ae2010-02-05 04:45:5094 // Use '--app=url' instead of just 'url' to launch the browser with minimal
95 // chrome.
96 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
[email protected]b10392932011-03-08 21:28:1497 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
[email protected]28375ae2010-02-05 04:45:5098 }
[email protected]b10392932011-03-08 21:28:1499 return new_cmd_line;
[email protected]28375ae2010-02-05 04:45:50100}
101
pmonette9fa59e882016-02-10 00:12:19102void AppendProfileArgs(const base::FilePath& profile_path,
103 base::CommandLine* command_line) {
[email protected]fb06aea82014-02-26 15:48:47104 DCHECK(command_line);
avi556c05022014-12-22 23:31:43105 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
[email protected]fb06aea82014-02-26 15:48:47106
107 // Use the same UserDataDir for new launches that we currently have set.
108 base::FilePath user_data_dir =
109 cmd_line.GetSwitchValuePath(switches::kUserDataDir);
110#if defined(OS_MACOSX) || defined(OS_WIN)
111 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
112#endif
113 if (!user_data_dir.empty()) {
114 // Make sure user_data_dir is an absolute path.
115 user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
116 if (!user_data_dir.empty() && base::PathExists(user_data_dir))
117 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
118 }
119
120#if defined(OS_CHROMEOS)
121 base::FilePath profile = cmd_line.GetSwitchValuePath(
122 chromeos::switches::kLoginProfile);
123 if (!profile.empty())
124 command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
125#else
126 if (!profile_path.empty())
127 command_line->AppendSwitchPath(switches::kProfileDirectory,
128 profile_path.BaseName());
129#endif
130}
131
[email protected]bd046bd42012-06-08 05:07:32132#if !defined(OS_WIN)
pmonette9fa59e882016-02-10 00:12:19133base::string16 GetAppShortcutsSubdirName() {
sdefresne9fb67692015-08-03 18:48:22134 if (chrome::GetChannel() == version_info::Channel::CANARY)
[email protected]2f5bc322013-12-04 08:58:15135 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
136 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
137}
pmonette868ca642015-09-02 14:34:02138#endif // !defined(OS_WIN)
[email protected]bd046bd42012-06-08 05:07:32139
pmonette034a03d92015-10-02 21:04:27140///////////////////////////////////////////////////////////////////////////////
pmonette9fa59e882016-02-10 00:12:19141// DefaultWebClientWorker
[email protected]d24c4012009-07-28 01:57:31142//
initial.commit09911bf2008-07-26 23:55:29143
pmonette9fa59e882016-02-10 00:12:19144void DefaultWebClientWorker::StartCheckIsDefault() {
pmonette9c1457f2015-11-19 20:29:31145 BrowserThread::PostTask(
146 BrowserThread::FILE, FROM_HERE,
pmonette60a702c2016-03-22 22:59:40147 base::Bind(&DefaultWebClientWorker::CheckIsDefault, this, false));
[email protected]4468a5b2011-05-26 07:48:02148}
149
pmonette9fa59e882016-02-10 00:12:19150void DefaultWebClientWorker::StartSetAsDefault() {
pmonette8db6a8be2016-02-11 01:50:31151 BrowserThread::PostTask(
152 BrowserThread::FILE, FROM_HERE,
153 base::Bind(&DefaultWebClientWorker::SetAsDefault, this));
initial.commit09911bf2008-07-26 23:55:29154}
155
[email protected]d24c4012009-07-28 01:57:31156///////////////////////////////////////////////////////////////////////////////
pmonette5207d8952016-03-22 15:53:08157// DefaultWebClientWorker, protected:
[email protected]d24c4012009-07-28 01:57:31158
pmonette5207d8952016-03-22 15:53:08159DefaultWebClientWorker::DefaultWebClientWorker(
160 const DefaultWebClientWorkerCallback& callback,
161 const char* worker_name)
162 : callback_(callback), worker_name_(worker_name) {}
163
164DefaultWebClientWorker::~DefaultWebClientWorker() = default;
[email protected]d24c4012009-07-28 01:57:31165
pmonette9fa59e882016-02-10 00:12:19166void DefaultWebClientWorker::OnCheckIsDefaultComplete(
pmonette60a702c2016-03-22 22:59:40167 DefaultWebClientState state,
168 bool is_following_set_as_default) {
thestig00844cea2015-09-08 21:44:52169 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]264f74d12009-09-04 23:39:58170 UpdateUI(state);
pmonette9c1457f2015-11-19 20:29:31171
pmonette60a702c2016-03-22 22:59:40172 if (is_following_set_as_default)
173 ReportSetDefaultResult(state);
pmonette5207d8952016-03-22 15:53:08174}
pmonettef89ac7c72015-10-06 03:22:01175
pmonette5207d8952016-03-22 15:53:08176///////////////////////////////////////////////////////////////////////////////
177// DefaultWebClientWorker, private:
pmonettef89ac7c72015-10-06 03:22:01178
pmonette60a702c2016-03-22 22:59:40179void DefaultWebClientWorker::CheckIsDefault(bool is_following_set_as_default) {
pmonette5207d8952016-03-22 15:53:08180 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
181 DefaultWebClientState state = CheckIsDefaultImpl();
182 BrowserThread::PostTask(
183 BrowserThread::UI, FROM_HERE,
pmonette60a702c2016-03-22 22:59:40184 base::Bind(&DefaultBrowserWorker::OnCheckIsDefaultComplete, this, state,
185 is_following_set_as_default));
pmonette5207d8952016-03-22 15:53:08186}
187
188void DefaultWebClientWorker::SetAsDefault() {
189 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
pmonette60a702c2016-03-22 22:59:40190 SetAsDefaultImpl();
191 CheckIsDefault(true);
[email protected]d24c4012009-07-28 01:57:31192}
193
pmonette60a702c2016-03-22 22:59:40194void DefaultWebClientWorker::ReportSetDefaultResult(
195 DefaultWebClientState state) {
pmonette9c1457f2015-11-19 20:29:31196 base::LinearHistogram::FactoryGet(
pmonette60a702c2016-03-22 22:59:40197 base::StringPrintf("%s.SetDefaultResult2", worker_name_), 1,
198 DefaultWebClientState::NUM_DEFAULT_STATES,
199 DefaultWebClientState::NUM_DEFAULT_STATES + 1,
pmonette9c1457f2015-11-19 20:29:31200 base::HistogramBase::kUmaTargetedHistogramFlag)
pmonette60a702c2016-03-22 22:59:40201 ->Add(state);
pmonettef89ac7c72015-10-06 03:22:01202}
203
pmonette9fa59e882016-02-10 00:12:19204void DefaultWebClientWorker::UpdateUI(DefaultWebClientState state) {
pmonettef9461d1a2016-03-08 18:13:47205 if (!callback_.is_null()) {
206 switch (state) {
207 case NOT_DEFAULT:
pmonetteb9204142016-03-08 20:02:44208 callback_.Run(NOT_DEFAULT);
pmonettef9461d1a2016-03-08 18:13:47209 break;
210 case IS_DEFAULT:
pmonetteb9204142016-03-08 20:02:44211 callback_.Run(IS_DEFAULT);
pmonettef9461d1a2016-03-08 18:13:47212 break;
213 case UNKNOWN_DEFAULT:
pmonetteb9204142016-03-08 20:02:44214 callback_.Run(UNKNOWN_DEFAULT);
pmonettef9461d1a2016-03-08 18:13:47215 break;
216 case NUM_DEFAULT_STATES:
pmonetteb9204142016-03-08 20:02:44217 NOTREACHED();
pmonettef9461d1a2016-03-08 18:13:47218 break;
219 }
[email protected]d24c4012009-07-28 01:57:31220 }
initial.commit09911bf2008-07-26 23:55:29221}
[email protected]4468a5b2011-05-26 07:48:02222
223///////////////////////////////////////////////////////////////////////////////
pmonette9fa59e882016-02-10 00:12:19224// DefaultBrowserWorker
[email protected]4468a5b2011-05-26 07:48:02225//
226
pmonette586ab5b32016-03-07 19:50:37227DefaultBrowserWorker::DefaultBrowserWorker(
228 const DefaultWebClientWorkerCallback& callback)
pmonette5207d8952016-03-22 15:53:08229 : DefaultWebClientWorker(callback, "DefaultBrowser") {}
pmonettef89ac7c72015-10-06 03:22:01230
[email protected]4468a5b2011-05-26 07:48:02231///////////////////////////////////////////////////////////////////////////////
232// DefaultBrowserWorker, private:
233
pmonette5207d8952016-03-22 15:53:08234DefaultBrowserWorker::~DefaultBrowserWorker() = default;
235
236DefaultWebClientState DefaultBrowserWorker::CheckIsDefaultImpl() {
237 return GetDefaultBrowser();
[email protected]4468a5b2011-05-26 07:48:02238}
239
pmonette60a702c2016-03-22 22:59:40240void DefaultBrowserWorker::SetAsDefaultImpl() {
pmonettef89ac7c72015-10-06 03:22:01241 switch (CanSetAsDefaultBrowser()) {
242 case SET_DEFAULT_NOT_ALLOWED:
[email protected]bd046bd42012-06-08 05:07:32243 NOTREACHED();
pmonettef89ac7c72015-10-06 03:22:01244 break;
245 case SET_DEFAULT_UNATTENDED:
pmonette60a702c2016-03-22 22:59:40246 SetAsDefaultBrowser();
pmonettef89ac7c72015-10-06 03:22:01247 break;
248 case SET_DEFAULT_INTERACTIVE:
pmonette60a702c2016-03-22 22:59:40249 if (interactive_permitted_)
250 SetAsDefaultBrowserInteractive();
pmonettef89ac7c72015-10-06 03:22:01251 break;
[email protected]bd046bd42012-06-08 05:07:32252 }
pmonette9c1457f2015-11-19 20:29:31253}
254
[email protected]4468a5b2011-05-26 07:48:02255///////////////////////////////////////////////////////////////////////////////
pmonette9fa59e882016-02-10 00:12:19256// DefaultProtocolClientWorker
[email protected]4468a5b2011-05-26 07:48:02257//
258
pmonette9fa59e882016-02-10 00:12:19259DefaultProtocolClientWorker::DefaultProtocolClientWorker(
pmonette586ab5b32016-03-07 19:50:37260 const DefaultWebClientWorkerCallback& callback,
261 const std::string& protocol)
pmonette5207d8952016-03-22 15:53:08262 : DefaultWebClientWorker(callback, "DefaultProtocolClient"),
263 protocol_(protocol) {}
264
265///////////////////////////////////////////////////////////////////////////////
266// DefaultProtocolClientWorker, protected:
267
268DefaultProtocolClientWorker::~DefaultProtocolClientWorker() = default;
[email protected]4468a5b2011-05-26 07:48:02269
270///////////////////////////////////////////////////////////////////////////////
271// DefaultProtocolClientWorker, private:
272
pmonette5207d8952016-03-22 15:53:08273DefaultWebClientState DefaultProtocolClientWorker::CheckIsDefaultImpl() {
274 return IsDefaultProtocolClient(protocol_);
[email protected]4468a5b2011-05-26 07:48:02275}
276
pmonette60a702c2016-03-22 22:59:40277void DefaultProtocolClientWorker::SetAsDefaultImpl() {
pmonettef89ac7c72015-10-06 03:22:01278 switch (CanSetAsDefaultProtocolClient()) {
279 case SET_DEFAULT_NOT_ALLOWED:
280 // Not allowed, do nothing.
[email protected]e7f79872013-09-13 01:39:31281 break;
pmonettef89ac7c72015-10-06 03:22:01282 case SET_DEFAULT_UNATTENDED:
pmonette60a702c2016-03-22 22:59:40283 SetAsDefaultProtocolClient(protocol_);
[email protected]ee9d89d2012-09-25 18:21:03284 break;
pmonettef89ac7c72015-10-06 03:22:01285 case SET_DEFAULT_INTERACTIVE:
pmonette60a702c2016-03-22 22:59:40286 if (interactive_permitted_) {
287 SetAsDefaultProtocolClientInteractive(protocol_);
[email protected]ee9d89d2012-09-25 18:21:03288 }
289 break;
[email protected]ee9d89d2012-09-25 18:21:03290 }
pmonette9c1457f2015-11-19 20:29:31291}
pmonette9fa59e882016-02-10 00:12:19292
293} // namespace shell_integration