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 identity_provider_->RemoveObserver(this); |
| 68 } |
| 69 |
| 70 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { |
| 71 // This might be called multiple times when the password changes. |
| 72 const std::string account_id = identity_provider_->GetActiveAccountId(); |
| 73 if (account_id == account_id_) |
| 74 return; |
| 75 account_id_ = account_id; |
| 76 |
| 77 driver_->OnSignedIn(); |
| 78 } |
| 79 |
| 80 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { |
| 81 driver_->Purge(); |
| 82 } |
| 83 |
| 84 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { |
| 85 return driver_->IsStarted() ? account_id_ : std::string(); |
| 86 } |
| 87 #endif // !defined(OS_ANDROID) |
| 88 |
31 // static | 89 // static |
32 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | 90 bool GCMProfileService::IsGCMEnabled(Profile* profile) { |
33 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); | 91 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); |
34 } | 92 } |
35 | 93 |
36 // static | 94 // static |
37 void GCMProfileService::RegisterProfilePrefs( | 95 void GCMProfileService::RegisterProfilePrefs( |
38 user_prefs::PrefRegistrySyncable* registry) { | 96 user_prefs::PrefRegistrySyncable* registry) { |
39 registry->RegisterBooleanPref( | 97 registry->RegisterBooleanPref( |
40 prefs::kGCMChannelEnabled, | 98 prefs::kGCMChannelEnabled, |
(...skipping 12 matching lines...) Expand all Loading... |
53 #else | 111 #else |
54 GCMProfileService::GCMProfileService( | 112 GCMProfileService::GCMProfileService( |
55 Profile* profile, | 113 Profile* profile, |
56 scoped_ptr<GCMClientFactory> gcm_client_factory) | 114 scoped_ptr<GCMClientFactory> gcm_client_factory) |
57 : profile_(profile), | 115 : profile_(profile), |
58 push_messaging_service_(this) { | 116 push_messaging_service_(this) { |
59 DCHECK(!profile->IsOffTheRecord()); | 117 DCHECK(!profile->IsOffTheRecord()); |
60 | 118 |
61 driver_ = CreateGCMDriverDesktop( | 119 driver_ = CreateGCMDriverDesktop( |
62 gcm_client_factory.Pass(), | 120 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), | 121 profile_->GetPath().Append(chrome::kGCMStoreDirname), |
68 profile_->GetRequestContext()); | 122 profile_->GetRequestContext()); |
| 123 |
| 124 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); |
69 } | 125 } |
70 #endif // defined(OS_ANDROID) | 126 #endif // defined(OS_ANDROID) |
71 | 127 |
72 GCMProfileService::GCMProfileService() | 128 GCMProfileService::GCMProfileService() |
73 : profile_(NULL), | 129 : profile_(NULL), |
74 push_messaging_service_(this) { | 130 push_messaging_service_(this) { |
75 } | 131 } |
76 | 132 |
77 GCMProfileService::~GCMProfileService() { | 133 GCMProfileService::~GCMProfileService() { |
78 } | 134 } |
(...skipping 10 matching lines...) Expand all Loading... |
89 } | 145 } |
90 | 146 |
91 void GCMProfileService::Register(const std::string& app_id, | 147 void GCMProfileService::Register(const std::string& app_id, |
92 const std::vector<std::string>& sender_ids, | 148 const std::vector<std::string>& sender_ids, |
93 const GCMDriver::RegisterCallback& callback) { | 149 const GCMDriver::RegisterCallback& callback) { |
94 if (driver_) | 150 if (driver_) |
95 driver_->Register(app_id, sender_ids, callback); | 151 driver_->Register(app_id, sender_ids, callback); |
96 } | 152 } |
97 | 153 |
98 void GCMProfileService::Shutdown() { | 154 void GCMProfileService::Shutdown() { |
| 155 #if !defined(OS_ANDROID) |
| 156 identity_observer_.reset(); |
| 157 #endif // !defined(OS_ANDROID) |
| 158 |
99 if (driver_) { | 159 if (driver_) { |
100 driver_->Shutdown(); | 160 driver_->Shutdown(); |
101 driver_.reset(); | 161 driver_.reset(); |
102 } | 162 } |
103 } | 163 } |
104 | 164 |
| 165 std::string GCMProfileService::SignedInUserName() const { |
| 166 #if defined(OS_ANDROID) |
| 167 return std::string(); |
| 168 #else |
| 169 return identity_observer_ ? identity_observer_->SignedInUserName() |
| 170 : std::string(); |
| 171 #endif // defined(OS_ANDROID) |
| 172 } |
| 173 |
105 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | 174 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { |
106 driver_.reset(driver); | 175 driver_.reset(driver); |
107 } | 176 } |
108 | 177 |
109 } // namespace gcm | 178 } // namespace gcm |
OLD | NEW |