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 "chrome/browser/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 int renderer_id, | 135 int renderer_id, |
136 int render_frame_id, | 136 int render_frame_id, |
137 bool user_gesture, | 137 bool user_gesture, |
138 const content::PushMessagingService::RegisterCallback& callback) { | 138 const content::PushMessagingService::RegisterCallback& callback) { |
139 if (!gcm_profile_service_->driver()) { | 139 if (!gcm_profile_service_->driver()) { |
140 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; | 140 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; |
141 } | 141 } |
142 | 142 |
143 if (profile_->GetPrefs()->GetInteger( | 143 if (profile_->GetPrefs()->GetInteger( |
144 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 144 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
145 DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); | 145 RegisterEnd( |
| 146 app_id, |
| 147 callback, |
| 148 std::string(), |
| 149 content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_LIMIT_REACHED); |
146 return; | 150 return; |
147 } | 151 } |
148 | 152 |
149 // If this is registering for the first time then the driver does not have | 153 // If this is registering for the first time then the driver does not have |
150 // this as an app handler and registration would fail. | 154 // this as an app handler and registration would fail. |
151 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) | 155 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) |
152 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); | 156 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); |
153 | 157 |
154 content::RenderFrameHost* render_frame_host = | 158 content::RenderFrameHost* render_frame_host = |
155 content::RenderFrameHost::FromID(renderer_id, render_frame_id); | 159 content::RenderFrameHost::FromID(renderer_id, render_frame_id); |
(...skipping 14 matching lines...) Expand all Loading... |
170 int bridge_id = -1; | 174 int bridge_id = -1; |
171 | 175 |
172 const PermissionRequestID id( | 176 const PermissionRequestID id( |
173 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); | 177 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); |
174 | 178 |
175 GURL embedder = web_contents->GetLastCommittedURL(); | 179 GURL embedder = web_contents->GetLastCommittedURL(); |
176 gcm::PushMessagingPermissionContext* permission_context = | 180 gcm::PushMessagingPermissionContext* permission_context = |
177 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 181 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
178 | 182 |
179 if (permission_context == NULL) { | 183 if (permission_context == NULL) { |
180 DidRegister(app_id, callback, std::string(), GCMClient::UNKNOWN_ERROR); | 184 RegisterEnd( |
| 185 app_id, |
| 186 callback, |
| 187 std::string(), |
| 188 content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED); |
181 return; | 189 return; |
182 } | 190 } |
183 | 191 |
184 permission_context->RequestPermission( | 192 permission_context->RequestPermission( |
185 web_contents, | 193 web_contents, |
186 id, | 194 id, |
187 embedder, | 195 embedder, |
188 user_gesture, | 196 user_gesture, |
189 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 197 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
190 weak_factory_.GetWeakPtr(), | 198 weak_factory_.GetWeakPtr(), |
191 sender_id, | 199 sender_id, |
192 app_id, | 200 app_id, |
193 callback)); | 201 callback)); |
194 } | 202 } |
195 | 203 |
196 void PushMessagingServiceImpl::DidRegister( | 204 void PushMessagingServiceImpl::RegisterEnd( |
197 const std::string& app_id, | 205 const std::string& app_id, |
198 const content::PushMessagingService::RegisterCallback& callback, | 206 const content::PushMessagingService::RegisterCallback& callback, |
199 const std::string& registration_id, | 207 const std::string& registration_id, |
200 GCMClient::Result result) { | 208 content::PushMessagingStatus status) { |
201 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 209 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
202 bool success = (result == GCMClient::SUCCESS); | 210 callback.Run(endpoint, registration_id, status); |
203 callback.Run(endpoint, registration_id, success); | 211 if (status == content::PUSH_MESSAGING_STATUS_OK) { |
204 if (success) { | |
205 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | 212 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. |
206 int registration_count = profile_->GetPrefs()->GetInteger( | 213 int registration_count = profile_->GetPrefs()->GetInteger( |
207 prefs::kPushMessagingRegistrationCount); | 214 prefs::kPushMessagingRegistrationCount); |
208 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 215 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
209 registration_count + 1); | 216 registration_count + 1); |
210 } | 217 } |
211 } | 218 } |
212 | 219 |
| 220 void PushMessagingServiceImpl::DidRegister( |
| 221 const std::string& app_id, |
| 222 const content::PushMessagingService::RegisterCallback& callback, |
| 223 const std::string& registration_id, |
| 224 GCMClient::Result result) { |
| 225 content::PushMessagingStatus status = |
| 226 result == GCMClient::SUCCESS |
| 227 ? content::PUSH_MESSAGING_STATUS_OK |
| 228 : content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_ERROR; |
| 229 RegisterEnd(app_id, callback, registration_id, status); |
| 230 } |
| 231 |
213 void PushMessagingServiceImpl::DidRequestPermission( | 232 void PushMessagingServiceImpl::DidRequestPermission( |
214 const std::string& sender_id, | 233 const std::string& sender_id, |
215 const std::string& app_id, | 234 const std::string& app_id, |
216 const content::PushMessagingService::RegisterCallback& register_callback, | 235 const content::PushMessagingService::RegisterCallback& register_callback, |
217 bool allow) { | 236 bool allow) { |
218 if (!allow) { | 237 if (!allow) { |
219 // TODO(miguelg) extend the error enum to allow for pemission failure. | 238 RegisterEnd( |
220 DidRegister(app_id, register_callback, std::string(), | 239 app_id, |
221 GCMClient::UNKNOWN_ERROR); | 240 register_callback, |
| 241 std::string(), |
| 242 content::PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_PERMISSION_DENIED); |
222 return; | 243 return; |
223 } | 244 } |
224 | 245 |
225 // The GCMDriver could be NULL if GCMProfileService has been shut down. | 246 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
226 if (!gcm_profile_service_->driver()) | 247 if (!gcm_profile_service_->driver()) |
227 return; | 248 return; |
228 | 249 |
229 std::vector<std::string> sender_ids(1, sender_id); | 250 std::vector<std::string> sender_ids(1, sender_id); |
230 | 251 |
231 gcm_profile_service_->driver()->Register( | 252 gcm_profile_service_->driver()->Register( |
232 app_id, | 253 app_id, |
233 sender_ids, | 254 sender_ids, |
234 base::Bind(&PushMessagingServiceImpl::DidRegister, | 255 base::Bind(&PushMessagingServiceImpl::DidRegister, |
235 weak_factory_.GetWeakPtr(), | 256 weak_factory_.GetWeakPtr(), |
236 app_id, | 257 app_id, |
237 register_callback)); | 258 register_callback)); |
238 } | 259 } |
239 | 260 |
240 // TODO(johnme): Unregister should decrement the pref, and call | 261 // TODO(johnme): Unregister should decrement the pref, and call |
241 // RemoveAppHandler if the count drops to zero. | 262 // RemoveAppHandler if the count drops to zero. |
242 | 263 |
243 } // namespace gcm | 264 } // namespace gcm |
OLD | NEW |