OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/gcm/gcm_api.h" | 5 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 const char kInvalidParameter[] = | 28 const char kInvalidParameter[] = |
29 "Function was called with invalid parameters."; | 29 "Function was called with invalid parameters."; |
30 const char kNotSignedIn[] = "Profile was not signed in."; | 30 const char kNotSignedIn[] = "Profile was not signed in."; |
31 const char kAsyncOperationPending[] = | 31 const char kAsyncOperationPending[] = |
32 "Asynchronous operation is pending."; | 32 "Asynchronous operation is pending."; |
33 const char kNetworkError[] = "Network error occurred."; | 33 const char kNetworkError[] = "Network error occurred."; |
34 const char kServerError[] = "Server error occurred."; | 34 const char kServerError[] = "Server error occurred."; |
35 const char kTtlExceeded[] = "Time-to-live exceeded."; | 35 const char kTtlExceeded[] = "Time-to-live exceeded."; |
36 const char kUnknownError[] = "Unknown error occurred."; | 36 const char kUnknownError[] = "Unknown error occurred."; |
37 | 37 |
38 const char* GcmResultToError(gcm::GCMClient::Result result) { | 38 const char* GcmResultToError(gcm::Result result) { |
39 switch (result) { | 39 switch (result) { |
40 case gcm::GCMClient::SUCCESS: | 40 case gcm::RESULT_SUCCESS: |
41 return ""; | 41 return ""; |
42 case gcm::GCMClient::INVALID_PARAMETER: | 42 case gcm::RESULT_INVALID_PARAMETER: |
43 return kInvalidParameter; | 43 return kInvalidParameter; |
44 case gcm::GCMClient::NOT_SIGNED_IN: | 44 case gcm::RESULT_NOT_SIGNED_IN: |
45 return kNotSignedIn; | 45 return kNotSignedIn; |
46 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 46 case gcm::RESULT_ASYNC_OPERATION_PENDING: |
47 return kAsyncOperationPending; | 47 return kAsyncOperationPending; |
48 case gcm::GCMClient::NETWORK_ERROR: | 48 case gcm::RESULT_NETWORK_ERROR: |
49 return kNetworkError; | 49 return kNetworkError; |
50 case gcm::GCMClient::SERVER_ERROR: | 50 case gcm::RESULT_SERVER_ERROR: |
51 return kServerError; | 51 return kServerError; |
52 case gcm::GCMClient::TTL_EXCEEDED: | 52 case gcm::RESULT_TTL_EXCEEDED: |
53 return kTtlExceeded; | 53 return kTtlExceeded; |
54 case gcm::GCMClient::UNKNOWN_ERROR: | 54 case gcm::RESULT_UNKNOWN_ERROR: |
55 return kUnknownError; | 55 return kUnknownError; |
56 default: | 56 default: |
57 NOTREACHED() << "Unexpected value of result cannot be converted: " | 57 NOTREACHED() << "Unexpected value of result cannot be converted: " |
58 << result; | 58 << result; |
59 } | 59 } |
60 | 60 |
61 // Never reached, but prevents missing return statement warning. | 61 // Never reached, but prevents missing return statement warning. |
62 return ""; | 62 return ""; |
63 } | 63 } |
64 | 64 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 GCMProfileService()->Register( | 113 GCMProfileService()->Register( |
114 GetExtension()->id(), | 114 GetExtension()->id(), |
115 params->sender_ids, | 115 params->sender_ids, |
116 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); | 116 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); |
117 | 117 |
118 return true; | 118 return true; |
119 } | 119 } |
120 | 120 |
121 void GcmRegisterFunction::CompleteFunctionWithResult( | 121 void GcmRegisterFunction::CompleteFunctionWithResult( |
122 const std::string& registration_id, | 122 const std::string& registration_id, |
123 gcm::GCMClient::Result result) { | 123 gcm::Result result) { |
124 SetResult(new base::StringValue(registration_id)); | 124 SetResult(new base::StringValue(registration_id)); |
125 SetError(GcmResultToError(result)); | 125 SetError(GcmResultToError(result)); |
126 SendResponse(gcm::GCMClient::SUCCESS == result); | 126 SendResponse(gcm::RESULT_SUCCESS == result); |
127 } | 127 } |
128 | 128 |
129 GcmUnregisterFunction::GcmUnregisterFunction() {} | 129 GcmUnregisterFunction::GcmUnregisterFunction() {} |
130 | 130 |
131 GcmUnregisterFunction::~GcmUnregisterFunction() {} | 131 GcmUnregisterFunction::~GcmUnregisterFunction() {} |
132 | 132 |
133 bool GcmUnregisterFunction::DoWork() { | 133 bool GcmUnregisterFunction::DoWork() { |
134 GCMProfileService()->Unregister( | 134 GCMProfileService()->Unregister( |
135 GetExtension()->id(), | 135 GetExtension()->id(), |
136 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); | 136 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); |
137 | 137 |
138 return true; | 138 return true; |
139 } | 139 } |
140 | 140 |
141 void GcmUnregisterFunction::CompleteFunctionWithResult( | 141 void GcmUnregisterFunction::CompleteFunctionWithResult( |
142 gcm::GCMClient::Result result) { | 142 gcm::Result result) { |
143 SetError(GcmResultToError(result)); | 143 SetError(GcmResultToError(result)); |
144 SendResponse(gcm::GCMClient::SUCCESS == result); | 144 SendResponse(gcm::RESULT_SUCCESS == result); |
145 } | 145 } |
146 | 146 |
147 GcmSendFunction::GcmSendFunction() {} | 147 GcmSendFunction::GcmSendFunction() {} |
148 | 148 |
149 GcmSendFunction::~GcmSendFunction() {} | 149 GcmSendFunction::~GcmSendFunction() {} |
150 | 150 |
151 bool GcmSendFunction::DoWork() { | 151 bool GcmSendFunction::DoWork() { |
152 scoped_ptr<api::gcm::Send::Params> params( | 152 scoped_ptr<api::gcm::Send::Params> params( |
153 api::gcm::Send::Params::Create(*args_)); | 153 api::gcm::Send::Params::Create(*args_)); |
154 EXTENSION_FUNCTION_VALIDATE(params.get()); | 154 EXTENSION_FUNCTION_VALIDATE(params.get()); |
155 EXTENSION_FUNCTION_VALIDATE( | 155 EXTENSION_FUNCTION_VALIDATE( |
156 ValidateMessageData(params->message.data.additional_properties)); | 156 ValidateMessageData(params->message.data.additional_properties)); |
157 | 157 |
158 gcm::GCMClient::OutgoingMessage outgoing_message; | 158 gcm::OutgoingMessage outgoing_message; |
159 outgoing_message.id = params->message.message_id; | 159 outgoing_message.id = params->message.message_id; |
160 outgoing_message.data = params->message.data.additional_properties; | 160 outgoing_message.data = params->message.data.additional_properties; |
161 if (params->message.time_to_live.get()) | 161 if (params->message.time_to_live.get()) |
162 outgoing_message.time_to_live = *params->message.time_to_live; | 162 outgoing_message.time_to_live = *params->message.time_to_live; |
163 | 163 |
164 GCMProfileService()->Send( | 164 GCMProfileService()->Send( |
165 GetExtension()->id(), | 165 GetExtension()->id(), |
166 params->message.destination_id, | 166 params->message.destination_id, |
167 outgoing_message, | 167 outgoing_message, |
168 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); | 168 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); |
169 | 169 |
170 return true; | 170 return true; |
171 } | 171 } |
172 | 172 |
173 void GcmSendFunction::CompleteFunctionWithResult( | 173 void GcmSendFunction::CompleteFunctionWithResult( |
174 const std::string& message_id, | 174 const std::string& message_id, |
175 gcm::GCMClient::Result result) { | 175 gcm::Result result) { |
176 SetResult(new base::StringValue(message_id)); | 176 SetResult(new base::StringValue(message_id)); |
177 SetError(GcmResultToError(result)); | 177 SetError(GcmResultToError(result)); |
178 SendResponse(gcm::GCMClient::SUCCESS == result); | 178 SendResponse(gcm::RESULT_SUCCESS == result); |
179 } | 179 } |
180 | 180 |
181 bool GcmSendFunction::ValidateMessageData( | 181 bool GcmSendFunction::ValidateMessageData( |
182 const gcm::GCMClient::MessageData& data) const { | 182 const gcm::MessageData& data) const { |
183 size_t total_size = 0u; | 183 size_t total_size = 0u; |
184 for (std::map<std::string, std::string>::const_iterator iter = data.begin(); | 184 for (std::map<std::string, std::string>::const_iterator iter = data.begin(); |
185 iter != data.end(); ++iter) { | 185 iter != data.end(); ++iter) { |
186 total_size += iter->first.size() + iter->second.size(); | 186 total_size += iter->first.size() + iter->second.size(); |
187 | 187 |
188 if (!IsMessageKeyValid(iter->first) || | 188 if (!IsMessageKeyValid(iter->first) || |
189 kMaximumMessageSize < iter->first.size() || | 189 kMaximumMessageSize < iter->first.size() || |
190 kMaximumMessageSize < iter->second.size() || | 190 kMaximumMessageSize < iter->second.size() || |
191 kMaximumMessageSize < total_size) | 191 kMaximumMessageSize < total_size) |
192 return false; | 192 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
206 } | 206 } |
207 | 207 |
208 GcmJsEventRouter::~GcmJsEventRouter() { | 208 GcmJsEventRouter::~GcmJsEventRouter() { |
209 EventRouter* event_router = EventRouter::Get(profile_); | 209 EventRouter* event_router = EventRouter::Get(profile_); |
210 if (event_router) | 210 if (event_router) |
211 event_router->UnregisterObserver(this); | 211 event_router->UnregisterObserver(this); |
212 } | 212 } |
213 | 213 |
214 void GcmJsEventRouter::OnMessage( | 214 void GcmJsEventRouter::OnMessage( |
215 const std::string& app_id, | 215 const std::string& app_id, |
216 const gcm::GCMClient::IncomingMessage& message) { | 216 const gcm::IncomingMessage& message) { |
217 api::gcm::OnMessage::Message message_arg; | 217 api::gcm::OnMessage::Message message_arg; |
218 message_arg.data.additional_properties = message.data; | 218 message_arg.data.additional_properties = message.data; |
219 if (!message.collapse_key.empty()) | 219 if (!message.collapse_key.empty()) |
220 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 220 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
221 | 221 |
222 scoped_ptr<Event> event(new Event( | 222 scoped_ptr<Event> event(new Event( |
223 api::gcm::OnMessage::kEventName, | 223 api::gcm::OnMessage::kEventName, |
224 api::gcm::OnMessage::Create(message_arg).Pass(), | 224 api::gcm::OnMessage::Create(message_arg).Pass(), |
225 profile_)); | 225 profile_)); |
226 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); | 226 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
227 } | 227 } |
228 | 228 |
229 void GcmJsEventRouter::OnMessagesDeleted(const std::string& app_id) { | 229 void GcmJsEventRouter::OnMessagesDeleted(const std::string& app_id) { |
230 scoped_ptr<Event> event(new Event( | 230 scoped_ptr<Event> event(new Event( |
231 api::gcm::OnMessagesDeleted::kEventName, | 231 api::gcm::OnMessagesDeleted::kEventName, |
232 api::gcm::OnMessagesDeleted::Create().Pass(), | 232 api::gcm::OnMessagesDeleted::Create().Pass(), |
233 profile_)); | 233 profile_)); |
234 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); | 234 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
235 } | 235 } |
236 | 236 |
237 void GcmJsEventRouter::OnSendError( | 237 void GcmJsEventRouter::OnSendError( |
238 const std::string& app_id, | 238 const std::string& app_id, |
239 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 239 const gcm::SendErrorDetails& send_error_details) { |
240 api::gcm::OnSendError::Error error; | 240 api::gcm::OnSendError::Error error; |
241 error.message_id.reset(new std::string(send_error_details.message_id)); | 241 error.message_id.reset(new std::string(send_error_details.message_id)); |
242 error.error_message = GcmResultToError(send_error_details.result); | 242 error.error_message = GcmResultToError(send_error_details.result); |
243 error.details.additional_properties = send_error_details.additional_data; | 243 error.details.additional_properties = send_error_details.additional_data; |
244 | 244 |
245 scoped_ptr<Event> event(new Event( | 245 scoped_ptr<Event> event(new Event( |
246 api::gcm::OnSendError::kEventName, | 246 api::gcm::OnSendError::kEventName, |
247 api::gcm::OnSendError::Create(error).Pass(), | 247 api::gcm::OnSendError::Create(error).Pass(), |
248 profile_)); | 248 profile_)); |
249 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); | 249 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
250 } | 250 } |
251 | 251 |
252 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { | 252 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { |
253 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | 253 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == |
254 gcm::GCMProfileService::ALWAYS_DISABLED) { | 254 gcm::GCMProfileService::ALWAYS_DISABLED) { |
255 return; | 255 return; |
256 } | 256 } |
257 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); | 257 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); |
258 } | 258 } |
259 | 259 |
260 } // namespace extensions | 260 } // namespace extensions |
OLD | NEW |