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

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: Switch statement for GetVisibilityState 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 (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_)) {
mlamouri (slow - plz ping) 2015/02/02 12:36:57 These checks do not really match the comment. Only
johnme 2015/02/02 15:58:36 Done (I changed it to profile_!=profile, since IsS
272 continue;
273 }
274
275 // Ignore minimized windows etc.
276 if (!active_web_contents)
mlamouri (slow - plz ping) 2015/02/02 12:36:58 Again, the check doesn't match the comment. Could
johnme 2015/02/02 15:58:36 Done (split this out).
277 continue;
278 switch (active_web_contents->GetMainFrame()->GetVisibilityState()) {
279 case blink::WebPageVisibilityStateHidden:
280 case blink::WebPageVisibilityStatePrerender:
281 continue;
282 case blink::WebPageVisibilityStateVisible:
283 break;
284 }
285
286 // Use the visible URL since that's the one the user is aware of (and it
287 // doesn't matter whether the page loaded successfully).
288 const GURL& active_url = active_web_contents->GetVisibleURL();
289
290 // Allow https://foo.example.com Service Worker to not show notification if
291 // an https://bar.example.com tab is visible (and hence might conceivably
292 // be showing UI in response to the push message); but http:// doesn't count
293 // as even with navigator.connect the Service Worker can't talk to it.
mlamouri (slow - plz ping) 2015/02/02 12:36:57 I wouldn't mention navigator.connect in the commen
johnme 2015/02/02 15:58:36 Chrome already implements most of navigator.connec
mlamouri (slow - plz ping) 2015/02/02 16:23:35 Chrome implementing it doesn't mean it will ship a
294 if (application_id.origin.scheme() != active_url.scheme())
295 continue;
296 if (net::registry_controlled_domains::SameDomainOrHost(
297 application_id.origin, active_url,
298 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
299 return;
300 }
301 }
302
246 // If we haven't returned yet, the site failed to show a notification, so we 303 // 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 304 // will show a generic notification. See https://crbug.com/437277
248 // TODO(johnme): The generic notification should probably automatically close 305 // TODO(johnme): The generic notification should probably automatically close
249 // itself when the next push message arrives? 306 // itself when the next push message arrives?
250 content::PlatformNotificationData notification_data; 307 content::PlatformNotificationData notification_data;
251 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698 308 // TODO(johnme): Switch to FormatOriginForDisplay from crbug.com/402698
252 notification_data.title = l10n_util::GetStringFUTF16( 309 notification_data.title = l10n_util::GetStringFUTF16(
253 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE, 310 IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_TITLE,
254 base::UTF8ToUTF16(application_id.origin.host())); 311 base::UTF8ToUTF16(application_id.origin.host()));
255 notification_data.direction = 312 notification_data.direction =
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) { 561 bool PushMessagingServiceImpl::HasPermission(const GURL& origin) {
505 gcm::PushMessagingPermissionContext* permission_context = 562 gcm::PushMessagingPermissionContext* permission_context =
506 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); 563 gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_);
507 DCHECK(permission_context); 564 DCHECK(permission_context);
508 565
509 return permission_context->GetPermissionStatus(origin, origin) == 566 return permission_context->GetPermissionStatus(origin, origin) ==
510 CONTENT_SETTING_ALLOW; 567 CONTENT_SETTING_ALLOW;
511 } 568 }
512 569
513 } // namespace gcm 570 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698