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( ...@@ -57,8 +57,6 @@ ChromeBrowserStateImplIOData::Handle::Handle(
ChromeBrowserStateImplIOData::Handle::~Handle() { ChromeBrowserStateImplIOData::Handle::~Handle() {
DCHECK_CURRENTLY_ON(web::WebThread::UI); DCHECK_CURRENTLY_ON(web::WebThread::UI);
if (io_data_->http_server_properties_manager_)
io_data_->http_server_properties_manager_->ShutdownOnPrefSequence();
io_data_->ShutdownOnUIThread(GetAllContextGetters()); io_data_->ShutdownOnUIThread(GetAllContextGetters());
} }
...@@ -151,13 +149,6 @@ void ChromeBrowserStateImplIOData::Handle::LazyInitialize() const { ...@@ -151,13 +149,6 @@ void ChromeBrowserStateImplIOData::Handle::LazyInitialize() const {
// Set initialized_ to true at the beginning in case any of the objects // Set initialized_ to true at the beginning in case any of the objects
// below try to get the ResourceContext pointer. // below try to get the ResourceContext pointer.
initialized_ = true; 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_); io_data_->InitializeOnUIThread(browser_state_);
} }
...@@ -188,7 +179,10 @@ ChromeBrowserStateImplIOData::ChromeBrowserStateImplIOData() ...@@ -188,7 +179,10 @@ ChromeBrowserStateImplIOData::ChromeBrowserStateImplIOData()
http_server_properties_manager_(nullptr), http_server_properties_manager_(nullptr),
app_cache_max_size_(0) {} app_cache_max_size_(0) {}
ChromeBrowserStateImplIOData::~ChromeBrowserStateImplIOData() {} ChromeBrowserStateImplIOData::~ChromeBrowserStateImplIOData() {
if (http_server_properties_manager_)
http_server_properties_manager_->ShutdownOnPrefSequence();
}
void ChromeBrowserStateImplIOData::InitializeInternal( void ChromeBrowserStateImplIOData::InitializeInternal(
std::unique_ptr<IOSChromeNetworkDelegate> chrome_network_delegate, std::unique_ptr<IOSChromeNetworkDelegate> chrome_network_delegate,
...@@ -212,8 +206,11 @@ void ChromeBrowserStateImplIOData::InitializeInternal( ...@@ -212,8 +206,11 @@ void ChromeBrowserStateImplIOData::InitializeInternal(
ApplyProfileParamsToContext(main_context); ApplyProfileParamsToContext(main_context);
if (http_server_properties_manager_) http_server_properties_manager_ =
http_server_properties_manager_->InitializeOnNetworkSequence(); 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()); main_context->set_transport_security_state(transport_security_state());
...@@ -357,5 +354,7 @@ void ChromeBrowserStateImplIOData::ClearNetworkingHistorySinceOnIOThread( ...@@ -357,5 +354,7 @@ void ChromeBrowserStateImplIOData::ClearNetworkingHistorySinceOnIOThread(
// Completes synchronously. // Completes synchronously.
transport_security_state()->DeleteAllDynamicDataSince(time); transport_security_state()->DeleteAllDynamicDataSince(time);
DCHECK(http_server_properties_manager_); 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 @@ ...@@ -4,10 +4,10 @@
#include "ios/chrome/browser/net/http_server_properties_manager_factory.h" #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/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/chrome/browser/pref_names.h"
#include "ios/web/public/web_thread.h" #include "ios/web/public/web_thread.h"
#include "net/http/http_server_properties_manager.h" #include "net/http/http_server_properties_manager.h"
...@@ -15,38 +15,61 @@ ...@@ -15,38 +15,61 @@
namespace { namespace {
class PrefServiceAdapter class PrefServiceAdapter
: public net::HttpServerPropertiesManager::PrefDelegate { : public net::HttpServerPropertiesManager::PrefDelegate,
public PrefStore::Observer {
public: public:
explicit PrefServiceAdapter(PrefService* pref_service) explicit PrefServiceAdapter(scoped_refptr<WriteablePrefStore> pref_store)
: pref_service_(pref_service), path_(prefs::kHttpServerProperties) { : pref_store_(std::move(pref_store)),
pref_change_registrar_.Init(pref_service_); path_(prefs::kHttpServerProperties) {
pref_store_->AddObserver(this);
} }
~PrefServiceAdapter() override {} ~PrefServiceAdapter() override { pref_store_->RemoveObserver(this); }
// PrefDelegate implementation. // PrefDelegate implementation.
bool HasServerProperties() override { bool HasServerProperties() override {
return pref_service_->HasPrefPath(path_); return pref_store_->GetValue(path_, nullptr);
} }
const base::DictionaryValue& GetServerProperties() const override { const base::DictionaryValue& GetServerProperties() const override {
// Guaranteed not to return null when the pref is registered const base::Value* value;
// (RegisterProfilePrefs was called). if (pref_store_->GetValue(path_, &value)) {
return *pref_service_->GetDictionary(path_); const base::DictionaryValue* dict;
if (value->GetAsDictionary(&dict))
return *dict;
}
return empty_dictionary_;
} }
void SetServerProperties(const base::DictionaryValue& value) override { 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 { void StartListeningForUpdates(const base::Closure& callback) override {
pref_change_registrar_.Add(path_, callback); on_changed_callback_ = callback;
} }
void StopListeningForUpdates() override { 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: private:
PrefService* pref_service_; scoped_refptr<WriteablePrefStore> pref_store_;
const std::string path_; 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); DISALLOW_COPY_AND_ASSIGN(PrefServiceAdapter);
}; };
...@@ -55,16 +78,12 @@ class PrefServiceAdapter ...@@ -55,16 +78,12 @@ class PrefServiceAdapter
// static // static
net::HttpServerPropertiesManager* net::HttpServerPropertiesManager*
HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service, HttpServerPropertiesManagerFactory::CreateManager(
net::NetLog* net_log) { scoped_refptr<WriteablePrefStore> pref_store,
net::NetLog* net_log) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
return new net::HttpServerPropertiesManager( return new net::HttpServerPropertiesManager(
new PrefServiceAdapter(pref_service), // Transfers ownership. new PrefServiceAdapter(std::move(pref_store)),
base::ThreadTaskRunnerHandle::Get(), web::WebThread::GetTaskRunnerForThread(web::WebThread::IO),
web::WebThread::GetTaskRunnerForThread(web::WebThread::IO), net_log); web::WebThread::GetTaskRunnerForThread(web::WebThread::IO), net_log);
} }
// static
void HttpServerPropertiesManagerFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kHttpServerProperties);
}
...@@ -6,29 +6,22 @@ ...@@ -6,29 +6,22 @@
#define IOS_CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_FACTORY_H_ #define IOS_CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_FACTORY_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h"
class PrefService; #include "components/prefs/writeable_pref_store.h"
namespace net { namespace net {
class HttpServerPropertiesManager; class HttpServerPropertiesManager;
class NetLog; class NetLog;
} }
namespace user_prefs {
class PrefRegistrySyncable;
}
// Class for registration and creation of HttpServerPropertiesManager // Class for registration and creation of HttpServerPropertiesManager
class HttpServerPropertiesManagerFactory { class HttpServerPropertiesManagerFactory {
public: public:
// Create an instance of HttpServerPropertiesManager. // Create an instance of HttpServerPropertiesManager.
static net::HttpServerPropertiesManager* CreateManager( static net::HttpServerPropertiesManager* CreateManager(
PrefService* pref_service, scoped_refptr<WriteablePrefStore> pref_store,
net::NetLog* net_log); net::NetLog* net_log);
// Register prefs for properties managed by HttpServerPropertiesManager.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
private: private:
DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerFactory); DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerFactory);
}; };
......
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
#import "ios/chrome/browser/geolocation/omnibox_geolocation_local_state.h" #import "ios/chrome/browser/geolocation/omnibox_geolocation_local_state.h"
#import "ios/chrome/browser/memory/memory_debugger_manager.h" #import "ios/chrome/browser/memory/memory_debugger_manager.h"
#import "ios/chrome/browser/metrics/ios_chrome_metrics_service_client.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/notification_promo.h"
#include "ios/chrome/browser/physical_web/physical_web_prefs_registration.h" #include "ios/chrome/browser/physical_web/physical_web_prefs_registration.h"
#include "ios/chrome/browser/pref_names.h" #include "ios/chrome/browser/pref_names.h"
...@@ -104,7 +103,6 @@ void RegisterBrowserStatePrefs(user_prefs::PrefRegistrySyncable* registry) { ...@@ -104,7 +103,6 @@ void RegisterBrowserStatePrefs(user_prefs::PrefRegistrySyncable* registry) {
FirstRun::RegisterProfilePrefs(registry); FirstRun::RegisterProfilePrefs(registry);
gcm::GCMChannelStatusSyncer::RegisterProfilePrefs(registry); gcm::GCMChannelStatusSyncer::RegisterProfilePrefs(registry);
HostContentSettingsMap::RegisterProfilePrefs(registry); HostContentSettingsMap::RegisterProfilePrefs(registry);
HttpServerPropertiesManagerFactory::RegisterProfilePrefs(registry);
language::UrlLanguageHistogram::RegisterProfilePrefs(registry); language::UrlLanguageHistogram::RegisterProfilePrefs(registry);
ntp_snippets::ClickBasedCategoryRanker::RegisterProfilePrefs(registry); ntp_snippets::ClickBasedCategoryRanker::RegisterProfilePrefs(registry);
ntp_snippets::ContentSuggestionsService::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