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