Pref service: Expose local state prefs to mash as a service.

Expose local state prefs from the browser using a
InProcessPrefServiceFactory in BrowserProcessImpl and register it as a
embedded service factory in ChromeContentBrowserClient.

Add plumbing of a PrefValueStore::Delegate to PrefServiceFactory to
match PrefServiceSyncableFactory to capture the PrefStores to be
exposed via the local state pref service.

Duplicate the existing pref service manifest as "local_state" to avoid
colliding with the per-profile pref service; each service may only be
packaged in a single service so a distinct service name is required.

Bug: 714855
Change-Id: I31131be6b5c720fa28e83a81febeea9c524c3cfe
Reviewed-on: https://chromium-review.googlesource.com/557580
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Noel Gordon <[email protected]>
Reviewed-by: Bernhard Bauer <[email protected]>
Reviewed-by: Tom Sepez <[email protected]>
Commit-Queue: Sam McNally <[email protected]>
Cr-Commit-Position: refs/heads/master@{#488070}
diff --git a/ash/shell.cc b/ash/shell.cc
index 2388832a..eaf3f417 100644
--- a/ash/shell.cc
+++ b/ash/shell.cc
@@ -420,6 +420,9 @@
 }
 
 PrefService* Shell::GetLocalStatePrefService() const {
+  if (shell_port_->GetAshConfig() == Config::MASH)
+    return local_state_.get();
+
   return shell_delegate_->GetLocalStatePrefService();
 }
 
@@ -633,7 +636,8 @@
       display_configurator_(new display::DisplayConfigurator()),
       native_cursor_manager_(nullptr),
       simulate_modal_window_open_for_testing_(false),
-      is_touch_hud_projection_enabled_(false) {
+      is_touch_hud_projection_enabled_(false),
+      weak_factory_(this) {
   // TODO(sky): better refactor cash/mash dependencies. Perhaps put all cash
   // state on ShellPortClassic. http://crbug.com/671246.
 
@@ -856,10 +860,17 @@
   if (config == Config::MASH && shell_delegate_->GetShellConnector()) {
     auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
     Shell::RegisterPrefs(pref_registry.get());
+    prefs::ConnectToPrefService(shell_delegate_->GetShellConnector(),
+                                std::move(pref_registry),
+                                base::Bind(&Shell::OnPrefServiceInitialized,
+                                           weak_factory_.GetWeakPtr()),
+                                prefs::mojom::kForwarderServiceName);
+    pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
     prefs::ConnectToPrefService(
         shell_delegate_->GetShellConnector(), std::move(pref_registry),
-        base::Bind(&Shell::OnPrefServiceInitialized, base::Unretained(this)),
-        prefs::mojom::kForwarderServiceName);
+        base::Bind(&Shell::OnLocalStatePrefServiceInitialized,
+                   weak_factory_.GetWeakPtr()),
+        prefs::mojom::kLocalStateServiceName);
   }
 
   // Some delegates access ShellPort during their construction. Create them here
@@ -1292,11 +1303,16 @@
 
 void Shell::OnPrefServiceInitialized(
     std::unique_ptr<::PrefService> pref_service) {
-  if (!instance_)
-    return;
   // |pref_service_| is null if can't connect to Chrome (as happens when
   // running mash outside of chrome --mash and chrome isn't built).
   pref_service_ = std::move(pref_service);
 }
 
+void Shell::OnLocalStatePrefServiceInitialized(
+    std::unique_ptr<::PrefService> pref_service) {
+  // |pref_service_| is null if can't connect to Chrome (as happens when
+  // running mash outside of chrome --mash and chrome isn't built).
+  local_state_ = std::move(pref_service);
+}
+
 }  // namespace ash