Commit f077446d authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

ios: Make PrefServiceAdapter use the network pref store.

This means that it only lives on on thread: The network thread, instead
of jumping between the UI thread and the IO thread, since the network
pref store lives on the IO thread, just like the network stack.

A similar change was made to chrome/, as part of network
servicification (Though in Chrome it's created by the NetworkService
now). In a followup CL, I'll simplify HttpServerPropertiesManager to
only support the case where the prefs thread is the network thread.

Bug: 768884
Change-Id: I92c97e8e5fb3f5cab46f1c998b92eaec48a3db5f
Reviewed-on: https://chromium-review.googlesource.com/687803
Commit-Queue: Matt Menke <mmenke@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505375}
parent deaa07be
......@@ -57,8 +57,6 @@ ChromeBrowserStateImplIOData::Handle::Handle(
ChromeBrowserStateImplIOData::Handle::~Handle() {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
if (io_data_->http_server_properties_manager_)
io_data_->http_server_properties_manager_->ShutdownOnPrefSequence();
io_data_->ShutdownOnUIThread(GetAllContextGetters());
}
......@@ -151,13 +149,6 @@ void ChromeBrowserStateImplIOData::Handle::LazyInitialize() const {
// Set initialized_ to true at the beginning in case any of the objects
// below try to get the ResourceContext pointer.
initialized_ = true;
PrefService* pref_service = browser_state_->GetPrefs();
io_data_->http_server_properties_manager_ =
HttpServerPropertiesManagerFactory::CreateManager(
pref_service,
GetApplicationContext()->GetIOSChromeIOThread()->net_log());
io_data_->set_http_server_properties(
base::WrapUnique(io_data_->http_server_properties_manager_));
io_data_->InitializeOnUIThread(browser_state_);
}
......@@ -188,7 +179,10 @@ ChromeBrowserStateImplIOData::ChromeBrowserStateImplIOData()
http_server_properties_manager_(nullptr),
app_cache_max_size_(0) {}
ChromeBrowserStateImplIOData::~ChromeBrowserStateImplIOData() {}
ChromeBrowserStateImplIOData::~ChromeBrowserStateImplIOData() {
if (http_server_properties_manager_)
http_server_properties_manager_->ShutdownOnPrefSequence();
}
void ChromeBrowserStateImplIOData::InitializeInternal(
std::unique_ptr<IOSChromeNetworkDelegate> chrome_network_delegate,
......@@ -212,7 +206,10 @@ void ChromeBrowserStateImplIOData::InitializeInternal(
ApplyProfileParamsToContext(main_context);
if (http_server_properties_manager_)
http_server_properties_manager_ =
HttpServerPropertiesManagerFactory::CreateManager(network_json_store_,
io_thread->net_log());
set_http_server_properties(base::WrapUnique(http_server_properties_manager_));
http_server_properties_manager_->InitializeOnNetworkSequence();
main_context->set_transport_security_state(transport_security_state());
......@@ -357,5 +354,7 @@ void ChromeBrowserStateImplIOData::ClearNetworkingHistorySinceOnIOThread(
// Completes synchronously.
transport_security_state()->DeleteAllDynamicDataSince(time);
DCHECK(http_server_properties_manager_);
http_server_properties_manager_->Clear(completion);
http_server_properties_manager_->Clear(
base::BindOnce(base::IgnoreResult(&web::WebThread::PostTask),
web::WebThread::UI, FROM_HERE, completion));
}
......@@ -4,10 +4,10 @@
#include "ios/chrome/browser/net/http_server_properties_manager_factory.h"
#include "base/threading/thread_task_runner_handle.h"
#include <memory>
#include "base/values.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "ios/chrome/browser/pref_names.h"
#include "ios/web/public/web_thread.h"
#include "net/http/http_server_properties_manager.h"
......@@ -15,38 +15,61 @@
namespace {
class PrefServiceAdapter
: public net::HttpServerPropertiesManager::PrefDelegate {
: public net::HttpServerPropertiesManager::PrefDelegate,
public PrefStore::Observer {
public:
explicit PrefServiceAdapter(PrefService* pref_service)
: pref_service_(pref_service), path_(prefs::kHttpServerProperties) {
pref_change_registrar_.Init(pref_service_);
explicit PrefServiceAdapter(scoped_refptr<WriteablePrefStore> pref_store)
: pref_store_(std::move(pref_store)),
path_(prefs::kHttpServerProperties) {
pref_store_->AddObserver(this);
}
~PrefServiceAdapter() override {}
~PrefServiceAdapter() override { pref_store_->RemoveObserver(this); }
// PrefDelegate implementation.
bool HasServerProperties() override {
return pref_service_->HasPrefPath(path_);
return pref_store_->GetValue(path_, nullptr);
}
const base::DictionaryValue& GetServerProperties() const override {
// Guaranteed not to return null when the pref is registered
// (RegisterProfilePrefs was called).
return *pref_service_->GetDictionary(path_);
const base::Value* value;
if (pref_store_->GetValue(path_, &value)) {
const base::DictionaryValue* dict;
if (value->GetAsDictionary(&dict))
return *dict;
}
return empty_dictionary_;
}
void SetServerProperties(const base::DictionaryValue& value) override {
return pref_service_->Set(path_, value);
return pref_store_->SetValue(path_, value.CreateDeepCopy(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
void StartListeningForUpdates(const base::Closure& callback) override {
pref_change_registrar_.Add(path_, callback);
on_changed_callback_ = callback;
}
void StopListeningForUpdates() override {
pref_change_registrar_.RemoveAll();
on_changed_callback_ = base::Closure();
}
// PrefStore::Observer implementation.
void OnPrefValueChanged(const std::string& key) override {
if (key == path_ && on_changed_callback_)
on_changed_callback_.Run();
}
void OnInitializationCompleted(bool succeeded) override {
if (succeeded && on_changed_callback_ && HasServerProperties())
on_changed_callback_.Run();
}
private:
PrefService* pref_service_;
scoped_refptr<WriteablePrefStore> pref_store_;
const std::string path_;
PrefChangeRegistrar pref_change_registrar_;
// Returned when the pref is not set. Since the method returns a const
// net::DictionaryValue&, can't just create one on the stack.
base::DictionaryValue empty_dictionary_;
base::Closure on_changed_callback_;
DISALLOW_COPY_AND_ASSIGN(PrefServiceAdapter);
};
......@@ -55,16 +78,12 @@ class PrefServiceAdapter
// static
net::HttpServerPropertiesManager*
HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service,
HttpServerPropertiesManagerFactory::CreateManager(
scoped_refptr<WriteablePrefStore> pref_store,
net::NetLog* net_log) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
return new net::HttpServerPropertiesManager(
new PrefServiceAdapter(pref_service), // Transfers ownership.
base::ThreadTaskRunnerHandle::Get(),
new PrefServiceAdapter(std::move(pref_store)),
web::WebThread::GetTaskRunnerForThread(web::WebThread::IO),
web::WebThread::GetTaskRunnerForThread(web::WebThread::IO), net_log);
}
// static
void HttpServerPropertiesManagerFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kHttpServerProperties);
}
......@@ -6,29 +6,22 @@
#define IOS_CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_FACTORY_H_
#include "base/macros.h"
class PrefService;
#include "base/memory/ref_counted.h"
#include "components/prefs/writeable_pref_store.h"
namespace net {
class HttpServerPropertiesManager;
class NetLog;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
// Class for registration and creation of HttpServerPropertiesManager
class HttpServerPropertiesManagerFactory {
public:
// Create an instance of HttpServerPropertiesManager.
static net::HttpServerPropertiesManager* CreateManager(
PrefService* pref_service,
scoped_refptr<WriteablePrefStore> pref_store,
net::NetLog* net_log);
// Register prefs for properties managed by HttpServerPropertiesManager.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
private:
DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerFactory);
};
......
......@@ -46,7 +46,6 @@
#import "ios/chrome/browser/geolocation/omnibox_geolocation_local_state.h"
#import "ios/chrome/browser/memory/memory_debugger_manager.h"
#import "ios/chrome/browser/metrics/ios_chrome_metrics_service_client.h"
#include "ios/chrome/browser/net/http_server_properties_manager_factory.h"
#include "ios/chrome/browser/notification_promo.h"
#include "ios/chrome/browser/physical_web/physical_web_prefs_registration.h"
#include "ios/chrome/browser/pref_names.h"
......@@ -104,7 +103,6 @@ void RegisterBrowserStatePrefs(user_prefs::PrefRegistrySyncable* registry) {
FirstRun::RegisterProfilePrefs(registry);
gcm::GCMChannelStatusSyncer::RegisterProfilePrefs(registry);
HostContentSettingsMap::RegisterProfilePrefs(registry);
HttpServerPropertiesManagerFactory::RegisterProfilePrefs(registry);
language::UrlLanguageHistogram::RegisterProfilePrefs(registry);
ntp_snippets::ClickBasedCategoryRanker::RegisterProfilePrefs(registry);
ntp_snippets::ContentSuggestionsService::RegisterProfilePrefs(registry);
......
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