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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | 21 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 22 #include "chrome/common/chrome_constants.h" | 22 #include "chrome/common/chrome_constants.h" |
| 23 #include "components/gcm_driver/gcm_client_factory.h" | 23 #include "components/gcm_driver/gcm_client_factory.h" |
| 24 #include "components/signin/core/browser/signin_manager.h" | 24 #include "components/signin/core/browser/signin_manager.h" |
| 25 #include "google_apis/gaia/identity_provider.h" | 25 #include "google_apis/gaia/identity_provider.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 namespace gcm { | 29 namespace gcm { |
| 30 | 30 |
| 31 #if !defined(OS_ANDROID) | |
| 32 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { | |
| 33 public: | |
| 34 IdentityObserver(Profile* profile, GCMDriver* driver); | |
| 35 virtual ~IdentityObserver(); | |
| 36 | |
| 37 // IdentityProvider::Observer: | |
| 38 virtual void OnActiveAccountLogin() OVERRIDE; | |
| 39 virtual void OnActiveAccountLogout() OVERRIDE; | |
| 40 | |
| 41 std::string SignedInUserName() const; | |
| 42 | |
| 43 private: | |
| 44 GCMDriver* driver_; | |
| 45 scoped_ptr<IdentityProvider> identity_provider_; | |
| 46 | |
| 47 // The account ID that this service is responsible for. Empty when the service | |
| 48 // is not running. | |
| 49 std::string account_id_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | |
| 52 }; | |
| 53 | |
| 54 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | |
| 55 GCMDriver* driver) | |
| 56 : driver_(driver) { | |
| 57 identity_provider_.reset(new ProfileIdentityProvider( | |
| 58 SigninManagerFactory::GetForProfile(profile), | |
| 59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | |
| 60 LoginUIServiceFactory::GetForProfile(profile))); | |
| 61 identity_provider_->AddObserver(this); | |
| 62 | |
| 63 OnActiveAccountLogin(); | |
| 64 } | |
| 65 | |
| 66 GCMProfileService::IdentityObserver::~IdentityObserver() { | |
| 67 if (identity_provider_) | |
|
bartfab (slow)
2014/06/16 09:17:06
How could |identity_provider_| ever be NULL? There
jianli
2014/06/16 16:52:06
Removed.
| |
| 68 identity_provider_->RemoveObserver(this); | |
| 69 } | |
| 70 | |
| 71 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { | |
| 72 // This might be called multiple times when the password changes. | |
| 73 const std::string account_id = identity_provider_->GetActiveAccountId(); | |
| 74 if (account_id == account_id_) | |
| 75 return; | |
| 76 account_id_ = account_id; | |
| 77 | |
| 78 driver_->OnSignedIn(); | |
| 79 } | |
| 80 | |
| 81 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { | |
| 82 driver_->Purge(); | |
| 83 } | |
| 84 | |
| 85 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { | |
| 86 return driver_->IsStarted() ? account_id_ : std::string(); | |
| 87 } | |
| 88 #endif // !defined(OS_ANDROID) | |
| 89 | |
| 31 // static | 90 // static |
| 32 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 91 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
| 33 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 92 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
| 34 } | 93 } |
| 35 | 94 |
| 36 // static | 95 // static |
| 37 void GCMProfileService::RegisterProfilePrefs( | 96 void GCMProfileService::RegisterProfilePrefs( |
| 38 user_prefs::PrefRegistrySyncable* registry) { | 97 user_prefs::PrefRegistrySyncable* registry) { |
| 39 registry->RegisterBooleanPref( | 98 registry->RegisterBooleanPref( |
| 40 prefs::kGCMChannelEnabled, | 99 prefs::kGCMChannelEnabled, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 53 #else | 112 #else |
| 54 GCMProfileService::GCMProfileService( | 113 GCMProfileService::GCMProfileService( |
| 55 Profile* profile, | 114 Profile* profile, |
| 56 scoped_ptr<GCMClientFactory> gcm_client_factory) | 115 scoped_ptr<GCMClientFactory> gcm_client_factory) |
| 57 : profile_(profile), | 116 : profile_(profile), |
| 58 push_messaging_service_(this) { | 117 push_messaging_service_(this) { |
| 59 DCHECK(!profile->IsOffTheRecord()); | 118 DCHECK(!profile->IsOffTheRecord()); |
| 60 | 119 |
| 61 driver_ = CreateGCMDriverDesktop( | 120 driver_ = CreateGCMDriverDesktop( |
| 62 gcm_client_factory.Pass(), | 121 gcm_client_factory.Pass(), |
| 63 scoped_ptr<IdentityProvider>(new ProfileIdentityProvider( | |
| 64 SigninManagerFactory::GetForProfile(profile_), | |
| 65 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), | |
| 66 LoginUIServiceFactory::GetForProfile(profile_))), | |
| 67 profile_->GetPath().Append(chrome::kGCMStoreDirname), | 122 profile_->GetPath().Append(chrome::kGCMStoreDirname), |
| 68 profile_->GetRequestContext()); | 123 profile_->GetRequestContext()); |
| 124 | |
| 125 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); | |
| 69 } | 126 } |
| 70 #endif // defined(OS_ANDROID) | 127 #endif // defined(OS_ANDROID) |
| 71 | 128 |
| 72 GCMProfileService::GCMProfileService() | 129 GCMProfileService::GCMProfileService() |
| 73 : profile_(NULL), | 130 : profile_(NULL), |
| 74 push_messaging_service_(this) { | 131 push_messaging_service_(this) { |
| 75 } | 132 } |
| 76 | 133 |
| 77 GCMProfileService::~GCMProfileService() { | 134 GCMProfileService::~GCMProfileService() { |
| 78 } | 135 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 89 } | 146 } |
| 90 | 147 |
| 91 void GCMProfileService::Register(const std::string& app_id, | 148 void GCMProfileService::Register(const std::string& app_id, |
| 92 const std::vector<std::string>& sender_ids, | 149 const std::vector<std::string>& sender_ids, |
| 93 const GCMDriver::RegisterCallback& callback) { | 150 const GCMDriver::RegisterCallback& callback) { |
| 94 if (driver_) | 151 if (driver_) |
| 95 driver_->Register(app_id, sender_ids, callback); | 152 driver_->Register(app_id, sender_ids, callback); |
| 96 } | 153 } |
| 97 | 154 |
| 98 void GCMProfileService::Shutdown() { | 155 void GCMProfileService::Shutdown() { |
| 156 #if !defined(OS_ANDROID) | |
| 157 identity_observer_.reset(); | |
| 158 #endif // !defined(OS_ANDROID) | |
| 159 | |
| 99 if (driver_) { | 160 if (driver_) { |
| 100 driver_->Shutdown(); | 161 driver_->Shutdown(); |
| 101 driver_.reset(); | 162 driver_.reset(); |
| 102 } | 163 } |
| 103 } | 164 } |
| 104 | 165 |
| 166 std::string GCMProfileService::SignedInUserName() const { | |
| 167 #if defined(OS_ANDROID) | |
| 168 return std::string(); | |
| 169 #else | |
| 170 return identity_observer_ ? identity_observer_->SignedInUserName() | |
| 171 : std::string(); | |
| 172 #endif // defined(OS_ANDROID) | |
| 173 } | |
| 174 | |
| 105 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 175 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
| 106 driver_.reset(driver); | 176 driver_.reset(driver); |
| 107 } | 177 } |
| 108 | 178 |
| 109 } // namespace gcm | 179 } // namespace gcm |
| OLD | NEW |