Skip to content

Commit

Permalink
[WebLayer] Make CaptivePortalService available
Browse files Browse the repository at this point in the history
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/+/2022671
Reviewed-by: Evan Stade <estade@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736777}
  • Loading branch information
colinblundell authored and Commit Bot committed Jan 30, 2020
1 parent 2b4cb7d commit 1fa65d2
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
14 changes: 14 additions & 0 deletions weblayer/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2019 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.
import("//components/captive_portal/core/features.gni")

import("//build/config/features.gni")
import("//build/config/locales.gni")
Expand Down Expand Up @@ -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 += [
"//build/config:precompiled_headers",

Expand All @@ -207,10 +215,12 @@ jumbo_static_library("weblayer_lib") {
"//components/autofill/content/renderer",
"//components/autofill/core/browser",
"//components/base32",
"//components/captive_portal/core:buildflags",
"//components/crash/content/app",
"//components/crash/content/browser",
"//components/embedder_support",
"//components/find_in_page",
"//components/keyed_service/content",
"//components/network_time",
"//components/prefs",
"//components/safe_browsing/core:features",
Expand Down Expand Up @@ -263,6 +273,10 @@ jumbo_static_library("weblayer_lib") {
"//weblayer/browser/webui:mojo_bindings",
]

if (enable_captive_portal_detection) {
deps += [ "//components/captive_portal/content" ]
}

if (use_browser_spellchecker) {
deps += [
"//components/spellcheck/browser",
Expand Down
2 changes: 2 additions & 0 deletions weblayer/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ include_rules = [
"+components/autofill/core/browser",
"+components/autofill/core/common",
"+components/base32",
"+components/captive_portal",
"+components/crash/content/browser",
"+components/download/public/common",
"+components/embedder_support",
"+components/find_in_page",
"+components/keyed_service/content",
"+components/network_time",
"+components/prefs",
"+components/user_prefs",
Expand Down
13 changes: 13 additions & 0 deletions weblayer/browser/browser_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "weblayer/browser/browser_context_impl.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/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
Expand Down Expand Up @@ -62,10 +64,16 @@ BrowserContextImpl::BrowserContextImpl(ProfileImpl* profile_impl,
content::BrowserContext::Initialize(this, path_);

CreateUserPrefService();

BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
this);
}

BrowserContextImpl::~BrowserContextImpl() {
NotifyWillBeDestroyed(this);

BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
this);
}

base::FilePath BrowserContextImpl::GetDefaultDownloadDirectory() {
Expand Down Expand Up @@ -200,6 +208,11 @@ void BrowserContextImpl::CreateUserPrefService() {
}

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);
}

Expand Down
21 changes: 21 additions & 0 deletions weblayer/browser/browser_main_parts_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "components/captive_portal/core/buildflags.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
Expand Down Expand Up @@ -43,10 +44,23 @@
#include "ui/base/ime/init/input_method_initializer.h"
#endif

#if BUILDFLAG(ENABLE_CAPTIVE_PORTAL_DETECTION)
#include "weblayer/browser/captive_portal_service_factory.h"
#endif

namespace weblayer {

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) {
for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
it.Advance()) {
Expand Down Expand Up @@ -104,6 +118,13 @@ int BrowserMainPartsImpl::PreEarlyInitialization() {

void BrowserMainPartsImpl::PreMainMessageLoopRun() {
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();

content::WebUIControllerFactory::RegisterFactory(
Expand Down
40 changes: 40 additions & 0 deletions weblayer/browser/captive_portal_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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;
}
45 changes: 45 additions & 0 deletions weblayer/browser/captive_portal_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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_

0 comments on commit 1fa65d2

Please sign in to comment.