| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 normalized_sender_ids, | 63 normalized_sender_ids, |
| 64 unregister_iter->second); | 64 unregister_iter->second); |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 RegisterImpl(app_id, normalized_sender_ids); | 68 RegisterImpl(app_id, normalized_sender_ids); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void GCMDriver::Unregister(const std::string& app_id, | 71 void GCMDriver::Unregister(const std::string& app_id, |
| 72 const UnregisterCallback& callback) { | 72 const UnregisterCallback& callback) { |
| 73 UnregisterInternal(app_id, nullptr /* sender_id */, callback); |
| 74 } |
| 75 |
| 76 void GCMDriver::UnregisterWithSenderId( |
| 77 const std::string& app_id, |
| 78 const std::string& sender_id, |
| 79 const UnregisterCallback& callback) { |
| 80 DCHECK(!sender_id.empty()); |
| 81 UnregisterInternal(app_id, &sender_id, callback); |
| 82 } |
| 83 |
| 84 void GCMDriver::UnregisterInternal(const std::string& app_id, |
| 85 const std::string* sender_id, |
| 86 const UnregisterCallback& callback) { |
| 73 DCHECK(!app_id.empty()); | 87 DCHECK(!app_id.empty()); |
| 74 DCHECK(!callback.is_null()); | 88 DCHECK(!callback.is_null()); |
| 75 | 89 |
| 76 GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); | 90 GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
| 77 if (result != GCMClient::SUCCESS) { | 91 if (result != GCMClient::SUCCESS) { |
| 78 callback.Run(result); | 92 callback.Run(result); |
| 79 return; | 93 return; |
| 80 } | 94 } |
| 81 | 95 |
| 82 // If previous un/register operation is still in progress, bail out. | 96 // If previous un/register operation is still in progress, bail out. |
| 83 if (register_callbacks_.find(app_id) != register_callbacks_.end() || | 97 if (register_callbacks_.find(app_id) != register_callbacks_.end() || |
| 84 unregister_callbacks_.find(app_id) != unregister_callbacks_.end()) { | 98 unregister_callbacks_.find(app_id) != unregister_callbacks_.end()) { |
| 85 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); | 99 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); |
| 86 return; | 100 return; |
| 87 } | 101 } |
| 88 | 102 |
| 89 unregister_callbacks_[app_id] = callback; | 103 unregister_callbacks_[app_id] = callback; |
| 90 | 104 |
| 91 UnregisterImpl(app_id); | 105 if (sender_id) |
| 106 UnregisterWithSenderIdImpl(app_id, *sender_id); |
| 107 else |
| 108 UnregisterImpl(app_id); |
| 92 } | 109 } |
| 93 | 110 |
| 94 void GCMDriver::Send(const std::string& app_id, | 111 void GCMDriver::Send(const std::string& app_id, |
| 95 const std::string& receiver_id, | 112 const std::string& receiver_id, |
| 96 const GCMClient::OutgoingMessage& message, | 113 const GCMClient::OutgoingMessage& message, |
| 97 const SendCallback& callback) { | 114 const SendCallback& callback) { |
| 98 DCHECK(!app_id.empty()); | 115 DCHECK(!app_id.empty()); |
| 99 DCHECK(!receiver_id.empty()); | 116 DCHECK(!receiver_id.empty()); |
| 100 DCHECK(!callback.is_null()); | 117 DCHECK(!callback.is_null()); |
| 101 | 118 |
| 102 GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); | 119 GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START); |
| 103 if (result != GCMClient::SUCCESS) { | 120 if (result != GCMClient::SUCCESS) { |
| 104 callback.Run(std::string(), result); | 121 callback.Run(std::string(), result); |
| 105 return; | 122 return; |
| 106 } | 123 } |
| 107 | 124 |
| 108 // If the message with send ID is still in progress, bail out. | 125 // If the message with send ID is still in progress, bail out. |
| 109 std::pair<std::string, std::string> key(app_id, message.id); | 126 std::pair<std::string, std::string> key(app_id, message.id); |
| 110 if (send_callbacks_.find(key) != send_callbacks_.end()) { | 127 if (send_callbacks_.find(key) != send_callbacks_.end()) { |
| 111 callback.Run(message.id, GCMClient::INVALID_PARAMETER); | 128 callback.Run(message.id, GCMClient::INVALID_PARAMETER); |
| 112 return; | 129 return; |
| 113 } | 130 } |
| 114 | 131 |
| 115 send_callbacks_[key] = callback; | 132 send_callbacks_[key] = callback; |
| 116 | 133 |
| 117 SendImpl(app_id, receiver_id, message); | 134 SendImpl(app_id, receiver_id, message); |
| 118 } | 135 } |
| 119 | 136 |
| 137 void GCMDriver::UnregisterWithSenderIdImpl(const std::string& app_id, |
| 138 const std::string& sender_id) { |
| 139 NOTREACHED(); |
| 140 } |
| 141 |
| 120 void GCMDriver::RegisterFinished(const std::string& app_id, | 142 void GCMDriver::RegisterFinished(const std::string& app_id, |
| 121 const std::string& registration_id, | 143 const std::string& registration_id, |
| 122 GCMClient::Result result) { | 144 GCMClient::Result result) { |
| 123 std::map<std::string, RegisterCallback>::iterator callback_iter = | 145 std::map<std::string, RegisterCallback>::iterator callback_iter = |
| 124 register_callbacks_.find(app_id); | 146 register_callbacks_.find(app_id); |
| 125 if (callback_iter == register_callbacks_.end()) { | 147 if (callback_iter == register_callbacks_.end()) { |
| 126 // The callback could have been removed when the app is uninstalled. | 148 // The callback could have been removed when the app is uninstalled. |
| 127 return; | 149 return; |
| 128 } | 150 } |
| 129 | 151 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 GCMClient::Result result) { | 238 GCMClient::Result result) { |
| 217 // Invoke the original unregister callback. | 239 // Invoke the original unregister callback. |
| 218 unregister_callback.Run(result); | 240 unregister_callback.Run(result); |
| 219 | 241 |
| 220 // Trigger the pending registration. | 242 // Trigger the pending registration. |
| 221 DCHECK(register_callbacks_.find(app_id) != register_callbacks_.end()); | 243 DCHECK(register_callbacks_.find(app_id) != register_callbacks_.end()); |
| 222 RegisterImpl(app_id, normalized_sender_ids); | 244 RegisterImpl(app_id, normalized_sender_ids); |
| 223 } | 245 } |
| 224 | 246 |
| 225 } // namespace gcm | 247 } // namespace gcm |
| OLD | NEW |