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/push_messaging/push_messaging_service_impl.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/notifications/notification_ui_manager.h" | 18 #include "chrome/browser/notifications/notification_ui_manager.h" |
19 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 19 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/push_messaging/push_messaging_application_id.h" | |
22 #include "chrome/browser/push_messaging/push_messaging_constants.h" | |
23 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" | |
24 #include "chrome/browser/push_messaging/push_messaging_permission_context_factor y.h" | |
21 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 25 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
22 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 26 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
23 #include "chrome/browser/services/gcm/push_messaging_application_id.h" | |
24 #include "chrome/browser/services/gcm/push_messaging_constants.h" | |
25 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" | |
26 #include "chrome/browser/services/gcm/push_messaging_permission_context_factory. h" | |
27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
30 #include "components/content_settings/core/browser/host_content_settings_map.h" | 30 #include "components/content_settings/core/browser/host_content_settings_map.h" |
31 #include "components/content_settings/core/common/permission_request_id.h" | 31 #include "components/content_settings/core/common/permission_request_id.h" |
32 #include "components/gcm_driver/gcm_driver.h" | 32 #include "components/gcm_driver/gcm_driver.h" |
33 #include "components/pref_registry/pref_registry_syncable.h" | 33 #include "components/pref_registry/pref_registry_syncable.h" |
34 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
35 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
36 #include "content/public/browser/render_frame_host.h" | 36 #include "content/public/browser/render_frame_host.h" |
37 #include "content/public/browser/service_worker_context.h" | 37 #include "content/public/browser/service_worker_context.h" |
38 #include "content/public/browser/storage_partition.h" | 38 #include "content/public/browser/storage_partition.h" |
39 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
40 #include "content/public/common/child_process_host.h" | 40 #include "content/public/common/child_process_host.h" |
41 #include "content/public/common/content_switches.h" | 41 #include "content/public/common/content_switches.h" |
42 #include "content/public/common/platform_notification_data.h" | 42 #include "content/public/common/platform_notification_data.h" |
43 #include "content/public/common/push_messaging_status.h" | 43 #include "content/public/common/push_messaging_status.h" |
44 #include "third_party/skia/include/core/SkBitmap.h" | 44 #include "third_party/skia/include/core/SkBitmap.h" |
45 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
46 | 46 |
47 #if defined(OS_ANDROID) | 47 #if defined(OS_ANDROID) |
48 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 48 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
49 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 49 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
50 #else | 50 #else |
51 #include "chrome/browser/ui/browser.h" | 51 #include "chrome/browser/ui/browser.h" |
52 #include "chrome/browser/ui/browser_iterator.h" | 52 #include "chrome/browser/ui/browser_iterator.h" |
53 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 53 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
54 #endif | 54 #endif |
55 | 55 |
56 namespace gcm { | |
57 | |
58 namespace { | 56 namespace { |
59 const int kMaxRegistrations = 1000000; | 57 const int kMaxRegistrations = 1000000; |
60 | 58 |
61 void RecordDeliveryStatus(content::PushDeliveryStatus status) { | 59 void RecordDeliveryStatus(content::PushDeliveryStatus status) { |
62 UMA_HISTOGRAM_ENUMERATION("PushMessaging.DeliveryStatus", | 60 UMA_HISTOGRAM_ENUMERATION("PushMessaging.DeliveryStatus", |
63 status, | 61 status, |
64 content::PUSH_DELIVERY_STATUS_LAST + 1); | 62 content::PUSH_DELIVERY_STATUS_LAST + 1); |
65 } | 63 } |
66 | 64 |
67 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { | 65 void RecordUserVisibleStatus(content::PushUserVisibleStatus status) { |
(...skipping 21 matching lines...) Expand all Loading... | |
89 // static | 87 // static |
90 void PushMessagingServiceImpl::RegisterProfilePrefs( | 88 void PushMessagingServiceImpl::RegisterProfilePrefs( |
91 user_prefs::PrefRegistrySyncable* registry) { | 89 user_prefs::PrefRegistrySyncable* registry) { |
92 registry->RegisterIntegerPref( | 90 registry->RegisterIntegerPref( |
93 prefs::kPushMessagingRegistrationCount, | 91 prefs::kPushMessagingRegistrationCount, |
94 0, | 92 0, |
95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 93 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
96 PushMessagingApplicationId::RegisterProfilePrefs(registry); | 94 PushMessagingApplicationId::RegisterProfilePrefs(registry); |
97 } | 95 } |
98 | 96 |
99 // static | 97 PushMessagingServiceImpl::PushMessagingServiceImpl(Profile* profile) |
100 void PushMessagingServiceImpl::InitializeForProfile(Profile* profile) { | 98 : gcm_profile_service_( |
101 // TODO(johnme): Consider whether push should be enabled in incognito. | 99 gcm::GCMProfileServiceFactory::GetForProfile(profile)), |
102 if (!profile || profile->IsOffTheRecord()) | 100 profile_(profile), |
103 return; | 101 push_registration_count_(0), |
104 | 102 pending_push_registration_count_(0), |
103 weak_factory_(this) { | |
105 // TODO(johnme): If push becomes enabled in incognito (and this still uses a | 104 // TODO(johnme): If push becomes enabled in incognito (and this still uses a |
106 // pref), be careful that this pref is read from the right profile, as prefs | 105 // pref), be careful that this pref is read from the right profile, as prefs |
107 // defined in a regular profile are visible in the corresponding incognito | 106 // defined in a regular profile are visible in the corresponding incognito |
108 // profile unless overridden. | 107 // profile unless overridden. |
109 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes. | 108 // TODO(johnme): Make sure this pref doesn't get out of sync after crashes. |
110 int count = profile->GetPrefs()->GetInteger( | 109 int count = profile->GetPrefs()->GetInteger( |
111 prefs::kPushMessagingRegistrationCount); | 110 prefs::kPushMessagingRegistrationCount); |
112 if (count <= 0) | 111 if (count <= 0) |
113 return; | 112 return; |
114 | 113 |
115 // Create the GCMProfileService, and hence instantiate this class. | 114 IncreasePushRegistrationCount(count, false /* is_pending */); |
116 GCMProfileService* gcm_service = | 115 profile_->GetHostContentSettingsMap()->AddObserver(this); |
117 GCMProfileServiceFactory::GetForProfile(profile); | |
118 PushMessagingServiceImpl* push_service = | |
119 static_cast<PushMessagingServiceImpl*>( | |
120 gcm_service->push_messaging_service()); | |
121 | |
122 push_service->IncreasePushRegistrationCount(count, false /* is_pending */); | |
123 } | |
124 | |
125 PushMessagingServiceImpl::PushMessagingServiceImpl( | |
126 GCMProfileService* gcm_profile_service, | |
127 Profile* profile) | |
128 : gcm_profile_service_(gcm_profile_service), | |
129 profile_(profile), | |
130 push_registration_count_(0), | |
131 pending_push_registration_count_(0), | |
132 weak_factory_(this) { | |
133 // In some tests, we might end up with |profile_| being null at this point. | |
134 // When that is the case |profile_| will be set in SetProfileForTesting(), at | |
135 // which point the service will start to observe HostContentSettingsMap. | |
136 if (profile_) | |
137 profile_->GetHostContentSettingsMap()->AddObserver(this); | |
138 } | 116 } |
139 | 117 |
140 PushMessagingServiceImpl::~PushMessagingServiceImpl() { | 118 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
141 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | |
142 // then we should call RemoveAppHandler. | |
143 profile_->GetHostContentSettingsMap()->RemoveObserver(this); | 119 profile_->GetHostContentSettingsMap()->RemoveObserver(this); |
144 } | 120 } |
145 | 121 |
146 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, | 122 void PushMessagingServiceImpl::IncreasePushRegistrationCount(int add, |
147 bool is_pending) { | 123 bool is_pending) { |
148 DCHECK(add > 0); | 124 DCHECK(add > 0); |
149 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 125 if (push_registration_count_ + pending_push_registration_count_ == 0) { |
150 gcm_profile_service_->driver()->AddAppHandler( | 126 gcm_driver()->AddAppHandler(kPushMessagingApplicationIdPrefix, this); |
151 kPushMessagingApplicationIdPrefix, this); | |
152 } | 127 } |
153 if (is_pending) { | 128 if (is_pending) { |
154 pending_push_registration_count_ += add; | 129 pending_push_registration_count_ += add; |
155 } else { | 130 } else { |
156 push_registration_count_ += add; | 131 push_registration_count_ += add; |
157 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 132 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
158 push_registration_count_); | 133 push_registration_count_); |
159 } | 134 } |
160 } | 135 } |
161 | 136 |
162 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract, | 137 void PushMessagingServiceImpl::DecreasePushRegistrationCount(int subtract, |
163 bool was_pending) { | 138 bool was_pending) { |
164 DCHECK(subtract > 0); | 139 DCHECK(subtract > 0); |
165 if (was_pending) { | 140 if (was_pending) { |
166 pending_push_registration_count_ -= subtract; | 141 pending_push_registration_count_ -= subtract; |
167 DCHECK(pending_push_registration_count_ >= 0); | 142 DCHECK(pending_push_registration_count_ >= 0); |
168 } else { | 143 } else { |
169 push_registration_count_ -= subtract; | 144 push_registration_count_ -= subtract; |
170 DCHECK(push_registration_count_ >= 0); | 145 DCHECK(push_registration_count_ >= 0); |
171 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | 146 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, |
172 push_registration_count_); | 147 push_registration_count_); |
173 } | 148 } |
174 if (push_registration_count_ + pending_push_registration_count_ == 0) { | 149 if (push_registration_count_ + pending_push_registration_count_ == 0) { |
175 gcm_profile_service_->driver()->RemoveAppHandler( | 150 gcm_driver()->RemoveAppHandler(kPushMessagingApplicationIdPrefix); |
176 kPushMessagingApplicationIdPrefix); | |
177 } | 151 } |
178 } | 152 } |
179 | 153 |
180 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | 154 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { |
181 return PushMessagingApplicationId::Get(profile_, app_id).IsValid(); | 155 return PushMessagingApplicationId::Get(profile_, app_id).IsValid(); |
182 } | 156 } |
183 | 157 |
184 void PushMessagingServiceImpl::ShutdownHandler() { | 158 void PushMessagingServiceImpl::ShutdownHandler() { |
185 // TODO(johnme): Do any necessary cleanup. | 159 // Shutdown should come before and it removes us from the list of app |
Peter Beverloo
2015/02/25 23:55:05
nit: s/Shutdown/Shutdown()/ to clarify that it's a
kbalazs
2015/02/27 22:40:36
Done.
| |
160 // handlers of gcm::GCMDriver so this shouldn't ever been called. | |
161 NOTREACHED(); | |
186 } | 162 } |
187 | 163 |
188 // OnMessage methods ----------------------------------------------------------- | 164 // OnMessage methods ----------------------------------------------------------- |
189 | 165 |
190 void PushMessagingServiceImpl::OnMessage( | 166 void PushMessagingServiceImpl::OnMessage( |
191 const std::string& app_id, | 167 const std::string& app_id, |
192 const GCMClient::IncomingMessage& message) { | 168 const gcm::GCMClient::IncomingMessage& message) { |
193 PushMessagingApplicationId application_id = | 169 PushMessagingApplicationId application_id = |
194 PushMessagingApplicationId::Get(profile_, app_id); | 170 PushMessagingApplicationId::Get(profile_, app_id); |
195 // Drop message and unregister if app id was unknown (maybe recently deleted). | 171 // Drop message and unregister if app id was unknown (maybe recently deleted). |
196 if (!application_id.IsValid()) { | 172 if (!application_id.IsValid()) { |
197 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, | 173 DeliverMessageCallback(app_id, GURL::EmptyGURL(), -1, message, |
198 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); | 174 content::PUSH_DELIVERY_STATUS_UNKNOWN_APP_ID); |
199 return; | 175 return; |
200 } | 176 } |
201 // Drop message and unregister if |origin| has lost push permission. | 177 // Drop message and unregister if |origin| has lost push permission. |
202 if (!HasPermission(application_id.origin())) { | 178 if (!HasPermission(application_id.origin())) { |
(...skipping 16 matching lines...) Expand all Loading... | |
219 // "data": "BAZ", | 195 // "data": "BAZ", |
220 // }, | 196 // }, |
221 // "delay_while_idle": true, | 197 // "delay_while_idle": true, |
222 // } | 198 // } |
223 // TODO(johnme): Make sure this is clearly documented for developers. | 199 // TODO(johnme): Make sure this is clearly documented for developers. |
224 std::string data; | 200 std::string data; |
225 // TODO(peter): Message payloads are disabled pending mandatory encryption. | 201 // TODO(peter): Message payloads are disabled pending mandatory encryption. |
226 // https://crbug.com/449184 | 202 // https://crbug.com/449184 |
227 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 203 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
228 switches::kEnablePushMessagePayload)) { | 204 switches::kEnablePushMessagePayload)) { |
229 GCMClient::MessageData::const_iterator it = message.data.find("data"); | 205 gcm::GCMClient::MessageData::const_iterator it = message.data.find("data"); |
230 if (it != message.data.end()) | 206 if (it != message.data.end()) |
231 data = it->second; | 207 data = it->second; |
232 } | 208 } |
233 | 209 |
234 content::BrowserContext::DeliverPushMessage( | 210 content::BrowserContext::DeliverPushMessage( |
235 profile_, | 211 profile_, |
236 application_id.origin(), | 212 application_id.origin(), |
237 application_id.service_worker_registration_id(), | 213 application_id.service_worker_registration_id(), |
238 data, | 214 data, |
239 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, | 215 base::Bind(&PushMessagingServiceImpl::DeliverMessageCallback, |
240 weak_factory_.GetWeakPtr(), | 216 weak_factory_.GetWeakPtr(), |
241 application_id.app_id_guid(), application_id.origin(), | 217 application_id.app_id_guid(), application_id.origin(), |
242 application_id.service_worker_registration_id(), message)); | 218 application_id.service_worker_registration_id(), message)); |
243 } | 219 } |
244 | 220 |
245 void PushMessagingServiceImpl::DeliverMessageCallback( | 221 void PushMessagingServiceImpl::DeliverMessageCallback( |
246 const std::string& app_id_guid, | 222 const std::string& app_id_guid, |
247 const GURL& requesting_origin, | 223 const GURL& requesting_origin, |
248 int64 service_worker_registration_id, | 224 int64 service_worker_registration_id, |
249 const GCMClient::IncomingMessage& message, | 225 const gcm::GCMClient::IncomingMessage& message, |
250 content::PushDeliveryStatus status) { | 226 content::PushDeliveryStatus status) { |
251 // TODO(mvanouwerkerk): Show a warning in the developer console of the | 227 // TODO(mvanouwerkerk): Show a warning in the developer console of the |
252 // Service Worker corresponding to app_id (and/or on an internals page). | 228 // Service Worker corresponding to app_id (and/or on an internals page). |
253 // TODO(mvanouwerkerk): Is there a way to recover from failure? | 229 // TODO(mvanouwerkerk): Is there a way to recover from failure? |
254 switch (status) { | 230 switch (status) { |
255 // Call RequireUserVisibleUX if the message was delivered to the Service | 231 // Call RequireUserVisibleUX if the message was delivered to the Service |
256 // Worker JS, even if the website's event handler failed (to prevent sites | 232 // Worker JS, even if the website's event handler failed (to prevent sites |
257 // deliberately failing in order to avoid having to show notifications). | 233 // deliberately failing in order to avoid having to show notifications). |
258 case content::PUSH_DELIVERY_STATUS_SUCCESS: | 234 case content::PUSH_DELIVERY_STATUS_SUCCESS: |
259 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: | 235 case content::PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED: |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 PlatformNotificationServiceImpl::GetInstance(); | 393 PlatformNotificationServiceImpl::GetInstance(); |
418 notification_service->DisplayPersistentNotification( | 394 notification_service->DisplayPersistentNotification( |
419 profile_, | 395 profile_, |
420 service_worker_registration_id, | 396 service_worker_registration_id, |
421 requesting_origin, | 397 requesting_origin, |
422 SkBitmap() /* icon */, | 398 SkBitmap() /* icon */, |
423 notification_data); | 399 notification_data); |
424 } | 400 } |
425 } | 401 } |
426 | 402 |
427 // Other GCMAppHandler methods ------------------------------------------------- | 403 // Other gcm::GCMAppHandler methods ------------------------------------------- |
428 | 404 |
429 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | 405 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { |
430 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | 406 // TODO(mvanouwerkerk): Fire push error event on the Service Worker |
431 // corresponding to app_id. | 407 // corresponding to app_id. |
432 } | 408 } |
433 | 409 |
434 void PushMessagingServiceImpl::OnSendError( | 410 void PushMessagingServiceImpl::OnSendError( |
435 const std::string& app_id, | 411 const std::string& app_id, |
436 const GCMClient::SendErrorDetails& send_error_details) { | 412 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
437 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 413 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
438 } | 414 } |
439 | 415 |
440 void PushMessagingServiceImpl::OnSendAcknowledged( | 416 void PushMessagingServiceImpl::OnSendAcknowledged( |
441 const std::string& app_id, | 417 const std::string& app_id, |
442 const std::string& message_id) { | 418 const std::string& message_id) { |
443 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | 419 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; |
444 } | 420 } |
445 | 421 |
446 // GetPushEndpoint method ------------------------------------------------------ | 422 // GetPushEndpoint method ------------------------------------------------------ |
447 | 423 |
448 GURL PushMessagingServiceImpl::GetPushEndpoint() { | 424 GURL PushMessagingServiceImpl::GetPushEndpoint() { |
449 return GURL(std::string(kPushMessagingEndpoint)); | 425 return GURL(std::string(kPushMessagingEndpoint)); |
450 } | 426 } |
451 | 427 |
452 // Register and GetPermissionStatus methods ------------------------------------ | 428 // Register and GetPermissionStatus methods ------------------------------------ |
453 | 429 |
454 void PushMessagingServiceImpl::RegisterFromDocument( | 430 void PushMessagingServiceImpl::RegisterFromDocument( |
455 const GURL& requesting_origin, | 431 const GURL& requesting_origin, |
456 int64 service_worker_registration_id, | 432 int64 service_worker_registration_id, |
457 const std::string& sender_id, | 433 const std::string& sender_id, |
458 int renderer_id, | 434 int renderer_id, |
459 int render_frame_id, | 435 int render_frame_id, |
460 bool user_visible_only, | 436 bool user_visible_only, |
461 const content::PushMessagingService::RegisterCallback& callback) { | 437 const content::PushMessagingService::RegisterCallback& callback) { |
462 if (!gcm_profile_service_->driver()) { | |
463 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; | |
464 return; | |
465 } | |
466 | |
467 PushMessagingApplicationId application_id = | 438 PushMessagingApplicationId application_id = |
468 PushMessagingApplicationId::Generate(requesting_origin, | 439 PushMessagingApplicationId::Generate(requesting_origin, |
469 service_worker_registration_id); | 440 service_worker_registration_id); |
470 DCHECK(application_id.IsValid()); | 441 DCHECK(application_id.IsValid()); |
471 | 442 |
472 if (push_registration_count_ + pending_push_registration_count_ | 443 if (push_registration_count_ + pending_push_registration_count_ |
473 >= kMaxRegistrations) { | 444 >= kMaxRegistrations) { |
474 RegisterEnd(callback, | 445 RegisterEnd(callback, |
475 std::string(), | 446 std::string(), |
476 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 447 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
(...skipping 10 matching lines...) Expand all Loading... | |
487 if (!web_contents) | 458 if (!web_contents) |
488 return; | 459 return; |
489 | 460 |
490 // TODO(miguelg) need to send this over IPC when bubble support is | 461 // TODO(miguelg) need to send this over IPC when bubble support is |
491 // implemented. | 462 // implemented. |
492 int bridge_id = -1; | 463 int bridge_id = -1; |
493 | 464 |
494 const PermissionRequestID id( | 465 const PermissionRequestID id( |
495 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); | 466 renderer_id, web_contents->GetRoutingID(), bridge_id, GURL()); |
496 | 467 |
497 gcm::PushMessagingPermissionContext* permission_context = | 468 PushMessagingPermissionContext* permission_context = |
498 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 469 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
499 | 470 |
500 if (permission_context == NULL || !user_visible_only) { | 471 if (permission_context == NULL || !user_visible_only) { |
501 RegisterEnd(callback, | 472 RegisterEnd(callback, |
502 std::string(), | 473 std::string(), |
503 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 474 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
504 return; | 475 return; |
505 } | 476 } |
506 | 477 |
507 // TODO(miguelg): Consider the value of |user_visible_only| when making | 478 // TODO(miguelg): Consider the value of |user_visible_only| when making |
508 // the permission request. | 479 // the permission request. |
509 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and | 480 // TODO(mlamouri): Move requesting Push permission over to using Mojo, and |
510 // re-introduce the ability of |user_gesture| when bubbles require this. | 481 // re-introduce the ability of |user_gesture| when bubbles require this. |
511 // https://crbug.com/423770. | 482 // https://crbug.com/423770. |
512 permission_context->RequestPermission( | 483 permission_context->RequestPermission( |
513 web_contents, id, requesting_origin, true /* user_gesture */, | 484 web_contents, id, requesting_origin, true /* user_gesture */, |
514 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, | 485 base::Bind(&PushMessagingServiceImpl::DidRequestPermission, |
515 weak_factory_.GetWeakPtr(), application_id, sender_id, | 486 weak_factory_.GetWeakPtr(), application_id, sender_id, |
516 callback)); | 487 callback)); |
517 } | 488 } |
518 | 489 |
519 void PushMessagingServiceImpl::RegisterFromWorker( | 490 void PushMessagingServiceImpl::RegisterFromWorker( |
520 const GURL& requesting_origin, | 491 const GURL& requesting_origin, |
521 int64 service_worker_registration_id, | 492 int64 service_worker_registration_id, |
522 const std::string& sender_id, | 493 const std::string& sender_id, |
523 const content::PushMessagingService::RegisterCallback& register_callback) { | 494 const content::PushMessagingService::RegisterCallback& register_callback) { |
524 if (!gcm_profile_service_->driver()) { | |
525 NOTREACHED() << "There is no GCMDriver. Has GCMProfileService shut down?"; | |
526 return; | |
527 } | |
528 | |
529 PushMessagingApplicationId application_id = | 495 PushMessagingApplicationId application_id = |
530 PushMessagingApplicationId::Generate(requesting_origin, | 496 PushMessagingApplicationId::Generate(requesting_origin, |
531 service_worker_registration_id); | 497 service_worker_registration_id); |
532 DCHECK(application_id.IsValid()); | 498 DCHECK(application_id.IsValid()); |
533 | 499 |
534 if (profile_->GetPrefs()->GetInteger( | 500 if (profile_->GetPrefs()->GetInteger( |
535 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { | 501 prefs::kPushMessagingRegistrationCount) >= kMaxRegistrations) { |
536 RegisterEnd(register_callback, std::string(), | 502 RegisterEnd(register_callback, std::string(), |
537 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); | 503 content::PUSH_REGISTRATION_STATUS_LIMIT_REACHED); |
538 return; | 504 return; |
539 } | 505 } |
540 | 506 |
541 GURL embedding_origin = requesting_origin; | 507 GURL embedding_origin = requesting_origin; |
542 blink::WebPushPermissionStatus permission_status = | 508 blink::WebPushPermissionStatus permission_status = |
543 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, | 509 PushMessagingServiceImpl::GetPermissionStatus(requesting_origin, |
544 embedding_origin); | 510 embedding_origin); |
545 if (permission_status != blink::WebPushPermissionStatusGranted) { | 511 if (permission_status != blink::WebPushPermissionStatusGranted) { |
546 RegisterEnd(register_callback, std::string(), | 512 RegisterEnd(register_callback, std::string(), |
547 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 513 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
548 return; | 514 return; |
549 } | 515 } |
550 | 516 |
551 IncreasePushRegistrationCount(1, true /* is_pending */); | 517 IncreasePushRegistrationCount(1, true /* is_pending */); |
552 std::vector<std::string> sender_ids(1, sender_id); | 518 std::vector<std::string> sender_ids(1, sender_id); |
553 gcm_profile_service_->driver()->Register( | 519 gcm_driver()->Register( |
554 application_id.app_id_guid(), sender_ids, | 520 application_id.app_id_guid(), sender_ids, |
555 base::Bind(&PushMessagingServiceImpl::DidRegister, | 521 base::Bind(&PushMessagingServiceImpl::DidRegister, |
556 weak_factory_.GetWeakPtr(), | 522 weak_factory_.GetWeakPtr(), |
557 application_id, register_callback)); | 523 application_id, register_callback)); |
558 } | 524 } |
559 | 525 |
560 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( | 526 blink::WebPushPermissionStatus PushMessagingServiceImpl::GetPermissionStatus( |
561 const GURL& requesting_origin, | 527 const GURL& requesting_origin, |
562 const GURL& embedding_origin) { | 528 const GURL& embedding_origin) { |
563 PushMessagingPermissionContext* permission_context = | 529 PushMessagingPermissionContext* permission_context = |
564 PushMessagingPermissionContextFactory::GetForProfile(profile_); | 530 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
565 return ToPushPermission(permission_context->GetPermissionStatus( | 531 return ToPushPermission(permission_context->GetPermissionStatus( |
566 requesting_origin, embedding_origin)); | 532 requesting_origin, embedding_origin)); |
567 } | 533 } |
568 | 534 |
569 void PushMessagingServiceImpl::RegisterEnd( | 535 void PushMessagingServiceImpl::RegisterEnd( |
570 const content::PushMessagingService::RegisterCallback& callback, | 536 const content::PushMessagingService::RegisterCallback& callback, |
571 const std::string& registration_id, | 537 const std::string& registration_id, |
572 content::PushRegistrationStatus status) { | 538 content::PushRegistrationStatus status) { |
573 callback.Run(registration_id, status); | 539 callback.Run(registration_id, status); |
574 } | 540 } |
575 | 541 |
576 void PushMessagingServiceImpl::DidRegister( | 542 void PushMessagingServiceImpl::DidRegister( |
577 const PushMessagingApplicationId& application_id, | 543 const PushMessagingApplicationId& application_id, |
578 const content::PushMessagingService::RegisterCallback& callback, | 544 const content::PushMessagingService::RegisterCallback& callback, |
579 const std::string& registration_id, | 545 const std::string& registration_id, |
580 GCMClient::Result result) { | 546 gcm::GCMClient::Result result) { |
581 content::PushRegistrationStatus status = | 547 content::PushRegistrationStatus status = |
582 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 548 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
583 switch (result) { | 549 switch (result) { |
584 case GCMClient::SUCCESS: | 550 case gcm::GCMClient::SUCCESS: |
585 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; | 551 status = content::PUSH_REGISTRATION_STATUS_SUCCESS_FROM_PUSH_SERVICE; |
586 application_id.PersistToDisk(profile_); | 552 application_id.PersistToDisk(profile_); |
587 IncreasePushRegistrationCount(1, false /* is_pending */); | 553 IncreasePushRegistrationCount(1, false /* is_pending */); |
588 break; | 554 break; |
589 case GCMClient::INVALID_PARAMETER: | 555 case gcm::GCMClient::INVALID_PARAMETER: |
590 case GCMClient::GCM_DISABLED: | 556 case gcm::GCMClient::GCM_DISABLED: |
591 case GCMClient::ASYNC_OPERATION_PENDING: | 557 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
592 case GCMClient::SERVER_ERROR: | 558 case gcm::GCMClient::SERVER_ERROR: |
593 case GCMClient::UNKNOWN_ERROR: | 559 case gcm::GCMClient::UNKNOWN_ERROR: |
594 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | 560 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
595 break; | 561 break; |
596 case GCMClient::NETWORK_ERROR: | 562 case gcm::GCMClient::NETWORK_ERROR: |
597 case GCMClient::TTL_EXCEEDED: | 563 case gcm::GCMClient::TTL_EXCEEDED: |
598 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | 564 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
599 break; | 565 break; |
600 } | 566 } |
601 RegisterEnd(callback, registration_id, status); | 567 RegisterEnd(callback, registration_id, status); |
602 DecreasePushRegistrationCount(1, true /* was_pending */); | 568 DecreasePushRegistrationCount(1, true /* was_pending */); |
603 } | 569 } |
604 | 570 |
605 void PushMessagingServiceImpl::DidRequestPermission( | 571 void PushMessagingServiceImpl::DidRequestPermission( |
606 const PushMessagingApplicationId& application_id, | 572 const PushMessagingApplicationId& application_id, |
607 const std::string& sender_id, | 573 const std::string& sender_id, |
608 const content::PushMessagingService::RegisterCallback& register_callback, | 574 const content::PushMessagingService::RegisterCallback& register_callback, |
609 bool allow) { | 575 bool allow) { |
610 if (!allow) { | 576 if (!allow) { |
611 RegisterEnd(register_callback, | 577 RegisterEnd(register_callback, |
612 std::string(), | 578 std::string(), |
613 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); | 579 content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); |
614 return; | 580 return; |
615 } | 581 } |
616 | 582 |
617 // The GCMDriver could be NULL if GCMProfileService has been shut down. | |
618 if (!gcm_profile_service_->driver()) | |
619 return; | |
620 | |
621 IncreasePushRegistrationCount(1, true /* is_pending */); | 583 IncreasePushRegistrationCount(1, true /* is_pending */); |
622 std::vector<std::string> sender_ids(1, sender_id); | 584 std::vector<std::string> sender_ids(1, sender_id); |
623 gcm_profile_service_->driver()->Register( | 585 gcm_driver()->Register( |
624 application_id.app_id_guid(), | 586 application_id.app_id_guid(), |
625 sender_ids, | 587 sender_ids, |
626 base::Bind(&PushMessagingServiceImpl::DidRegister, | 588 base::Bind(&PushMessagingServiceImpl::DidRegister, |
627 weak_factory_.GetWeakPtr(), | 589 weak_factory_.GetWeakPtr(), |
628 application_id, register_callback)); | 590 application_id, register_callback)); |
629 } | 591 } |
630 | 592 |
631 // Unregister methods ---------------------------------------------------------- | 593 // Unregister methods ---------------------------------------------------------- |
632 | 594 |
633 void PushMessagingServiceImpl::Unregister( | 595 void PushMessagingServiceImpl::Unregister( |
634 const GURL& requesting_origin, | 596 const GURL& requesting_origin, |
635 int64 service_worker_registration_id, | 597 int64 service_worker_registration_id, |
636 const std::string& sender_id, | 598 const std::string& sender_id, |
637 bool retry_on_failure, | 599 bool retry_on_failure, |
638 const content::PushMessagingService::UnregisterCallback& callback) { | 600 const content::PushMessagingService::UnregisterCallback& callback) { |
639 DCHECK(gcm_profile_service_->driver()); | |
640 | |
641 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( | 601 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( |
642 profile_, requesting_origin, service_worker_registration_id); | 602 profile_, requesting_origin, service_worker_registration_id); |
643 if (!application_id.IsValid()) { | 603 if (!application_id.IsValid()) { |
644 if (!callback.is_null()) { | 604 if (!callback.is_null()) { |
645 callback.Run( | 605 callback.Run( |
646 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 606 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
647 } | 607 } |
648 return; | 608 return; |
649 } | 609 } |
650 | 610 |
651 Unregister(application_id.app_id_guid(), sender_id, retry_on_failure, | 611 Unregister(application_id.app_id_guid(), sender_id, retry_on_failure, |
652 callback); | 612 callback); |
653 } | 613 } |
654 | 614 |
655 void PushMessagingServiceImpl::Unregister( | 615 void PushMessagingServiceImpl::Unregister( |
656 const std::string& app_id_guid, | 616 const std::string& app_id_guid, |
657 const std::string& sender_id, | 617 const std::string& sender_id, |
658 bool retry_on_failure, | 618 bool retry_on_failure, |
659 const content::PushMessagingService::UnregisterCallback& callback) { | 619 const content::PushMessagingService::UnregisterCallback& callback) { |
660 DCHECK(gcm_profile_service_->driver()); | |
661 | |
662 if (retry_on_failure) { | 620 if (retry_on_failure) { |
663 // Delete the mapping for this app id, to guarantee that no messages get | 621 // Delete the mapping for this app id, to guarantee that no messages get |
664 // delivered in future (even if unregistration fails). | 622 // delivered in future (even if unregistration fails). |
665 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, | 623 // TODO(johnme): Instead of deleting these app ids, store them elsewhere, |
666 // and retry unregistration if it fails due to network errors. | 624 // and retry unregistration if it fails due to network errors. |
667 PushMessagingApplicationId application_id = | 625 PushMessagingApplicationId application_id = |
668 PushMessagingApplicationId::Get(profile_, app_id_guid); | 626 PushMessagingApplicationId::Get(profile_, app_id_guid); |
669 if (application_id.IsValid()) | 627 if (application_id.IsValid()) |
670 application_id.DeleteFromDisk(profile_); | 628 application_id.DeleteFromDisk(profile_); |
671 } | 629 } |
672 | 630 |
673 const auto& unregister_callback = | 631 const auto& unregister_callback = |
674 base::Bind(&PushMessagingServiceImpl::DidUnregister, | 632 base::Bind(&PushMessagingServiceImpl::DidUnregister, |
675 weak_factory_.GetWeakPtr(), | 633 weak_factory_.GetWeakPtr(), |
676 app_id_guid, retry_on_failure, callback); | 634 app_id_guid, retry_on_failure, callback); |
677 #if defined(OS_ANDROID) | 635 #if defined(OS_ANDROID) |
678 // On Android the backend is different, and requires the original sender_id. | 636 // On Android the backend is different, and requires the original sender_id. |
679 gcm_profile_service_->driver()->UnregisterWithSenderId(app_id_guid, sender_id, | 637 gcm_driver()->UnregisterWithSenderId(app_id_guid, sender_id, |
680 unregister_callback); | 638 unregister_callback); |
681 #else | 639 #else |
682 gcm_profile_service_->driver()->Unregister(app_id_guid, unregister_callback); | 640 gcm_driver()->Unregister(app_id_guid, unregister_callback); |
683 #endif | 641 #endif |
684 } | 642 } |
685 | 643 |
686 void PushMessagingServiceImpl::DidUnregister( | 644 void PushMessagingServiceImpl::DidUnregister( |
687 const std::string& app_id_guid, | 645 const std::string& app_id_guid, |
688 bool retry_on_failure, | 646 bool retry_on_failure, |
689 const content::PushMessagingService::UnregisterCallback& callback, | 647 const content::PushMessagingService::UnregisterCallback& callback, |
690 GCMClient::Result result) { | 648 gcm::GCMClient::Result result) { |
691 if (result == GCMClient::SUCCESS) { | 649 if (result == gcm::GCMClient::SUCCESS) { |
692 PushMessagingApplicationId application_id = | 650 PushMessagingApplicationId application_id = |
693 PushMessagingApplicationId::Get(profile_, app_id_guid); | 651 PushMessagingApplicationId::Get(profile_, app_id_guid); |
694 if (!application_id.IsValid()) { | 652 if (!application_id.IsValid()) { |
695 if (!callback.is_null()) { | 653 if (!callback.is_null()) { |
696 callback.Run( | 654 callback.Run( |
697 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); | 655 content::PUSH_UNREGISTRATION_STATUS_SUCCESS_WAS_NOT_REGISTERED); |
698 } | 656 } |
699 return; | 657 return; |
700 } | 658 } |
701 | 659 |
702 application_id.DeleteFromDisk(profile_); | 660 application_id.DeleteFromDisk(profile_); |
703 DecreasePushRegistrationCount(1, false /* was_pending */); | 661 DecreasePushRegistrationCount(1, false /* was_pending */); |
704 } | 662 } |
705 | 663 |
706 // Internal calls pass a null callback. | 664 // Internal calls pass a null callback. |
707 if (!callback.is_null()) { | 665 if (!callback.is_null()) { |
708 switch (result) { | 666 switch (result) { |
709 case GCMClient::SUCCESS: | 667 case gcm::GCMClient::SUCCESS: |
710 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); | 668 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SUCCESS_UNREGISTERED); |
711 break; | 669 break; |
712 case GCMClient::INVALID_PARAMETER: | 670 case gcm::GCMClient::INVALID_PARAMETER: |
713 case GCMClient::GCM_DISABLED: | 671 case gcm::GCMClient::GCM_DISABLED: |
714 case GCMClient::ASYNC_OPERATION_PENDING: | 672 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
715 case GCMClient::SERVER_ERROR: | 673 case gcm::GCMClient::SERVER_ERROR: |
716 case GCMClient::UNKNOWN_ERROR: | 674 case gcm::GCMClient::UNKNOWN_ERROR: |
717 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SERVICE_ERROR); | 675 callback.Run(content::PUSH_UNREGISTRATION_STATUS_SERVICE_ERROR); |
718 break; | 676 break; |
719 case GCMClient::NETWORK_ERROR: | 677 case gcm::GCMClient::NETWORK_ERROR: |
720 case GCMClient::TTL_EXCEEDED: | 678 case gcm::GCMClient::TTL_EXCEEDED: |
721 callback.Run( | 679 callback.Run( |
722 retry_on_failure | 680 retry_on_failure |
723 ? content:: | 681 ? content:: |
724 PUSH_UNREGISTRATION_STATUS_PENDING_WILL_RETRY_NETWORK_ERROR | 682 PUSH_UNREGISTRATION_STATUS_PENDING_WILL_RETRY_NETWORK_ERROR |
725 : content::PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR); | 683 : content::PUSH_UNREGISTRATION_STATUS_NETWORK_ERROR); |
726 break; | 684 break; |
727 } | 685 } |
728 } | 686 } |
729 } | 687 } |
730 | 688 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
765 const std::string& sender_id, bool success, bool not_found) { | 723 const std::string& sender_id, bool success, bool not_found) { |
766 // Unregister the PushMessagingApplicationId with the push service. | 724 // Unregister the PushMessagingApplicationId with the push service. |
767 Unregister(id.app_id_guid(), sender_id, true /* retry_on_failure */, | 725 Unregister(id.app_id_guid(), sender_id, true /* retry_on_failure */, |
768 UnregisterCallback()); | 726 UnregisterCallback()); |
769 | 727 |
770 // Clear the associated service worker push registration id. | 728 // Clear the associated service worker push registration id. |
771 PushMessagingService::ClearPushRegistrationID( | 729 PushMessagingService::ClearPushRegistrationID( |
772 profile_, id.origin(), id.service_worker_registration_id()); | 730 profile_, id.origin(), id.service_worker_registration_id()); |
773 } | 731 } |
774 | 732 |
733 // KeyedService methods ------------------------------------------------------- | |
734 | |
735 void PushMessagingServiceImpl::Shutdown() { | |
736 gcm_driver()->RemoveAppHandler( | |
737 kPushMessagingApplicationIdPrefix); | |
Peter Beverloo
2015/02/25 23:55:05
nit: this would fit on a single line
kbalazs
2015/02/27 22:40:36
Done.
| |
738 } | |
739 | |
775 // Helper methods -------------------------------------------------------------- | 740 // Helper methods -------------------------------------------------------------- |
776 | 741 |
777 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 742 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
778 gcm::PushMessagingPermissionContext* permission_context = | 743 PushMessagingPermissionContext* permission_context = |
779 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 744 PushMessagingPermissionContextFactory::GetForProfile(profile_); |
780 DCHECK(permission_context); | 745 DCHECK(permission_context); |
781 | 746 |
782 return permission_context->GetPermissionStatus(origin, origin) == | 747 return permission_context->GetPermissionStatus(origin, origin) == |
783 CONTENT_SETTING_ALLOW; | 748 CONTENT_SETTING_ALLOW; |
784 } | 749 } |
785 | 750 |
786 void PushMessagingServiceImpl::SetProfileForTesting(Profile* profile) { | 751 gcm::GCMDriver* PushMessagingServiceImpl::gcm_driver() const { |
787 profile_ = profile; | 752 // The driver has to be created not later then we are created and should be |
788 profile_->GetHostContentSettingsMap()->AddObserver(this); | 753 // alive till we are. This is assured by PushMessagingServiceFactory |
754 // being dependant on GCMProfileServiceFactory. | |
755 gcm::GCMDriver* driver = gcm_profile_service_->driver(); | |
756 CHECK(driver); | |
757 return driver; | |
789 } | 758 } |
790 | |
791 } // namespace gcm | |
OLD | NEW |