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 15 matching lines...) Expand all Loading... | |
26 #include "chrome/grit/generated_resources.h" | 26 #include "chrome/grit/generated_resources.h" |
27 #include "components/content_settings/core/common/permission_request_id.h" | 27 #include "components/content_settings/core/common/permission_request_id.h" |
28 #include "components/gcm_driver/gcm_driver.h" | 28 #include "components/gcm_driver/gcm_driver.h" |
29 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
30 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
33 #include "content/public/common/child_process_host.h" | 33 #include "content/public/common/child_process_host.h" |
34 #include "content/public/common/content_switches.h" | 34 #include "content/public/common/content_switches.h" |
35 #include "content/public/common/platform_notification_data.h" | 35 #include "content/public/common/platform_notification_data.h" |
36 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
36 #include "third_party/skia/include/core/SkBitmap.h" | 37 #include "third_party/skia/include/core/SkBitmap.h" |
37 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
38 | 39 |
40 #if defined(OS_ANDROID) | |
41 #include "chrome/browser/ui/android/tab_model/tab_model.h" | |
42 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | |
43 #else | |
44 #include "chrome/browser/ui/browser.h" | |
45 #include "chrome/browser/ui/browser_iterator.h" | |
46 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
47 #endif | |
48 | |
39 namespace gcm { | 49 namespace gcm { |
40 | 50 |
41 namespace { | 51 namespace { |
42 const int kMaxRegistrations = 1000000; | 52 const int kMaxRegistrations = 1000000; |
43 | 53 |
44 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { | 54 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { |
45 switch (setting) { | 55 switch (setting) { |
46 case CONTENT_SETTING_ALLOW: | 56 case CONTENT_SETTING_ALLOW: |
47 return blink::WebPushPermissionStatusGranted; | 57 return blink::WebPushPermissionStatusGranted; |
48 case CONTENT_SETTING_BLOCK: | 58 case CONTENT_SETTING_BLOCK: |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 PlatformNotificationServiceImpl::GetInstance(); | 246 PlatformNotificationServiceImpl::GetInstance(); |
237 // Can't use g_browser_process->notification_ui_manager(), since the test uses | 247 // Can't use g_browser_process->notification_ui_manager(), since the test uses |
238 // PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting. | 248 // PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting. |
239 // TODO(peter): Remove the need to use both APIs here once Notification.get() | 249 // TODO(peter): Remove the need to use both APIs here once Notification.get() |
240 // is supported. | 250 // is supported. |
241 int notification_count = notification_service->GetNotificationUIManager()-> | 251 int notification_count = notification_service->GetNotificationUIManager()-> |
242 GetAllIdsByProfileAndSourceOrigin(profile_, application_id.origin).size(); | 252 GetAllIdsByProfileAndSourceOrigin(profile_, application_id.origin).size(); |
243 if (notification_count > 0) | 253 if (notification_count > 0) |
244 return; | 254 return; |
245 | 255 |
256 // Sites with a currently visible tab don't need to show notifications. | |
257 #if defined(OS_ANDROID) | |
258 for (TabModel* tab_model : TabModelList) { | |
259 Profile* profile = tab_model->GetProfile(); | |
260 content::WebContents* active_web_contents = | |
261 tab_model->GetActiveWebContents(); | |
262 #else | |
263 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
264 Profile* profile = it->profile(); | |
265 content::WebContents* active_web_contents = | |
266 it->tab_strip_model()->GetActiveWebContents(); | |
267 #endif | |
268 | |
269 // Don't leak information from other profiles. | |
270 if (!profile || profile->IsOffTheRecord() || | |
271 !profile->IsSameProfile(profile_)) { | |
272 continue; | |
273 } | |
274 | |
275 // Ignore minimized windows etc. | |
276 if (!active_web_contents || | |
277 active_web_contents->GetMainFrame()->GetVisibilityState() | |
278 != blink::WebPageVisibilityStateVisible) | |
mlamouri (slow - plz ping)
2015/01/28 09:26:57
I thought I had left a comment about that yesterda
johnme
2015/02/02 12:14:04
Done.
| |
279 continue; | |
280 | |
281 // Use the visible URL since that's the one the user is aware of (and it | |
282 // doesn't matter whether the page loaded successfully). | |
283 const GURL& active_url = active_web_contents->GetVisibleURL(); | |
284 | |
285 // Allow https://foo.example.com Service Worker to not show notification if | |
286 // an https://bar.example.com tab is visible (and hence might conceivably | |
287 // be showing UI in response to the push message); but http:// doesn't count | |
288 // as even with navigator.connect the Service Worker can't talk to it. | |
289 if (application_id.origin.scheme() != active_url.scheme()) | |
290 continue; | |
291 if (net::registry_controlled_domains::SameDomainOrHost( | |
292 application_id.origin, active_url, | |
293 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | |
294 return; | |
295 } | |
296 } | |
297 | |
246 // If we haven't returned yet, the site failed to show a notification, so we | 298 // If we haven't returned yet, the site failed to show a notification, so we |
247 // will show a generic notification. See https://crbug.com/437277 | 299 // will show a generic notification. See https://crbug.com/437277 |
248 // TODO(johnme): The generic notification should probably automatically close | 300 // TODO(johnme): The generic notification should probably automatically close |
249 // itself when the next push message arrives? | 301 // itself when the next push message arrives? |
250 content::PlatformNotificationData notification_data; | 302 content::PlatformNotificationData notification_data; |
251 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 | 303 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 |
252 notification_data.title = l10n_util::GetStringFUTF16( | 304 notification_data.title = l10n_util::GetStringFUTF16( |
253 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, | 305 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, |
254 base::UTF8ToUTF16(application_id.origin.host())); | 306 base::UTF8ToUTF16(application_id.origin.host())); |
255 notification_data.direction = | 307 notification_data.direction = |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
504 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 556 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
505 gcm::PushMessagingPermissionContext* permission_context = | 557 gcm::PushMessagingPermissionContext* permission_context = |
506 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 558 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
507 DCHECK(permission_context); | 559 DCHECK(permission_context); |
508 | 560 |
509 return permission_context->GetPermissionStatus(origin, origin) == | 561 return permission_context->GetPermissionStatus(origin, origin) == |
510 CONTENT_SETTING_ALLOW; | 562 CONTENT_SETTING_ALLOW; |
511 } | 563 } |
512 | 564 |
513 } // namespace gcm | 565 } // namespace gcm |
OLD | NEW |