| 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.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "components/gcm_driver/gcm_app_handler.h" | 10 #include "components/gcm_driver/gcm_app_handler.h" |
| 11 | 11 |
| 12 namespace gcm { | 12 namespace gcm { |
| 13 | 13 |
| 14 GCMDriver::GCMDriver() { | 14 GCMDriver::GCMDriver() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 GCMDriver::~GCMDriver() { | 17 GCMDriver::~GCMDriver() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 void GCMDriver::SetLazyLoadAppHandlersClosure( |
| 21 const base::Closure& lazy_load_app_handlers) { |
| 22 lazy_load_app_handlers_ = lazy_load_app_handlers; |
| 23 } |
| 24 |
| 20 void GCMDriver::Register(const std::string& app_id, | 25 void GCMDriver::Register(const std::string& app_id, |
| 21 const std::vector<std::string>& sender_ids, | 26 const std::vector<std::string>& sender_ids, |
| 22 const RegisterCallback& callback) { | 27 const RegisterCallback& callback) { |
| 23 DCHECK(!app_id.empty()); | 28 DCHECK(!app_id.empty()); |
| 24 DCHECK(!sender_ids.empty()); | 29 DCHECK(!sender_ids.empty()); |
| 25 DCHECK(!callback.is_null()); | 30 DCHECK(!callback.is_null()); |
| 26 | 31 |
| 27 GCMClient::Result result = EnsureStarted(); | 32 GCMClient::Result result = EnsureStarted(); |
| 28 if (result != GCMClient::SUCCESS) { | 33 if (result != GCMClient::SUCCESS) { |
| 29 callback.Run(std::string(), result); | 34 callback.Run(std::string(), result); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 iter != app_handlers_.end(); ++iter) { | 146 iter != app_handlers_.end(); ++iter) { |
| 142 iter->second->ShutdownHandler(); | 147 iter->second->ShutdownHandler(); |
| 143 } | 148 } |
| 144 app_handlers_.clear(); | 149 app_handlers_.clear(); |
| 145 } | 150 } |
| 146 | 151 |
| 147 void GCMDriver::AddAppHandler(const std::string& app_id, | 152 void GCMDriver::AddAppHandler(const std::string& app_id, |
| 148 GCMAppHandler* handler) { | 153 GCMAppHandler* handler) { |
| 149 DCHECK(!app_id.empty()); | 154 DCHECK(!app_id.empty()); |
| 150 DCHECK(handler); | 155 DCHECK(handler); |
| 151 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); | 156 DCHECK(app_handlers_.find(app_id) == app_handlers_.end() || |
| 157 app_handlers_[app_id] == handler); |
| 152 app_handlers_[app_id] = handler; | 158 app_handlers_[app_id] = handler; |
| 153 } | 159 } |
| 154 | 160 |
| 155 void GCMDriver::RemoveAppHandler(const std::string& app_id) { | 161 void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 156 DCHECK(!app_id.empty()); | 162 DCHECK(!app_id.empty()); |
| 157 app_handlers_.erase(app_id); | 163 app_handlers_.erase(app_id); |
| 158 } | 164 } |
| 159 | 165 |
| 160 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { | 166 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { |
| 167 if (!lazy_load_app_handlers_.is_null()) { |
| 168 lazy_load_app_handlers_.Run(); |
| 169 lazy_load_app_handlers_.Reset(); |
| 170 } |
| 161 GCMAppHandlerMap::const_iterator iter = app_handlers_.find(app_id); | 171 GCMAppHandlerMap::const_iterator iter = app_handlers_.find(app_id); |
| 162 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; | 172 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; |
| 163 } | 173 } |
| 164 | 174 |
| 165 bool GCMDriver::HasRegisterCallback(const std::string& app_id) { | 175 bool GCMDriver::HasRegisterCallback(const std::string& app_id) { |
| 166 return register_callbacks_.find(app_id) != register_callbacks_.end(); | 176 return register_callbacks_.find(app_id) != register_callbacks_.end(); |
| 167 } | 177 } |
| 168 | 178 |
| 169 void GCMDriver::ClearCallbacks() { | 179 void GCMDriver::ClearCallbacks() { |
| 170 register_callbacks_.clear(); | 180 register_callbacks_.clear(); |
| 171 unregister_callbacks_.clear(); | 181 unregister_callbacks_.clear(); |
| 172 send_callbacks_.clear(); | 182 send_callbacks_.clear(); |
| 173 } | 183 } |
| 174 | 184 |
| 175 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { | 185 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { |
| 176 return register_callbacks_.find(app_id) != register_callbacks_.end() || | 186 return register_callbacks_.find(app_id) != register_callbacks_.end() || |
| 177 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); | 187 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); |
| 178 } | 188 } |
| 179 | 189 |
| 180 } // namespace gcm | 190 } // namespace gcm |
| OLD | NEW |