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 14 matching lines...) Expand all Loading... |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
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/platform_notification_data.h" | 34 #include "content/public/common/platform_notification_data.h" |
| 35 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
35 #include "third_party/skia/include/core/SkBitmap.h" | 36 #include "third_party/skia/include/core/SkBitmap.h" |
36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
37 | 38 |
| 39 #if defined(OS_ANDROID) |
| 40 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 41 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 42 #else |
| 43 #include "chrome/browser/ui/browser.h" |
| 44 #include "chrome/browser/ui/browser_iterator.h" |
| 45 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 46 #endif |
| 47 |
38 namespace gcm { | 48 namespace gcm { |
39 | 49 |
40 namespace { | 50 namespace { |
41 const int kMaxRegistrations = 1000000; | 51 const int kMaxRegistrations = 1000000; |
42 | 52 |
43 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { | 53 blink::WebPushPermissionStatus ToPushPermission(ContentSetting setting) { |
44 switch (setting) { | 54 switch (setting) { |
45 case CONTENT_SETTING_ALLOW: | 55 case CONTENT_SETTING_ALLOW: |
46 return blink::WebPushPermissionStatusGranted; | 56 return blink::WebPushPermissionStatusGranted; |
47 case CONTENT_SETTING_BLOCK: | 57 case CONTENT_SETTING_BLOCK: |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 PlatformNotificationServiceImpl::GetInstance(); | 234 PlatformNotificationServiceImpl::GetInstance(); |
225 // Can't use g_browser_process->notification_ui_manager(), since the test uses | 235 // Can't use g_browser_process->notification_ui_manager(), since the test uses |
226 // PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting. | 236 // PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting. |
227 // TODO(peter): Remove the need to use both APIs here once Notification.get() | 237 // TODO(peter): Remove the need to use both APIs here once Notification.get() |
228 // is supported. | 238 // is supported. |
229 int notification_count = notification_service->GetNotificationUIManager()-> | 239 int notification_count = notification_service->GetNotificationUIManager()-> |
230 GetAllIdsByProfileAndSourceOrigin(profile_, application_id.origin).size(); | 240 GetAllIdsByProfileAndSourceOrigin(profile_, application_id.origin).size(); |
231 if (notification_count > 0) | 241 if (notification_count > 0) |
232 return; | 242 return; |
233 | 243 |
| 244 // Sites with a currently visible tab don't need to show notifications. |
| 245 #if defined(OS_ANDROID) |
| 246 for (TabModel* tab_model : TabModelList) { |
| 247 Profile* profile = tab_model->GetProfile(); |
| 248 content::WebContents* active_web_contents = |
| 249 tab_model->GetActiveWebContents(); |
| 250 #else |
| 251 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 252 Profile* profile = it->profile(); |
| 253 content::WebContents* active_web_contents = |
| 254 it->tab_strip_model()->GetActiveWebContents(); |
| 255 #endif |
| 256 |
| 257 // Don't leak information from other profiles. |
| 258 if (!profile || profile->IsOffTheRecord() || |
| 259 !profile->IsSameProfile(profile_)) |
| 260 continue; |
| 261 |
| 262 // Ignore minimized windows etc. |
| 263 if (!active_web_contents || !IsWebContentsUserVisible(active_web_contents)) |
| 264 continue; |
| 265 |
| 266 // Allow https://foo.example.com Service Worker to not show notification if |
| 267 // an https://bar.example.com tab is visible (and hence might conceivably |
| 268 // be showing UI in response to the push message); but http:// doesn't count |
| 269 // as even with navigator.connect the Service Worker can't talk to it. |
| 270 if (application_id.origin.scheme() != |
| 271 active_web_contents->GetVisibleURL().scheme()) |
| 272 continue; |
| 273 if (net::registry_controlled_domains::SameDomainOrHost( |
| 274 application_id.origin, |
| 275 active_web_contents->GetVisibleURL(), |
| 276 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) |
| 277 return; |
| 278 } |
| 279 |
234 // If we haven't returned yet, the site failed to show a notification, so we | 280 // If we haven't returned yet, the site failed to show a notification, so we |
235 // will show a generic notification. See https://crbug.com/437277 | 281 // will show a generic notification. See https://crbug.com/437277 |
236 // TODO(johnme): The generic notification should probably automatically close | 282 // TODO(johnme): The generic notification should probably automatically close |
237 // itself when the next push message arrives? | 283 // itself when the next push message arrives? |
238 content::PlatformNotificationData notification_data; | 284 content::PlatformNotificationData notification_data; |
239 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 | 285 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 |
240 notification_data.title = l10n_util::GetStringFUTF16( | 286 notification_data.title = l10n_util::GetStringFUTF16( |
241 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, | 287 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, |
242 base::UTF8ToUTF16(application_id.origin.host())); | 288 base::UTF8ToUTF16(application_id.origin.host())); |
243 notification_data.direction = | 289 notification_data.direction = |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { | 538 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { |
493 gcm::PushMessagingPermissionContext* permission_context = | 539 gcm::PushMessagingPermissionContext* permission_context = |
494 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); | 540 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); |
495 DCHECK(permission_context); | 541 DCHECK(permission_context); |
496 | 542 |
497 return permission_context->GetPermissionStatus(origin, origin) == | 543 return permission_context->GetPermissionStatus(origin, origin) == |
498 CONTENT_SETTING_ALLOW; | 544 CONTENT_SETTING_ALLOW; |
499 } | 545 } |
500 | 546 |
501 } // namespace gcm | 547 } // namespace gcm |
OLD | NEW |