diff options
author | miguelg <miguelg@chromium.org> | 2014-12-12 06:39:53 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-12 14:41:02 +0000 |
commit | 202115f03eb3c90a7139e69e0a1cb2062c0754cf (patch) | |
tree | 0be91c557b6d0ccd33c4bb0db7d12380f57af128 | |
parent | 2bd10c6a723dba5ea1b5e308540a558147f42f82 (diff) | |
download | chromium_src-202115f03eb3c90a7139e69e0a1cb2062c0754cf.zip chromium_src-202115f03eb3c90a7139e69e0a1cb2062c0754cf.tar.gz chromium_src-202115f03eb3c90a7139e69e0a1cb2062c0754cf.tar.bz2 |
[Push] Register: require user-visible to be present in the manifest
Prompt for notifications if the Notification permission was not
previously granted.
Refactor a bit the browser tests to accomodate pages with
different manifests.
BUG=432930
TBR=fgorski
Review URL: https://codereview.chromium.org/785993003
Cr-Commit-Position: refs/heads/master@{#308075}
15 files changed, 299 insertions, 329 deletions
diff --git a/chrome/browser/content_settings/permission_context_base_unittest.cc b/chrome/browser/content_settings/permission_context_base_unittest.cc index 51983af..b1f6185 100644 --- a/chrome/browser/content_settings/permission_context_base_unittest.cc +++ b/chrome/browser/content_settings/permission_context_base_unittest.cc @@ -82,7 +82,7 @@ class TestPermissionContext : public PermissionContextBase { // saved for future use. TEST_F(PermissionContextBaseTests, TestAskAndGrant) { TestPermissionContext permission_context(profile(), - CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); + CONTENT_SETTINGS_TYPE_NOTIFICATIONS); GURL url("http://www.google.com"); content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); @@ -105,7 +105,7 @@ TEST_F(PermissionContextBaseTests, TestAskAndGrant) { ContentSetting setting = profile()->GetHostContentSettingsMap()->GetContentSetting( url.GetOrigin(), url.GetOrigin(), - CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string()); + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()); EXPECT_EQ(CONTENT_SETTING_ALLOW , setting); }; diff --git a/chrome/browser/content_settings/permission_queue_controller.cc b/chrome/browser/content_settings/permission_queue_controller.cc index 122de95..19048ad 100644 --- a/chrome/browser/content_settings/permission_queue_controller.cc +++ b/chrome/browser/content_settings/permission_queue_controller.cc @@ -12,7 +12,6 @@ #include "chrome/browser/media/midi_permission_infobar_delegate.h" #include "chrome/browser/notifications/desktop_notification_infobar_delegate.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/services/gcm/push_messaging_infobar_delegate.h" #include "chrome/browser/tab_contents/tab_util.h" #include "chrome/common/pref_names.h" #include "components/content_settings/core/browser/host_content_settings_map.h" @@ -121,11 +120,6 @@ void PermissionQueueController::PendingInfobarRequest::CreateInfoBar( GetInfoBarService(id_), controller, id_, requesting_frame_, display_languages, type_); break; - case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING: - infobar_ = gcm::PushMessagingInfoBarDelegate::Create( - GetInfoBarService(id_), controller, id_, requesting_frame_, - display_languages, type_); - break; #if defined(OS_ANDROID) case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create( diff --git a/chrome/browser/services/gcm/push_messaging_browsertest.cc b/chrome/browser/services/gcm/push_messaging_browsertest.cc index e799e76..b3f0205 100644 --- a/chrome/browser/services/gcm/push_messaging_browsertest.cc +++ b/chrome/browser/services/gcm/push_messaging_browsertest.cc @@ -143,7 +143,7 @@ class PushMessagingBrowserTest : public InProcessBrowserTest { void loadTestPage() { ui_test_utils::NavigateToURL( - browser(), https_server_->GetURL("files/push_messaging/test.html")); + browser(), https_server_->GetURL(GetTestURL())); } bool RunScript(const std::string& script, std::string* result) { @@ -162,6 +162,11 @@ class PushMessagingBrowserTest : public InProcessBrowserTest { gcm_service_->push_messaging_service()); } + protected: + virtual std::string GetTestURL() { + return "files/push_messaging/test.html"; + } + private: scoped_ptr<net::SpawnedTestServer> https_server_; FakeGCMProfileService* gcm_service_; @@ -169,7 +174,25 @@ class PushMessagingBrowserTest : public InProcessBrowserTest { DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); }; -IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { +class PushMessagingBadManifestBrowserTest : public PushMessagingBrowserTest { + std::string GetTestURL() override { + return "files/push_messaging/test_bad_manifest.html"; + } +}; + +IN_PROC_BROWSER_TEST_F(PushMessagingBadManifestBrowserTest, + RegisterFailsNotVisibleMessages) { + std::string script_result; + + ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); + ASSERT_EQ("ok - service worker registered", script_result); + ASSERT_TRUE(RunScript("registerPush()", &script_result)); + EXPECT_EQ("AbortError - Registration failed - permission denied", + script_result); +} + +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, + RegisterSuccessNotificationsGranted) { std::string script_result; ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); @@ -188,15 +211,19 @@ IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { } IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, - RegisterFailureNoPushPermission) { + RegisterSuccessNotificationsPrompt) { std::string script_result; ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); ASSERT_EQ("ok - service worker registered", script_result); + InfoBarResponder accepting_responder(browser(), true); ASSERT_TRUE(RunScript("registerPush()", &script_result)); - EXPECT_EQ("AbortError - Registration failed - permission denied", - script_result); + EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); + + PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); + EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); + EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); } IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, @@ -215,7 +242,7 @@ IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, script_result); } -IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoSenderId) { +IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterFailureNoManifest) { std::string script_result; ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); diff --git a/chrome/browser/services/gcm/push_messaging_infobar_delegate.cc b/chrome/browser/services/gcm/push_messaging_infobar_delegate.cc deleted file mode 100644 index 46cc906..0000000 --- a/chrome/browser/services/gcm/push_messaging_infobar_delegate.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2014 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/services/gcm/push_messaging_infobar_delegate.h" - -#include "chrome/grit/generated_resources.h" -#include "components/infobars/core/infobar.h" -#include "grit/theme_resources.h" -#include "net/base/net_util.h" -#include "ui/base/l10n/l10n_util.h" - -namespace gcm { - -// static -infobars::InfoBar* PushMessagingInfoBarDelegate::Create( - InfoBarService* infobar_service, - PermissionQueueController* controller, - const PermissionRequestID& id, - const GURL& requesting_frame, - const std::string& display_languages, - ContentSettingsType type) { - DCHECK(infobar_service); - DCHECK(controller); - return infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( - scoped_ptr<ConfirmInfoBarDelegate>(new PushMessagingInfoBarDelegate( - controller, id, requesting_frame, display_languages, type)))); -} - -PushMessagingInfoBarDelegate::PushMessagingInfoBarDelegate( - PermissionQueueController* controller, - const PermissionRequestID& id, - const GURL& requesting_frame, - const std::string& display_languages, - ContentSettingsType type) - : PermissionInfobarDelegate(controller, id, requesting_frame, type), - requesting_origin_(requesting_frame.GetOrigin()), - display_languages_(display_languages) { -} - -PushMessagingInfoBarDelegate::~PushMessagingInfoBarDelegate() { -} - -base::string16 PushMessagingInfoBarDelegate::GetMessageText() const { - return l10n_util::GetStringFUTF16( - IDS_PUSH_MESSAGES_PERMISSION_QUESTION, - net::FormatUrl(requesting_origin_, display_languages_, - net::kFormatUrlOmitUsernamePassword | - net::kFormatUrlOmitTrailingSlashOnBareHostname, - net::UnescapeRule::SPACES, NULL, NULL, NULL)); -} - -int PushMessagingInfoBarDelegate::GetIconID() const { - // TODO(miguelg): change once we have an icon - return IDR_INFOBAR_WARNING; -} - -} // namespace gcm diff --git a/chrome/browser/services/gcm/push_messaging_infobar_delegate.h b/chrome/browser/services/gcm/push_messaging_infobar_delegate.h deleted file mode 100644 index 702220a..0000000 --- a/chrome/browser/services/gcm/push_messaging_infobar_delegate.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2014 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. - -#ifndef CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_INFOBAR_DELEGATE_H_ -#define CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_INFOBAR_DELEGATE_H_ - -#include "chrome/browser/content_settings/permission_infobar_delegate.h" -#include "components/content_settings/core/common/content_settings_types.h" - -class GURL; -class InfoBarService; - -namespace gcm { - -// Delegate to allow GCM push messages registration. -class PushMessagingInfoBarDelegate : public PermissionInfobarDelegate { - public: - - // Creates a Push Permission infobar and delegate and adds the infobar to - // |infobar_service|. Returns the infobar if it was successfully added. - static infobars::InfoBar* Create(InfoBarService* infobar_service, - PermissionQueueController* controller, - const PermissionRequestID& id, - const GURL& requesting_frame, - const std::string& display_languages, - ContentSettingsType type); - - private: - PushMessagingInfoBarDelegate(PermissionQueueController* controller, - const PermissionRequestID& id, - const GURL& requesting_frame, - const std::string& display_languages, - ContentSettingsType type); - ~PushMessagingInfoBarDelegate() override; - - // ConfirmInfoBarDelegate: - base::string16 GetMessageText() const override; - int GetIconID() const override; - - const GURL requesting_origin_; - const std::string display_languages_; - - DISALLOW_COPY_AND_ASSIGN(PushMessagingInfoBarDelegate); -}; - -} // namespace gcm -#endif // CHROME_BROWSER_SERVICES_GCM_PUSH_MESSAGING_INFOBAR_DELEGATE_H_ - diff --git a/chrome/browser/services/gcm/push_messaging_permission_context.cc b/chrome/browser/services/gcm/push_messaging_permission_context.cc index 418b174..f210ed4 100644 --- a/chrome/browser/services/gcm/push_messaging_permission_context.cc +++ b/chrome/browser/services/gcm/push_messaging_permission_context.cc @@ -4,21 +4,25 @@ #include "chrome/browser/services/gcm/push_messaging_permission_context.h" +#include "chrome/browser/content_settings/permission_context_uma_util.h" +#include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/profiles/profile.h" #include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/common/permission_request_id.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_delegate.h" const ContentSettingsType kPushSettingType = CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; -const ContentSettingsType kNotificationSettingType = - CONTENT_SETTINGS_TYPE_NOTIFICATIONS; namespace gcm { PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING), - profile_(profile) { + profile_(profile), + weak_factory_ui_thread_(this) { } PushMessagingPermissionContext::~PushMessagingPermissionContext() { @@ -27,6 +31,7 @@ PushMessagingPermissionContext::~PushMessagingPermissionContext() { ContentSetting PushMessagingPermissionContext::GetPermissionStatus( const GURL& requesting_origin, const GURL& embedding_origin) const { +#if defined(ENABLE_NOTIFICATIONS) if (requesting_origin != embedding_origin) return CONTENT_SETTING_BLOCK; @@ -34,22 +39,28 @@ ContentSetting PushMessagingPermissionContext::GetPermissionStatus( profile_->GetHostContentSettingsMap()->GetContentSetting( requesting_origin, embedding_origin, kPushSettingType, std::string()); - ContentSetting notifications_content_setting = - profile_->GetHostContentSettingsMap()->GetContentSetting( - requesting_origin, embedding_origin, kNotificationSettingType, - std::string()); + DesktopNotificationService* notification_service = + DesktopNotificationServiceFactory::GetForProfile(profile_); + DCHECK(notification_service); + + ContentSetting notifications_permission = + notification_service->GetPermissionStatus(requesting_origin, + embedding_origin); - if (notifications_content_setting == CONTENT_SETTING_BLOCK || + if (notifications_permission == CONTENT_SETTING_BLOCK || push_content_setting == CONTENT_SETTING_BLOCK) { return CONTENT_SETTING_BLOCK; } - if (notifications_content_setting == CONTENT_SETTING_ASK || + if (notifications_permission == CONTENT_SETTING_ASK || push_content_setting == CONTENT_SETTING_ASK) { return CONTENT_SETTING_ASK; } - DCHECK_EQ(CONTENT_SETTING_ALLOW, notifications_content_setting); + DCHECK_EQ(CONTENT_SETTING_ALLOW, notifications_permission); DCHECK_EQ(CONTENT_SETTING_ALLOW, push_content_setting); return CONTENT_SETTING_ALLOW; +#else + return CONTENT_SETTING_BLOCK; +#endif } // Unlike other permissions, push is decided by the following algorithm @@ -65,23 +76,34 @@ void PushMessagingPermissionContext::DecidePermission( const GURL& embedding_origin, bool user_gesture, const BrowserPermissionCallback& callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); +#if defined(ENABLE_NOTIFICATIONS) if (requesting_origin != embedding_origin) { NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, false /* persist */, false /* granted */); } - ContentSetting notifications_content_setting = - profile_->GetHostContentSettingsMap() - ->GetContentSettingAndMaybeUpdateLastUsage( - requesting_origin, embedding_origin, kNotificationSettingType, - std::string()); - - if (notifications_content_setting != CONTENT_SETTING_ALLOW) { - DVLOG(1) << "Notification permission has not been granted."; - NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, - false /* persist */, false /* granted */); - return; - } + DesktopNotificationService* notification_service = + DesktopNotificationServiceFactory::GetForProfile(profile_); + DCHECK(notification_service); + + notification_service->RequestPermission( + web_contents, id, requesting_origin, user_gesture, + base::Bind(&PushMessagingPermissionContext::DecidePushPermission, + weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin, + embedding_origin, callback)); +#else + NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, + false /* persist */, false /* granted */); +#endif +} +void PushMessagingPermissionContext::DecidePushPermission( + const PermissionRequestID& id, + const GURL& requesting_origin, + const GURL& embedding_origin, + const BrowserPermissionCallback& callback, + bool notifications_permission_granted) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); ContentSetting push_content_setting = profile_->GetHostContentSettingsMap() ->GetContentSettingAndMaybeUpdateLastUsage( @@ -90,13 +112,24 @@ void PushMessagingPermissionContext::DecidePermission( if (push_content_setting == CONTENT_SETTING_BLOCK) { DVLOG(1) << "Push permission was explicitly blocked."; + PermissionContextUmaUtil::PermissionDenied(kPushSettingType, + requesting_origin); + NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, + true /* persist */, false /* granted */); + return; + } + + if (!notifications_permission_granted) { + DVLOG(1) << "Notification permission has not been granted."; NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, false /* persist */, false /* granted */); return; } + PermissionContextUmaUtil::PermissionGranted(kPushSettingType, + requesting_origin); NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, true /* persist */, true /* granted */); } - } // namespace gcm + diff --git a/chrome/browser/services/gcm/push_messaging_permission_context.h b/chrome/browser/services/gcm/push_messaging_permission_context.h index 2ae4e6c..d85f5a1 100644 --- a/chrome/browser/services/gcm/push_messaging_permission_context.h +++ b/chrome/browser/services/gcm/push_messaging_permission_context.h @@ -9,6 +9,7 @@ #include "components/content_settings/core/common/content_settings_types.h" +class PermissionRequestID; class Profile; namespace gcm { @@ -34,8 +35,23 @@ class PushMessagingPermissionContext : public PermissionContextBase { const BrowserPermissionCallback& callback) override; private: + FRIEND_TEST_ALL_PREFIXES(PushMessagingPermissionContextTest, + DecidePushPermission); + + // Used to decide the permission for push, once the permission for + // Notification has been granted/denied. + void DecidePushPermission(const PermissionRequestID& id, + const GURL& requesting_origin, + const GURL& embedding_origin, + const BrowserPermissionCallback& callback, + bool notifications_permission_granted); + Profile* profile_; + // Must be the last member, to ensure that it will be + // destroyed first, which will invalidate weak pointers + base::WeakPtrFactory<PushMessagingPermissionContext> weak_factory_ui_thread_; + DISALLOW_COPY_AND_ASSIGN(PushMessagingPermissionContext); }; diff --git a/chrome/browser/services/gcm/push_messaging_permission_context_unittest.cc b/chrome/browser/services/gcm/push_messaging_permission_context_unittest.cc index 653d1d4..a42e2de 100644 --- a/chrome/browser/services/gcm/push_messaging_permission_context_unittest.cc +++ b/chrome/browser/services/gcm/push_messaging_permission_context_unittest.cc @@ -27,18 +27,6 @@ class TestPushMessagingPermissionContext bool was_persisted() const { return was_persisted_; } bool was_granted() const { return permission_granted_; } - // PushMessagingPermissionContext: - void DecidePermission(content::WebContents* web_contents, - const PermissionRequestID& id, - const GURL& requesting_origin, - const GURL& embedder_origin, - bool user_gesture, - const BrowserPermissionCallback& callback) override { - PushMessagingPermissionContext::DecidePermission( - web_contents, id, requesting_origin, embedder_origin, user_gesture, - callback); - } - private: // PushMessagingPermissionContext: void NotifyPermissionSet(const PermissionRequestID& id, @@ -59,43 +47,38 @@ class PushMessagingPermissionContextTest : public testing::Test { public: PushMessagingPermissionContextTest() {} - void SetUp() override { - HostContentSettingsMap* host_content_settings_map = - profile_.GetHostContentSettingsMap(); - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); - } - protected: - void SetContentSetting(ContentSettingsType setting, ContentSetting value) { + void SetContentSetting(Profile* profile, + ContentSettingsType setting, + ContentSetting value) { ContentSettingsPattern pattern = ContentSettingsPattern::FromString(kOriginA); HostContentSettingsMap* host_content_settings_map = - profile_.GetHostContentSettingsMap(); + profile->GetHostContentSettingsMap(); host_content_settings_map->SetContentSetting(pattern, pattern, setting, std::string(), value); } - TestingProfile profile_; content::TestBrowserThreadBundle thread_bundle_; }; TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { - PushMessagingPermissionContext context(&profile_); + TestingProfile profile; + PushMessagingPermissionContext context(&profile); EXPECT_EQ(CONTENT_SETTING_ASK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); // Just granting notifications should still prompt - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ASK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); // Just granting push should still prompt - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ASK); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ASK, @@ -103,75 +86,81 @@ TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { } TEST_F(PushMessagingPermissionContextTest, HasPermissionDenySettingsMismatch) { - PushMessagingPermissionContext context(&profile_); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); + TestingProfile profile; + PushMessagingPermissionContext context(&profile); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ASK); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ASK); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ASK); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); } TEST_F(PushMessagingPermissionContextTest, HasPermissionDenyDifferentOrigins) { - PushMessagingPermissionContext context(&profile_); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); - + TestingProfile profile; + PushMessagingPermissionContext context(&profile); EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(GURL(kOriginB), GURL(kOriginA))); } TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { - PushMessagingPermissionContext context(&profile_); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + TestingProfile profile; + PushMessagingPermissionContext context(&profile); + + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + CONTENT_SETTING_ALLOW); + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ALLOW, context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); } -TEST_F(PushMessagingPermissionContextTest, DecidePermission) { - TestPushMessagingPermissionContext context(&profile_); +TEST_F(PushMessagingPermissionContextTest, DecidePushPermission) { + TestingProfile profile; + TestPushMessagingPermissionContext context(&profile); PermissionRequestID request_id(-1, -1, -1, GURL(kOriginA)); BrowserPermissionCallback callback; - context.DecidePermission(NULL, request_id, GURL(kOriginA), GURL(kOriginA), - true, callback); + context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), + callback, false); EXPECT_FALSE(context.was_persisted()); EXPECT_FALSE(context.was_granted()); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_BLOCK); - context.DecidePermission(NULL, request_id, GURL(kOriginA), GURL(kOriginA), - true, callback); - EXPECT_FALSE(context.was_persisted()); - EXPECT_FALSE(context.was_granted()); - SetContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + CONTENT_SETTING_ALLOW); + context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), + callback, true); + EXPECT_TRUE(context.was_persisted()); + EXPECT_TRUE(context.was_granted()); + + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_BLOCK); - context.DecidePermission(NULL, request_id, GURL(kOriginA), GURL(kOriginA), - true, callback); - EXPECT_FALSE(context.was_persisted()); + context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), + callback, true); + EXPECT_TRUE(context.was_persisted()); EXPECT_FALSE(context.was_granted()); - SetContentSetting(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, CONTENT_SETTING_ASK); - context.DecidePermission(NULL, request_id, GURL(kOriginA), GURL(kOriginA), - true, callback); + + SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, + CONTENT_SETTING_ASK); + context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), + callback, true); EXPECT_TRUE(context.was_persisted()); EXPECT_TRUE(context.was_granted()); - context.DecidePermission(NULL, request_id, GURL(kOriginB), GURL(kOriginA), - true, callback); - EXPECT_FALSE(context.was_persisted()); - EXPECT_FALSE(context.was_granted()); } } // namespace gcm diff --git a/chrome/browser/services/gcm/push_messaging_service_impl.cc b/chrome/browser/services/gcm/push_messaging_service_impl.cc index 80051f9..501f6ce 100644 --- a/chrome/browser/services/gcm/push_messaging_service_impl.cc +++ b/chrome/browser/services/gcm/push_messaging_service_impl.cc @@ -275,7 +275,7 @@ void PushMessagingServiceImpl::RegisterFromDocument( gcm::PushMessagingPermissionContext* permission_context = gcm::PushMessagingPermissionContextFactory::GetForProfile(profile_); - if (permission_context == NULL) { + if (permission_context == NULL || !user_visible_only) { RegisterEnd(callback, std::string(), content::PUSH_REGISTRATION_STATUS_PERMISSION_DENIED); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index f2281db..64dc935 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2460,8 +2460,6 @@ 'browser/services/gcm/push_messaging_application_id.h', 'browser/services/gcm/push_messaging_constants.cc', 'browser/services/gcm/push_messaging_constants.h', - 'browser/services/gcm/push_messaging_infobar_delegate.cc', - 'browser/services/gcm/push_messaging_infobar_delegate.h', 'browser/services/gcm/push_messaging_permission_context.cc', 'browser/services/gcm/push_messaging_permission_context.h', 'browser/services/gcm/push_messaging_permission_context_factory.cc', diff --git a/chrome/test/data/push_messaging/manifest.json b/chrome/test/data/push_messaging/manifest.json index 7499a63..51cc951 100644 --- a/chrome/test/data/push_messaging/manifest.json +++ b/chrome/test/data/push_messaging/manifest.json @@ -1,3 +1,4 @@ { - "gcm_sender_id": "1234567890" -}
\ No newline at end of file + "gcm_sender_id": "1234567890", + "gcm_user_visible_only" : true +} diff --git a/chrome/test/data/push_messaging/manifest_without_user_visible_only.json b/chrome/test/data/push_messaging/manifest_without_user_visible_only.json new file mode 100644 index 0000000..b43e22c --- /dev/null +++ b/chrome/test/data/push_messaging/manifest_without_user_visible_only.json @@ -0,0 +1,3 @@ +{ + "gcm_sender_id": "1234567890" +} diff --git a/chrome/test/data/push_messaging/push_test.js b/chrome/test/data/push_messaging/push_test.js new file mode 100644 index 0000000..70794dd0 --- /dev/null +++ b/chrome/test/data/push_messaging/push_test.js @@ -0,0 +1,121 @@ +// Copyright 2014 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. + +'use strict'; + +var pushData = new FutureData(); + +// Sends data back to the test. This must be in response to an earlier +// request, but it's ok to respond asynchronously. The request blocks until +// the response is sent. +function sendResultToTest(result) { + console.log('sendResultToTest: ' + result); + if (window.domAutomationController) { + domAutomationController.send('' + result); + } +} + +function sendErrorToTest(error) { + sendResultToTest(error.name + ' - ' + error.message); +} + +// A container for a single piece of data. The data does not have to be +// available yet when the getter is called, as all responses to the test are +// asynchronous. +function FutureData() { + this.data = null; + this.waiting = false; +} + +// Sends the data to the test if it is available. Otherwise sets the +// |waiting| flag. +FutureData.prototype.get = function() { + if (this.data) { + sendResultToTest(this.data); + } else { + this.waiting = true; + } +}; + +// Sets a new data value. If the |waiting| flag is on, it is turned off and +// the data is sent to the test. +FutureData.prototype.set = function(data) { + this.data = data; + if (this.waiting) { + sendResultToTest(data); + this.waiting = false; + } +}; + +FutureData.prototype.getImmediately = function() { + sendResultToTest(this.data); +}; + +// Notification permission has been coalesced with Push permission. After +// this is granted, Push API registration can succeed. +function requestNotificationPermission() { + Notification.requestPermission(function(permission) { + sendResultToTest('permission status - ' + permission); + }); +} + +function registerServiceWorker() { + navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( + function(swRegistration) { + sendResultToTest('ok - service worker registered'); + }, sendErrorToTest); +} + +function unregisterServiceWorker() { + navigator.serviceWorker.getRegistration().then(function(swRegistration) { + swRegistration.unregister().then(function(result) { + sendResultToTest('service worker unregistration status: ' + result); + }) + }).catch(sendErrorToTest); +} + +function removeManifest() { + var element = document.querySelector('link[rel="manifest"]'); + if (element) { + element.parentNode.removeChild(element); + sendResultToTest('manifest removed'); + } else + sendResultToTest('unable to find manifest element'); +} + +function registerPush() { + navigator.serviceWorker.ready.then(function(swRegistration) { + // TODO(mvanouwerkerk): Cleanup once the final API is exposed. + var pushManager = swRegistration.pushManager || navigator.push; + return pushManager.register().then(function(pushRegistration) { + sendResultToTest(pushRegistration.pushEndpoint + ' - ' + + pushRegistration.pushRegistrationId); + }); + }).catch(sendErrorToTest); +} + +function hasPermission() { + navigator.serviceWorker.ready.then(function(swRegistration) { + // TODO(mvanouwerkerk): Cleanup once the final API is exposed. + var pushManager = swRegistration.pushManager || navigator.push; + return pushManager.hasPermission().then(function(permission) { + sendResultToTest('permission status - ' + permission); + }); + }).catch(sendErrorToTest); +} + +function isControlled() { + if (navigator.serviceWorker.controller) { + sendResultToTest('true - is controlled'); + } else { + sendResultToTest('false - is not controlled'); + } +} + +addEventListener('message', function(event) { + var message = JSON.parse(event.data); + if (message.type == 'push') { + pushData.set(message.data); + } +}, false); diff --git a/chrome/test/data/push_messaging/test.html b/chrome/test/data/push_messaging/test.html index 66b6e2f..30ebc22 100644 --- a/chrome/test/data/push_messaging/test.html +++ b/chrome/test/data/push_messaging/test.html @@ -3,123 +3,7 @@ <head> <title>Push API Test</title> <link rel="manifest" href="manifest.json"> - <script> - var pushData = new FutureData(); - - // Sends data back to the test. This must be in response to an earlier - // request, but it's ok to respond asynchronously. The request blocks until - // the response is sent. - function sendResultToTest(result) { - console.log('sendResultToTest: ' + result); - if (window.domAutomationController) { - domAutomationController.send('' + result); - } - } - - function sendErrorToTest(error) { - sendResultToTest(error.name + ' - ' + error.message); - } - - // A container for a single piece of data. The data does not have to be - // available yet when the getter is called, as all responses to the test are - // asynchronous. - function FutureData() { - this.data = null; - this.waiting = false; - } - - // Sends the data to the test if it is available. Otherwise sets the - // |waiting| flag. - FutureData.prototype.get = function() { - if (this.data) { - sendResultToTest(this.data); - } else { - this.waiting = true; - } - }; - - // Sets a new data value. If the |waiting| flag is on, it is turned off and - // the data is sent to the test. - FutureData.prototype.set = function(data) { - this.data = data; - if (this.waiting) { - sendResultToTest(data); - this.waiting = false; - } - }; - - FutureData.prototype.getImmediately = function() { - sendResultToTest(this.data); - }; - - // Notification permission has been coalesced with Push permission. After - // this is granted, Push API registration can succeed. - function requestNotificationPermission() { - Notification.requestPermission(function(permission) { - sendResultToTest('permission status - ' + permission); - }); - } - - function registerServiceWorker() { - navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( - function(swRegistration) { - sendResultToTest('ok - service worker registered'); - }, sendErrorToTest); - } - - function unregisterServiceWorker() { - navigator.serviceWorker.getRegistration().then(function(swRegistration) { - swRegistration.unregister().then(function(result) { - sendResultToTest('service worker unregistration status: ' + result); - }) - }).catch(sendErrorToTest); - } - - function removeManifest() { - var element = document.querySelector('link[rel="manifest"]'); - if (element) { - element.parentNode.removeChild(element); - sendResultToTest('manifest removed'); - } else - sendResultToTest('unable to find manifest element'); - } - - function registerPush() { - navigator.serviceWorker.ready.then(function(swRegistration) { - // TODO(mvanouwerkerk): Cleanup once the final API is exposed. - var pushManager = swRegistration.pushManager || navigator.push; - pushManager.register().then(function(pushRegistration) { - sendResultToTest(pushRegistration.pushEndpoint + ' - ' + - pushRegistration.pushRegistrationId); - }, sendErrorToTest); - }, sendErrorToTest); - } - - function hasPermission() { - navigator.serviceWorker.ready.then(function(swRegistration) { - // TODO(mvanouwerkerk): Cleanup once the final API is exposed. - var pushManager = swRegistration.pushManager || navigator.push; - pushManager.hasPermission().then(function(permission) { - sendResultToTest('permission status - ' + permission); - }, sendErrorToTest); - }, sendErrorToTest); - } - - function isControlled() { - if (navigator.serviceWorker.controller) { - sendResultToTest('true - is controlled'); - } else { - sendResultToTest('false - is not controlled'); - } - } - - addEventListener('message', function(event) { - var message = JSON.parse(event.data); - if (message.type == 'push') { - pushData.set(message.data); - } - }, false); - </script> + <script src="push_test.js"></script> </head> <body> <h1>Push API Test</h1> diff --git a/chrome/test/data/push_messaging/test_bad_manifest.html b/chrome/test/data/push_messaging/test_bad_manifest.html new file mode 100644 index 0000000..0ca3cbb --- /dev/null +++ b/chrome/test/data/push_messaging/test_bad_manifest.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html> + <head> + <title>Push API Bad Manifest Test</title> + <link rel="manifest" href="manifest_without_user_visible_only.json"> + <script src="push_test.js"></script> + </head> + <body> + <h1>Push API Bad Manifest Test</h1> + </body> +</html> |