Commit 0035b7af authored by mvanouwerkerk's avatar mvanouwerkerk Committed by Commit bot

PushMessagingNotificationManager: extract IsTabVisible and test it.

BUG=558402

Review URL: https://codereview.chromium.org/1467573003

Cr-Commit-Position: refs/heads/master@{#361101}
parent af6b1818
......@@ -141,11 +141,9 @@ void PushMessagingNotificationManager::DidGetNotificationsFromDatabase(
// notification doesn't count.
int notification_count = success ? data.size() : 0;
bool notification_shown = notification_count > 0;
bool notification_needed = true;
// Sites with a currently visible tab don't need to show notifications.
#if defined(OS_ANDROID)
for (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) {
Profile* profile = (*it)->GetProfile();
......@@ -156,26 +154,7 @@ void PushMessagingNotificationManager::DidGetNotificationsFromDatabase(
WebContents* active_web_contents =
it->tab_strip_model()->GetActiveWebContents();
#endif
if (!active_web_contents || !active_web_contents->GetMainFrame())
continue;
// Don't leak information from other profiles.
if (profile != profile_)
continue;
// Ignore minimized windows etc.
switch (active_web_contents->GetMainFrame()->GetVisibilityState()) {
case blink::WebPageVisibilityStateHidden:
case blink::WebPageVisibilityStatePrerender:
continue;
case blink::WebPageVisibilityStateVisible:
break;
}
// Use the visible URL since that's the one the user is aware of (and it
// doesn't matter whether the page loaded successfully).
const GURL& active_url = active_web_contents->GetVisibleURL();
if (origin == active_url.GetOrigin()) {
if (IsTabVisible(profile, active_web_contents, origin)) {
notification_needed = false;
break;
}
......@@ -229,6 +208,31 @@ void PushMessagingNotificationManager::DidGetNotificationsFromDatabase(
}
}
bool PushMessagingNotificationManager::IsTabVisible(
Profile* profile,
WebContents* active_web_contents,
const GURL& origin) {
if (!active_web_contents || !active_web_contents->GetMainFrame())
return false;
// Don't leak information from other profiles.
if (profile != profile_)
return false;
// Ignore minimized windows.
switch (active_web_contents->GetMainFrame()->GetVisibilityState()) {
case blink::WebPageVisibilityStateHidden:
case blink::WebPageVisibilityStatePrerender:
return false;
case blink::WebPageVisibilityStateVisible:
break;
}
// Use the visible URL since that's the one the user is aware of (and it
// doesn't matter whether the page loaded successfully).
return origin == active_web_contents->GetVisibleURL().GetOrigin();
}
void PushMessagingNotificationManager::DidGetNotificationsShownAndNeeded(
const GURL& origin,
int64_t service_worker_registration_id,
......
......@@ -9,6 +9,7 @@
#include <vector>
#include "base/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
......@@ -18,6 +19,7 @@ class Profile;
namespace content {
struct NotificationDatabaseData;
struct PlatformNotificationData;
class WebContents;
}
// Developers may be required to display a Web Notification in response to an
......@@ -45,6 +47,8 @@ class PushMessagingNotificationManager {
const base::Closure& message_handled_closure);
private:
FRIEND_TEST_ALL_PREFIXES(PushMessagingNotificationManagerTest, IsTabVisible);
static void DidGetNotificationsFromDatabaseIOProxy(
const base::WeakPtr<PushMessagingNotificationManager>& ui_weak_ptr,
const GURL& origin,
......@@ -60,6 +64,13 @@ class PushMessagingNotificationManager {
bool success,
const std::vector<content::NotificationDatabaseData>& data);
// Checks whether |profile| is the one owning this instance,
// |active_web_contents| exists and its main frame is visible, and the URL
// currently visible to the user is for |origin|.
bool IsTabVisible(Profile* profile,
content::WebContents* active_web_contents,
const GURL& origin);
void DidGetNotificationsShownAndNeeded(
const GURL& origin,
int64_t service_worker_registration_id,
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/push_messaging/push_messaging_notification_manager.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
class PushMessagingNotificationManagerTest
: public ChromeRenderViewHostTestHarness {};
TEST_F(PushMessagingNotificationManagerTest, IsTabVisible) {
PushMessagingNotificationManager manager(profile());
GURL origin("https://google.com/");
NavigateAndCommit(origin);
EXPECT_FALSE(manager.IsTabVisible(profile(), nullptr, origin));
EXPECT_FALSE(manager.IsTabVisible(profile(), web_contents(),
GURL("https://chrome.com/")));
EXPECT_TRUE(manager.IsTabVisible(profile(), web_contents(), origin));
content::RenderViewHostTester::For(rvh())->SimulateWasHidden();
EXPECT_FALSE(manager.IsTabVisible(profile(), web_contents(), origin));
content::RenderViewHostTester::For(rvh())->SimulateWasShown();
EXPECT_TRUE(manager.IsTabVisible(profile(), web_contents(), origin));
}
......@@ -198,6 +198,7 @@
'browser/profiles/profile_statistics_unittest.cc',
'browser/profiles/profile_shortcut_manager_unittest_win.cc',
'browser/push_messaging/push_messaging_app_identifier_unittest.cc',
'browser/push_messaging/push_messaging_notification_manager_unittest.cc',
'browser/push_messaging/push_messaging_permission_context_unittest.cc',
'browser/renderer_host/chrome_render_widget_host_view_mac_history_swiper_unit_test.mm',
'browser/resources_util_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment