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 |
| 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 // |account_removed| indicates whether an account has been removed since the |
| 55 // last time the callback was called. |
| 56 void AccountsUpdated(const std::map<std::string, std::string>& account_tokens, |
| 57 bool removed_account); |
| 58 |
43 private: | 59 private: |
44 GCMDriver* driver_; | 60 Profile* profile_; |
| 61 GCMDriverDesktop* driver_; |
| 62 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; |
45 scoped_ptr<IdentityProvider> identity_provider_; | 63 scoped_ptr<IdentityProvider> identity_provider_; |
46 | 64 |
47 // The account ID that this service is responsible for. Empty when the service | 65 // The account ID that this service is responsible for. Empty when the service |
48 // is not running. | 66 // is not running. |
49 std::string account_id_; | 67 std::string account_id_; |
50 | 68 |
| 69 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; |
| 70 |
51 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | 71 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); |
52 }; | 72 }; |
53 | 73 |
54 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | 74 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, |
55 GCMDriver* driver) | 75 GCMDriverDesktop* driver) |
56 : driver_(driver) { | 76 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { |
57 identity_provider_.reset(new ProfileIdentityProvider( | 77 identity_provider_.reset(new ProfileIdentityProvider( |
58 SigninManagerFactory::GetForProfile(profile), | 78 SigninManagerFactory::GetForProfile(profile), |
59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | 79 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), |
60 LoginUIServiceFactory::GetForProfile(profile))); | 80 LoginUIServiceFactory::GetForProfile(profile))); |
61 identity_provider_->AddObserver(this); | 81 identity_provider_->AddObserver(this); |
62 | 82 |
63 OnActiveAccountLogin(); | 83 OnActiveAccountLogin(); |
64 } | 84 } |
65 | 85 |
66 GCMProfileService::IdentityObserver::~IdentityObserver() { | 86 GCMProfileService::IdentityObserver::~IdentityObserver() { |
| 87 gcm_account_tracker_->Shutdown(); |
67 identity_provider_->RemoveObserver(this); | 88 identity_provider_->RemoveObserver(this); |
68 } | 89 } |
69 | 90 |
70 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { | 91 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { |
71 // This might be called multiple times when the password changes. | 92 // This might be called multiple times when the password changes. |
72 const std::string account_id = identity_provider_->GetActiveAccountId(); | 93 const std::string account_id = identity_provider_->GetActiveAccountId(); |
73 if (account_id == account_id_) | 94 if (account_id == account_id_) |
74 return; | 95 return; |
75 account_id_ = account_id; | 96 account_id_ = account_id; |
76 | 97 |
77 driver_->OnSignedIn(); | 98 driver_->OnSignedIn(); |
| 99 |
| 100 if (!gcm_account_tracker_) { |
| 101 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( |
| 102 new gaia::AccountTracker(identity_provider_.get(), |
| 103 profile_->GetRequestContext())); |
| 104 |
| 105 gcm_account_tracker_.reset(new GCMAccountTracker( |
| 106 gaia_account_tracker.Pass(), |
| 107 base::Bind(&GCMProfileService::IdentityObserver::AccountsUpdated, |
| 108 weak_ptr_factory_.GetWeakPtr()))); |
| 109 } |
| 110 |
| 111 gcm_account_tracker_->Start(); |
78 } | 112 } |
79 | 113 |
80 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { | 114 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { |
| 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 bool account_removed) { |
| 128 driver_->SetAccountsForCheckin(account_tokens, account_removed); |
| 129 } |
87 #endif // !defined(OS_ANDROID) | 130 #endif // !defined(OS_ANDROID) |
88 | 131 |
89 // static | 132 // static |
90 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 133 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
91 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 134 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
92 } | 135 } |
93 | 136 |
94 // static | 137 // static |
95 void GCMProfileService::RegisterProfilePrefs( | 138 void GCMProfileService::RegisterProfilePrefs( |
96 user_prefs::PrefRegistrySyncable* registry) { | 139 user_prefs::PrefRegistrySyncable* registry) { |
(...skipping 18 matching lines...) Expand all Loading... |
115 scoped_ptr<GCMClientFactory> gcm_client_factory) | 158 scoped_ptr<GCMClientFactory> gcm_client_factory) |
116 : profile_(profile), | 159 : profile_(profile), |
117 push_messaging_service_(this, profile) { | 160 push_messaging_service_(this, profile) { |
118 DCHECK(!profile->IsOffTheRecord()); | 161 DCHECK(!profile->IsOffTheRecord()); |
119 | 162 |
120 driver_ = CreateGCMDriverDesktop( | 163 driver_ = CreateGCMDriverDesktop( |
121 gcm_client_factory.Pass(), | 164 gcm_client_factory.Pass(), |
122 profile_->GetPath().Append(chrome::kGCMStoreDirname), | 165 profile_->GetPath().Append(chrome::kGCMStoreDirname), |
123 profile_->GetRequestContext()); | 166 profile_->GetRequestContext()); |
124 | 167 |
125 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); | 168 identity_observer_.reset(new IdentityObserver( |
| 169 profile, static_cast<gcm::GCMDriverDesktop*>(driver_.get()))); |
126 } | 170 } |
127 #endif // defined(OS_ANDROID) | 171 #endif // defined(OS_ANDROID) |
128 | 172 |
129 GCMProfileService::GCMProfileService() | 173 GCMProfileService::GCMProfileService() |
130 : profile_(NULL), | 174 : profile_(NULL), |
131 push_messaging_service_(this, NULL) { | 175 push_messaging_service_(this, NULL) { |
132 } | 176 } |
133 | 177 |
134 GCMProfileService::~GCMProfileService() { | 178 GCMProfileService::~GCMProfileService() { |
135 } | 179 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 return identity_observer_ ? identity_observer_->SignedInUserName() | 214 return identity_observer_ ? identity_observer_->SignedInUserName() |
171 : std::string(); | 215 : std::string(); |
172 #endif // defined(OS_ANDROID) | 216 #endif // defined(OS_ANDROID) |
173 } | 217 } |
174 | 218 |
175 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 219 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
176 driver_.reset(driver); | 220 driver_.reset(driver); |
177 } | 221 } |
178 | 222 |
179 } // namespace gcm | 223 } // namespace gcm |
OLD | NEW |