1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/shell_integration.h"
#include <utility>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/i18n/file_util_icu.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/lazy_thread_pool_task_runner.h"
#include "base/task/single_thread_task_runner_thread_mode.h"
#include "base/task/task_traits.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/policy/policy_path_parser.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "components/prefs/pref_service.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/win/windows_version.h"
#include "chrome/browser/shell_integration_win.h"
#include "chrome/installer/util/shell_util.h"
#endif
#if !BUILDFLAG(IS_WIN)
#include "chrome/common/channel_info.h"
#include "chrome/grit/branded_strings.h"
#include "ui/base/l10n/l10n_util.h"
#endif
using content::BrowserThread;
namespace shell_integration {
namespace {
// TODO(crbug.com/40544199): Remove |g_sequenced_task_runner| and use an
// instance field / singleton instead.
#if BUILDFLAG(IS_WIN)
base::LazyThreadPoolCOMSTATaskRunner g_sequenced_task_runner =
LAZY_COM_STA_TASK_RUNNER_INITIALIZER(
base::TaskTraits(base::MayBlock()),
base::SingleThreadTaskRunnerThreadMode::SHARED);
#else
base::LazyThreadPoolSequencedTaskRunner g_sequenced_task_runner =
LAZY_THREAD_POOL_SEQUENCED_TASK_RUNNER_INITIALIZER(
base::TaskTraits(base::MayBlock()));
#endif
bool IsValidDefaultWebClientState(DefaultWebClientState state) {
switch (state) {
case NOT_DEFAULT:
case IS_DEFAULT:
case UNKNOWN_DEFAULT:
case OTHER_MODE_IS_DEFAULT:
return true;
case NUM_DEFAULT_STATES:
break;
}
NOTREACHED();
}
void RunCallback(DefaultWebClientWorkerCallback callback,
DefaultWebClientState state) {
if (!callback.is_null() && IsValidDefaultWebClientState(state)) {
std::move(callback).Run(state);
return;
}
}
DefaultWebClientSetPermission GetDefaultWebClientSetPermission(
internal::WebClientSetMethod method) {
#if BUILDFLAG(CHROME_FOR_TESTING)
return SET_DEFAULT_NOT_ALLOWED;
#else
return internal::GetPlatformSpecificDefaultWebClientSetPermission(method);
#endif
}
} // namespace
DefaultWebClientSetPermission GetDefaultBrowserSetPermission() {
return GetDefaultWebClientSetPermission(
internal::WebClientSetMethod::kDefaultBrowser);
}
DefaultWebClientSetPermission GetDefaultSchemeClientSetPermission() {
return GetDefaultWebClientSetPermission(
internal::WebClientSetMethod::kDefaultSchemeHandler);
}
bool CanSetAsDefaultBrowser() {
return GetDefaultBrowserSetPermission() != SET_DEFAULT_NOT_ALLOWED;
}
base::CommandLine CommandLineArgsForLauncher(
const GURL& url,
const std::string& extension_app_id,
const base::FilePath& profile_path,
const std::string& run_on_os_login_mode) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);
AppendProfileArgs(
extension_app_id.empty() ? base::FilePath() : profile_path,
&new_cmd_line);
// If |extension_app_id| is present, we use the kAppId switch rather than
// the kApp switch (the launch url will be read from the extension app
// during launch.
if (!extension_app_id.empty()) {
new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
} else {
// Use '--app=url' instead of just 'url' to launch the browser with minimal
// chrome.
// Note: Do not change this flag! Old Gears shortcuts will break if you do!
new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
}
if (!run_on_os_login_mode.empty()) {
new_cmd_line.AppendSwitchASCII(switches::kAppRunOnOsLoginMode,
run_on_os_login_mode);
}
return new_cmd_line;
}
base::CommandLine CommandLineArgsForUrlShortcut(
const base::FilePath& chrome_exe_program,
const base::FilePath& profile_path,
const GURL& url) {
CHECK(!chrome_exe_program.empty());
CHECK(!profile_path.empty());
CHECK(url.is_valid());
base::CommandLine new_cmd_line(chrome_exe_program);
AppendProfileArgs(profile_path, &new_cmd_line);
new_cmd_line.AppendSwitch(switches::kIgnoreProfileDirectoryIfNotExists);
new_cmd_line.AppendArg(url.spec());
return new_cmd_line;
}
void AppendProfileArgs(const base::FilePath& profile_path,
base::CommandLine* command_line) {
DCHECK(command_line);
const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
// Use the same UserDataDir for new launches that we currently have set.
base::FilePath user_data_dir =
cmd_line.GetSwitchValuePath(switches::kUserDataDir);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
#endif
if (!user_data_dir.empty()) {
// Make sure user_data_dir is an absolute path.
user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
if (!user_data_dir.empty() && base::PathExists(user_data_dir))
command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
}
#if BUILDFLAG(IS_CHROMEOS)
base::FilePath profile =
cmd_line.GetSwitchValuePath(ash::switches::kLoginProfile);
if (!profile.empty())
command_line->AppendSwitchPath(ash::switches::kLoginProfile, profile);
#else
if (!profile_path.empty())
command_line->AppendSwitchPath(switches::kProfileDirectory,
profile_path.BaseName());
#endif
}
#if !BUILDFLAG(IS_WIN)
std::u16string GetAppShortcutsSubdirName() {
if (chrome::GetChannel() == version_info::Channel::CANARY)
return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
}
#endif // !BUILDFLAG(IS_WIN)
///////////////////////////////////////////////////////////////////////////////
// DefaultWebClientWorker
//
void DefaultWebClientWorker::StartCheckIsDefault(
DefaultWebClientWorkerCallback callback) {
g_sequenced_task_runner.Get()->PostTask(
FROM_HERE, base::BindOnce(&DefaultWebClientWorker::CheckIsDefault, this,
false, std::move(callback)));
}
void DefaultWebClientWorker::StartSetAsDefault(
DefaultWebClientWorkerCallback callback) {
g_sequenced_task_runner.Get()->PostTask(
FROM_HERE, base::BindOnce(&DefaultWebClientWorker::SetAsDefault, this,
std::move(callback)));
}
///////////////////////////////////////////////////////////////////////////////
// DefaultWebClientWorker, protected:
DefaultWebClientWorker::DefaultWebClientWorker(const char* worker_name)
: worker_name_(worker_name) {}
DefaultWebClientWorker::~DefaultWebClientWorker() = default;
void DefaultWebClientWorker::OnCheckIsDefaultComplete(
DefaultWebClientState state,
bool is_following_set_as_default,
DefaultWebClientWorkerCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunCallback(std::move(callback), state);
if (is_following_set_as_default)
ReportSetDefaultResult(state);
}
///////////////////////////////////////////////////////////////////////////////
// DefaultWebClientWorker, private:
void DefaultWebClientWorker::CheckIsDefault(
bool is_following_set_as_default,
DefaultWebClientWorkerCallback callback) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
DefaultWebClientState state = CheckIsDefaultImpl();
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&DefaultBrowserWorker::OnCheckIsDefaultComplete, this,
state, is_following_set_as_default, std::move(callback)));
}
void DefaultWebClientWorker::SetAsDefault(
DefaultWebClientWorkerCallback callback) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
// SetAsDefaultImpl will make sure the callback is executed exactly once.
SetAsDefaultImpl(base::BindOnce(&DefaultWebClientWorker::CheckIsDefault, this,
true, std::move(callback)));
}
void DefaultWebClientWorker::ReportSetDefaultResult(
DefaultWebClientState state) {
base::LinearHistogram::FactoryGet(
base::StringPrintf("%s.SetDefaultResult2", worker_name_), 1,
DefaultWebClientState::NUM_DEFAULT_STATES,
DefaultWebClientState::NUM_DEFAULT_STATES + 1,
base::HistogramBase::kUmaTargetedHistogramFlag)
->Add(state);
}
///////////////////////////////////////////////////////////////////////////////
// DefaultBrowserWorker
//
DefaultBrowserWorker::DefaultBrowserWorker()
: DefaultWebClientWorker("DefaultBrowser") {}
///////////////////////////////////////////////////////////////////////////////
// DefaultBrowserWorker, private:
DefaultBrowserWorker::~DefaultBrowserWorker() = default;
// static
bool DefaultBrowserWorker::g_disable_set_as_default_for_testing = false;
// static
void DefaultBrowserWorker::DisableSetAsDefaultForTesting() {
g_disable_set_as_default_for_testing = true;
}
DefaultWebClientState DefaultBrowserWorker::CheckIsDefaultImpl() {
return GetDefaultBrowser();
}
void DefaultBrowserWorker::SetAsDefaultImpl(
base::OnceClosure on_finished_callback) {
if (!g_disable_set_as_default_for_testing) {
switch (GetDefaultBrowserSetPermission()) {
case SET_DEFAULT_NOT_ALLOWED:
// This is a no-op on channels where set-default is not allowed, but not
// an error.
break;
case SET_DEFAULT_UNATTENDED:
SetAsDefaultBrowser();
break;
case SET_DEFAULT_INTERACTIVE:
#if BUILDFLAG(IS_WIN)
if (interactive_permitted_) {
win::SetAsDefaultBrowserUsingSystemSettings(
std::move(on_finished_callback));
// Early return because the function above takes care of calling
// `on_finished_callback`.
return;
}
#endif // BUILDFLAG(IS_WIN)
break;
}
}
std::move(on_finished_callback).Run();
}
///////////////////////////////////////////////////////////////////////////////
// DefaultSchemeClientWorker
//
DefaultSchemeClientWorker::DefaultSchemeClientWorker(const std::string& scheme)
: DefaultWebClientWorker("DefaultSchemeClient"), scheme_(scheme) {}
DefaultSchemeClientWorker::DefaultSchemeClientWorker(const GURL& url)
: DefaultWebClientWorker("DefaultSchemeClient"),
scheme_(url.scheme()),
url_(url) {}
void DefaultSchemeClientWorker::StartCheckIsDefaultAndGetDefaultClientName(
DefaultSchemeHandlerWorkerCallback callback) {
g_sequenced_task_runner.Get()->PostTask(
FROM_HERE,
base::BindOnce(
&DefaultSchemeClientWorker::CheckIsDefaultAndGetDefaultClientName,
this, std::move(callback)));
}
///////////////////////////////////////////////////////////////////////////////
// DefaultSchemeClientWorker, protected:
DefaultSchemeClientWorker::~DefaultSchemeClientWorker() = default;
void DefaultSchemeClientWorker::OnCheckIsDefaultAndGetDefaultClientNameComplete(
DefaultWebClientState state,
std::u16string program_name,
DefaultSchemeHandlerWorkerCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!callback.is_null() && IsValidDefaultWebClientState(state)) {
std::move(callback).Run(state, program_name);
}
}
///////////////////////////////////////////////////////////////////////////////
// DefaultSchemeClientWorker, private:
void DefaultSchemeClientWorker::CheckIsDefaultAndGetDefaultClientName(
DefaultSchemeHandlerWorkerCallback callback) {
DCHECK(!url_.is_empty());
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
DefaultWebClientState state = CheckIsDefaultImpl();
std::u16string program_name = GetDefaultClientNameImpl();
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&DefaultSchemeClientWorker::
OnCheckIsDefaultAndGetDefaultClientNameComplete,
this, state, program_name, std::move(callback)));
}
DefaultWebClientState DefaultSchemeClientWorker::CheckIsDefaultImpl() {
return IsDefaultClientForScheme(scheme_);
}
std::u16string DefaultSchemeClientWorker::GetDefaultClientNameImpl() {
return GetApplicationNameForScheme(url_);
}
void DefaultSchemeClientWorker::SetAsDefaultImpl(
base::OnceClosure on_finished_callback) {
switch (GetDefaultSchemeClientSetPermission()) {
case SET_DEFAULT_NOT_ALLOWED:
// Not allowed, do nothing.
break;
case SET_DEFAULT_UNATTENDED:
SetAsDefaultClientForScheme(scheme_);
break;
case SET_DEFAULT_INTERACTIVE:
#if BUILDFLAG(IS_WIN)
if (interactive_permitted_) {
win::SetAsDefaultClientForSchemeUsingSystemSettings(
scheme_, std::move(on_finished_callback));
// Early return because the function above takes care of calling
// `on_finished_callback`.
return;
}
#endif // BUILDFLAG(IS_WIN)
break;
}
std::move(on_finished_callback).Run();
}
} // namespace shell_integration
|