| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/prefs/pref_service.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/common/chrome_constants.h" | |
| 13 #include "components/gcm_driver/gcm_driver.h" | |
| 14 #include "components/pref_registry/pref_registry_syncable.h" | |
| 15 | |
| 16 #if defined(OS_ANDROID) | |
| 17 #include "base/sequenced_task_runner.h" | |
| 18 #include "base/threading/sequenced_worker_pool.h" | |
| 19 #include "components/gcm_driver/gcm_driver_android.h" | |
| 20 #include "content/public/browser/browser_thread.h" | |
| 21 #else | |
| 22 #include "base/bind.h" | |
| 23 #include "base/files/file_path.h" | |
| 24 #include "base/memory/weak_ptr.h" | |
| 25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 26 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 27 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 28 #include "chrome/common/channel_info.h" | |
| 29 #include "components/gcm_driver/gcm_account_tracker.h" | |
| 30 #include "components/gcm_driver/gcm_channel_status_syncer.h" | |
| 31 #include "components/gcm_driver/gcm_client_factory.h" | |
| 32 #include "components/gcm_driver/gcm_desktop_utils.h" | |
| 33 #include "components/gcm_driver/gcm_driver_desktop.h" | |
| 34 #include "components/signin/core/browser/profile_identity_provider.h" | |
| 35 #include "components/signin/core/browser/signin_manager.h" | |
| 36 #include "content/public/browser/browser_thread.h" | |
| 37 #include "google_apis/gaia/account_tracker.h" | |
| 38 #include "google_apis/gaia/identity_provider.h" | |
| 39 #include "net/url_request/url_request_context_getter.h" | |
| 40 #endif | |
| 41 | |
| 42 namespace gcm { | |
| 43 | |
| 44 #if !defined(OS_ANDROID) | |
| 45 // Identity observer only has actual work to do when the user is actually signed | |
| 46 // in. It ensures that account tracker is taking | |
| 47 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { | |
| 48 public: | |
| 49 IdentityObserver(Profile* profile, GCMDriver* driver); | |
| 50 ~IdentityObserver() override; | |
| 51 | |
| 52 // IdentityProvider::Observer: | |
| 53 void OnActiveAccountLogin() override; | |
| 54 void OnActiveAccountLogout() override; | |
| 55 | |
| 56 private: | |
| 57 void StartAccountTracker(); | |
| 58 | |
| 59 Profile* profile_; | |
| 60 GCMDriver* driver_; | |
| 61 scoped_ptr<IdentityProvider> identity_provider_; | |
| 62 scoped_ptr<GCMAccountTracker> gcm_account_tracker_; | |
| 63 | |
| 64 // The account ID that this service is responsible for. Empty when the service | |
| 65 // is not running. | |
| 66 std::string account_id_; | |
| 67 | |
| 68 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); | |
| 71 }; | |
| 72 | |
| 73 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, | |
| 74 GCMDriver* driver) | |
| 75 : profile_(profile), driver_(driver), weak_ptr_factory_(this) { | |
| 76 identity_provider_.reset(new ProfileIdentityProvider( | |
| 77 SigninManagerFactory::GetForProfile(profile), | |
| 78 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), | |
| 79 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile))); | |
| 80 identity_provider_->AddObserver(this); | |
| 81 | |
| 82 OnActiveAccountLogin(); | |
| 83 StartAccountTracker(); | |
| 84 } | |
| 85 | |
| 86 GCMProfileService::IdentityObserver::~IdentityObserver() { | |
| 87 if (gcm_account_tracker_) | |
| 88 gcm_account_tracker_->Shutdown(); | |
| 89 identity_provider_->RemoveObserver(this); | |
| 90 } | |
| 91 | |
| 92 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { | |
| 93 // This might be called multiple times when the password changes. | |
| 94 const std::string account_id = identity_provider_->GetActiveAccountId(); | |
| 95 if (account_id == account_id_) | |
| 96 return; | |
| 97 account_id_ = account_id; | |
| 98 | |
| 99 // Still need to notify GCMDriver for UMA purpose. | |
| 100 driver_->OnSignedIn(); | |
| 101 } | |
| 102 | |
| 103 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { | |
| 104 account_id_.clear(); | |
| 105 | |
| 106 // Still need to notify GCMDriver for UMA purpose. | |
| 107 driver_->OnSignedOut(); | |
| 108 } | |
| 109 | |
| 110 void GCMProfileService::IdentityObserver::StartAccountTracker() { | |
| 111 if (gcm_account_tracker_) | |
| 112 return; | |
| 113 | |
| 114 scoped_ptr<gaia::AccountTracker> gaia_account_tracker( | |
| 115 new gaia::AccountTracker(identity_provider_.get(), | |
| 116 profile_->GetRequestContext())); | |
| 117 | |
| 118 gcm_account_tracker_.reset( | |
| 119 new GCMAccountTracker(gaia_account_tracker.Pass(), driver_)); | |
| 120 | |
| 121 gcm_account_tracker_->Start(); | |
| 122 } | |
| 123 | |
| 124 #endif // !defined(OS_ANDROID) | |
| 125 | |
| 126 // static | |
| 127 bool GCMProfileService::IsGCMEnabled(Profile* profile) { | |
| 128 #if defined(OS_ANDROID) | |
| 129 return true; | |
| 130 #else | |
| 131 return profile->GetPrefs()->GetBoolean(gcm::prefs::kGCMChannelStatus); | |
| 132 #endif // defined(OS_ANDROID) | |
| 133 } | |
| 134 | |
| 135 #if defined(OS_ANDROID) | |
| 136 GCMProfileService::GCMProfileService(Profile* profile) | |
| 137 : profile_(profile) { | |
| 138 DCHECK(!profile->IsOffTheRecord()); | |
| 139 | |
| 140 scoped_refptr<base::SequencedWorkerPool> worker_pool( | |
| 141 content::BrowserThread::GetBlockingPool()); | |
| 142 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
| 143 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 144 worker_pool->GetSequenceToken(), | |
| 145 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
| 146 | |
| 147 driver_.reset(new GCMDriverAndroid( | |
| 148 profile_->GetPath().Append(chrome::kGCMStoreDirname), | |
| 149 blocking_task_runner)); | |
| 150 } | |
| 151 #else | |
| 152 GCMProfileService::GCMProfileService( | |
| 153 Profile* profile, | |
| 154 scoped_ptr<GCMClientFactory> gcm_client_factory) | |
| 155 : profile_(profile) { | |
| 156 DCHECK(!profile->IsOffTheRecord()); | |
| 157 | |
| 158 base::SequencedWorkerPool* worker_pool = | |
| 159 content::BrowserThread::GetBlockingPool(); | |
| 160 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
| 161 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 162 worker_pool->GetSequenceToken(), | |
| 163 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
| 164 | |
| 165 driver_ = CreateGCMDriverDesktop( | |
| 166 gcm_client_factory.Pass(), | |
| 167 profile_->GetPrefs(), | |
| 168 profile_->GetPath().Append(chrome::kGCMStoreDirname), | |
| 169 profile_->GetRequestContext(), | |
| 170 chrome::GetChannel(), | |
| 171 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 172 content::BrowserThread::UI), | |
| 173 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 174 content::BrowserThread::IO), | |
| 175 blocking_task_runner); | |
| 176 | |
| 177 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); | |
| 178 } | |
| 179 #endif // defined(OS_ANDROID) | |
| 180 | |
| 181 GCMProfileService::GCMProfileService() | |
| 182 : profile_(NULL) { | |
| 183 } | |
| 184 | |
| 185 GCMProfileService::~GCMProfileService() { | |
| 186 } | |
| 187 | |
| 188 void GCMProfileService::Shutdown() { | |
| 189 #if !defined(OS_ANDROID) | |
| 190 identity_observer_.reset(); | |
| 191 #endif // !defined(OS_ANDROID) | |
| 192 if (driver_) { | |
| 193 driver_->Shutdown(); | |
| 194 driver_.reset(); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { | |
| 199 driver_.reset(driver); | |
| 200 #if !defined(OS_ANDROID) | |
| 201 if (identity_observer_) | |
| 202 identity_observer_.reset(new IdentityObserver(profile_, driver)); | |
| 203 #endif // !defined(OS_ANDROID) | |
| 204 } | |
| 205 | |
| 206 } // namespace gcm | |
| OLD | NEW |