Fix refresh token is not available after Chrome restart on crash.

In case Chrome crashed and restarted, refresh token was not reloaded.
This caused several problems, including random user image profile
and impossibility to opt in ARC.

TEST=Manually simulated crash. On Chrome restart profile image is
     correct and ARC can be opted in. Also logs show that token
     is loaded correctly.
BUG=701866

Review-Url: https://codereview.chromium.org/2752873002
Cr-Commit-Position: refs/heads/master@{#457619}
diff --git a/chrome/browser/chromeos/login/session/user_session_manager.cc b/chrome/browser/chromeos/login/session/user_session_manager.cc
index abb9800f..6430ca7 100644
--- a/chrome/browser/chromeos/login/session/user_session_manager.cc
+++ b/chrome/browser/chromeos/login/session/user_session_manager.cc
@@ -548,6 +548,16 @@
 
   const user_manager::User* user =
       ProfileHelper::Get()->GetUserByProfile(user_profile);
+
+  const SigninManagerBase* signin_manager =
+      SigninManagerFactory::GetForProfile(user_profile);
+  const bool account_id_valid =
+      signin_manager && !signin_manager->GetAuthenticatedAccountId().empty();
+  if (!account_id_valid)
+    LOG(ERROR) << "No account is associated with sign-in manager on restore.";
+  UMA_HISTOGRAM_BOOLEAN("UserSessionManager.RestoreOnCrash.AccountIdValid",
+                        account_id_valid);
+
   DCHECK(user);
   if (!net::NetworkChangeNotifier::IsOffline()) {
     pending_signin_restore_sessions_.erase(user->GetAccountId().GetUserEmail());
diff --git a/components/signin/core/browser/signin_manager_base.cc b/components/signin/core/browser/signin_manager_base.cc
index 68d68b4..b36d21d 100644
--- a/components/signin/core/browser/signin_manager_base.cc
+++ b/components/signin/core/browser/signin_manager_base.cc
@@ -216,6 +216,10 @@
                                  account_id);
   client_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername,
                                  info.email);
+
+  // Commit authenticated account info immediately so that it does not get lost
+  // if Chrome crashes before the next commit interval.
+  client_->GetPrefs()->CommitPendingWrite();
 }
 
 bool SigninManagerBase::IsAuthenticated() const {
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index a9161fc..a663e70 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -74477,6 +74477,14 @@
   </summary>
 </histogram>
 
+<histogram name="UserSessionManager.RestoreOnCrash.AccountIdValid"
+    enum="BooleanValid">
+  <owner>msarda@chromium.org</owner>
+  <summary>
+    The result of restoring account id on Chrome restart after crash.
+  </summary>
+</histogram>
+
 <histogram name="UserSessionManager.UserPodsDisplay" enum="UserPodsDisplay">
   <owner>achuith@chromium.org</owner>
   <summary>