Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Side by Side Diff: chrome/browser/services/gcm/push_messaging_service_impl.cc

Issue 866443003: Push API: Don't require notification if tab is visible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ycm_outdir
Patch Set: Fix Android compile Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) {
259 Profile* profile = (*it)->GetProfile();
260 content::WebContents* active_web_contents =
261 (*it)->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 if (!active_web_contents)
269 continue;
270
271 // Don't leak information from other profiles.
272 if (profile != profile_)
273 continue;
274
275 // Ignore minimized windows etc.
276 switch (active_web_contents->GetMainFrame()->GetVisibilityState()) {
277 case blink::WebPageVisibilityStateHidden:
278 case blink::WebPageVisibilityStatePrerender:
279 continue;
280 case blink::WebPageVisibilityStateVisible:
281 break;
282 }
283
284 // Use the visible URL since that's the one the user is aware of (and it
285 // doesn't matter whether the page loaded successfully).
286 const GURL& active_url = active_web_contents->GetVisibleURL();
287
288 // Allow https://foo.example.com Service Worker to not show notification if
289 // an https://bar.example.com tab is visible (and hence might conceivably
290 // be showing UI in response to the push message); but http:// doesn't count
291 // as the Service Worker can't talk to it, even with navigator.connect.
292 if (application_id.origin.scheme() != active_url.scheme())
293 continue;
294 if (net::registry_controlled_domains::SameDomainOrHost(
295 application_id.origin, active_url,
296 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
297 return;
298 }
299 }
300
246 // If we haven't returned yet, the site failed to show a notification, so we 301 // 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 302 // will show a generic notification. See https://crbug.com/437277
248 // TODO(johnme): The generic notification should probably automatically close 303 // TODO(johnme): The generic notification should probably automatically close
249 // itself when the next push message arrives? 304 // itself when the next push message arrives?
250 content::PlatformNotificationData notification_data; 305 content::PlatformNotificationData notification_data;
251 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 306 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698
252 notification_data.title = l10n_util::GetStringFUTF16( 307 notification_data.title = l10n_util::GetStringFUTF16(
253 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, 308 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE,
254 base::UTF8ToUTF16(application_id.origin.host())); 309 base::UTF8ToUTF16(application_id.origin.host()));
255 notification_data.direction = 310 notification_data.direction =
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { 559 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) {
505 gcm::PushMessagingPermissionContext* permission_context = 560 gcm::PushMessagingPermissionContext* permission_context =
506 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); 561 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_);
507 DCHECK(permission_context); 562 DCHECK(permission_context);
508 563
509 return permission_context->GetPermissionStatus(origin, origin) == 564 return permission_context->GetPermissionStatus(origin, origin) ==
510 CONTENT_SETTING_ALLOW; 565 CONTENT_SETTING_ALLOW;
511 } 566 }
512 567
513 } // namespace gcm 568 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/push_messaging_browsertest.cc ('k') | chrome/browser/ui/android/tab_model/tab_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698