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 |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/services/gcm/gcm_driver.h" | |
16 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
18 #include "chrome/common/extensions/api/gcm.h" | 19 #include "chrome/common/extensions/api/gcm.h" |
20 #include "extensions/browser/event_router.h" | |
19 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 const size_t kMaximumMessageSize = 4096; // in bytes. | 25 const size_t kMaximumMessageSize = 4096; // in bytes. |
24 const char kCollapseKey[] = "collapse_key"; | 26 const char kCollapseKey[] = "collapse_key"; |
25 const char kGoogDotRestrictedPrefix[] = "goog."; | 27 const char kGoogDotRestrictedPrefix[] = "goog."; |
26 const char kGoogleRestrictedPrefix[] = "google"; | 28 const char kGoogleRestrictedPrefix[] = "google"; |
27 | 29 |
28 // Error messages. | 30 // Error messages. |
29 const char kInvalidParameter[] = | 31 const char kInvalidParameter[] = |
30 "Function was called with invalid parameters."; | 32 "Function was called with invalid parameters."; |
33 const char kGCMDisabled[] = "GCM was disabled. Please try again later."; | |
fgorski
2014/05/19 20:37:09
Why not simply
"GCM is currently disabled."
or
"GC
jianli
2014/05/19 23:03:48
Done.
| |
31 const char kNotSignedIn[] = "Profile was not signed in."; | 34 const char kNotSignedIn[] = "Profile was not signed in."; |
32 const char kAsyncOperationPending[] = | 35 const char kAsyncOperationPending[] = |
33 "Asynchronous operation is pending."; | 36 "Asynchronous operation is pending."; |
34 const char kNetworkError[] = "Network error occurred."; | 37 const char kNetworkError[] = "Network error occurred."; |
35 const char kServerError[] = "Server error occurred."; | 38 const char kServerError[] = "Server error occurred."; |
36 const char kTtlExceeded[] = "Time-to-live exceeded."; | 39 const char kTtlExceeded[] = "Time-to-live exceeded."; |
37 const char kUnknownError[] = "Unknown error occurred."; | 40 const char kUnknownError[] = "Unknown error occurred."; |
38 | 41 |
39 const char* GcmResultToError(gcm::GCMClient::Result result) { | 42 const char* GcmResultToError(gcm::GCMClient::Result result) { |
40 switch (result) { | 43 switch (result) { |
41 case gcm::GCMClient::SUCCESS: | 44 case gcm::GCMClient::SUCCESS: |
42 return ""; | 45 return ""; |
43 case gcm::GCMClient::INVALID_PARAMETER: | 46 case gcm::GCMClient::INVALID_PARAMETER: |
44 return kInvalidParameter; | 47 return kInvalidParameter; |
48 case gcm::GCMClient::GCM_DISABLED: | |
49 return kGCMDisabled; | |
45 case gcm::GCMClient::NOT_SIGNED_IN: | 50 case gcm::GCMClient::NOT_SIGNED_IN: |
46 return kNotSignedIn; | 51 return kNotSignedIn; |
47 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 52 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
48 return kAsyncOperationPending; | 53 return kAsyncOperationPending; |
49 case gcm::GCMClient::NETWORK_ERROR: | 54 case gcm::GCMClient::NETWORK_ERROR: |
50 return kNetworkError; | 55 return kNetworkError; |
51 case gcm::GCMClient::SERVER_ERROR: | 56 case gcm::GCMClient::SERVER_ERROR: |
52 return kServerError; | 57 return kServerError; |
53 case gcm::GCMClient::TTL_EXCEEDED: | 58 case gcm::GCMClient::TTL_EXCEEDED: |
54 return kTtlExceeded; | 59 return kTtlExceeded; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 Profile* profile = Profile::FromBrowserContext(browser_context()); | 95 Profile* profile = Profile::FromBrowserContext(browser_context()); |
91 | 96 |
92 // GCM is not supported in incognito mode. | 97 // GCM is not supported in incognito mode. |
93 if (profile->IsOffTheRecord()) | 98 if (profile->IsOffTheRecord()) |
94 return false; | 99 return false; |
95 | 100 |
96 return gcm::GCMProfileService::GetGCMEnabledState(profile) != | 101 return gcm::GCMProfileService::GetGCMEnabledState(profile) != |
97 gcm::GCMProfileService::ALWAYS_DISABLED; | 102 gcm::GCMProfileService::ALWAYS_DISABLED; |
98 } | 103 } |
99 | 104 |
100 gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const { | 105 gcm::GCMDriver* GcmApiFunction::GetGCMDriver() const { |
101 return gcm::GCMProfileServiceFactory::GetForProfile( | 106 return gcm::GCMProfileServiceFactory::GetForProfile( |
102 Profile::FromBrowserContext(browser_context())); | 107 Profile::FromBrowserContext(browser_context()))->driver(); |
103 } | 108 } |
104 | 109 |
105 GcmRegisterFunction::GcmRegisterFunction() {} | 110 GcmRegisterFunction::GcmRegisterFunction() {} |
106 | 111 |
107 GcmRegisterFunction::~GcmRegisterFunction() {} | 112 GcmRegisterFunction::~GcmRegisterFunction() {} |
108 | 113 |
109 bool GcmRegisterFunction::DoWork() { | 114 bool GcmRegisterFunction::DoWork() { |
110 scoped_ptr<api::gcm::Register::Params> params( | 115 scoped_ptr<api::gcm::Register::Params> params( |
111 api::gcm::Register::Params::Create(*args_)); | 116 api::gcm::Register::Params::Create(*args_)); |
112 EXTENSION_FUNCTION_VALIDATE(params.get()); | 117 EXTENSION_FUNCTION_VALIDATE(params.get()); |
113 | 118 |
114 GCMProfileService()->Register( | 119 GetGCMDriver()->Register( |
115 GetExtension()->id(), | 120 GetExtension()->id(), |
116 params->sender_ids, | 121 params->sender_ids, |
117 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); | 122 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); |
118 | 123 |
119 return true; | 124 return true; |
120 } | 125 } |
121 | 126 |
122 void GcmRegisterFunction::CompleteFunctionWithResult( | 127 void GcmRegisterFunction::CompleteFunctionWithResult( |
123 const std::string& registration_id, | 128 const std::string& registration_id, |
124 gcm::GCMClient::Result result) { | 129 gcm::GCMClient::Result result) { |
125 SetResult(new base::StringValue(registration_id)); | 130 SetResult(new base::StringValue(registration_id)); |
126 SetError(GcmResultToError(result)); | 131 SetError(GcmResultToError(result)); |
127 SendResponse(gcm::GCMClient::SUCCESS == result); | 132 SendResponse(gcm::GCMClient::SUCCESS == result); |
128 } | 133 } |
129 | 134 |
130 GcmUnregisterFunction::GcmUnregisterFunction() {} | 135 GcmUnregisterFunction::GcmUnregisterFunction() {} |
131 | 136 |
132 GcmUnregisterFunction::~GcmUnregisterFunction() {} | 137 GcmUnregisterFunction::~GcmUnregisterFunction() {} |
133 | 138 |
134 bool GcmUnregisterFunction::DoWork() { | 139 bool GcmUnregisterFunction::DoWork() { |
135 UMA_HISTOGRAM_BOOLEAN("GCM.APICallUnregister", true); | 140 UMA_HISTOGRAM_BOOLEAN("GCM.APICallUnregister", true); |
136 | 141 |
137 GCMProfileService()->Unregister( | 142 GetGCMDriver()->Unregister( |
138 GetExtension()->id(), | 143 GetExtension()->id(), |
139 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); | 144 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); |
140 | 145 |
141 return true; | 146 return true; |
142 } | 147 } |
143 | 148 |
144 void GcmUnregisterFunction::CompleteFunctionWithResult( | 149 void GcmUnregisterFunction::CompleteFunctionWithResult( |
145 gcm::GCMClient::Result result) { | 150 gcm::GCMClient::Result result) { |
146 SetError(GcmResultToError(result)); | 151 SetError(GcmResultToError(result)); |
147 SendResponse(gcm::GCMClient::SUCCESS == result); | 152 SendResponse(gcm::GCMClient::SUCCESS == result); |
148 } | 153 } |
149 | 154 |
150 GcmSendFunction::GcmSendFunction() {} | 155 GcmSendFunction::GcmSendFunction() {} |
151 | 156 |
152 GcmSendFunction::~GcmSendFunction() {} | 157 GcmSendFunction::~GcmSendFunction() {} |
153 | 158 |
154 bool GcmSendFunction::DoWork() { | 159 bool GcmSendFunction::DoWork() { |
155 scoped_ptr<api::gcm::Send::Params> params( | 160 scoped_ptr<api::gcm::Send::Params> params( |
156 api::gcm::Send::Params::Create(*args_)); | 161 api::gcm::Send::Params::Create(*args_)); |
157 EXTENSION_FUNCTION_VALIDATE(params.get()); | 162 EXTENSION_FUNCTION_VALIDATE(params.get()); |
158 EXTENSION_FUNCTION_VALIDATE( | 163 EXTENSION_FUNCTION_VALIDATE( |
159 ValidateMessageData(params->message.data.additional_properties)); | 164 ValidateMessageData(params->message.data.additional_properties)); |
160 | 165 |
161 gcm::GCMClient::OutgoingMessage outgoing_message; | 166 gcm::GCMClient::OutgoingMessage outgoing_message; |
162 outgoing_message.id = params->message.message_id; | 167 outgoing_message.id = params->message.message_id; |
163 outgoing_message.data = params->message.data.additional_properties; | 168 outgoing_message.data = params->message.data.additional_properties; |
164 if (params->message.time_to_live.get()) | 169 if (params->message.time_to_live.get()) |
165 outgoing_message.time_to_live = *params->message.time_to_live; | 170 outgoing_message.time_to_live = *params->message.time_to_live; |
166 | 171 |
167 GCMProfileService()->Send( | 172 GetGCMDriver()->Send( |
168 GetExtension()->id(), | 173 GetExtension()->id(), |
169 params->message.destination_id, | 174 params->message.destination_id, |
170 outgoing_message, | 175 outgoing_message, |
171 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); | 176 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); |
172 | 177 |
173 return true; | 178 return true; |
174 } | 179 } |
175 | 180 |
176 void GcmSendFunction::CompleteFunctionWithResult( | 181 void GcmSendFunction::CompleteFunctionWithResult( |
177 const std::string& message_id, | 182 const std::string& message_id, |
(...skipping 14 matching lines...) Expand all Loading... | |
192 kMaximumMessageSize < iter->first.size() || | 197 kMaximumMessageSize < iter->first.size() || |
193 kMaximumMessageSize < iter->second.size() || | 198 kMaximumMessageSize < iter->second.size() || |
194 kMaximumMessageSize < total_size) | 199 kMaximumMessageSize < total_size) |
195 return false; | 200 return false; |
196 } | 201 } |
197 | 202 |
198 return total_size != 0; | 203 return total_size != 0; |
199 } | 204 } |
200 | 205 |
201 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { | 206 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { |
202 EventRouter* event_router = EventRouter::Get(profile_); | |
203 if (!event_router) | |
204 return; | |
205 | |
206 event_router->RegisterObserver(this, api::gcm::OnMessage::kEventName); | |
207 event_router->RegisterObserver(this, api::gcm::OnMessagesDeleted::kEventName); | |
208 event_router->RegisterObserver(this, api::gcm::OnSendError::kEventName); | |
209 } | 207 } |
210 | 208 |
211 GcmJsEventRouter::~GcmJsEventRouter() { | 209 GcmJsEventRouter::~GcmJsEventRouter() { |
212 EventRouter* event_router = EventRouter::Get(profile_); | |
213 if (event_router) | |
214 event_router->UnregisterObserver(this); | |
215 } | 210 } |
216 | 211 |
217 void GcmJsEventRouter::OnMessage( | 212 void GcmJsEventRouter::OnMessage( |
218 const std::string& app_id, | 213 const std::string& app_id, |
219 const gcm::GCMClient::IncomingMessage& message) { | 214 const gcm::GCMClient::IncomingMessage& message) { |
220 api::gcm::OnMessage::Message message_arg; | 215 api::gcm::OnMessage::Message message_arg; |
221 message_arg.data.additional_properties = message.data; | 216 message_arg.data.additional_properties = message.data; |
222 if (!message.collapse_key.empty()) | 217 if (!message.collapse_key.empty()) |
223 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 218 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
224 | 219 |
(...skipping 20 matching lines...) Expand all Loading... | |
245 error.error_message = GcmResultToError(send_error_details.result); | 240 error.error_message = GcmResultToError(send_error_details.result); |
246 error.details.additional_properties = send_error_details.additional_data; | 241 error.details.additional_properties = send_error_details.additional_data; |
247 | 242 |
248 scoped_ptr<Event> event(new Event( | 243 scoped_ptr<Event> event(new Event( |
249 api::gcm::OnSendError::kEventName, | 244 api::gcm::OnSendError::kEventName, |
250 api::gcm::OnSendError::Create(error).Pass(), | 245 api::gcm::OnSendError::Create(error).Pass(), |
251 profile_)); | 246 profile_)); |
252 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); | 247 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
253 } | 248 } |
254 | 249 |
255 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { | |
256 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | |
257 gcm::GCMProfileService::ALWAYS_DISABLED) { | |
258 return; | |
259 } | |
260 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); | |
261 } | |
262 | |
263 } // namespace extensions | 250 } // namespace extensions |
OLD | NEW |