Commit 1fa65d26 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Make CaptivePortalService available

In preparation for sharing //chrome's SSLErrorHandler implementation,
this CL makes CaptivePortalService available in WebLayer by adding a
KeyedServiceFactory for it. //chrome's SSLErrorHandler requires a
CaptivePortalService when captive portal detection is enabled (which
it is on desktop platforms, i.e., Linux). The factory is modeled after
that from //chrome, with a difference for the //weblayer environment:
- The PrefService is passed via user_prefs::UserPrefs::Get() rather
  than via Profile::GetPrefs(). //weblayer's BrowserContextImpl has
  no getter for the PrefService, and as that getter is just a synonym
  for user_prefs::UserPrefs::Get() in //chrome, I see no reason to add
  it here.

Note that this CL (and the bringup of support for Chrome's captive
portal detection in WebLayer) will have no impact on Android, where
Chrome's captive portal detection service is not enabled as
SSLErrorHandler relies on the OS support.

As this is the first instance of a KeyedServiceFactory in //weblayer,
this CL also brings up the KeyedServiceFactory infrastructure in the
//weblayer embedder.

Bug: 1030692
Change-Id: Ic24c8a003f53598f9dd08f0da0ac87869398074f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2022671Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736777}
parent 2b4cb7d5
# Copyright 2019 The Chromium Authors. All rights reserved. # Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import("//components/captive_portal/core/features.gni")
import("//build/config/features.gni") import("//build/config/features.gni")
import("//build/config/locales.gni") import("//build/config/locales.gni")
...@@ -190,6 +191,13 @@ jumbo_static_library("weblayer_lib") { ...@@ -190,6 +191,13 @@ jumbo_static_library("weblayer_lib") {
] ]
} }
if (enable_captive_portal_detection) {
sources += [
"browser/captive_portal_service_factory.cc",
"browser/captive_portal_service_factory.h",
]
}
configs += [ configs += [
"//build/config:precompiled_headers", "//build/config:precompiled_headers",
...@@ -207,10 +215,12 @@ jumbo_static_library("weblayer_lib") { ...@@ -207,10 +215,12 @@ jumbo_static_library("weblayer_lib") {
"//components/autofill/content/renderer", "//components/autofill/content/renderer",
"//components/autofill/core/browser", "//components/autofill/core/browser",
"//components/base32", "//components/base32",
"//components/captive_portal/core:buildflags",
"//components/crash/content/app", "//components/crash/content/app",
"//components/crash/content/browser", "//components/crash/content/browser",
"//components/embedder_support", "//components/embedder_support",
"//components/find_in_page", "//components/find_in_page",
"//components/keyed_service/content",
"//components/network_time", "//components/network_time",
"//components/prefs", "//components/prefs",
"//components/safe_browsing/core:features", "//components/safe_browsing/core:features",
...@@ -263,6 +273,10 @@ jumbo_static_library("weblayer_lib") { ...@@ -263,6 +273,10 @@ jumbo_static_library("weblayer_lib") {
"//weblayer/browser/webui:mojo_bindings", "//weblayer/browser/webui:mojo_bindings",
] ]
if (enable_captive_portal_detection) {
deps += [ "//components/captive_portal/content" ]
}
if (use_browser_spellchecker) { if (use_browser_spellchecker) {
deps += [ deps += [
"//components/spellcheck/browser", "//components/spellcheck/browser",
......
...@@ -5,10 +5,12 @@ include_rules = [ ...@@ -5,10 +5,12 @@ include_rules = [
"+components/autofill/core/browser", "+components/autofill/core/browser",
"+components/autofill/core/common", "+components/autofill/core/common",
"+components/base32", "+components/base32",
"+components/captive_portal",
"+components/crash/content/browser", "+components/crash/content/browser",
"+components/download/public/common", "+components/download/public/common",
"+components/embedder_support", "+components/embedder_support",
"+components/find_in_page", "+components/find_in_page",
"+components/keyed_service/content",
"+components/network_time", "+components/network_time",
"+components/prefs", "+components/prefs",
"+components/user_prefs", "+components/user_prefs",
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "weblayer/browser/browser_context_impl.h" #include "weblayer/browser/browser_context_impl.h"
#include "components/download/public/common/in_progress_download_manager.h" #include "components/download/public/common/in_progress_download_manager.h"
#include "components/embedder_support/pref_names.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/in_memory_pref_store.h" #include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -62,10 +64,16 @@ BrowserContextImpl::BrowserContextImpl(ProfileImpl* profile_impl, ...@@ -62,10 +64,16 @@ BrowserContextImpl::BrowserContextImpl(ProfileImpl* profile_impl,
content::BrowserContext::Initialize(this, path_); content::BrowserContext::Initialize(this, path_);
CreateUserPrefService(); CreateUserPrefService();
BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
this);
} }
BrowserContextImpl::~BrowserContextImpl() { BrowserContextImpl::~BrowserContextImpl() {
NotifyWillBeDestroyed(this); NotifyWillBeDestroyed(this);
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
this);
} }
base::FilePath BrowserContextImpl::GetDefaultDownloadDirectory() { base::FilePath BrowserContextImpl::GetDefaultDownloadDirectory() {
...@@ -200,6 +208,11 @@ void BrowserContextImpl::CreateUserPrefService() { ...@@ -200,6 +208,11 @@ void BrowserContextImpl::CreateUserPrefService() {
} }
void BrowserContextImpl::RegisterPrefs(PrefRegistrySimple* pref_registry) { void BrowserContextImpl::RegisterPrefs(PrefRegistrySimple* pref_registry) {
// This pref is used by CaptivePortalService (as well as other potential use
// cases in the future, as it is used for various purposes through //chrome).
pref_registry->RegisterBooleanPref(
embedder_support::kAlternateErrorPagesEnabled, true);
safe_browsing::RegisterProfilePrefs(pref_registry); safe_browsing::RegisterProfilePrefs(pref_registry);
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "cc/base/switches.h" #include "cc/base/switches.h"
#include "components/captive_portal/core/buildflags.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h" #include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_agent_host.h"
...@@ -43,10 +44,23 @@ ...@@ -43,10 +44,23 @@
#include "ui/base/ime/init/input_method_initializer.h" #include "ui/base/ime/init/input_method_initializer.h"
#endif #endif
#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
#include "weblayer/browser/captive_portal_service_factory.h"
#endif
namespace weblayer { namespace weblayer {
namespace { namespace {
// Instantiates all weblayer KeyedService factories, which is
// especially important for services that should be created at profile
// creation time as compared to lazily on first access.
static void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
CaptivePortalServiceFactory::GetInstance();
#endif
}
void StopMessageLoop(base::OnceClosure quit_closure) { void StopMessageLoop(base::OnceClosure quit_closure) {
for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd(); for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
it.Advance()) { it.Advance()) {
...@@ -104,6 +118,13 @@ int BrowserMainPartsImpl::PreEarlyInitialization() { ...@@ -104,6 +118,13 @@ int BrowserMainPartsImpl::PreEarlyInitialization() {
void BrowserMainPartsImpl::PreMainMessageLoopRun() { void BrowserMainPartsImpl::PreMainMessageLoopRun() {
ui::MaterialDesignController::Initialize(); ui::MaterialDesignController::Initialize();
// It's necessary to have a complete dependency graph of
// BrowserContextKeyedServices before calling out to the delegate (which
// will potentially create a profile), so that a profile creation message is
// properly dispatched to the factories that want to create their services
// at profile creation time.
EnsureBrowserContextKeyedServiceFactoriesBuilt();
params_->delegate->PreMainMessageLoopRun(); params_->delegate->PreMainMessageLoopRun();
content::WebUIControllerFactory::RegisterFactory( content::WebUIControllerFactory::RegisterFactory(
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/browser/captive_portal_service_factory.h"
#include "components/captive_portal/content/captive_portal_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
// static
CaptivePortalService* CaptivePortalServiceFactory::GetForBrowserContext(
content::BrowserContext* browser_context) {
return static_cast<CaptivePortalService*>(
GetInstance()->GetServiceForBrowserContext(browser_context, true));
}
// static
CaptivePortalServiceFactory* CaptivePortalServiceFactory::GetInstance() {
return base::Singleton<CaptivePortalServiceFactory>::get();
}
CaptivePortalServiceFactory::CaptivePortalServiceFactory()
: BrowserContextKeyedServiceFactory(
"CaptivePortalService",
BrowserContextDependencyManager::GetInstance()) {}
CaptivePortalServiceFactory::~CaptivePortalServiceFactory() = default;
KeyedService* CaptivePortalServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* browser_context) const {
return new CaptivePortalService(browser_context,
user_prefs::UserPrefs::Get(browser_context));
}
content::BrowserContext* CaptivePortalServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return context;
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WEBLAYER_BROWSER_CAPTIVE_PORTAL_SERVICE_FACTORY_H_
#define WEBLAYER_BROWSER_CAPTIVE_PORTAL_SERVICE_FACTORY_H_
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class CaptivePortalService;
// Singleton that owns all CaptivePortalServices and associates them with
// BrowserContextImpl instances.
class CaptivePortalServiceFactory : public BrowserContextKeyedServiceFactory {
public:
// Returns the CaptivePortalService for |browser_context|.
static CaptivePortalService* GetForBrowserContext(
content::BrowserContext* browser_context);
static CaptivePortalServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<CaptivePortalServiceFactory>;
CaptivePortalServiceFactory();
~CaptivePortalServiceFactory() override;
CaptivePortalServiceFactory(const CaptivePortalServiceFactory&) = delete;
CaptivePortalServiceFactory& operator=(const CaptivePortalServiceFactory&) =
delete;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* profile) const override;
// Incognito profiles have their own instance of CaptivePortalService rather
// than the default behavior of the service being null if the profile is
// incognito.
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};
#endif // WEBLAYER_BROWSER_CAPTIVE_PORTAL_SERVICE_FACTORY_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment