Commit bf333c3b authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Adds BrowserStatePolicyConnector.

This new class manages connecting the policy system with BrowserState
(user-level) prefs. ChromeBrowserStateImpl directly owns its
BrowserStatePolicyConnector and creates one before bringing up prefs. (A
KeyedService cannot be used because BrowserStatePolicyConnector needs to
outlive prefs.)

The overall set of classes and factories was chosen to mimic
desktop. They provide the necessary structure for implementing cloud
policy as a potential feature in the future.

BUG=1024115

Change-Id: I6addab6e877e406006702a581414bf20b7c6cfaa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2067871Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744696}
parent 9c25d8db
......@@ -417,9 +417,14 @@ void ApplicationContextImpl::CreateLocalState() {
// Register local state preferences.
RegisterLocalStatePrefs(pref_registry.get());
local_state_ =
::CreateLocalState(local_state_path, local_state_task_runner_.get(),
pref_registry, GetBrowserPolicyConnector());
policy::BrowserPolicyConnector* browser_policy_connector =
GetBrowserPolicyConnector();
policy::PolicyService* policy_service =
browser_policy_connector ? browser_policy_connector->GetPolicyService()
: nullptr;
local_state_ = ::CreateLocalState(
local_state_path, local_state_task_runner_.get(), pref_registry,
policy_service, browser_policy_connector);
DCHECK(local_state_);
sessions::SessionIdGenerator::GetInstance()->Init(local_state_.get());
......
......@@ -75,6 +75,7 @@ source_set("browser_state_impl") {
"//components/metrics",
"//components/net_log",
"//components/password_manager/core/browser",
"//components/policy/core/common",
"//components/pref_registry",
"//components/prefs",
"//components/proxy_config",
......@@ -106,6 +107,7 @@ source_set("browser_state_impl") {
"//ios/chrome/browser/net",
"//ios/chrome/browser/ntp_snippets",
"//ios/chrome/browser/passwords",
"//ios/chrome/browser/policy",
"//ios/chrome/browser/prefs",
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/reading_list",
......
......@@ -16,6 +16,7 @@
#include "ios/web/public/browser_state.h"
#include "net/url_request/url_request_job_factory.h"
class BrowserStatePolicyConnector;
class ChromeBrowserStateIOData;
class PrefProxyConfigTracker;
class PrefService;
......@@ -75,6 +76,10 @@ class ChromeBrowserState : public web::BrowserState {
// ChromeBrowserState, if one exists.
virtual void DestroyOffTheRecordChromeBrowserState() = 0;
// Retrieves a pointer to the BrowserStatePolicyConnector that manages policy
// for this BrowserState. May return nullptr if policy is disabled.
virtual BrowserStatePolicyConnector* GetPolicyConnector() = 0;
// Retrieves a pointer to the PrefService that manages the preferences.
virtual PrefService* GetPrefs() = 0;
......
......@@ -13,6 +13,7 @@
#include "base/threading/thread_restrictions.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "components/policy/core/common/schema_registry.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_service.h"
......@@ -28,6 +29,11 @@
#include "ios/chrome/browser/chrome_paths_internal.h"
#include "ios/chrome/browser/file_metadata_util.h"
#include "ios/chrome/browser/net/ios_chrome_url_request_context_getter.h"
#include "ios/chrome/browser/policy/browser_policy_connector_ios.h"
#include "ios/chrome/browser/policy/browser_state_policy_connector.h"
#include "ios/chrome/browser/policy/browser_state_policy_connector_factory.h"
#include "ios/chrome/browser/policy/policy_features.h"
#include "ios/chrome/browser/policy/schema_registry_factory.h"
#include "ios/chrome/browser/pref_names.h"
#include "ios/chrome/browser/prefs/browser_prefs.h"
#include "ios/chrome/browser/prefs/ios_chrome_pref_service_factory.h"
......@@ -88,12 +94,24 @@ ChromeBrowserStateImpl::ChromeBrowserStateImpl(
state_path_, otr_state_path_, base_cache_path);
DCHECK(directories_created);
// Bring up the policy system before creating |prefs_|.
if (IsEnterprisePolicyEnabled()) {
BrowserPolicyConnectorIOS* connector =
GetApplicationContext()->GetBrowserPolicyConnector();
DCHECK(connector);
policy_schema_registry_ = BuildSchemaRegistryForBrowserState(
this, connector->GetChromeSchema(), connector->GetSchemaRegistry());
policy_connector_ = BuildBrowserStatePolicyConnector(connector);
}
RegisterBrowserStatePrefs(pref_registry_.get());
BrowserStateDependencyManager::GetInstance()
->RegisterBrowserStatePrefsForServices(pref_registry_.get());
prefs_ = CreateBrowserStatePrefs(state_path_, GetIOTaskRunner().get(),
pref_registry_);
prefs_ = CreateBrowserStatePrefs(
state_path_, GetIOTaskRunner().get(), pref_registry_,
policy_connector_ ? policy_connector_->GetPolicyService() : nullptr,
GetApplicationContext()->GetBrowserPolicyConnector());
// Register on BrowserState.
user_prefs::UserPrefs::Set(this, prefs_.get());
......@@ -153,6 +171,14 @@ void ChromeBrowserStateImpl::DestroyOffTheRecordChromeBrowserState() {
otr_state_.reset();
}
BrowserStatePolicyConnector* ChromeBrowserStateImpl::GetPolicyConnector() {
if (policy_connector_.get()) {
DCHECK(IsEnterprisePolicyEnabled());
return policy_connector_.get();
}
return nullptr;
}
PrefService* ChromeBrowserStateImpl::GetPrefs() {
DCHECK(prefs_); // Should explicitly be initialized.
return prefs_.get();
......
......@@ -11,6 +11,10 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_impl_io_data.h"
namespace policy {
class SchemaRegistry;
}
namespace sync_preferences {
class PrefServiceSyncable;
}
......@@ -33,6 +37,7 @@ class ChromeBrowserStateImpl : public ChromeBrowserState {
ChromeBrowserState* GetOffTheRecordChromeBrowserState() override;
void DestroyOffTheRecordChromeBrowserState() override;
PrefProxyConfigTracker* GetProxyConfigTracker() override;
BrowserStatePolicyConnector* GetPolicyConnector() override;
PrefService* GetPrefs() override;
PrefService* GetOffTheRecordPrefs() override;
ChromeBrowserStateIOData* GetIOData() override;
......@@ -71,8 +76,13 @@ class ChromeBrowserStateImpl : public ChromeBrowserState {
// that the declaration occurs AFTER things it depends on as destruction
// happens in reverse order of declaration.
// Keep |prefs_| on top for destruction order because |io_data_| and others
// store pointers to |prefs_| and shall be destructed first.
// |policy_connector_| and its associated |policy_schema_registry_| must
// outlive |prefs_|.
std::unique_ptr<policy::SchemaRegistry> policy_schema_registry_;
std::unique_ptr<BrowserStatePolicyConnector> policy_connector_;
// Keep |prefs_| above the rest for destruction order because |io_data_| and
// others store pointers to |prefs_| and shall be destructed first.
scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs_;
std::unique_ptr<sync_preferences::PrefServiceSyncable> otr_prefs_;
......
......@@ -63,6 +63,12 @@ void OffTheRecordChromeBrowserStateImpl::
NOTREACHED();
}
BrowserStatePolicyConnector*
OffTheRecordChromeBrowserStateImpl::GetPolicyConnector() {
// Forward the call to the original (non-OTR) browser state.
return GetOriginalChromeBrowserState()->GetPolicyConnector();
}
PrefService* OffTheRecordChromeBrowserStateImpl::GetPrefs() {
return prefs_;
}
......
......@@ -26,6 +26,7 @@ class OffTheRecordChromeBrowserStateImpl : public ChromeBrowserState {
ChromeBrowserState* GetOffTheRecordChromeBrowserState() override;
void DestroyOffTheRecordChromeBrowserState() override;
PrefProxyConfigTracker* GetProxyConfigTracker() override;
BrowserStatePolicyConnector* GetPolicyConnector() override;
PrefService* GetPrefs() override;
PrefService* GetOffTheRecordPrefs() override;
ChromeBrowserStateIOData* GetIOData() override;
......
......@@ -48,6 +48,7 @@ class TestChromeBrowserState : public ChromeBrowserState {
bool HasOffTheRecordChromeBrowserState() const override;
ChromeBrowserState* GetOffTheRecordChromeBrowserState() override;
PrefProxyConfigTracker* GetProxyConfigTracker() override;
BrowserStatePolicyConnector* GetPolicyConnector() override;
PrefService* GetPrefs() override;
PrefService* GetOffTheRecordPrefs() override;
ChromeBrowserStateIOData* GetIOData() override;
......
......@@ -271,6 +271,12 @@ PrefProxyConfigTracker* TestChromeBrowserState::GetProxyConfigTracker() {
return nullptr;
}
BrowserStatePolicyConnector* TestChromeBrowserState::GetPolicyConnector() {
// TODO(crbug.com/1055318): Determine what level of support is needed for
// unittesting and return a mock or fake here.
return nullptr;
}
PrefService* TestChromeBrowserState::GetPrefs() {
return prefs_.get();
}
......
......@@ -6,14 +6,21 @@ source_set("policy") {
sources = [
"browser_policy_connector_ios.h",
"browser_policy_connector_ios.mm",
"browser_state_policy_connector.h",
"browser_state_policy_connector.mm",
"browser_state_policy_connector_factory.h",
"browser_state_policy_connector_factory.mm",
"configuration_policy_handler_list_factory.h",
"configuration_policy_handler_list_factory.mm",
"schema_registry_factory.h",
"schema_registry_factory.mm",
]
deps = [
"//base",
"//components/policy:generated",
"//components/policy/core/common",
"//ios/chrome/browser/browser_state",
"//services/network/public/cpp",
]
......
......@@ -28,6 +28,11 @@ class BrowserPolicyConnectorIOS : public policy::BrowserPolicyConnector {
~BrowserPolicyConnectorIOS() override;
// Returns the platform provider used by this BrowserPolicyConnectorIOS. Can
// be overridden for testing via
// BrowserPolicyConnectorBase::SetPolicyProviderForTesting().
policy::ConfigurationPolicyProvider* GetPlatformProvider();
// BrowserPolicyConnector.
void Init(PrefService* local_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
......@@ -42,7 +47,6 @@ class BrowserPolicyConnectorIOS : public policy::BrowserPolicyConnector {
private:
std::unique_ptr<policy::ConfigurationPolicyProvider> CreatePlatformProvider();
policy::ConfigurationPolicyProvider* GetPlatformProvider();
// Owned by base class.
policy::ConfigurationPolicyProvider* platform_provider_ = nullptr;
......
......@@ -39,6 +39,12 @@ BrowserPolicyConnectorIOS::BrowserPolicyConnectorIOS(
BrowserPolicyConnectorIOS::~BrowserPolicyConnectorIOS() {}
ConfigurationPolicyProvider* BrowserPolicyConnectorIOS::GetPlatformProvider() {
ConfigurationPolicyProvider* provider =
BrowserPolicyConnectorBase::GetPolicyProviderForTesting();
return provider ? provider : platform_provider_;
}
void BrowserPolicyConnectorIOS::Init(
PrefService* local_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
......@@ -77,9 +83,3 @@ BrowserPolicyConnectorIOS::CreatePlatformProvider() {
return std::make_unique<AsyncPolicyProvider>(GetSchemaRegistry(),
std::move(loader));
}
ConfigurationPolicyProvider* BrowserPolicyConnectorIOS::GetPlatformProvider() {
ConfigurationPolicyProvider* provider =
BrowserPolicyConnectorBase::GetPolicyProviderForTesting();
return provider ? provider : platform_provider_;
}
// 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 IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_H_
#define IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_H_
#include <memory>
#include <vector>
class BrowserPolicyConnectorIOS;
namespace policy {
class ConfigurationPolicyProvider;
class PolicyService;
} // namespace policy
// BrowserStatePolicyConnector creates and manages the per-BrowserState policy
// components and their integration with PrefService.
class BrowserStatePolicyConnector {
public:
BrowserStatePolicyConnector();
~BrowserStatePolicyConnector();
BrowserStatePolicyConnector(const BrowserStatePolicyConnector&) = delete;
BrowserStatePolicyConnector& operator=(const BrowserStatePolicyConnector&) =
delete;
// Initializes this connector.
void Init(BrowserPolicyConnectorIOS* browser_policy_connector);
// Shuts this connector down in preparation for destruction.
void Shutdown();
// Returns the PolicyService managed by this connector. This is never
// nullptr.
policy::PolicyService* GetPolicyService() const {
return policy_service_.get();
}
private:
// |policy_providers_| contains a list of the policy providers available for
// the PolicyService of this connector, in decreasing order of priority.
//
// Note: All the providers appended to this vector must eventually become
// initialized for every policy domain, otherwise some subsystems will never
// use the policies exposed by the PolicyService!
// The default ConfigurationPolicyProvider::IsInitializationComplete()
// result is true, so take care if a provider overrides that.
std::vector<policy::ConfigurationPolicyProvider*> policy_providers_;
std::unique_ptr<policy::PolicyService> policy_service_;
};
#endif // IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_H_
// 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.
#import "ios/chrome/browser/policy/browser_state_policy_connector.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "ios/chrome/browser/policy/browser_policy_connector_ios.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
BrowserStatePolicyConnector::BrowserStatePolicyConnector() = default;
BrowserStatePolicyConnector::~BrowserStatePolicyConnector() = default;
void BrowserStatePolicyConnector::Init(
BrowserPolicyConnectorIOS* browser_policy_connector) {
policy_providers_ = browser_policy_connector->GetPolicyProviders();
policy_service_ =
std::make_unique<policy::PolicyServiceImpl>(policy_providers_);
}
void BrowserStatePolicyConnector::Shutdown() {}
// 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 IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_FACTORY_H_
#define IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_FACTORY_H_
#include <memory>
class BrowserPolicyConnectorIOS;
class BrowserStatePolicyConnector;
std::unique_ptr<BrowserStatePolicyConnector> BuildBrowserStatePolicyConnector(
BrowserPolicyConnectorIOS* browser_policy_connector);
#endif // IOS_CHROME_BROWSER_POLICY_BROWSER_STATE_POLICY_CONNECTOR_FACTORY_H_
// 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.
#import "ios/chrome/browser/policy/browser_state_policy_connector_factory.h"
#include "base/logging.h"
#include "ios/chrome/browser/policy/browser_state_policy_connector.h"
#include "ios/chrome/browser/policy/policy_features.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
std::unique_ptr<BrowserStatePolicyConnector> BuildBrowserStatePolicyConnector(
BrowserPolicyConnectorIOS* browser_policy_connector) {
DCHECK(IsEnterprisePolicyEnabled());
auto connector = std::make_unique<BrowserStatePolicyConnector>();
connector->Init(browser_policy_connector);
return connector;
}
// 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 IOS_CHROME_BROWSER_POLICY_SCHEMA_REGISTRY_FACTORY_H_
#define IOS_CHROME_BROWSER_POLICY_SCHEMA_REGISTRY_FACTORY_H_
#include <memory>
class ChromeBrowserState;
namespace policy {
class CombinedSchemaRegistry;
class Schema;
class SchemaRegistry;
} // namespace policy
std::unique_ptr<policy::SchemaRegistry> BuildSchemaRegistryForBrowserState(
ChromeBrowserState* browser_state,
const policy::Schema& chrome_schema,
policy::CombinedSchemaRegistry* global_registry);
#endif // IOS_CHROME_BROWSER_POLICY_SCHEMA_REGISTRY_FACTORY_H_
// 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.
#import "ios/chrome/browser/policy/schema_registry_factory.h"
#include "base/logging.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/core/common/schema_registry.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/policy/policy_features.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
std::unique_ptr<policy::SchemaRegistry> BuildSchemaRegistryForBrowserState(
ChromeBrowserState* browser_state,
const policy::Schema& chrome_schema,
policy::CombinedSchemaRegistry* global_registry) {
DCHECK(IsEnterprisePolicyEnabled());
DCHECK(!browser_state->IsOffTheRecord());
auto registry = std::make_unique<policy::SchemaRegistry>();
if (chrome_schema.valid()) {
registry->RegisterComponent(
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, ""),
chrome_schema);
}
registry->SetDomainReady(policy::POLICY_DOMAIN_CHROME);
if (global_registry) {
global_registry->Track(registry.get());
}
return registry;
}
......@@ -40,11 +40,11 @@ void HandleReadError(PersistentPrefStore::PrefReadError error) {
void PrepareFactory(sync_preferences::PrefServiceSyncableFactory* factory,
const base::FilePath& pref_filename,
base::SequencedTaskRunner* pref_io_task_runner,
policy::PolicyService* policy_service,
policy::BrowserPolicyConnector* policy_connector) {
if (policy_connector) {
if (policy_service || policy_connector) {
DCHECK(IsEnterprisePolicyEnabled());
policy::PolicyService* policy_service =
policy_connector->GetPolicyService();
DCHECK(policy_service && policy_connector);
factory->SetManagedPolicies(policy_service, policy_connector);
factory->SetRecommendedPolicies(policy_service, policy_connector);
}
......@@ -63,9 +63,10 @@ std::unique_ptr<PrefService> CreateLocalState(
const base::FilePath& pref_filename,
base::SequencedTaskRunner* pref_io_task_runner,
const scoped_refptr<PrefRegistry>& pref_registry,
policy::PolicyService* policy_service,
policy::BrowserPolicyConnector* policy_connector) {
sync_preferences::PrefServiceSyncableFactory factory;
PrepareFactory(&factory, pref_filename, pref_io_task_runner,
PrepareFactory(&factory, pref_filename, pref_io_task_runner, policy_service,
policy_connector);
return factory.Create(pref_registry.get());
}
......@@ -73,7 +74,9 @@ std::unique_ptr<PrefService> CreateLocalState(
std::unique_ptr<sync_preferences::PrefServiceSyncable> CreateBrowserStatePrefs(
const base::FilePath& browser_state_path,
base::SequencedTaskRunner* pref_io_task_runner,
const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry) {
const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry,
policy::PolicyService* policy_service,
policy::BrowserPolicyConnector* policy_connector) {
// chrome_prefs::CreateProfilePrefs uses ProfilePrefStoreManager to create
// the preference store however since Chrome on iOS does not need to track
// preference modifications (as applications are sand-boxed), it can use a
......@@ -81,7 +84,7 @@ std::unique_ptr<sync_preferences::PrefServiceSyncable> CreateBrowserStatePrefs(
// on platforms that do not track preference modifications).
sync_preferences::PrefServiceSyncableFactory factory;
PrepareFactory(&factory, browser_state_path.Append(kPreferencesFilename),
pref_io_task_runner, nullptr);
pref_io_task_runner, policy_service, policy_connector);
std::unique_ptr<sync_preferences::PrefServiceSyncable> pref_service =
factory.CreateSyncable(pref_registry.get());
return pref_service;
......
......@@ -19,6 +19,7 @@ class SequencedTaskRunner;
namespace policy {
class BrowserPolicyConnector;
class PolicyService;
}
namespace sync_preferences {
......@@ -38,12 +39,15 @@ std::unique_ptr<PrefService> CreateLocalState(
const base::FilePath& pref_filename,
base::SequencedTaskRunner* pref_io_task_runner,
const scoped_refptr<PrefRegistry>& pref_registry,
policy::PolicyService* policy_service,
policy::BrowserPolicyConnector* policy_connector);
std::unique_ptr<sync_preferences::PrefServiceSyncable> CreateBrowserStatePrefs(
const base::FilePath& browser_state_path,
base::SequencedTaskRunner* pref_io_task_runner,
const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry);
const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry,
policy::PolicyService* policy_service,
policy::BrowserPolicyConnector* policy_connector);
// Creates an incognito copy of |pref_service| that shares most prefs but uses
// a fresh non-persistent overlay for the user pref store.
......
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