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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 public: | 45 public: |
46 IdentityObserver(Profile* profile, GCMDriver* driver); | 46 IdentityObserver(Profile* profile, GCMDriver* driver); |
47 virtual ~IdentityObserver(); | 47 virtual ~IdentityObserver(); |
48 | 48 |
49 // IdentityProvider::Observer: | 49 // IdentityProvider::Observer: |
50 virtual void OnActiveAccountLogin() override; | 50 virtual void OnActiveAccountLogin() override; |
51 virtual void OnActiveAccountLogout() override; | 51 virtual void OnActiveAccountLogout() override; |
52 | 52 |
53 std::string SignedInUserName() const; | 53 std::string SignedInUserName() const; |
54 | 54 |
55 // Called to inform IdentityObserver that a list of accounts was updated. | 55 private: |
56 // |account_tokens| is a list of email addresses, account IDs and OAuth2 | 56 void StartAccountTracker(); |
57 // access tokens. | |
58 void AccountsUpdated( | |
59 const std::vector<GCMClient::AccountTokenInfo>& account_tokens); | |
60 | 57 |
61 private: | |
62 Profile* profile_; | 58 Profile* profile_; |
63 GCMDriver* driver_; | 59 GCMDriver* driver_; |
64 scoped_ptr<IdentityProvider> identity_provider_; | 60 scoped_ptr<IdentityProvider> identity_provider_; |
65 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; | 61 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; |
66 | 62 |
67 // 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 |
68 // is not running. | 64 // is not running. |
69 std::string account_id_; | 65 std::string account_id_; |
70 | 66 |
71 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; | 67 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; |
72 | 68 |
73 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | 69 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); |
74 }; | 70 }; |
75 | 71 |
76 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | 72 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, |
77 GCMDriver* driver) | 73 GCMDriver* driver) |
78 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { | 74 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { |
79 identity_provider_.reset(new ProfileIdentityProvider( | 75 identity_provider_.reset(new ProfileIdentityProvider( |
80 SigninManagerFactory::GetForProfile(profile), | 76 SigninManagerFactory::GetForProfile(profile), |
81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 77 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
82 LoginUIServiceFactory::GetForProfile(profile))); | 78 LoginUIServiceFactory::GetForProfile(profile))); |
83 identity_provider_->AddObserver(this); | 79 identity_provider_->AddObserver(this); |
84 | 80 |
85 OnActiveAccountLogin(); | 81 OnActiveAccountLogin(); |
| 82 StartAccountTracker(); |
86 } | 83 } |
87 | 84 |
88 GCMProfileService::IdentityObserver::~IdentityObserver() { | 85 GCMProfileService::IdentityObserver::~IdentityObserver() { |
89 if (gcm_account_tracker_) | 86 if (gcm_account_tracker_) |
90 gcm_account_tracker_->Shutdown(); | 87 gcm_account_tracker_->Shutdown(); |
91 identity_provider_->RemoveObserver(this); | 88 identity_provider_->RemoveObserver(this); |
92 } | 89 } |
93 | 90 |
94 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { | 91 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { |
95 // This might be called multiple times when the password changes. | 92 // This might be called multiple times when the password changes. |
96 const std::string account_id = identity_provider_->GetActiveAccountId(); | 93 const std::string account_id = identity_provider_->GetActiveAccountId(); |
97 if (account_id == account_id_) | 94 if (account_id == account_id_) |
98 return; | 95 return; |
| 96 |
99 account_id_ = account_id; | 97 account_id_ = account_id; |
100 | |
101 driver_->OnSignedIn(); | 98 driver_->OnSignedIn(); |
102 | |
103 if (!gcm_account_tracker_) { | |
104 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( | |
105 new gaia::AccountTracker(identity_provider_.get(), | |
106 profile_->GetRequestContext())); | |
107 | |
108 gcm_account_tracker_.reset(new GCMAccountTracker( | |
109 gaia_account_tracker.Pass(), | |
110 base::Bind(&GCMProfileService::IdentityObserver::AccountsUpdated, | |
111 weak_ptr_factory_.GetWeakPtr()))); | |
112 } | |
113 | |
114 gcm_account_tracker_->Start(); | |
115 } | 99 } |
116 | 100 |
117 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { | 101 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { |
118 account_id_.clear(); | 102 account_id_.clear(); |
119 | 103 |
120 // Check is necessary to not crash browser_tests. | |
121 if (gcm_account_tracker_) | |
122 gcm_account_tracker_->Stop(); | |
123 // When sign-in enforcement is not dropped, OnSignedOut will also clear all | 104 // When sign-in enforcement is not dropped, OnSignedOut will also clear all |
124 // the GCM data and a new GCM ID will be retrieved after the user signs in | 105 // the GCM data and a new GCM ID will be retrieved after the user signs in |
125 // again. Otherwise, the user sign-out will not affect the existing GCM | 106 // again. Otherwise, the user sign-out will not affect the existing GCM |
126 // data. | 107 // data. |
127 driver_->OnSignedOut(); | 108 driver_->OnSignedOut(); |
128 } | 109 } |
129 | 110 |
130 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { | 111 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { |
131 return driver_->IsStarted() ? account_id_ : std::string(); | 112 return driver_->IsStarted() ? account_id_ : std::string(); |
132 } | 113 } |
133 | 114 |
134 void GCMProfileService::IdentityObserver::AccountsUpdated( | 115 void GCMProfileService::IdentityObserver::StartAccountTracker() { |
135 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 116 if (gcm_account_tracker_) |
136 driver_->SetAccountTokens(account_tokens); | 117 return; |
| 118 |
| 119 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( |
| 120 new gaia::AccountTracker(identity_provider_.get(), |
| 121 profile_->GetRequestContext())); |
| 122 |
| 123 gcm_account_tracker_.reset( |
| 124 new GCMAccountTracker(gaia_account_tracker.Pass(), driver_)); |
| 125 |
| 126 gcm_account_tracker_->Start(); |
137 } | 127 } |
| 128 |
138 #endif // !defined(OS_ANDROID) | 129 #endif // !defined(OS_ANDROID) |
139 | 130 |
140 // static | 131 // static |
141 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 132 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
142 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 133 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
143 } | 134 } |
144 | 135 |
145 // static | 136 // static |
146 void GCMProfileService::RegisterProfilePrefs( | 137 void GCMProfileService::RegisterProfilePrefs( |
147 user_prefs::PrefRegistrySyncable* registry) { | 138 user_prefs::PrefRegistrySyncable* registry) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 #if defined(OS_ANDROID) | 218 #if defined(OS_ANDROID) |
228 return std::string(); | 219 return std::string(); |
229 #else | 220 #else |
230 return identity_observer_ ? identity_observer_->SignedInUserName() | 221 return identity_observer_ ? identity_observer_->SignedInUserName() |
231 : std::string(); | 222 : std::string(); |
232 #endif // defined(OS_ANDROID) | 223 #endif // defined(OS_ANDROID) |
233 } | 224 } |
234 | 225 |
235 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 226 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
236 driver_.reset(driver); | 227 driver_.reset(driver); |
| 228 #if !defined(OS_ANDROID) |
| 229 if (identity_observer_) |
| 230 identity_observer_.reset(new IdentityObserver(profile_, driver)); |
| 231 #endif // !defined(OS_ANDROID) |
237 } | 232 } |
238 | 233 |
239 } // namespace gcm | 234 } // namespace gcm |
OLD | NEW |