From 83ab5f200ee5c9601bfa60d1786f99c77ff1a88a Mon Sep 17 00:00:00 2001 From: "johnnyg@chromium.org" Date: Thu, 12 Nov 2009 22:43:52 +0000 Subject: Notifications permission infobar should only be shown in response to a user gesture, not arbitrary script. Includes UI test which verify the infobar is shown from a gesture, and not shown from raw script. BUG=27215 TEST=included Review URL: http://codereview.chromium.org/387011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31839 0039d316-1c4b-4281-b951-d872f2087c98 --- .../browser/notifications/notifications_uitest.cc | 67 ++++++++++++++++++++++ chrome/chrome.gyp | 1 + .../active_notification_tracker.cc | 14 ++--- chrome/renderer/notification_provider.cc | 4 ++ .../notifications_request_function.html | 13 +++++ .../notifications_request_inline.html | 10 ++++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100755 chrome/browser/notifications/notifications_uitest.cc create mode 100755 chrome/test/data/notifications/notifications_request_function.html create mode 100755 chrome/test/data/notifications/notifications_request_inline.html (limited to 'chrome') diff --git a/chrome/browser/notifications/notifications_uitest.cc b/chrome/browser/notifications/notifications_uitest.cc new file mode 100755 index 0000000..22284bc --- /dev/null +++ b/chrome/browser/notifications/notifications_uitest.cc @@ -0,0 +1,67 @@ +// Copyright (c) 2006-2009 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 "base/file_path.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/test/automation/browser_proxy.h" +#include "chrome/test/automation/tab_proxy.h" +#include "chrome/test/ui/ui_test.h" +#include "net/base/net_util.h" +#include "net/url_request/url_request_unittest.h" + +class NotificationsPermissionTest : public UITest { + public: + NotificationsPermissionTest() { + launch_arguments_.AppendSwitch(switches::kEnableDesktopNotifications); + dom_automation_enabled_ = true; + show_window_ = true; + } +}; + +#if defined(OS_WIN) +TEST_F(NotificationsPermissionTest, TestUserGestureInfobar) { + const wchar_t kDocRoot[] = L"chrome/test/data"; + scoped_refptr server = + HTTPTestServer::CreateServer(kDocRoot, NULL); + ASSERT_TRUE(server.get() != NULL); + + scoped_refptr browser(automation()->GetBrowserWindow(0)); + scoped_refptr tab(browser->GetActiveTab()); + tab->NavigateToURL(server->TestServerPageW( + L"files/notifications/notifications_request_function.html")); + WaitUntilTabCount(1); + + // Request permission by calling request() while eval'ing an inline script; + // That's considered a user gesture to webkit, and should produce an infobar. + bool result; + tab->ExecuteAndExtractBool(L"", + L"window.domAutomationController.send(request());", + &result); + EXPECT_TRUE(result); + + int info_bar_count; + tab->GetInfoBarCount(&info_bar_count); + EXPECT_EQ(1, info_bar_count); +} + +TEST_F(NotificationsPermissionTest, TestNoUserGestureInfobar) { + const wchar_t kDocRoot[] = L"chrome/test/data"; + scoped_refptr server = + HTTPTestServer::CreateServer(kDocRoot, NULL); + ASSERT_TRUE(server.get() != NULL); + + scoped_refptr browser(automation()->GetBrowserWindow(0)); + scoped_refptr tab(browser->GetActiveTab()); + + // Load a page which just does a request; no user gesture should result + // in no infobar. + tab->NavigateToURL(server->TestServerPageW( + L"files/notifications/notifications_request_inline.html")); + WaitUntilTabCount(1); + + int info_bar_count; + tab->GetInfoBarCount(&info_bar_count); + EXPECT_EQ(0, info_bar_count); +} +#endif // OS_WIN diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index cc5c492..2182c47 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -4287,6 +4287,7 @@ 'browser/login_prompt_uitest.cc', 'browser/media_uitest.cc', 'browser/metrics/metrics_service_uitest.cc', + 'browser/notifications/notifications_uitest.cc', 'browser/printing/printing_layout_uitest.cc', 'browser/process_singleton_linux_uitest.cc', 'browser/renderer_host/resource_dispatcher_host_uitest.cc', diff --git a/chrome/common/desktop_notifications/active_notification_tracker.cc b/chrome/common/desktop_notifications/active_notification_tracker.cc index 3357ab3..d3d50cd 100644 --- a/chrome/common/desktop_notifications/active_notification_tracker.cc +++ b/chrome/common/desktop_notifications/active_notification_tracker.cc @@ -13,7 +13,7 @@ using WebKit::WebNotificationPermissionCallback; bool ActiveNotificationTracker::GetId( const WebNotification& notification, int& id) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); ReverseTable::iterator iter = reverse_notification_table_.find(notification); if (iter == reverse_notification_table_.end()) return false; @@ -23,7 +23,7 @@ bool ActiveNotificationTracker::GetId( bool ActiveNotificationTracker::GetNotification( int id, WebNotification* notification) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); WebNotification* lookup = notification_table_.Lookup(id); if (!lookup) return false; @@ -34,7 +34,7 @@ bool ActiveNotificationTracker::GetNotification( int ActiveNotificationTracker::RegisterNotification( const WebKit::WebNotification& proxy) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); WebNotification* notification = new WebNotification(proxy); int id = notification_table_.Add(notification); reverse_notification_table_[proxy] = id; @@ -42,7 +42,7 @@ int ActiveNotificationTracker::RegisterNotification( } void ActiveNotificationTracker::UnregisterNotification(int id) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); // We want to free the notification after removing it from the table. scoped_ptr notification(notification_table_.Lookup(id)); notification_table_.Remove(id); @@ -53,17 +53,17 @@ void ActiveNotificationTracker::UnregisterNotification(int id) { WebNotificationPermissionCallback* ActiveNotificationTracker::GetCallback( int id) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); return callback_table_.Lookup(id); } int ActiveNotificationTracker::RegisterPermissionRequest( WebNotificationPermissionCallback* callback) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); return callback_table_.Add(callback); } void ActiveNotificationTracker::OnPermissionRequestComplete(int id) { - DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_UI); + DCHECK(MessageLoop::current()->type() == MessageLoop::TYPE_DEFAULT); callback_table_.Remove(id); } diff --git a/chrome/renderer/notification_provider.cc b/chrome/renderer/notification_provider.cc index 9510cab..6746350 100644 --- a/chrome/renderer/notification_provider.cc +++ b/chrome/renderer/notification_provider.cc @@ -55,6 +55,10 @@ WebNotificationPresenter::Permission NotificationProvider::checkPermission( void NotificationProvider::requestPermission( const WebString& origin, WebNotificationPermissionCallback* callback) { + // We only request permission in response to a user gesture. + if (!view_->webview()->mainFrame()->isProcessingUserGesture()) + return; + int id = manager_.RegisterPermissionRequest(callback); Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(), diff --git a/chrome/test/data/notifications/notifications_request_function.html b/chrome/test/data/notifications/notifications_request_function.html new file mode 100755 index 0000000..55b6a86 --- /dev/null +++ b/chrome/test/data/notifications/notifications_request_function.html @@ -0,0 +1,13 @@ + + + + + +Standing by to request permission... + + \ No newline at end of file diff --git a/chrome/test/data/notifications/notifications_request_inline.html b/chrome/test/data/notifications/notifications_request_inline.html new file mode 100755 index 0000000..97db796 --- /dev/null +++ b/chrome/test/data/notifications/notifications_request_inline.html @@ -0,0 +1,10 @@ + + + + +Requesting permission... + + + \ No newline at end of file -- cgit v1.1