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> | 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" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 #include "components/pref_registry/pref_registry_syncable.h" | 13 #include "components/pref_registry/pref_registry_syncable.h" |
14 | 14 |
15 #if defined(OS_ANDROID) | 15 #if defined(OS_ANDROID) |
16 #include "components/gcm_driver/gcm_driver_android.h" | 16 #include "components/gcm_driver/gcm_driver_android.h" |
17 #else | 17 #else |
(...skipping 18 matching lines...) Expand all Loading... |
36 #include "net/url_request/url_request_context_getter.h" | 36 #include "net/url_request/url_request_context_getter.h" |
37 #endif | 37 #endif |
38 | 38 |
39 namespace gcm { | 39 namespace gcm { |
40 | 40 |
41 #if !defined(OS_ANDROID) | 41 #if !defined(OS_ANDROID) |
42 // Identity observer only has actual work to do when the user is actually signed | 42 // Identity observer only has actual work to do when the user is actually signed |
43 // in. It ensures that account tracker is taking | 43 // in. It ensures that account tracker is taking |
44 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { | 44 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { |
45 public: | 45 public: |
46 IdentityObserver(Profile* profile, GCMDriverDesktop* 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 // Called to inform IdentityObserver that a list of accounts was updated. |
56 // |account_tokens| maps email addresses to OAuth2 access tokens. | 56 // |account_tokens| is a list of email addresses, account IDs and OAuth2 |
| 57 // access tokens. |
57 void AccountsUpdated( | 58 void AccountsUpdated( |
58 const std::map<std::string, std::string>& account_tokens); | 59 const std::vector<GCMClient::AccountTokenInfo>& account_tokens); |
59 | 60 |
60 private: | 61 private: |
61 Profile* profile_; | 62 Profile* profile_; |
62 GCMDriverDesktop* driver_; | 63 GCMDriver* driver_; |
63 scoped_ptr<IdentityProvider> identity_provider_; | 64 scoped_ptr<IdentityProvider> identity_provider_; |
64 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; | 65 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; |
65 | 66 |
66 // The account ID that this service is responsible for. Empty when the service | 67 // The account ID that this service is responsible for. Empty when the service |
67 // is not running. | 68 // is not running. |
68 std::string account_id_; | 69 std::string account_id_; |
69 | 70 |
70 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; | 71 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; |
71 | 72 |
72 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | 73 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); |
73 }; | 74 }; |
74 | 75 |
75 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | 76 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, |
76 GCMDriverDesktop* driver) | 77 GCMDriver* driver) |
77 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { | 78 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { |
78 identity_provider_.reset(new ProfileIdentityProvider( | 79 identity_provider_.reset(new ProfileIdentityProvider( |
79 SigninManagerFactory::GetForProfile(profile), | 80 SigninManagerFactory::GetForProfile(profile), |
80 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
81 LoginUIServiceFactory::GetForProfile(profile))); | 82 LoginUIServiceFactory::GetForProfile(profile))); |
82 identity_provider_->AddObserver(this); | 83 identity_provider_->AddObserver(this); |
83 | 84 |
84 OnActiveAccountLogin(); | 85 OnActiveAccountLogin(); |
85 } | 86 } |
86 | 87 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 // again. Otherwise, the user sign-out will not affect the existing GCM | 125 // again. Otherwise, the user sign-out will not affect the existing GCM |
125 // data. | 126 // data. |
126 driver_->OnSignedOut(); | 127 driver_->OnSignedOut(); |
127 } | 128 } |
128 | 129 |
129 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { | 130 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { |
130 return driver_->IsStarted() ? account_id_ : std::string(); | 131 return driver_->IsStarted() ? account_id_ : std::string(); |
131 } | 132 } |
132 | 133 |
133 void GCMProfileService::IdentityObserver::AccountsUpdated( | 134 void GCMProfileService::IdentityObserver::AccountsUpdated( |
134 const std::map<std::string, std::string>& account_tokens) { | 135 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
135 driver_->SetAccountsForCheckin(account_tokens); | 136 driver_->SetAccountTokens(account_tokens); |
136 } | 137 } |
137 #endif // !defined(OS_ANDROID) | 138 #endif // !defined(OS_ANDROID) |
138 | 139 |
139 // static | 140 // static |
140 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 141 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
141 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 142 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
142 } | 143 } |
143 | 144 |
144 // static | 145 // static |
145 void GCMProfileService::RegisterProfilePrefs( | 146 void GCMProfileService::RegisterProfilePrefs( |
(...skipping 25 matching lines...) Expand all Loading... |
171 gcm_client_factory.Pass(), | 172 gcm_client_factory.Pass(), |
172 profile_->GetPrefs(), | 173 profile_->GetPrefs(), |
173 profile_->GetPath().Append(chrome::kGCMStoreDirname), | 174 profile_->GetPath().Append(chrome::kGCMStoreDirname), |
174 profile_->GetRequestContext()); | 175 profile_->GetRequestContext()); |
175 | 176 |
176 #if defined(OS_CHROMEOS) | 177 #if defined(OS_CHROMEOS) |
177 chromeos_connection_observer_.reset(new gcm::ChromeOSGCMConnectionObserver); | 178 chromeos_connection_observer_.reset(new gcm::ChromeOSGCMConnectionObserver); |
178 driver_->AddConnectionObserver(chromeos_connection_observer_.get()); | 179 driver_->AddConnectionObserver(chromeos_connection_observer_.get()); |
179 #endif | 180 #endif |
180 | 181 |
181 identity_observer_.reset(new IdentityObserver( | 182 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); |
182 profile, static_cast<gcm::GCMDriverDesktop*>(driver_.get()))); | |
183 } | 183 } |
184 #endif // defined(OS_ANDROID) | 184 #endif // defined(OS_ANDROID) |
185 | 185 |
186 GCMProfileService::GCMProfileService() | 186 GCMProfileService::GCMProfileService() |
187 : profile_(NULL), | 187 : profile_(NULL), |
188 push_messaging_service_(this, NULL) { | 188 push_messaging_service_(this, NULL) { |
189 } | 189 } |
190 | 190 |
191 GCMProfileService::~GCMProfileService() { | 191 GCMProfileService::~GCMProfileService() { |
192 } | 192 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 return identity_observer_ ? identity_observer_->SignedInUserName() | 230 return identity_observer_ ? identity_observer_->SignedInUserName() |
231 : std::string(); | 231 : std::string(); |
232 #endif // defined(OS_ANDROID) | 232 #endif // defined(OS_ANDROID) |
233 } | 233 } |
234 | 234 |
235 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 235 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
236 driver_.reset(driver); | 236 driver_.reset(driver); |
237 } | 237 } |
238 | 238 |
239 } // namespace gcm | 239 } // namespace gcm |
OLD | NEW |