| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_driver.h" | 5 #include "chrome/browser/services/gcm/gcm_driver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
| 17 #include "components/gcm_driver/gcm_app_handler.h" | 18 #include "components/gcm_driver/gcm_app_handler.h" |
| 18 #include "components/gcm_driver/gcm_client_factory.h" | 19 #include "components/gcm_driver/gcm_client_factory.h" |
| 19 #include "components/gcm_driver/system_encryptor.h" | 20 #include "components/gcm_driver/system_encryptor.h" |
| 20 #include "content/public/browser/browser_thread.h" | |
| 21 #include "google_apis/gaia/oauth2_token_service.h" | 21 #include "google_apis/gaia/oauth2_token_service.h" |
| 22 #include "google_apis/gcm/protocol/android_checkin.pb.h" | 22 #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 23 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 24 | 24 |
| 25 namespace gcm { | 25 namespace gcm { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 checkin_proto::ChromeBuildProto_Platform GetPlatform() { | 29 checkin_proto::ChromeBuildProto_Platform GetPlatform() { |
| 30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 DCHECK(ready_); | 119 DCHECK(ready_); |
| 120 | 120 |
| 121 for (size_t i = 0; i < delayed_tasks_.size(); ++i) | 121 for (size_t i = 0; i < delayed_tasks_.size(); ++i) |
| 122 delayed_tasks_[i].Run(); | 122 delayed_tasks_[i].Run(); |
| 123 delayed_tasks_.clear(); | 123 delayed_tasks_.clear(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 class GCMDriver::IOWorker : public GCMClient::Delegate { | 126 class GCMDriver::IOWorker : public GCMClient::Delegate { |
| 127 public: | 127 public: |
| 128 // Called on UI thread. | 128 // Called on UI thread. |
| 129 IOWorker(); | 129 IOWorker(const scoped_refptr<base::MessageLoopProxy>& ui_thread, |
| 130 const scoped_refptr<base::MessageLoopProxy>& io_thread); |
| 130 virtual ~IOWorker(); | 131 virtual ~IOWorker(); |
| 131 | 132 |
| 132 // Overridden from GCMClient::Delegate: | 133 // Overridden from GCMClient::Delegate: |
| 133 // Called on IO thread. | 134 // Called on IO thread. |
| 134 virtual void OnRegisterFinished(const std::string& app_id, | 135 virtual void OnRegisterFinished(const std::string& app_id, |
| 135 const std::string& registration_id, | 136 const std::string& registration_id, |
| 136 GCMClient::Result result) OVERRIDE; | 137 GCMClient::Result result) OVERRIDE; |
| 137 virtual void OnUnregisterFinished(const std::string& app_id, | 138 virtual void OnUnregisterFinished(const std::string& app_id, |
| 138 GCMClient::Result result) OVERRIDE; | 139 GCMClient::Result result) OVERRIDE; |
| 139 virtual void OnSendFinished(const std::string& app_id, | 140 virtual void OnSendFinished(const std::string& app_id, |
| 140 const std::string& message_id, | 141 const std::string& message_id, |
| 141 GCMClient::Result result) OVERRIDE; | 142 GCMClient::Result result) OVERRIDE; |
| 142 virtual void OnMessageReceived( | 143 virtual void OnMessageReceived( |
| 143 const std::string& app_id, | 144 const std::string& app_id, |
| 144 const GCMClient::IncomingMessage& message) OVERRIDE; | 145 const GCMClient::IncomingMessage& message) OVERRIDE; |
| 145 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 146 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 146 virtual void OnMessageSendError( | 147 virtual void OnMessageSendError( |
| 147 const std::string& app_id, | 148 const std::string& app_id, |
| 148 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 149 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
| 149 virtual void OnGCMReady() OVERRIDE; | 150 virtual void OnGCMReady() OVERRIDE; |
| 150 virtual void OnActivityRecorded() OVERRIDE; | 151 virtual void OnActivityRecorded() OVERRIDE; |
| 151 | 152 |
| 152 // Called on IO thread. | 153 // Called on IO thread. |
| 153 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory, | 154 void Initialize( |
| 154 const base::FilePath& store_path, | 155 scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 155 const std::vector<std::string>& account_ids, | 156 const base::FilePath& store_path, |
| 156 const scoped_refptr<net::URLRequestContextGetter>& | 157 const std::vector<std::string>& account_ids, |
| 157 url_request_context_getter); | 158 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 159 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
| 158 void Start(const base::WeakPtr<GCMDriver>& service); | 160 void Start(const base::WeakPtr<GCMDriver>& service); |
| 159 void Stop(); | 161 void Stop(); |
| 160 void CheckOut(); | 162 void CheckOut(); |
| 161 void Register(const std::string& app_id, | 163 void Register(const std::string& app_id, |
| 162 const std::vector<std::string>& sender_ids); | 164 const std::vector<std::string>& sender_ids); |
| 163 void Unregister(const std::string& app_id); | 165 void Unregister(const std::string& app_id); |
| 164 void Send(const std::string& app_id, | 166 void Send(const std::string& app_id, |
| 165 const std::string& receiver_id, | 167 const std::string& receiver_id, |
| 166 const GCMClient::OutgoingMessage& message); | 168 const GCMClient::OutgoingMessage& message); |
| 167 void GetGCMStatistics(bool clear_logs); | 169 void GetGCMStatistics(bool clear_logs); |
| 168 void SetGCMRecording(bool recording); | 170 void SetGCMRecording(bool recording); |
| 169 | 171 |
| 170 // For testing purpose. Can be called from UI thread. Use with care. | 172 // For testing purpose. Can be called from UI thread. Use with care. |
| 171 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } | 173 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
| 172 | 174 |
| 173 private: | 175 private: |
| 176 scoped_refptr<base::MessageLoopProxy> ui_thread_; |
| 177 scoped_refptr<base::MessageLoopProxy> io_thread_; |
| 178 |
| 174 base::WeakPtr<GCMDriver> service_; | 179 base::WeakPtr<GCMDriver> service_; |
| 175 | 180 |
| 176 scoped_ptr<GCMClient> gcm_client_; | 181 scoped_ptr<GCMClient> gcm_client_; |
| 177 | 182 |
| 178 DISALLOW_COPY_AND_ASSIGN(IOWorker); | 183 DISALLOW_COPY_AND_ASSIGN(IOWorker); |
| 179 }; | 184 }; |
| 180 | 185 |
| 181 GCMDriver::IOWorker::IOWorker() { | 186 GCMDriver::IOWorker::IOWorker( |
| 182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 187 const scoped_refptr<base::MessageLoopProxy>& ui_thread, |
| 188 const scoped_refptr<base::MessageLoopProxy>& io_thread) |
| 189 : ui_thread_(ui_thread), |
| 190 io_thread_(io_thread) { |
| 191 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 183 } | 192 } |
| 184 | 193 |
| 185 GCMDriver::IOWorker::~IOWorker() { | 194 GCMDriver::IOWorker::~IOWorker() { |
| 186 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 195 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 187 } | 196 } |
| 188 | 197 |
| 189 void GCMDriver::IOWorker::Initialize( | 198 void GCMDriver::IOWorker::Initialize( |
| 190 scoped_ptr<GCMClientFactory> gcm_client_factory, | 199 scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 191 const base::FilePath& store_path, | 200 const base::FilePath& store_path, |
| 192 const std::vector<std::string>& account_ids, | 201 const std::vector<std::string>& account_ids, |
| 193 const scoped_refptr<net::URLRequestContextGetter>& | 202 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 194 url_request_context_getter) { | 203 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
| 195 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 204 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 196 | 205 |
| 197 gcm_client_ = gcm_client_factory->BuildInstance(); | 206 gcm_client_ = gcm_client_factory->BuildInstance(); |
| 198 | 207 |
| 199 checkin_proto::ChromeBuildProto chrome_build_proto; | 208 checkin_proto::ChromeBuildProto chrome_build_proto; |
| 200 chrome_build_proto.set_platform(GetPlatform()); | 209 chrome_build_proto.set_platform(GetPlatform()); |
| 201 chrome_build_proto.set_chrome_version(GetVersion()); | 210 chrome_build_proto.set_chrome_version(GetVersion()); |
| 202 chrome_build_proto.set_channel(GetChannel()); | 211 chrome_build_proto.set_channel(GetChannel()); |
| 203 | 212 |
| 204 scoped_refptr<base::SequencedWorkerPool> worker_pool( | |
| 205 content::BrowserThread::GetBlockingPool()); | |
| 206 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
| 207 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 208 worker_pool->GetSequenceToken(), | |
| 209 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
| 210 | |
| 211 gcm_client_->Initialize(chrome_build_proto, | 213 gcm_client_->Initialize(chrome_build_proto, |
| 212 store_path, | 214 store_path, |
| 213 account_ids, | 215 account_ids, |
| 214 blocking_task_runner, | 216 blocking_task_runner, |
| 215 url_request_context_getter, | 217 request_context, |
| 216 make_scoped_ptr<Encryptor>(new SystemEncryptor), | 218 make_scoped_ptr<Encryptor>(new SystemEncryptor), |
| 217 this); | 219 this); |
| 218 } | 220 } |
| 219 | 221 |
| 220 void GCMDriver::IOWorker::OnRegisterFinished( | 222 void GCMDriver::IOWorker::OnRegisterFinished( |
| 221 const std::string& app_id, | 223 const std::string& app_id, |
| 222 const std::string& registration_id, | 224 const std::string& registration_id, |
| 223 GCMClient::Result result) { | 225 GCMClient::Result result) { |
| 224 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 226 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 225 | 227 |
| 226 content::BrowserThread::PostTask(content::BrowserThread::UI, | 228 ui_thread_->PostTask( |
| 227 FROM_HERE, | 229 FROM_HERE, |
| 228 base::Bind(&GCMDriver::RegisterFinished, | 230 base::Bind(&GCMDriver::RegisterFinished, service_, app_id, |
| 229 service_, | 231 registration_id, result)); |
| 230 app_id, | |
| 231 registration_id, | |
| 232 result)); | |
| 233 } | 232 } |
| 234 | 233 |
| 235 void GCMDriver::IOWorker::OnUnregisterFinished(const std::string& app_id, | 234 void GCMDriver::IOWorker::OnUnregisterFinished(const std::string& app_id, |
| 236 GCMClient::Result result) { | 235 GCMClient::Result result) { |
| 237 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 236 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 238 | 237 |
| 239 content::BrowserThread::PostTask( | 238 ui_thread_->PostTask( |
| 240 content::BrowserThread::UI, | |
| 241 FROM_HERE, | 239 FROM_HERE, |
| 242 base::Bind(&GCMDriver::UnregisterFinished, service_, app_id, result)); | 240 base::Bind(&GCMDriver::UnregisterFinished, service_, app_id, result)); |
| 243 } | 241 } |
| 244 | 242 |
| 245 void GCMDriver::IOWorker::OnSendFinished(const std::string& app_id, | 243 void GCMDriver::IOWorker::OnSendFinished(const std::string& app_id, |
| 246 const std::string& message_id, | 244 const std::string& message_id, |
| 247 GCMClient::Result result) { | 245 GCMClient::Result result) { |
| 248 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 246 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 249 | 247 |
| 250 content::BrowserThread::PostTask(content::BrowserThread::UI, | 248 ui_thread_->PostTask( |
| 251 FROM_HERE, | 249 FROM_HERE, |
| 252 base::Bind(&GCMDriver::SendFinished, | 250 base::Bind(&GCMDriver::SendFinished, service_, app_id, message_id, |
| 253 service_, | 251 result)); |
| 254 app_id, | |
| 255 message_id, | |
| 256 result)); | |
| 257 } | 252 } |
| 258 | 253 |
| 259 void GCMDriver::IOWorker::OnMessageReceived( | 254 void GCMDriver::IOWorker::OnMessageReceived( |
| 260 const std::string& app_id, | 255 const std::string& app_id, |
| 261 const GCMClient::IncomingMessage& message) { | 256 const GCMClient::IncomingMessage& message) { |
| 262 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 257 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 263 | 258 |
| 264 content::BrowserThread::PostTask(content::BrowserThread::UI, | 259 ui_thread_->PostTask( |
| 265 FROM_HERE, | 260 FROM_HERE, |
| 266 base::Bind(&GCMDriver::MessageReceived, | 261 base::Bind(&GCMDriver::MessageReceived, service_, app_id, message)); |
| 267 service_, | |
| 268 app_id, | |
| 269 message)); | |
| 270 } | 262 } |
| 271 | 263 |
| 272 void GCMDriver::IOWorker::OnMessagesDeleted(const std::string& app_id) { | 264 void GCMDriver::IOWorker::OnMessagesDeleted(const std::string& app_id) { |
| 273 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 265 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 274 | 266 |
| 275 content::BrowserThread::PostTask(content::BrowserThread::UI, | 267 ui_thread_->PostTask( |
| 276 FROM_HERE, | 268 FROM_HERE, |
| 277 base::Bind(&GCMDriver::MessagesDeleted, | 269 base::Bind(&GCMDriver::MessagesDeleted, service_, app_id)); |
| 278 service_, | |
| 279 app_id)); | |
| 280 } | 270 } |
| 281 | 271 |
| 282 void GCMDriver::IOWorker::OnMessageSendError( | 272 void GCMDriver::IOWorker::OnMessageSendError( |
| 283 const std::string& app_id, | 273 const std::string& app_id, |
| 284 const GCMClient::SendErrorDetails& send_error_details) { | 274 const GCMClient::SendErrorDetails& send_error_details) { |
| 285 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 275 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 286 | 276 |
| 287 content::BrowserThread::PostTask(content::BrowserThread::UI, | 277 ui_thread_->PostTask( |
| 288 FROM_HERE, | 278 FROM_HERE, |
| 289 base::Bind(&GCMDriver::MessageSendError, | 279 base::Bind(&GCMDriver::MessageSendError, service_, app_id, |
| 290 service_, | 280 send_error_details)); |
| 291 app_id, | |
| 292 send_error_details)); | |
| 293 } | 281 } |
| 294 | 282 |
| 295 void GCMDriver::IOWorker::OnGCMReady() { | 283 void GCMDriver::IOWorker::OnGCMReady() { |
| 296 content::BrowserThread::PostTask(content::BrowserThread::UI, | 284 ui_thread_->PostTask( |
| 297 FROM_HERE, | 285 FROM_HERE, |
| 298 base::Bind(&GCMDriver::GCMClientReady, | 286 base::Bind(&GCMDriver::GCMClientReady, service_)); |
| 299 service_)); | |
| 300 } | 287 } |
| 301 | 288 |
| 302 void GCMDriver::IOWorker::OnActivityRecorded() { | 289 void GCMDriver::IOWorker::OnActivityRecorded() { |
| 303 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 290 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 304 // When an activity is recorded, get all the stats and refresh the UI of | 291 // When an activity is recorded, get all the stats and refresh the UI of |
| 305 // gcm-internals page. | 292 // gcm-internals page. |
| 306 GetGCMStatistics(false); | 293 GetGCMStatistics(false); |
| 307 } | 294 } |
| 308 | 295 |
| 309 void GCMDriver::IOWorker::Start(const base::WeakPtr<GCMDriver>& service) { | 296 void GCMDriver::IOWorker::Start(const base::WeakPtr<GCMDriver>& service) { |
| 310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 297 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 311 | 298 |
| 312 service_ = service; | 299 service_ = service; |
| 313 gcm_client_->Start(); | 300 gcm_client_->Start(); |
| 314 } | 301 } |
| 315 | 302 |
| 316 void GCMDriver::IOWorker::Stop() { | 303 void GCMDriver::IOWorker::Stop() { |
| 317 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 304 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 318 | 305 |
| 319 gcm_client_->Stop(); | 306 gcm_client_->Stop(); |
| 320 } | 307 } |
| 321 | 308 |
| 322 void GCMDriver::IOWorker::CheckOut() { | 309 void GCMDriver::IOWorker::CheckOut() { |
| 323 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 310 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 324 | 311 |
| 325 gcm_client_->CheckOut(); | 312 gcm_client_->CheckOut(); |
| 326 | 313 |
| 327 // Note that we still need to keep GCMClient instance alive since the | 314 // Note that we still need to keep GCMClient instance alive since the |
| 328 // GCMDriver may check in again. | 315 // GCMDriver may check in again. |
| 329 } | 316 } |
| 330 | 317 |
| 331 void GCMDriver::IOWorker::Register( | 318 void GCMDriver::IOWorker::Register( |
| 332 const std::string& app_id, | 319 const std::string& app_id, |
| 333 const std::vector<std::string>& sender_ids) { | 320 const std::vector<std::string>& sender_ids) { |
| 334 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 321 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 335 | 322 |
| 336 gcm_client_->Register(app_id, sender_ids); | 323 gcm_client_->Register(app_id, sender_ids); |
| 337 } | 324 } |
| 338 | 325 |
| 339 void GCMDriver::IOWorker::Unregister(const std::string& app_id) { | 326 void GCMDriver::IOWorker::Unregister(const std::string& app_id) { |
| 340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 327 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 341 | 328 |
| 342 gcm_client_->Unregister(app_id); | 329 gcm_client_->Unregister(app_id); |
| 343 } | 330 } |
| 344 | 331 |
| 345 void GCMDriver::IOWorker::Send(const std::string& app_id, | 332 void GCMDriver::IOWorker::Send(const std::string& app_id, |
| 346 const std::string& receiver_id, | 333 const std::string& receiver_id, |
| 347 const GCMClient::OutgoingMessage& message) { | 334 const GCMClient::OutgoingMessage& message) { |
| 348 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 335 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 349 | 336 |
| 350 gcm_client_->Send(app_id, receiver_id, message); | 337 gcm_client_->Send(app_id, receiver_id, message); |
| 351 } | 338 } |
| 352 | 339 |
| 353 void GCMDriver::IOWorker::GetGCMStatistics(bool clear_logs) { | 340 void GCMDriver::IOWorker::GetGCMStatistics(bool clear_logs) { |
| 354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 341 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 355 gcm::GCMClient::GCMStatistics stats; | 342 gcm::GCMClient::GCMStatistics stats; |
| 356 | 343 |
| 357 if (gcm_client_.get()) { | 344 if (gcm_client_.get()) { |
| 358 if (clear_logs) | 345 if (clear_logs) |
| 359 gcm_client_->ClearActivityLogs(); | 346 gcm_client_->ClearActivityLogs(); |
| 360 stats = gcm_client_->GetStatistics(); | 347 stats = gcm_client_->GetStatistics(); |
| 361 } | 348 } |
| 362 | 349 |
| 363 content::BrowserThread::PostTask( | 350 ui_thread_->PostTask( |
| 364 content::BrowserThread::UI, | |
| 365 FROM_HERE, | 351 FROM_HERE, |
| 366 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); | 352 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); |
| 367 } | 353 } |
| 368 | 354 |
| 369 void GCMDriver::IOWorker::SetGCMRecording(bool recording) { | 355 void GCMDriver::IOWorker::SetGCMRecording(bool recording) { |
| 370 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 356 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 371 gcm::GCMClient::GCMStatistics stats; | 357 gcm::GCMClient::GCMStatistics stats; |
| 372 | 358 |
| 373 if (gcm_client_.get()) { | 359 if (gcm_client_.get()) { |
| 374 gcm_client_->SetRecording(recording); | 360 gcm_client_->SetRecording(recording); |
| 375 stats = gcm_client_->GetStatistics(); | 361 stats = gcm_client_->GetStatistics(); |
| 376 stats.gcm_client_created = true; | 362 stats.gcm_client_created = true; |
| 377 } | 363 } |
| 378 | 364 |
| 379 content::BrowserThread::PostTask( | 365 ui_thread_->PostTask( |
| 380 content::BrowserThread::UI, | |
| 381 FROM_HERE, | 366 FROM_HERE, |
| 382 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); | 367 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); |
| 383 } | 368 } |
| 384 | 369 |
| 385 GCMDriver::GCMDriver( | 370 GCMDriver::GCMDriver( |
| 386 scoped_ptr<GCMClientFactory> gcm_client_factory, | 371 scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 387 scoped_ptr<IdentityProvider> identity_provider, | 372 scoped_ptr<IdentityProvider> identity_provider, |
| 388 const base::FilePath& store_path, | 373 const base::FilePath& store_path, |
| 389 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 374 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 375 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, |
| 376 const scoped_refptr<base::MessageLoopProxy>& ui_thread, |
| 377 const scoped_refptr<base::MessageLoopProxy>& io_thread) |
| 390 : gcm_enabled_(true), | 378 : gcm_enabled_(true), |
| 391 gcm_client_ready_(false), | 379 gcm_client_ready_(false), |
| 392 identity_provider_(identity_provider.Pass()), | 380 identity_provider_(identity_provider.Pass()), |
| 381 ui_thread_(ui_thread), |
| 382 io_thread_(io_thread), |
| 393 weak_ptr_factory_(this) { | 383 weak_ptr_factory_(this) { |
| 394 // Get the list of available accounts. | 384 // Get the list of available accounts. |
| 395 std::vector<std::string> account_ids; | 385 std::vector<std::string> account_ids; |
| 396 #if !defined(OS_ANDROID) | 386 #if !defined(OS_ANDROID) |
| 397 account_ids = identity_provider_->GetTokenService()->GetAccounts(); | 387 account_ids = identity_provider_->GetTokenService()->GetAccounts(); |
| 398 #endif | 388 #endif |
| 399 | 389 |
| 400 // Create and initialize the GCMClient. Note that this does not initiate the | 390 // Create and initialize the GCMClient. Note that this does not initiate the |
| 401 // GCM check-in. | 391 // GCM check-in. |
| 402 io_worker_.reset(new IOWorker()); | 392 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
| 403 content::BrowserThread::PostTask( | 393 io_thread_->PostTask( |
| 404 content::BrowserThread::IO, | |
| 405 FROM_HERE, | 394 FROM_HERE, |
| 406 base::Bind(&GCMDriver::IOWorker::Initialize, | 395 base::Bind(&GCMDriver::IOWorker::Initialize, |
| 407 base::Unretained(io_worker_.get()), | 396 base::Unretained(io_worker_.get()), |
| 408 base::Passed(&gcm_client_factory), | 397 base::Passed(&gcm_client_factory), |
| 409 store_path, | 398 store_path, |
| 410 account_ids, | 399 account_ids, |
| 411 request_context)); | 400 request_context, |
| 401 blocking_task_runner)); |
| 412 | 402 |
| 413 identity_provider_->AddObserver(this); | 403 identity_provider_->AddObserver(this); |
| 414 } | 404 } |
| 415 | 405 |
| 416 GCMDriver::GCMDriver() | 406 GCMDriver::GCMDriver() |
| 417 : gcm_enabled_(true), | 407 : gcm_enabled_(true), |
| 418 gcm_client_ready_(false), | 408 gcm_client_ready_(false), |
| 419 weak_ptr_factory_(this) { | 409 weak_ptr_factory_(this) { |
| 420 } | 410 } |
| 421 | 411 |
| 422 GCMDriver::~GCMDriver() { | 412 GCMDriver::~GCMDriver() { |
| 423 } | 413 } |
| 424 | 414 |
| 425 void GCMDriver::Enable() { | 415 void GCMDriver::Enable() { |
| 426 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 416 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 427 | 417 |
| 428 if (gcm_enabled_) | 418 if (gcm_enabled_) |
| 429 return; | 419 return; |
| 430 gcm_enabled_ = true; | 420 gcm_enabled_ = true; |
| 431 | 421 |
| 432 EnsureStarted(); | 422 EnsureStarted(); |
| 433 } | 423 } |
| 434 | 424 |
| 435 void GCMDriver::Disable() { | 425 void GCMDriver::Disable() { |
| 436 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 426 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 437 | 427 |
| 438 if (!gcm_enabled_) | 428 if (!gcm_enabled_) |
| 439 return; | 429 return; |
| 440 gcm_enabled_ = false; | 430 gcm_enabled_ = false; |
| 441 | 431 |
| 442 Stop(); | 432 Stop(); |
| 443 } | 433 } |
| 444 | 434 |
| 445 void GCMDriver::Stop() { | 435 void GCMDriver::Stop() { |
| 446 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 436 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 447 | 437 |
| 448 // No need to stop GCM service if not started yet. | 438 // No need to stop GCM service if not started yet. |
| 449 if (account_id_.empty()) | 439 if (account_id_.empty()) |
| 450 return; | 440 return; |
| 451 | 441 |
| 452 RemoveCachedData(); | 442 RemoveCachedData(); |
| 453 | 443 |
| 454 content::BrowserThread::PostTask( | 444 io_thread_->PostTask( |
| 455 content::BrowserThread::IO, | |
| 456 FROM_HERE, | 445 FROM_HERE, |
| 457 base::Bind(&GCMDriver::IOWorker::Stop, | 446 base::Bind(&GCMDriver::IOWorker::Stop, |
| 458 base::Unretained(io_worker_.get()))); | 447 base::Unretained(io_worker_.get()))); |
| 459 } | 448 } |
| 460 | 449 |
| 461 void GCMDriver::Shutdown() { | 450 void GCMDriver::Shutdown() { |
| 462 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 451 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 463 identity_provider_->RemoveObserver(this); | 452 identity_provider_->RemoveObserver(this); |
| 464 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); | 453 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
| 465 iter != app_handlers_.end(); ++iter) { | 454 iter != app_handlers_.end(); ++iter) { |
| 466 iter->second->ShutdownHandler(); | 455 iter->second->ShutdownHandler(); |
| 467 } | 456 } |
| 468 app_handlers_.clear(); | 457 app_handlers_.clear(); |
| 469 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, | 458 io_thread_->DeleteSoon(FROM_HERE, io_worker_.release()); |
| 470 FROM_HERE, | |
| 471 io_worker_.release()); | |
| 472 } | 459 } |
| 473 | 460 |
| 474 void GCMDriver::AddAppHandler(const std::string& app_id, | 461 void GCMDriver::AddAppHandler(const std::string& app_id, |
| 475 GCMAppHandler* handler) { | 462 GCMAppHandler* handler) { |
| 476 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 463 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 477 DCHECK(!app_id.empty()); | 464 DCHECK(!app_id.empty()); |
| 478 DCHECK(handler); | 465 DCHECK(handler); |
| 479 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); | 466 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); |
| 480 | 467 |
| 481 app_handlers_[app_id] = handler; | 468 app_handlers_[app_id] = handler; |
| 482 | 469 |
| 483 // Ensures that the GCM service is started when there is an interest. | 470 // Ensures that the GCM service is started when there is an interest. |
| 484 EnsureStarted(); | 471 EnsureStarted(); |
| 485 } | 472 } |
| 486 | 473 |
| 487 void GCMDriver::RemoveAppHandler(const std::string& app_id) { | 474 void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 488 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 475 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 489 DCHECK(!app_id.empty()); | 476 DCHECK(!app_id.empty()); |
| 490 | 477 |
| 491 app_handlers_.erase(app_id); | 478 app_handlers_.erase(app_id); |
| 492 } | 479 } |
| 493 | 480 |
| 494 void GCMDriver::Register(const std::string& app_id, | 481 void GCMDriver::Register(const std::string& app_id, |
| 495 const std::vector<std::string>& sender_ids, | 482 const std::vector<std::string>& sender_ids, |
| 496 const RegisterCallback& callback) { | 483 const RegisterCallback& callback) { |
| 497 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 484 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 498 DCHECK(!app_id.empty()); | 485 DCHECK(!app_id.empty()); |
| 499 DCHECK(!sender_ids.empty()); | 486 DCHECK(!sender_ids.empty()); |
| 500 DCHECK(!callback.is_null()); | 487 DCHECK(!callback.is_null()); |
| 501 | 488 |
| 502 GCMClient::Result result = EnsureStarted(); | 489 GCMClient::Result result = EnsureStarted(); |
| 503 if (result != GCMClient::SUCCESS) { | 490 if (result != GCMClient::SUCCESS) { |
| 504 callback.Run(std::string(), result); | 491 callback.Run(std::string(), result); |
| 505 return; | 492 return; |
| 506 } | 493 } |
| 507 | 494 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 520 app_id, | 507 app_id, |
| 521 sender_ids)); | 508 sender_ids)); |
| 522 return; | 509 return; |
| 523 } | 510 } |
| 524 | 511 |
| 525 DoRegister(app_id, sender_ids); | 512 DoRegister(app_id, sender_ids); |
| 526 } | 513 } |
| 527 | 514 |
| 528 void GCMDriver::DoRegister(const std::string& app_id, | 515 void GCMDriver::DoRegister(const std::string& app_id, |
| 529 const std::vector<std::string>& sender_ids) { | 516 const std::vector<std::string>& sender_ids) { |
| 530 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 517 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 531 std::map<std::string, RegisterCallback>::iterator callback_iter = | 518 std::map<std::string, RegisterCallback>::iterator callback_iter = |
| 532 register_callbacks_.find(app_id); | 519 register_callbacks_.find(app_id); |
| 533 if (callback_iter == register_callbacks_.end()) { | 520 if (callback_iter == register_callbacks_.end()) { |
| 534 // The callback could have been removed when the app is uninstalled. | 521 // The callback could have been removed when the app is uninstalled. |
| 535 return; | 522 return; |
| 536 } | 523 } |
| 537 | 524 |
| 538 // Normalize the sender IDs by making them sorted. | 525 // Normalize the sender IDs by making them sorted. |
| 539 std::vector<std::string> normalized_sender_ids = sender_ids; | 526 std::vector<std::string> normalized_sender_ids = sender_ids; |
| 540 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); | 527 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
| 541 | 528 |
| 542 content::BrowserThread::PostTask( | 529 io_thread_->PostTask( |
| 543 content::BrowserThread::IO, | |
| 544 FROM_HERE, | 530 FROM_HERE, |
| 545 base::Bind(&GCMDriver::IOWorker::Register, | 531 base::Bind(&GCMDriver::IOWorker::Register, |
| 546 base::Unretained(io_worker_.get()), | 532 base::Unretained(io_worker_.get()), |
| 547 app_id, | 533 app_id, |
| 548 normalized_sender_ids)); | 534 normalized_sender_ids)); |
| 549 } | 535 } |
| 550 | 536 |
| 551 void GCMDriver::Unregister(const std::string& app_id, | 537 void GCMDriver::Unregister(const std::string& app_id, |
| 552 const UnregisterCallback& callback) { | 538 const UnregisterCallback& callback) { |
| 553 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 539 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 554 DCHECK(!app_id.empty()); | 540 DCHECK(!app_id.empty()); |
| 555 DCHECK(!callback.is_null()); | 541 DCHECK(!callback.is_null()); |
| 556 | 542 |
| 557 GCMClient::Result result = EnsureStarted(); | 543 GCMClient::Result result = EnsureStarted(); |
| 558 if (result != GCMClient::SUCCESS) { | 544 if (result != GCMClient::SUCCESS) { |
| 559 callback.Run(result); | 545 callback.Run(result); |
| 560 return; | 546 return; |
| 561 } | 547 } |
| 562 | 548 |
| 563 // If previous un/register operation is still in progress, bail out. | 549 // If previous un/register operation is still in progress, bail out. |
| 564 if (IsAsyncOperationPending(app_id)) { | 550 if (IsAsyncOperationPending(app_id)) { |
| 565 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); | 551 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); |
| 566 return; | 552 return; |
| 567 } | 553 } |
| 568 | 554 |
| 569 unregister_callbacks_[app_id] = callback; | 555 unregister_callbacks_[app_id] = callback; |
| 570 | 556 |
| 571 // Delay the unregister operation until GCMClient is ready. | 557 // Delay the unregister operation until GCMClient is ready. |
| 572 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { | 558 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
| 573 delayed_task_controller_->AddTask(base::Bind(&GCMDriver::DoUnregister, | 559 delayed_task_controller_->AddTask(base::Bind(&GCMDriver::DoUnregister, |
| 574 weak_ptr_factory_.GetWeakPtr(), | 560 weak_ptr_factory_.GetWeakPtr(), |
| 575 app_id)); | 561 app_id)); |
| 576 return; | 562 return; |
| 577 } | 563 } |
| 578 | 564 |
| 579 DoUnregister(app_id); | 565 DoUnregister(app_id); |
| 580 } | 566 } |
| 581 | 567 |
| 582 void GCMDriver::DoUnregister(const std::string& app_id) { | 568 void GCMDriver::DoUnregister(const std::string& app_id) { |
| 583 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 569 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 584 | 570 |
| 585 // Ask the server to unregister it. There could be a small chance that the | 571 // Ask the server to unregister it. There could be a small chance that the |
| 586 // unregister request fails. If this occurs, it does not bring any harm since | 572 // unregister request fails. If this occurs, it does not bring any harm since |
| 587 // we simply reject the messages/events received from the server. | 573 // we simply reject the messages/events received from the server. |
| 588 content::BrowserThread::PostTask( | 574 io_thread_->PostTask( |
| 589 content::BrowserThread::IO, | |
| 590 FROM_HERE, | 575 FROM_HERE, |
| 591 base::Bind(&GCMDriver::IOWorker::Unregister, | 576 base::Bind(&GCMDriver::IOWorker::Unregister, |
| 592 base::Unretained(io_worker_.get()), | 577 base::Unretained(io_worker_.get()), |
| 593 app_id)); | 578 app_id)); |
| 594 } | 579 } |
| 595 | 580 |
| 596 void GCMDriver::Send(const std::string& app_id, | 581 void GCMDriver::Send(const std::string& app_id, |
| 597 const std::string& receiver_id, | 582 const std::string& receiver_id, |
| 598 const GCMClient::OutgoingMessage& message, | 583 const GCMClient::OutgoingMessage& message, |
| 599 const SendCallback& callback) { | 584 const SendCallback& callback) { |
| 600 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 585 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 601 DCHECK(!app_id.empty()); | 586 DCHECK(!app_id.empty()); |
| 602 DCHECK(!receiver_id.empty()); | 587 DCHECK(!receiver_id.empty()); |
| 603 DCHECK(!callback.is_null()); | 588 DCHECK(!callback.is_null()); |
| 604 | 589 |
| 605 GCMClient::Result result = EnsureStarted(); | 590 GCMClient::Result result = EnsureStarted(); |
| 606 if (result != GCMClient::SUCCESS) { | 591 if (result != GCMClient::SUCCESS) { |
| 607 callback.Run(std::string(), result); | 592 callback.Run(std::string(), result); |
| 608 return; | 593 return; |
| 609 } | 594 } |
| 610 | 595 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 626 message)); | 611 message)); |
| 627 return; | 612 return; |
| 628 } | 613 } |
| 629 | 614 |
| 630 DoSend(app_id, receiver_id, message); | 615 DoSend(app_id, receiver_id, message); |
| 631 } | 616 } |
| 632 | 617 |
| 633 void GCMDriver::DoSend(const std::string& app_id, | 618 void GCMDriver::DoSend(const std::string& app_id, |
| 634 const std::string& receiver_id, | 619 const std::string& receiver_id, |
| 635 const GCMClient::OutgoingMessage& message) { | 620 const GCMClient::OutgoingMessage& message) { |
| 636 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 621 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 637 content::BrowserThread::PostTask( | 622 io_thread_->PostTask( |
| 638 content::BrowserThread::IO, | |
| 639 FROM_HERE, | 623 FROM_HERE, |
| 640 base::Bind(&GCMDriver::IOWorker::Send, | 624 base::Bind(&GCMDriver::IOWorker::Send, |
| 641 base::Unretained(io_worker_.get()), | 625 base::Unretained(io_worker_.get()), |
| 642 app_id, | 626 app_id, |
| 643 receiver_id, | 627 receiver_id, |
| 644 message)); | 628 message)); |
| 645 } | 629 } |
| 646 | 630 |
| 647 GCMClient* GCMDriver::GetGCMClientForTesting() const { | 631 GCMClient* GCMDriver::GetGCMClientForTesting() const { |
| 648 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 632 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 649 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; | 633 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; |
| 650 } | 634 } |
| 651 | 635 |
| 652 bool GCMDriver::IsStarted() const { | 636 bool GCMDriver::IsStarted() const { |
| 653 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 637 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 654 return !account_id_.empty(); | 638 return !account_id_.empty(); |
| 655 } | 639 } |
| 656 | 640 |
| 657 bool GCMDriver::IsGCMClientReady() const { | 641 bool GCMDriver::IsGCMClientReady() const { |
| 658 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 642 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 659 return gcm_client_ready_; | 643 return gcm_client_ready_; |
| 660 } | 644 } |
| 661 | 645 |
| 662 void GCMDriver::GetGCMStatistics(const GetGCMStatisticsCallback& callback, | 646 void GCMDriver::GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
| 663 bool clear_logs) { | 647 bool clear_logs) { |
| 664 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 648 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 665 DCHECK(!callback.is_null()); | 649 DCHECK(!callback.is_null()); |
| 666 | 650 |
| 667 request_gcm_statistics_callback_ = callback; | 651 request_gcm_statistics_callback_ = callback; |
| 668 content::BrowserThread::PostTask( | 652 io_thread_->PostTask( |
| 669 content::BrowserThread::IO, | |
| 670 FROM_HERE, | 653 FROM_HERE, |
| 671 base::Bind(&GCMDriver::IOWorker::GetGCMStatistics, | 654 base::Bind(&GCMDriver::IOWorker::GetGCMStatistics, |
| 672 base::Unretained(io_worker_.get()), | 655 base::Unretained(io_worker_.get()), |
| 673 clear_logs)); | 656 clear_logs)); |
| 674 } | 657 } |
| 675 | 658 |
| 676 void GCMDriver::SetGCMRecording(const GetGCMStatisticsCallback& callback, | 659 void GCMDriver::SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 677 bool recording) { | 660 bool recording) { |
| 678 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 661 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 679 | 662 |
| 680 request_gcm_statistics_callback_ = callback; | 663 request_gcm_statistics_callback_ = callback; |
| 681 content::BrowserThread::PostTask( | 664 io_thread_->PostTask( |
| 682 content::BrowserThread::IO, | |
| 683 FROM_HERE, | 665 FROM_HERE, |
| 684 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, | 666 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, |
| 685 base::Unretained(io_worker_.get()), | 667 base::Unretained(io_worker_.get()), |
| 686 recording)); | 668 recording)); |
| 687 } | 669 } |
| 688 | 670 |
| 689 void GCMDriver::OnActiveAccountLogin() { | 671 void GCMDriver::OnActiveAccountLogin() { |
| 690 EnsureStarted(); | 672 EnsureStarted(); |
| 691 } | 673 } |
| 692 | 674 |
| 693 void GCMDriver::OnActiveAccountLogout() { | 675 void GCMDriver::OnActiveAccountLogout() { |
| 694 CheckOut(); | 676 CheckOut(); |
| 695 } | 677 } |
| 696 | 678 |
| 697 GCMClient::Result GCMDriver::EnsureStarted() { | 679 GCMClient::Result GCMDriver::EnsureStarted() { |
| 698 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 680 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 699 | 681 |
| 700 if (!gcm_enabled_) | 682 if (!gcm_enabled_) |
| 701 return GCMClient::GCM_DISABLED; | 683 return GCMClient::GCM_DISABLED; |
| 702 | 684 |
| 703 // Is the user signed in? | 685 // Is the user signed in? |
| 704 const std::string account_id = identity_provider_->GetActiveAccountId(); | 686 const std::string account_id = identity_provider_->GetActiveAccountId(); |
| 705 if (account_id.empty()) | 687 if (account_id.empty()) |
| 706 return GCMClient::NOT_SIGNED_IN; | 688 return GCMClient::NOT_SIGNED_IN; |
| 707 | 689 |
| 708 // CheckIn could be called more than once when: | 690 // CheckIn could be called more than once when: |
| 709 // 1) The password changes. | 691 // 1) The password changes. |
| 710 // 2) Register/send function calls it to ensure CheckIn is done. | 692 // 2) Register/send function calls it to ensure CheckIn is done. |
| 711 if (account_id_ == account_id) | 693 if (account_id_ == account_id) |
| 712 return GCMClient::SUCCESS; | 694 return GCMClient::SUCCESS; |
| 713 account_id_ = account_id; | 695 account_id_ = account_id; |
| 714 | 696 |
| 715 DCHECK(!delayed_task_controller_); | 697 DCHECK(!delayed_task_controller_); |
| 716 delayed_task_controller_.reset(new DelayedTaskController); | 698 delayed_task_controller_.reset(new DelayedTaskController); |
| 717 | 699 |
| 718 // Note that we need to pass weak pointer again since the existing weak | 700 // Note that we need to pass weak pointer again since the existing weak |
| 719 // pointer in IOWorker might have been invalidated when check-out occurs. | 701 // pointer in IOWorker might have been invalidated when check-out occurs. |
| 720 content::BrowserThread::PostTask( | 702 io_thread_->PostTask( |
| 721 content::BrowserThread::IO, | |
| 722 FROM_HERE, | 703 FROM_HERE, |
| 723 base::Bind(&GCMDriver::IOWorker::Start, | 704 base::Bind(&GCMDriver::IOWorker::Start, |
| 724 base::Unretained(io_worker_.get()), | 705 base::Unretained(io_worker_.get()), |
| 725 weak_ptr_factory_.GetWeakPtr())); | 706 weak_ptr_factory_.GetWeakPtr())); |
| 726 | 707 |
| 727 return GCMClient::SUCCESS; | 708 return GCMClient::SUCCESS; |
| 728 } | 709 } |
| 729 | 710 |
| 730 void GCMDriver::RemoveCachedData() { | 711 void GCMDriver::RemoveCachedData() { |
| 731 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 712 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 732 // Remove all the queued tasks since they no longer make sense after | 713 // Remove all the queued tasks since they no longer make sense after |
| 733 // GCM service is stopped. | 714 // GCM service is stopped. |
| 734 weak_ptr_factory_.InvalidateWeakPtrs(); | 715 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 735 | 716 |
| 736 account_id_.clear(); | 717 account_id_.clear(); |
| 737 gcm_client_ready_ = false; | 718 gcm_client_ready_ = false; |
| 738 delayed_task_controller_.reset(); | 719 delayed_task_controller_.reset(); |
| 739 register_callbacks_.clear(); | 720 register_callbacks_.clear(); |
| 740 send_callbacks_.clear(); | 721 send_callbacks_.clear(); |
| 741 } | 722 } |
| 742 | 723 |
| 743 void GCMDriver::CheckOut() { | 724 void GCMDriver::CheckOut() { |
| 744 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 725 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 745 | 726 |
| 746 // We still proceed with the check-out logic even if the check-in is not | 727 // We still proceed with the check-out logic even if the check-in is not |
| 747 // initiated in the current session. This will make sure that all the | 728 // initiated in the current session. This will make sure that all the |
| 748 // persisted data written previously will get purged. | 729 // persisted data written previously will get purged. |
| 749 | 730 |
| 750 RemoveCachedData(); | 731 RemoveCachedData(); |
| 751 | 732 |
| 752 content::BrowserThread::PostTask( | 733 io_thread_->PostTask( |
| 753 content::BrowserThread::IO, | |
| 754 FROM_HERE, | 734 FROM_HERE, |
| 755 base::Bind(&GCMDriver::IOWorker::CheckOut, | 735 base::Bind(&GCMDriver::IOWorker::CheckOut, |
| 756 base::Unretained(io_worker_.get()))); | 736 base::Unretained(io_worker_.get()))); |
| 757 } | 737 } |
| 758 | 738 |
| 759 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { | 739 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { |
| 760 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 740 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 761 return register_callbacks_.find(app_id) != register_callbacks_.end() || | 741 return register_callbacks_.find(app_id) != register_callbacks_.end() || |
| 762 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); | 742 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); |
| 763 } | 743 } |
| 764 | 744 |
| 765 void GCMDriver::RegisterFinished(const std::string& app_id, | 745 void GCMDriver::RegisterFinished(const std::string& app_id, |
| 766 const std::string& registration_id, | 746 const std::string& registration_id, |
| 767 GCMClient::Result result) { | 747 GCMClient::Result result) { |
| 768 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 748 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 769 | 749 |
| 770 std::map<std::string, RegisterCallback>::iterator callback_iter = | 750 std::map<std::string, RegisterCallback>::iterator callback_iter = |
| 771 register_callbacks_.find(app_id); | 751 register_callbacks_.find(app_id); |
| 772 if (callback_iter == register_callbacks_.end()) { | 752 if (callback_iter == register_callbacks_.end()) { |
| 773 // The callback could have been removed when the app is uninstalled. | 753 // The callback could have been removed when the app is uninstalled. |
| 774 return; | 754 return; |
| 775 } | 755 } |
| 776 | 756 |
| 777 RegisterCallback callback = callback_iter->second; | 757 RegisterCallback callback = callback_iter->second; |
| 778 register_callbacks_.erase(callback_iter); | 758 register_callbacks_.erase(callback_iter); |
| 779 callback.Run(registration_id, result); | 759 callback.Run(registration_id, result); |
| 780 } | 760 } |
| 781 | 761 |
| 782 void GCMDriver::UnregisterFinished(const std::string& app_id, | 762 void GCMDriver::UnregisterFinished(const std::string& app_id, |
| 783 GCMClient::Result result) { | 763 GCMClient::Result result) { |
| 784 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 764 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 785 | 765 |
| 786 std::map<std::string, UnregisterCallback>::iterator callback_iter = | 766 std::map<std::string, UnregisterCallback>::iterator callback_iter = |
| 787 unregister_callbacks_.find(app_id); | 767 unregister_callbacks_.find(app_id); |
| 788 if (callback_iter == unregister_callbacks_.end()) | 768 if (callback_iter == unregister_callbacks_.end()) |
| 789 return; | 769 return; |
| 790 | 770 |
| 791 UnregisterCallback callback = callback_iter->second; | 771 UnregisterCallback callback = callback_iter->second; |
| 792 unregister_callbacks_.erase(callback_iter); | 772 unregister_callbacks_.erase(callback_iter); |
| 793 callback.Run(result); | 773 callback.Run(result); |
| 794 } | 774 } |
| 795 | 775 |
| 796 void GCMDriver::SendFinished(const std::string& app_id, | 776 void GCMDriver::SendFinished(const std::string& app_id, |
| 797 const std::string& message_id, | 777 const std::string& message_id, |
| 798 GCMClient::Result result) { | 778 GCMClient::Result result) { |
| 799 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 779 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 800 | 780 |
| 801 std::map<std::pair<std::string, std::string>, SendCallback>::iterator | 781 std::map<std::pair<std::string, std::string>, SendCallback>::iterator |
| 802 callback_iter = send_callbacks_.find( | 782 callback_iter = send_callbacks_.find( |
| 803 std::pair<std::string, std::string>(app_id, message_id)); | 783 std::pair<std::string, std::string>(app_id, message_id)); |
| 804 if (callback_iter == send_callbacks_.end()) { | 784 if (callback_iter == send_callbacks_.end()) { |
| 805 // The callback could have been removed when the app is uninstalled. | 785 // The callback could have been removed when the app is uninstalled. |
| 806 return; | 786 return; |
| 807 } | 787 } |
| 808 | 788 |
| 809 SendCallback callback = callback_iter->second; | 789 SendCallback callback = callback_iter->second; |
| 810 send_callbacks_.erase(callback_iter); | 790 send_callbacks_.erase(callback_iter); |
| 811 callback.Run(message_id, result); | 791 callback.Run(message_id, result); |
| 812 } | 792 } |
| 813 | 793 |
| 814 void GCMDriver::MessageReceived(const std::string& app_id, | 794 void GCMDriver::MessageReceived(const std::string& app_id, |
| 815 GCMClient::IncomingMessage message) { | 795 GCMClient::IncomingMessage message) { |
| 816 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 796 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 817 | 797 |
| 818 // Drop the event if signed out. | 798 // Drop the event if signed out. |
| 819 if (account_id_.empty()) | 799 if (account_id_.empty()) |
| 820 return; | 800 return; |
| 821 | 801 |
| 822 GetAppHandler(app_id)->OnMessage(app_id, message); | 802 GetAppHandler(app_id)->OnMessage(app_id, message); |
| 823 } | 803 } |
| 824 | 804 |
| 825 void GCMDriver::MessagesDeleted(const std::string& app_id) { | 805 void GCMDriver::MessagesDeleted(const std::string& app_id) { |
| 826 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 806 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 827 | 807 |
| 828 // Drop the event if signed out. | 808 // Drop the event if signed out. |
| 829 if (account_id_.empty()) | 809 if (account_id_.empty()) |
| 830 return; | 810 return; |
| 831 | 811 |
| 832 GetAppHandler(app_id)->OnMessagesDeleted(app_id); | 812 GetAppHandler(app_id)->OnMessagesDeleted(app_id); |
| 833 } | 813 } |
| 834 | 814 |
| 835 void GCMDriver::MessageSendError( | 815 void GCMDriver::MessageSendError( |
| 836 const std::string& app_id, | 816 const std::string& app_id, |
| 837 const GCMClient::SendErrorDetails& send_error_details) { | 817 const GCMClient::SendErrorDetails& send_error_details) { |
| 838 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 818 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 839 | 819 |
| 840 // Drop the event if signed out. | 820 // Drop the event if signed out. |
| 841 if (account_id_.empty()) | 821 if (account_id_.empty()) |
| 842 return; | 822 return; |
| 843 | 823 |
| 844 GetAppHandler(app_id)->OnSendError(app_id, send_error_details); | 824 GetAppHandler(app_id)->OnSendError(app_id, send_error_details); |
| 845 } | 825 } |
| 846 | 826 |
| 847 void GCMDriver::GCMClientReady() { | 827 void GCMDriver::GCMClientReady() { |
| 848 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 828 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 849 | 829 |
| 850 if (gcm_client_ready_) | 830 if (gcm_client_ready_) |
| 851 return; | 831 return; |
| 852 gcm_client_ready_ = true; | 832 gcm_client_ready_ = true; |
| 853 | 833 |
| 854 delayed_task_controller_->SetReady(); | 834 delayed_task_controller_->SetReady(); |
| 855 } | 835 } |
| 856 | 836 |
| 857 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { | 837 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { |
| 858 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 838 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 859 | 839 |
| 860 std::map<std::string, GCMAppHandler*>::const_iterator iter = | 840 std::map<std::string, GCMAppHandler*>::const_iterator iter = |
| 861 app_handlers_.find(app_id); | 841 app_handlers_.find(app_id); |
| 862 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; | 842 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; |
| 863 } | 843 } |
| 864 | 844 |
| 865 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { | 845 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { |
| 866 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 846 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 867 | 847 |
| 868 // Normally request_gcm_statistics_callback_ would not be null. | 848 // Normally request_gcm_statistics_callback_ would not be null. |
| 869 if (!request_gcm_statistics_callback_.is_null()) | 849 if (!request_gcm_statistics_callback_.is_null()) |
| 870 request_gcm_statistics_callback_.Run(stats); | 850 request_gcm_statistics_callback_.Run(stats); |
| 871 else | 851 else |
| 872 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 852 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 873 } | 853 } |
| 874 | 854 |
| 875 std::string GCMDriver::SignedInUserName() const { | 855 std::string GCMDriver::SignedInUserName() const { |
| 876 if (IsStarted()) | 856 if (IsStarted()) |
| 877 return identity_provider_->GetActiveUsername(); | 857 return identity_provider_->GetActiveUsername(); |
| 878 return std::string(); | 858 return std::string(); |
| 879 } | 859 } |
| 880 | 860 |
| 881 } // namespace gcm | 861 } // namespace gcm |
| OLD | NEW |