[cros login] Delay clearing signin profile till merge session is complete

Regression after https://chromiumcodereview.appspot.com/14846025

BUG=245543
NOTRY=true

Review URL: https://chromiumcodereview.appspot.com/16544007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204826 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/profiles/profile_helper.cc b/chrome/browser/chromeos/profiles/profile_helper.cc
index 7fc8553..c94cf27 100644
--- a/chrome/browser/chromeos/profiles/profile_helper.cc
+++ b/chrome/browser/chromeos/profiles/profile_helper.cc
@@ -4,6 +4,7 @@
 
 #include "chrome/browser/chromeos/profiles/profile_helper.h"
 
+#include "base/callback.h"
 #include "base/command_line.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/browsing_data/browsing_data_helper.h"
@@ -26,6 +27,12 @@
 }
 
 ProfileHelper::~ProfileHelper() {
+  // Checking whether UserManager is initialized covers case
+  // when ScopedTestUserManager is used.
+  if (UserManager::IsInitialized()) {
+    UserManager::Get()->RemoveObserver(this);
+    UserManager::Get()->RemoveSessionStateObserver(this);
+  }
 }
 
 // static
@@ -108,6 +115,7 @@
 }
 
 void ProfileHelper::Initialize() {
+  UserManager::Get()->AddObserver(this);
   UserManager::Get()->AddSessionStateObserver(this);
 }
 
@@ -136,6 +144,15 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// ProfileHelper, UserManager::Observer implementation:
+
+void ProfileHelper::MergeSessionStateChanged(
+    UserManager::MergeSessionState state) {
+  if (state ==  UserManager:: MERGE_STATUS_DONE)
+    ClearSigninProfile(base::Closure());
+}
+
+////////////////////////////////////////////////////////////////////////////////
 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
 
 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {