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