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
|
// Copyright 2021 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/custom_handlers/chrome_protocol_handler_registry_delegate.h"
#include <utility>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/child_process_security_policy.h"
class PrefService;
using content::BrowserThread;
using content::ChildProcessSecurityPolicy;
ChromeProtocolHandlerRegistryDelegate::ChromeProtocolHandlerRegistryDelegate() =
default;
ChromeProtocolHandlerRegistryDelegate::
~ChromeProtocolHandlerRegistryDelegate() = default;
// ProtocolHandlerRegistry::Delegate:
void ChromeProtocolHandlerRegistryDelegate::RegisterExternalHandler(
const std::string& protocol) {
ChildProcessSecurityPolicy* policy =
ChildProcessSecurityPolicy::GetInstance();
if (!policy->IsWebSafeScheme(protocol)) {
policy->RegisterWebSafeScheme(protocol);
}
}
bool ChromeProtocolHandlerRegistryDelegate::IsExternalHandlerRegistered(
const std::string& protocol) {
// NOTE(koz): This function is safe to call from any thread, despite living
// in ProfileIOData.
return ProfileIOData::IsHandledProtocol(protocol);
}
void ChromeProtocolHandlerRegistryDelegate::RegisterWithOSAsDefaultClient(
const std::string& protocol,
DefaultClientCallback callback) {
// The worker pointer is reference counted. While it is running, the
// sequence it runs on will hold references it will be automatically freed
// once all its tasks have finished.
base::MakeRefCounted<shell_integration::DefaultSchemeClientWorker>(protocol)
->StartSetAsDefault(
GetDefaultWebClientCallback(protocol, std::move(callback)));
}
void ChromeProtocolHandlerRegistryDelegate::CheckDefaultClientWithOS(
const std::string& protocol,
DefaultClientCallback callback) {
// The worker pointer is reference counted. While it is running, the
// sequence it runs on will hold references it will be automatically freed
// once all its tasks have finished.
base::MakeRefCounted<shell_integration::DefaultSchemeClientWorker>(protocol)
->StartCheckIsDefault(
GetDefaultWebClientCallback(protocol, std::move(callback)));
}
// If true default protocol handlers will be removed if the OS level
// registration for a protocol is no longer Chrome.
bool ChromeProtocolHandlerRegistryDelegate::ShouldRemoveHandlersNotInOS() {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// We don't do this on Linux as the OS registration there is not reliable,
// and Chrome OS doesn't have any notion of OS registration.
// TODO(benwells): When Linux support is more reliable remove this
// difference (http://crbug.com/88255).
return false;
#else
return shell_integration::GetDefaultSchemeClientSetPermission() !=
shell_integration::SET_DEFAULT_NOT_ALLOWED;
#endif
}
void ChromeProtocolHandlerRegistryDelegate::
OnSetAsDefaultClientForSchemeFinished(
const std::string& protocol,
DefaultClientCallback callback,
shell_integration::DefaultWebClientState state) {
bool is_default = state != shell_integration::NOT_DEFAULT &&
state != shell_integration::OTHER_MODE_IS_DEFAULT;
std::move(callback).Run(is_default);
}
shell_integration::DefaultWebClientWorkerCallback
ChromeProtocolHandlerRegistryDelegate::GetDefaultWebClientCallback(
const std::string& protocol,
DefaultClientCallback callback) {
return base::BindOnce(&ChromeProtocolHandlerRegistryDelegate::
OnSetAsDefaultClientForSchemeFinished,
weak_ptr_factory_.GetWeakPtr(), protocol,
std::move(callback));
}
|