Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 11 #include "components/pref_registry/pref_registry_syncable.h" | 13 #include "components/pref_registry/pref_registry_syncable.h" |
| 12 | 14 |
| 13 #if defined(OS_ANDROID) | 15 #if defined(OS_ANDROID) |
| 14 #include "components/gcm_driver/gcm_driver_android.h" | 16 #include "components/gcm_driver/gcm_driver_android.h" |
| 15 #else | 17 #else |
| 18 #include "base/bind.h" | |
| 16 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 20 #include "base/memory/weak_ptr.h" | |
| 21 #include "chrome/browser/services/gcm/gcm_account_tracker.h" | |
| 17 #include "chrome/browser/services/gcm/gcm_desktop_utils.h" | 22 #include "chrome/browser/services/gcm/gcm_desktop_utils.h" |
| 18 #include "chrome/browser/signin/profile_identity_provider.h" | 23 #include "chrome/browser/signin/profile_identity_provider.h" |
| 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 20 #include "chrome/browser/signin/signin_manager_factory.h" | 25 #include "chrome/browser/signin/signin_manager_factory.h" |
| 21 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | 26 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 22 #include "chrome/common/chrome_constants.h" | 27 #include "chrome/common/chrome_constants.h" |
| 23 #include "components/gcm_driver/gcm_client_factory.h" | 28 #include "components/gcm_driver/gcm_client_factory.h" |
| 29 #include "components/gcm_driver/gcm_driver_desktop.h" | |
| 24 #include "components/signin/core/browser/signin_manager.h" | 30 #include "components/signin/core/browser/signin_manager.h" |
| 31 #include "google_apis/gaia/account_tracker.h" | |
| 25 #include "google_apis/gaia/identity_provider.h" | 32 #include "google_apis/gaia/identity_provider.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 33 #include "net/url_request/url_request_context_getter.h" |
| 27 #endif | 34 #endif |
| 28 | 35 |
| 29 namespace gcm { | 36 namespace gcm { |
| 30 | 37 |
| 31 #if !defined(OS_ANDROID) | 38 #if !defined(OS_ANDROID) |
| 39 // Identity observer only has actual work to do when the user is acutally signed | |
|
Nicolas Zea
2014/07/16 20:53:54
nit: acutally
fgorski
2014/07/17 03:35:34
Done.
| |
| 40 // in. It ensures that account tracker is taking | |
| 32 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { | 41 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { |
| 33 public: | 42 public: |
| 34 IdentityObserver(Profile* profile, GCMDriver* driver); | 43 IdentityObserver(Profile* profile, GCMDriverDesktop* driver); |
| 35 virtual ~IdentityObserver(); | 44 virtual ~IdentityObserver(); |
| 36 | 45 |
| 37 // IdentityProvider::Observer: | 46 // IdentityProvider::Observer: |
| 38 virtual void OnActiveAccountLogin() OVERRIDE; | 47 virtual void OnActiveAccountLogin() OVERRIDE; |
| 39 virtual void OnActiveAccountLogout() OVERRIDE; | 48 virtual void OnActiveAccountLogout() OVERRIDE; |
| 40 | 49 |
| 41 std::string SignedInUserName() const; | 50 std::string SignedInUserName() const; |
| 42 | 51 |
| 52 // Called to inform IdentityObserver that a list of accounts was updated. | |
| 53 // |account_tokens| maps email addresses to OAuth2 access tokens. | |
| 54 void AccountsUpdated( | |
| 55 const std::map<std::string, std::string>& account_tokens); | |
| 56 | |
| 43 private: | 57 private: |
| 44 GCMDriver* driver_; | 58 Profile* profile_; |
| 59 GCMDriverDesktop* driver_; | |
| 45 scoped_ptr<IdentityProvider> identity_provider_; | 60 scoped_ptr<IdentityProvider> identity_provider_; |
| 61 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; | |
| 46 | 62 |
| 47 // The account ID that this service is responsible for. Empty when the service | 63 // The account ID that this service is responsible for. Empty when the service |
| 48 // is not running. | 64 // is not running. |
| 49 std::string account_id_; | 65 std::string account_id_; |
| 50 | 66 |
| 67 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; | |
| 68 | |
| 51 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | 69 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); |
| 52 }; | 70 }; |
| 53 | 71 |
| 54 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | 72 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, |
| 55 GCMDriver* driver) | 73 GCMDriverDesktop* driver) |
| 56 : driver_(driver) { | 74 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { |
| 57 identity_provider_.reset(new ProfileIdentityProvider( | 75 identity_provider_.reset(new ProfileIdentityProvider( |
| 58 SigninManagerFactory::GetForProfile(profile), | 76 SigninManagerFactory::GetForProfile(profile), |
| 59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 77 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
| 60 LoginUIServiceFactory::GetForProfile(profile))); | 78 LoginUIServiceFactory::GetForProfile(profile))); |
| 61 identity_provider_->AddObserver(this); | 79 identity_provider_->AddObserver(this); |
| 62 | 80 |
| 63 OnActiveAccountLogin(); | 81 OnActiveAccountLogin(); |
| 64 } | 82 } |
| 65 | 83 |
| 66 GCMProfileService::IdentityObserver::~IdentityObserver() { | 84 GCMProfileService::IdentityObserver::~IdentityObserver() { |
| 85 if (gcm_account_tracker_) | |
| 86 gcm_account_tracker_->Shutdown(); | |
| 67 identity_provider_->RemoveObserver(this); | 87 identity_provider_->RemoveObserver(this); |
| 68 } | 88 } |
| 69 | 89 |
| 70 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { | 90 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { |
| 71 // This might be called multiple times when the password changes. | 91 // This might be called multiple times when the password changes. |
| 72 const std::string account_id = identity_provider_->GetActiveAccountId(); | 92 const std::string account_id = identity_provider_->GetActiveAccountId(); |
| 73 if (account_id == account_id_) | 93 if (account_id == account_id_) |
| 74 return; | 94 return; |
| 75 account_id_ = account_id; | 95 account_id_ = account_id; |
| 76 | 96 |
| 77 driver_->OnSignedIn(); | 97 driver_->OnSignedIn(); |
| 98 | |
| 99 if (!gcm_account_tracker_) { | |
| 100 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( | |
| 101 new gaia::AccountTracker(identity_provider_.get(), | |
| 102 profile_->GetRequestContext())); | |
| 103 | |
| 104 gcm_account_tracker_.reset(new GCMAccountTracker( | |
| 105 gaia_account_tracker.Pass(), | |
| 106 base::Bind(&GCMProfileService::IdentityObserver::AccountsUpdated, | |
| 107 weak_ptr_factory_.GetWeakPtr()))); | |
| 108 } | |
| 109 | |
| 110 gcm_account_tracker_->Start(); | |
| 78 } | 111 } |
| 79 | 112 |
| 80 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { | 113 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { |
| 114 if (gcm_account_tracker_) | |
|
Nicolas Zea
2014/07/16 20:53:54
Shouldn't we always have an account tracker at thi
fgorski
2014/07/17 03:35:34
Replacing with DCHECK and starting trybots.
| |
| 115 gcm_account_tracker_->Stop(); | |
| 116 // TODO(fgorski): If we purge here, what should happen when we get | |
| 117 // OnActiveAccountLogin() right after that? | |
| 81 driver_->Purge(); | 118 driver_->Purge(); |
| 82 } | 119 } |
| 83 | 120 |
| 84 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { | 121 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { |
| 85 return driver_->IsStarted() ? account_id_ : std::string(); | 122 return driver_->IsStarted() ? account_id_ : std::string(); |
| 86 } | 123 } |
| 124 | |
| 125 void GCMProfileService::IdentityObserver::AccountsUpdated( | |
| 126 const std::map<std::string, std::string>& account_tokens) { | |
| 127 driver_->SetAccountsForCheckin(account_tokens); | |
| 128 } | |
| 87 #endif // !defined(OS_ANDROID) | 129 #endif // !defined(OS_ANDROID) |
| 88 | 130 |
| 89 // static | 131 // static |
| 90 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 132 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
| 91 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 133 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
| 92 } | 134 } |
| 93 | 135 |
| 94 // static | 136 // static |
| 95 void GCMProfileService::RegisterProfilePrefs( | 137 void GCMProfileService::RegisterProfilePrefs( |
| 96 user_prefs::PrefRegistrySyncable* registry) { | 138 user_prefs::PrefRegistrySyncable* registry) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 115 scoped_ptr<GCMClientFactory> gcm_client_factory) | 157 scoped_ptr<GCMClientFactory> gcm_client_factory) |
| 116 : profile_(profile), | 158 : profile_(profile), |
| 117 push_messaging_service_(this, profile) { | 159 push_messaging_service_(this, profile) { |
| 118 DCHECK(!profile->IsOffTheRecord()); | 160 DCHECK(!profile->IsOffTheRecord()); |
| 119 | 161 |
| 120 driver_ = CreateGCMDriverDesktop( | 162 driver_ = CreateGCMDriverDesktop( |
| 121 gcm_client_factory.Pass(), | 163 gcm_client_factory.Pass(), |
| 122 profile_->GetPath().Append(chrome::kGCMStoreDirname), | 164 profile_->GetPath().Append(chrome::kGCMStoreDirname), |
| 123 profile_->GetRequestContext()); | 165 profile_->GetRequestContext()); |
| 124 | 166 |
| 125 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); | 167 identity_observer_.reset(new IdentityObserver( |
| 168 profile, static_cast<gcm::GCMDriverDesktop*>(driver_.get()))); | |
| 126 } | 169 } |
| 127 #endif // defined(OS_ANDROID) | 170 #endif // defined(OS_ANDROID) |
| 128 | 171 |
| 129 GCMProfileService::GCMProfileService() | 172 GCMProfileService::GCMProfileService() |
| 130 : profile_(NULL), | 173 : profile_(NULL), |
| 131 push_messaging_service_(this, NULL) { | 174 push_messaging_service_(this, NULL) { |
| 132 } | 175 } |
| 133 | 176 |
| 134 GCMProfileService::~GCMProfileService() { | 177 GCMProfileService::~GCMProfileService() { |
| 135 } | 178 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 return identity_observer_ ? identity_observer_->SignedInUserName() | 213 return identity_observer_ ? identity_observer_->SignedInUserName() |
| 171 : std::string(); | 214 : std::string(); |
| 172 #endif // defined(OS_ANDROID) | 215 #endif // defined(OS_ANDROID) |
| 173 } | 216 } |
| 174 | 217 |
| 175 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 218 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
| 176 driver_.reset(driver); | 219 driver_.reset(driver); |
| 177 } | 220 } |
| 178 | 221 |
| 179 } // namespace gcm | 222 } // namespace gcm |
| OLD | NEW |