Maintain a connection to each profile's PrefService from ash.

Move responsibility for managing per-user PrefService instances into
ash::SessionController. When a new user is added, SessionController uses
its Connector to connect to the "ash_pref_connector" service to obtain a
prefs::mojom::PrefStoreConnector for that user, which it then uses to
construct a client PrefService.

Provide access to per-user prefs via GetUserPrefServiceForUser() which
returns the PrefService for a particular AccountId or null if the
connection has not completed successfully, and
GetLastActiveUserPrefService(), which returns the PrefService for the
last active user that had a PrefService.

Consolidate the mash and non-mash code-paths for per-user prefs to
always access per-user PrefService over mojo.

Add a test-only method to SessionController so unit tests can inject a
PrefService for an individual user without mocking out service manager.
Change TestSessionControllerClient to default to injecting a
PrefServicefPrefService for each user session it adds. Update ash
unit tests to no longer inject their own PrefServices now the test
helper handles it.

Replace the preferences_forwarder service with ash_preferences_connector
service. Instead of forwarding connections to the pref service for the
active user, it provides access to a prefs::mojom::PrefStoreConnector
for the AccountId request by SessionController.

Add a new overload to prefs::ConnectToPrefService that takes a
user-provided PrefStoreConnectorPtr for SessionController to use.

Change prefs::ConnectToPrefService to wait until all PrefStores are
initialized instead of just the user prefs pref store and add plumbing
to PrefService and PrefValueStore to expose this information.

Fix PrefRegistrySyncable::ForkForIncognito() to copy all fields from
PrefRegistry.

Bug: 752993,753149
Change-Id: Id36a18988b1e713199f3029cb04a72dd63bf1b34
Reviewed-on: https://chromium-review.googlesource.com/605027
Commit-Queue: Sam McNally <[email protected]>
Reviewed-by: Bernhard Bauer <[email protected]>
Reviewed-by: Michael Wasserman <[email protected]>
Reviewed-by: Xiyuan Xia <[email protected]>
Reviewed-by: Lei Zhang <[email protected]>
Reviewed-by: Oliver Chang <[email protected]>
Reviewed-by: James Cook <[email protected]>
Cr-Commit-Position: refs/heads/master@{#494684}
diff --git a/ash/shelf/shelf_controller.cc b/ash/shelf/shelf_controller.cc
index 93bcbf32..be74793 100644
--- a/ash/shelf/shelf_controller.cc
+++ b/ash/shelf/shelf_controller.cc
@@ -43,7 +43,7 @@
   // otherwise this wrongly tries to set the alignment on a secondary display
   // during login before the ShelfLockingManager is created.
   SessionController* session_controller = Shell::Get()->session_controller();
-  PrefService* prefs = Shell::Get()->GetActiveUserPrefService();
+  PrefService* prefs = session_controller->GetLastActiveUserPrefService();
   if (!prefs || !session_controller->IsActiveUserSessionStarted())
     return;
 
@@ -64,7 +64,7 @@
   // otherwise this wrongly tries to set the alignment on a secondary display
   // during login before the ShelfLockingManager is created.
   SessionController* session_controller = Shell::Get()->session_controller();
-  PrefService* prefs = Shell::Get()->GetActiveUserPrefService();
+  PrefService* prefs = session_controller->GetLastActiveUserPrefService();
   if (!prefs || !session_controller->IsActiveUserSessionStarted())
     return;
 
@@ -94,13 +94,13 @@
   model_.Set(0, item);
 
   model_.AddObserver(this);
-  Shell::Get()->AddShellObserver(this);
+  Shell::Get()->session_controller()->AddObserver(this);
   Shell::Get()->window_tree_host_manager()->AddObserver(this);
 }
 
 ShelfController::~ShelfController() {
   Shell::Get()->window_tree_host_manager()->RemoveObserver(this);
-  Shell::Get()->RemoveShellObserver(this);
+  Shell::Get()->session_controller()->RemoveObserver(this);
   model_.RemoveObserver(this);
 }
 
@@ -278,9 +278,6 @@
 
 void ShelfController::OnActiveUserPrefServiceChanged(
     PrefService* pref_service) {
-  pref_change_registrar_.reset();
-  if (!pref_service)  // Null during startup, user switch and tests.
-    return;
   SetShelfBehaviorsFromPrefs();
   pref_change_registrar_ = base::MakeUnique<PrefChangeRegistrar>();
   pref_change_registrar_->Init(pref_service);