#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/simple_test_clock.h"
+#include "base/time/clock.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/content_settings/content_settings_usages_state.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
-#include "chrome/browser/infobars/confirm_infobar_delegate.h"
-#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
-#include "chrome/common/content_settings_pattern.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
+#include "components/content_settings/core/common/content_settings_pattern.h"
+#include "components/infobars/core/confirm_infobar_delegate.h"
+#include "components/infobars/core/infobar.h"
#include "content/public/browser/dom_operation_notification_details.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
-#include "content/public/browser/render_view_host.h"
+#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/net_util.h"
namespace {
-
// IFrameLoader ---------------------------------------------------------------
// Used to block until an iframe is loaded via a javascript call.
"window.domAutomationController.setAutomationId(0);"
"window.domAutomationController.send(addIFrame(%d, \"%s\"));",
iframe_id, url.spec().c_str()));
- web_contents->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
- string16(), UTF8ToUTF16(script));
+ web_contents->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(script));
content::RunMessageLoop();
EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id), javascript_response_);
base::MessageLoopForUI::current()->Quit();
}
-
// GeolocationNotificationObserver --------------------------------------------
class GeolocationNotificationObserver : public content::NotificationObserver {
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
- void AddWatchAndWaitForNotification(content::RenderViewHost* render_view_host,
- const std::string& iframe_xpath);
+ void AddWatchAndWaitForNotification(
+ content::RenderFrameHost* render_frame_host);
bool has_infobar() const { return !!infobar_; }
- InfoBarDelegate* infobar() { return infobar_; }
+ infobars::InfoBar* infobar() { return infobar_; }
private:
content::NotificationRegistrar registrar_;
bool wait_for_infobar_;
- InfoBarDelegate* infobar_;
+ infobars::InfoBar* infobar_;
bool navigation_started_;
bool navigation_completed_;
std::string javascript_response_;
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED) {
- infobar_ = content::Details<InfoBarAddedDetails>(details).ptr();
- ASSERT_FALSE(infobar_->GetIcon().IsEmpty());
- ASSERT_TRUE(infobar_->AsConfirmInfoBarDelegate());
+ infobar_ = content::Details<infobars::InfoBar::AddedDetails>(details).ptr();
+ ASSERT_FALSE(infobar_->delegate()->GetIcon().IsEmpty());
+ ASSERT_TRUE(infobar_->delegate()->AsConfirmInfoBarDelegate());
} else if (type == content::NOTIFICATION_DOM_OPERATION_RESPONSE) {
content::Details<DomOperationNotificationDetails> dom_op_details(details);
javascript_response_ = dom_op_details->json;
}
void GeolocationNotificationObserver::AddWatchAndWaitForNotification(
- content::RenderViewHost* render_view_host,
- const std::string& iframe_xpath) {
+ content::RenderFrameHost* render_frame_host) {
LOG(WARNING) << "will add geolocation watch";
std::string script(
"window.domAutomationController.setAutomationId(0);"
"window.domAutomationController.send(geoStart());");
- render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(iframe_xpath),
- UTF8ToUTF16(script));
+ render_frame_host->ExecuteJavaScript(base::UTF8ToUTF16(script));
content::RunMessageLoop();
registrar_.RemoveAll();
LOG(WARNING) << "got geolocation watch" << javascript_response_;
void set_html_for_tests(const std::string& html_for_tests) {
html_for_tests_ = html_for_tests;
}
- const std::string& iframe_xpath() const { return iframe_xpath_; }
- void set_iframe_xpath(const std::string& iframe_xpath) {
- iframe_xpath_ = iframe_xpath;
- }
+ content::RenderFrameHost* frame_host() const { return render_frame_host_; }
const GURL& current_url() const { return current_url_; }
const GURL& iframe_url(size_t i) const { return iframe_urls_[i]; }
double fake_latitude() const { return fake_latitude_; }
// Loads the specified number of iframes.
void LoadIFrames(int number_iframes);
+ // Specifies which frame is to be used for JavaScript calls.
+ void SetFrameHost(const std::string& frame_name);
+
// Start watching for geolocation notifications. If |wait_for_infobar| is
// true, wait for the infobar to be displayed. Otherwise wait for a javascript
// response.
// cancel it.
void SetInfoBarResponse(const GURL& requesting_url, bool allowed);
- // Executes |function| in |web_contents| and checks that the return value
+ // Executes |function| in |render_frame_host| and checks that the return value
// matches |expected|.
- void CheckStringValueFromJavascriptForTab(const std::string& expected,
- const std::string& function,
- WebContents* web_contents);
+ void CheckStringValueFromJavascriptForFrame(
+ const std::string& expected,
+ const std::string& function,
+ content::RenderFrameHost* render_frame_host);
// Executes |function| and checks that the return value matches |expected|.
void CheckStringValueFromJavascript(const std::string& expected,
void NotifyGeoposition(double latitude, double longitude);
private:
- InfoBarDelegate* infobar_;
+ infobars::InfoBar* infobar_;
Browser* current_browser_;
// path element of a URL referencing the html content for this test.
std::string html_for_tests_;
- // This member defines the iframe (or top-level page, if empty) where the
- // javascript calls will run.
- std::string iframe_xpath_;
+ // This member defines the frame where the JavaScript calls will run.
+ content::RenderFrameHost* render_frame_host_;
// The current url for the top level page.
GURL current_url_;
// If not empty, the GURLs for the iframes loaded by LoadIFrames().
: infobar_(NULL),
current_browser_(NULL),
html_for_tests_("/geolocation/simple.html"),
+ render_frame_host_(NULL),
fake_latitude_(1.23),
fake_longitude_(4.56) {
}
}
}
+void GeolocationBrowserTest::SetFrameHost(const std::string& frame_name) {
+ WebContents* web_contents =
+ current_browser_->tab_strip_model()->GetActiveWebContents();
+ render_frame_host_ = NULL;
+
+ if (frame_name.empty()) {
+ render_frame_host_ = web_contents->GetMainFrame();
+ } else {
+ render_frame_host_ = content::FrameMatchingPredicate(
+ web_contents, base::Bind(&content::FrameMatchesName, frame_name));
+ }
+ DCHECK(render_frame_host_);
+}
+
void GeolocationBrowserTest::AddGeolocationWatch(bool wait_for_infobar) {
GeolocationNotificationObserver notification_observer(wait_for_infobar);
- notification_observer.AddWatchAndWaitForNotification(
- current_browser_->tab_strip_model()->GetActiveWebContents()->
- GetRenderViewHost(),
- iframe_xpath());
+ notification_observer.AddWatchAndWaitForNotification(render_frame_host_);
if (wait_for_infobar) {
EXPECT_TRUE(notification_observer.has_infobar());
infobar_ = notification_observer.infobar();
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&web_contents->GetController()));
if (allowed)
- infobar_->AsConfirmInfoBarDelegate()->Accept();
+ infobar_->delegate()->AsConfirmInfoBarDelegate()->Accept();
else
- infobar_->AsConfirmInfoBarDelegate()->Cancel();
+ infobar_->delegate()->AsConfirmInfoBarDelegate()->Cancel();
observer.Wait();
}
- InfoBarService::FromWebContents(web_contents)->RemoveInfoBar(infobar_);
+ InfoBarService* infobar_service =
+ InfoBarService::FromWebContents(web_contents);
+ infobar_service->RemoveInfoBar(infobar_);
LOG(WARNING) << "infobar response set";
infobar_ = NULL;
EXPECT_GT(usages_state.state_map().size(), state_map_size);
usages_state.state_map().find(requesting_origin)->second);
}
-void GeolocationBrowserTest::CheckStringValueFromJavascriptForTab(
+void GeolocationBrowserTest::CheckStringValueFromJavascriptForFrame(
const std::string& expected,
const std::string& function,
- WebContents* web_contents) {
+ content::RenderFrameHost* render_frame_host) {
std::string script(base::StringPrintf(
"window.domAutomationController.send(%s)", function.c_str()));
std::string result;
- ASSERT_TRUE(content::ExecuteScriptInFrameAndExtractString(
- web_contents, iframe_xpath_, script, &result));
+ ASSERT_TRUE(content::ExecuteScriptAndExtractString(
+ render_frame_host, script, &result));
EXPECT_EQ(expected, result);
}
void GeolocationBrowserTest::CheckStringValueFromJavascript(
const std::string& expected,
const std::string& function) {
- CheckStringValueFromJavascriptForTab(
- expected, function,
- current_browser_->tab_strip_model()->GetActiveWebContents());
+ CheckStringValueFromJavascriptForFrame(
+ expected, function, render_frame_host_);
}
void GeolocationBrowserTest::NotifyGeoposition(double latitude,
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DisplaysPermissionBar) {
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, Geoposition) {
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
SetInfoBarResponse(current_url(), true);
CheckGeoposition(fake_latitude(), fake_longitude());
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest,
ErrorOnPermissionDenied) {
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
// Infobar was displayed, deny access and check for error code.
SetInfoBarResponse(current_url(), false);
// See http://crbug.com/308358
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DISABLED_NoInfobarForSecondTab) {
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
SetInfoBarResponse(current_url(), true);
// Disables further prompts from this tab.
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
- // Checks infobar will not be created a second tab.
+ // Checks infobar will not be created in a second tab.
ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB));
+ SetFrameHost("");
AddGeolocationWatch(false);
CheckGeoposition(fake_latitude(), fake_longitude());
}
ContentSettingsPattern::FromURLNoWildcard(current_url()),
ContentSettingsPattern::FromURLNoWildcard(current_url()),
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_BLOCK);
+ SetFrameHost("");
AddGeolocationWatch(false);
// Checks we have an error for this denied origin.
CheckStringValueFromJavascript("1", "geoGetLastError()");
// Checks infobar will not be created a second tab.
ASSERT_TRUE(Initialize(INITIALIZATION_NEWTAB));
+ SetFrameHost("");
AddGeolocationWatch(false);
CheckStringValueFromJavascript("1", "geoGetLastError()");
}
ContentSettingsPattern::FromURLNoWildcard(current_url()),
CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW);
// Checks no infobar will be created and there's no error callback.
+ SetFrameHost("");
AddGeolocationWatch(false);
CheckGeoposition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoInfobarForOffTheRecord) {
// First, check infobar will be created for regular profile
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
- // Response will be persisted
+ // Response will be persisted.
SetInfoBarResponse(current_url(), true);
CheckGeoposition(fake_latitude(), fake_longitude());
// Disables further prompts from this tab.
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
// Go incognito, and checks no infobar will be created.
ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD));
+ SetFrameHost("");
AddGeolocationWatch(false);
CheckGeoposition(fake_latitude(), fake_longitude());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoLeakFromOffTheRecord) {
// First, check infobar will be created for incognito profile.
ASSERT_TRUE(Initialize(INITIALIZATION_OFFTHERECORD));
+ SetFrameHost("");
AddGeolocationWatch(true);
// Response won't be persisted.
SetInfoBarResponse(current_url(), true);
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
// Go to the regular profile, infobar will be created.
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ SetFrameHost("");
AddGeolocationWatch(true);
SetInfoBarResponse(current_url(), false);
CheckStringValueFromJavascript("1", "geoGetLastError()");
LoadIFrames(2);
LOG(WARNING) << "frames loaded";
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
AddGeolocationWatch(true);
SetInfoBarResponse(iframe_url(0), true);
CheckGeoposition(fake_latitude(), fake_longitude());
// Test second iframe from a different origin with a cached geoposition will
// create the infobar.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
AddGeolocationWatch(true);
// Back to the first frame, enable navigation and refresh geoposition.
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
CheckStringValueFromJavascript("1", "geoSetMaxNavigateCount(1)");
double fresh_position_latitude = 3.17;
double fresh_position_longitude = 4.23;
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
// Now go ahead an authorize the second frame.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
// Infobar was displayed, allow access and check there's no error code.
SetInfoBarResponse(iframe_url(1), true);
LOG(WARNING) << "Checking position...";
ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES));
LoadIFrames(2);
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
AddGeolocationWatch(true);
SetInfoBarResponse(iframe_url(0), true);
CheckGeoposition(fake_latitude(), fake_longitude());
// Disable navigation for this frame.
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
- // Now go ahead an authorize the second frame.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ // Now go ahead and authorize the second frame.
+ SetFrameHost("iframe_1");
AddGeolocationWatch(true);
// WebKit will use its cache, but we also broadcast a position shortly
// afterwards. We're only interested in the first navigation for the success
LoadIFrames(2);
LOG(WARNING) << "frames loaded";
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
AddGeolocationWatch(true);
SetInfoBarResponse(iframe_url(0), true);
CheckGeoposition(fake_latitude(), fake_longitude());
// Test second iframe from a different origin with a cached geoposition will
// create the infobar.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
AddGeolocationWatch(true);
InfoBarService* infobar_service = InfoBarService::FromWebContents(
// correctly. Also acts as a regression test for http://crbug.com/40478
set_html_for_tests("/geolocation/invalid_request_url.html");
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+
+ SetFrameHost("");
WebContents* original_tab =
current_browser()->tab_strip_model()->GetActiveWebContents();
CheckStringValueFromJavascript("1", "requestGeolocationFromInvalidUrl()");
- CheckStringValueFromJavascriptForTab("1", "isAlive()", original_tab);
+ CheckStringValueFromJavascriptForFrame("1", "isAlive()",
+ original_tab->GetMainFrame());
}
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoInfoBarBeforeStart) {
LOG(WARNING) << "frames loaded";
// Access navigator.geolocation, but ensure it won't request permission.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
CheckStringValueFromJavascript("object", "geoAccessNavigatorGeolocation()");
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
AddGeolocationWatch(true);
SetInfoBarResponse(iframe_url(0), true);
CheckGeoposition(fake_latitude(), fake_longitude());
CheckStringValueFromJavascript("0", "geoSetMaxNavigateCount(0)");
// Permission should be requested after adding a watch.
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
AddGeolocationWatch(true);
SetInfoBarResponse(iframe_url(1), true);
CheckGeoposition(fake_latitude(), fake_longitude());
IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, TwoWatchesInOneFrame) {
set_html_for_tests("/geolocation/two_watches.html");
ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+
// First, set the JavaScript to navigate when it receives |final_position|.
double final_position_latitude = 3.17;
double final_position_longitude = 4.23;
EXPECT_EQ(js_result, "ok");
// Send a position which both geolocation watches will receive.
+ SetFrameHost("");
AddGeolocationWatch(true);
SetInfoBarResponse(current_url(), true);
CheckGeoposition(fake_latitude(), fake_longitude());
ASSERT_TRUE(Initialize(INITIALIZATION_IFRAMES));
LoadIFrames(3);
- set_iframe_xpath("//iframe[@id='iframe_0']");
+ SetFrameHost("iframe_0");
AddGeolocationWatch(true);
- set_iframe_xpath("//iframe[@id='iframe_1']");
+ SetFrameHost("iframe_1");
AddGeolocationWatch(false);
- set_iframe_xpath("//iframe[@id='iframe_2']");
+ SetFrameHost("iframe_2");
AddGeolocationWatch(false);
std::string script =
current_browser()->tab_strip_model()->GetActiveWebContents(), script);
EXPECT_EQ(result, true);
}
+
+IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, LastUsageUpdated) {
+ ASSERT_TRUE(Initialize(INITIALIZATION_NONE));
+ base::SimpleTestClock* clock_ = new base::SimpleTestClock();
+ current_browser()
+ ->profile()
+ ->GetHostContentSettingsMap()
+ ->SetPrefClockForTesting(scoped_ptr<base::Clock>(clock_));
+ clock_->SetNow(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(10));
+
+ // Setting the permission should trigger the last usage.
+ current_browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
+ ContentSettingsPattern::FromURLNoWildcard(current_url()),
+ ContentSettingsPattern::FromURLNoWildcard(current_url()),
+ CONTENT_SETTINGS_TYPE_GEOLOCATION,
+ std::string(),
+ CONTENT_SETTING_ALLOW);
+
+ // Permission has been used at the starting time.
+ EXPECT_EQ(current_browser()
+ ->profile()
+ ->GetHostContentSettingsMap()
+ ->GetLastUsage(current_url().GetOrigin(),
+ current_url().GetOrigin(),
+ CONTENT_SETTINGS_TYPE_GEOLOCATION)
+ .ToDoubleT(),
+ 10);
+
+ clock_->Advance(base::TimeDelta::FromSeconds(3));
+
+ // Watching should trigger the last usage update.
+ SetFrameHost("");
+ AddGeolocationWatch(false);
+ CheckGeoposition(fake_latitude(), fake_longitude());
+
+ // Last usage has been updated.
+ EXPECT_EQ(current_browser()
+ ->profile()
+ ->GetHostContentSettingsMap()
+ ->GetLastUsage(current_url().GetOrigin(),
+ current_url().GetOrigin(),
+ CONTENT_SETTINGS_TYPE_GEOLOCATION)
+ .ToDoubleT(),
+ 13);
+}