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