Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / gaia_info_update_service.cc
index 7901303..a70a05b 100644 (file)
@@ -4,16 +4,18 @@
 
 #include "chrome/browser/profiles/gaia_info_update_service.h"
 
-#include "base/command_line.h"
 #include "base/prefs/pref_service.h"
+#include "base/strings/utf_string_conversions.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/chrome_notification_types.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/profiles/profile_info_cache.h"
 #include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/profiles/profiles_state.h"
+#include "chrome/browser/signin/signin_manager_factory.h"
 #include "chrome/browser/sync/profile_sync_service.h"
-#include "chrome/common/chrome_switches.h"
 #include "chrome/common/pref_names.h"
+#include "components/signin/core/common/profile_management_switches.h"
 #include "content/public/browser/notification_details.h"
 #include "third_party/skia/include/core/SkBitmap.h"
 #include "ui/gfx/image/image.h"
@@ -27,28 +29,29 @@ const int kUpdateIntervalHours = 24;
 // before starting an update. This avoids slowdown during startup.
 const int kMinUpdateIntervalSeconds = 5;
 
-} // namespace
+}  // namespace
 
 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile)
     : profile_(profile) {
-  PrefService* prefs = profile_->GetPrefs();
-  username_pref_.Init(prefs::kGoogleServicesUsername, prefs,
-                      base::Bind(&GAIAInfoUpdateService::OnUsernameChanged,
-                                 base::Unretained(this)));
+  SigninManagerBase* signin_manager =
+      SigninManagerFactory::GetForProfile(profile_);
+  signin_manager->AddObserver(this);
 
+  PrefService* prefs = profile_->GetPrefs();
   last_updated_ = base::Time::FromInternalValue(
       prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime));
   ScheduleNextUpdate();
 }
 
 GAIAInfoUpdateService::~GAIAInfoUpdateService() {
+  DCHECK(!profile_) << "Shutdown not called before dtor";
 }
 
 void GAIAInfoUpdateService::Update() {
   // The user must be logged in.
-  std::string username = profile_->GetPrefs()->GetString(
-      prefs::kGoogleServicesUsername);
-  if (username.empty())
+  SigninManagerBase* signin_manager =
+      SigninManagerFactory::GetForProfile(profile_);
+  if (!signin_manager->IsAuthenticated())
     return;
 
   if (profile_image_downloader_)
@@ -68,10 +71,8 @@ bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) {
     return false;
 
   // To enable this feature for testing pass "--google-profile-info".
-  if (CommandLine::ForCurrentProcess()->HasSwitch(
-      switches::kGoogleProfileInfo)) {
+  if (switches::IsGoogleProfileInfo())
     return true;
-  }
 
   // This feature is disable by default.
   return false;
@@ -105,8 +106,8 @@ void GAIAInfoUpdateService::OnProfileDownloadSuccess(
                                  last_updated_.ToInternalValue());
   ScheduleNextUpdate();
 
-  string16 full_name = downloader->GetProfileFullName();
-  string16 given_name = downloader->GetProfileGivenName();
+  base::string16 full_name = downloader->GetProfileFullName();
+  base::string16 given_name = downloader->GetProfileGivenName();
   SkBitmap bitmap = downloader->GetProfilePicture();
   ProfileDownloader::PictureStatus picture_status =
       downloader->GetProfilePictureStatus();
@@ -119,12 +120,15 @@ void GAIAInfoUpdateService::OnProfileDownloadSuccess(
     return;
 
   cache.SetGAIANameOfProfileAtIndex(profile_index, full_name);
-  cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name);
+  // The profile index may have changed.
+  profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
+  DCHECK_NE(profile_index, std::string::npos);
 
+  cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name);
   // The profile index may have changed.
   profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
-  if (profile_index == std::string::npos)
-    return;
+  DCHECK_NE(profile_index, std::string::npos);
+
   if (picture_status == ProfileDownloader::PICTURE_SUCCESS) {
     profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
                                     picture_url);
@@ -134,20 +138,10 @@ void GAIAInfoUpdateService::OnProfileDownloadSuccess(
     cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
   }
 
-  // If this profile hasn't switched to using GAIA information for the profile
-  // name and picture then switch it now. Once the profile has switched this
-  // preference guards against clobbering the user's custom settings.
-  if (!cache.GetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index)) {
-    cache.SetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index, true);
-    // Order matters here for shortcut management, like in
-    // ProfileShortcutManagerWin::OnProfileAdded, as the picture update does not
-    // allow us to change the target, so we have to apply any renaming first. We
-    // also need to re-fetch the index, as SetIsUsingGAIANameOfProfileAtIndex
-    // may alter it.
-    cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
-    profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
-    cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
-  }
+  const base::string16 hosted_domain = downloader->GetProfileHostedDomain();
+  profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain,
+      (hosted_domain.empty() ? Profile::kNoHostedDomainFound :
+                               base::UTF16ToUTF8(hosted_domain)));
 }
 
 void GAIAInfoUpdateService::OnProfileDownloadFailure(
@@ -162,18 +156,17 @@ void GAIAInfoUpdateService::OnProfileDownloadFailure(
   ScheduleNextUpdate();
 }
 
-void GAIAInfoUpdateService::OnUsernameChanged() {
+void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) {
   ProfileInfoCache& cache =
       g_browser_process->profile_manager()->GetProfileInfoCache();
   size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
   if (profile_index == std::string::npos)
     return;
 
-  std::string username = profile_->GetPrefs()->GetString(
-      prefs::kGoogleServicesUsername);
   if (username.empty()) {
     // Unset the old user's GAIA info.
-    cache.SetGAIANameOfProfileAtIndex(profile_index, string16());
+    cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16());
+    cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, base::string16());
     // The profile index may have changed.
     profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
     if (profile_index == std::string::npos)
@@ -187,6 +180,19 @@ void GAIAInfoUpdateService::OnUsernameChanged() {
   }
 }
 
+void GAIAInfoUpdateService::Shutdown() {
+  timer_.Stop();
+  profile_image_downloader_.reset();
+  SigninManagerBase* signin_manager =
+      SigninManagerFactory::GetForProfile(profile_);
+  signin_manager->RemoveObserver(this);
+
+  // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not
+  // access it again.  This pointer is also used to implement the delegate for
+  // |profile_image_downloader_|.  However that object was destroyed above.
+  profile_ = NULL;
+}
+
 void GAIAInfoUpdateService::ScheduleNextUpdate() {
   if (timer_.IsRunning())
     return;
@@ -203,3 +209,15 @@ void GAIAInfoUpdateService::ScheduleNextUpdate() {
 
   timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);
 }
+
+void GAIAInfoUpdateService::GoogleSigninSucceeded(
+    const std::string& account_id,
+    const std::string& username,
+    const std::string& password) {
+  OnUsernameChanged(username);
+}
+
+void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id,
+                                            const std::string& username) {
+  OnUsernameChanged(std::string());
+}