diff options
author | jnd@chromium.org <jnd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-03 23:17:25 +0000 |
---|---|---|
committer | jnd@chromium.org <jnd@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-03 23:17:25 +0000 |
commit | a2efc35d612a4d36cb09e8111556c88f905fc778 (patch) | |
tree | 3e1927127f0bd13ceb651893c6caac596ac68809 | |
parent | 30f42fbde0e858b2cee607c0eb3316c97baa7d64 (diff) | |
download | chromium_src-a2efc35d612a4d36cb09e8111556c88f905fc778.zip chromium_src-a2efc35d612a4d36cb09e8111556c88f905fc778.tar.gz chromium_src-a2efc35d612a4d36cb09e8111556c88f905fc778.tar.bz2 |
Support POST in browser navaigation.
The NavigationController already supports POST when navigating a URL, But PageNavigator does not.
This change adds some fieds in OpenURLParams and NavigateParams to pass parameters needed by POST to NavigationController::LoadURLParams.
BUG=89945
TEST=BrowserNavigatorTest.SendRequestUsingPOST
Review URL: https://chromiumcodereview.appspot.com/21378002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@215518 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/browser_navigator.cc | 18 | ||||
-rw-r--r-- | chrome/browser/ui/browser_navigator.h | 11 | ||||
-rw-r--r-- | chrome/browser/ui/browser_navigator_browsertest.cc | 68 | ||||
-rw-r--r-- | chrome/browser/ui/browser_navigator_browsertest.h | 7 | ||||
-rw-r--r-- | content/public/browser/page_navigator.cc | 5 | ||||
-rw-r--r-- | content/public/browser/page_navigator.h | 13 |
6 files changed, 116 insertions, 6 deletions
diff --git a/chrome/browser/ui/browser_navigator.cc b/chrome/browser/ui/browser_navigator.cc index 51a674b..990693e 100644 --- a/chrome/browser/ui/browser_navigator.cc +++ b/chrome/browser/ui/browser_navigator.cc @@ -45,6 +45,7 @@ #endif using content::GlobalRequestID; +using content::NavigationController; using content::WebContents; class BrowserNavigatorWebContentsAdoption { @@ -243,7 +244,7 @@ Profile* GetSourceProfile(chrome::NavigateParams* params) { void LoadURLInContents(WebContents* target_contents, const GURL& url, chrome::NavigateParams* params) { - content::NavigationController::LoadURLParams load_url_params(url); + NavigationController::LoadURLParams load_url_params(url); load_url_params.referrer = params->referrer; load_url_params.transition_type = params->transition; load_url_params.extra_headers = params->extra_headers; @@ -257,6 +258,14 @@ void LoadURLInContents(WebContents* target_contents, } else if (params->is_renderer_initiated) { load_url_params.is_renderer_initiated = true; } + + // Only allows the browser-initiated navigation to use POST. + if (params->uses_post && !params->is_renderer_initiated) { + load_url_params.load_type = + NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST; + load_url_params.browser_initiated_post_data = + params->browser_initiated_post_data; + } target_contents->GetController().LoadURLWithParams(load_url_params); } @@ -382,6 +391,7 @@ NavigateParams::NavigateParams(Browser* a_browser, const GURL& a_url, content::PageTransition a_transition) : url(a_url), + uses_post(false), target_contents(NULL), source_contents(NULL), disposition(CURRENT_TAB), @@ -402,7 +412,8 @@ NavigateParams::NavigateParams(Browser* a_browser, NavigateParams::NavigateParams(Browser* a_browser, WebContents* a_target_contents) - : target_contents(a_target_contents), + : uses_post(false), + target_contents(a_target_contents), source_contents(NULL), disposition(CURRENT_TAB), transition(content::PAGE_TRANSITION_LINK), @@ -424,6 +435,7 @@ NavigateParams::NavigateParams(Profile* a_profile, const GURL& a_url, content::PageTransition a_transition) : url(a_url), + uses_post(false), target_contents(NULL), source_contents(NULL), disposition(NEW_FOREGROUND_TAB), @@ -455,6 +467,8 @@ void FillNavigateParamsFromOpenURLParams(chrome::NavigateParams* nav_params, params.transferred_global_request_id; nav_params->should_replace_current_entry = params.should_replace_current_entry; + nav_params->uses_post = params.uses_post; + nav_params->browser_initiated_post_data = params.browser_initiated_post_data; } void Navigate(NavigateParams* params) { diff --git a/chrome/browser/ui/browser_navigator.h b/chrome/browser/ui/browser_navigator.h index 9270a40..3d84109 100644 --- a/chrome/browser/ui/browser_navigator.h +++ b/chrome/browser/ui/browser_navigator.h @@ -7,6 +7,8 @@ #include <string> +#include "base/memory/ref_counted.h" +#include "base/memory/ref_counted_memory.h" #include "chrome/browser/ui/host_desktop.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/global_request_id.h" @@ -63,6 +65,15 @@ struct NavigateParams { GURL url; content::Referrer referrer; + // Indicates whether this navigation will be sent using POST. + // The POST method is limited support for basic POST data by leveraging + // NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST. + // It is not for things like file uploads. + bool uses_post; + + // The post data when the navigation uses POST. + scoped_refptr<base::RefCountedMemory> browser_initiated_post_data; + // Extra headers to add to the request for this page. Headers are // represented as "<name>: <value>" and separated by \r\n. The entire string // is terminated by \r\n. May be empty if no extra headers are needed. diff --git a/chrome/browser/ui/browser_navigator_browsertest.cc b/chrome/browser/ui/browser_navigator_browsertest.cc index 8cd9080..28424fd 100644 --- a/chrome/browser/ui/browser_navigator_browsertest.cc +++ b/chrome/browser/ui/browser_navigator_browsertest.cc @@ -6,6 +6,7 @@ #include "base/command_line.h" #include "base/prefs/pref_service.h" +#include "base/strings/utf_string_conversions.h" #include "chrome/app/chrome_command_ids.h" #include "chrome/browser/prefs/incognito_mode_prefs.h" #include "chrome/browser/profiles/profile.h" @@ -32,6 +33,9 @@ using content::WebContents; namespace { +const char kExpectedTitle[] = "PASSED!"; +const char kEchoTitleCommand[] = "echotitle"; + GURL GetGoogleURL() { return GURL("http://www.google.com/"); } @@ -62,7 +66,7 @@ GURL ShortenUberURL(const GURL& url) { return GURL(url_string); } -} // namespace +} // namespace chrome::NavigateParams BrowserNavigatorTest::MakeNavigateParams() const { return MakeNavigateParams(browser()); @@ -76,6 +80,34 @@ chrome::NavigateParams BrowserNavigatorTest::MakeNavigateParams( return params; } +bool BrowserNavigatorTest::OpenPOSTURLInNewForegroundTabAndGetTitle( + const GURL& url, const std::string& post_data, bool is_browser_initiated, + base::string16* title) { + chrome::NavigateParams param(MakeNavigateParams()); + param.disposition = NEW_FOREGROUND_TAB; + param.url = url; + param.is_renderer_initiated = !is_browser_initiated; + param.uses_post = true; + param.browser_initiated_post_data = new base::RefCountedStaticMemory( + reinterpret_cast<const uint8*>(post_data.data()), post_data.size()); + + ui_test_utils::NavigateToURL(¶m); + if (!param.target_contents) + return false; + + // Navigate() should have opened the contents in new foreground tab in the + // current Browser. + EXPECT_EQ(browser(), param.browser); + EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), + param.target_contents); + // We should have one window, with one tab. + EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); + EXPECT_EQ(2, browser()->tab_strip_model()->count()); + + *title = param.target_contents->GetTitle(); + return true; +} + Browser* BrowserNavigatorTest::CreateEmptyBrowserForType(Browser::Type type, Profile* profile) { Browser* browser = new Browser( @@ -1291,4 +1323,36 @@ IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, ViewSourceIsntSingleton) { EXPECT_EQ(-1, chrome::GetIndexOfSingletonTab(&singleton_params)); } -} // namespace +// This test verifies that browser initiated navigations can send requests +// using POST. +IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, + SendBrowserInitiatedRequestUsingPOST) { + // Uses a test sever to verify POST request. + ASSERT_TRUE(test_server()->Start()); + + // Open a browser initiated POST request in new foreground tab. + string16 expected_title(base::ASCIIToUTF16(kExpectedTitle)); + std::string post_data = kExpectedTitle; + string16 title; + ASSERT_TRUE(OpenPOSTURLInNewForegroundTabAndGetTitle( + test_server()->GetURL(kEchoTitleCommand), post_data, true, &title)); + EXPECT_EQ(expected_title, title); +} + +// This test verifies that renderer initiated navigations can NOT send requests +// using POST. +IN_PROC_BROWSER_TEST_F(BrowserNavigatorTest, + SendRendererInitiatedRequestUsingPOST) { + // Uses a test sever to verify POST request. + ASSERT_TRUE(test_server()->Start()); + + // Open a renderer initiated POST request in new foreground tab. + string16 expected_title(base::ASCIIToUTF16(kExpectedTitle)); + std::string post_data = kExpectedTitle; + string16 title; + ASSERT_TRUE(OpenPOSTURLInNewForegroundTabAndGetTitle( + test_server()->GetURL(kEchoTitleCommand), post_data, false, &title)); + EXPECT_NE(expected_title, title); +} + +} // namespace diff --git a/chrome/browser/ui/browser_navigator_browsertest.h b/chrome/browser/ui/browser_navigator_browsertest.h index 4fbf814..45f507e 100644 --- a/chrome/browser/ui/browser_navigator_browsertest.h +++ b/chrome/browser/ui/browser_navigator_browsertest.h @@ -5,6 +5,8 @@ #ifndef CHROME_BROWSER_UI_BROWSER_NAVIGATOR_BROWSERTEST_H_ #define CHROME_BROWSER_UI_BROWSER_NAVIGATOR_BROWSERTEST_H_ +#include <string> + #include "chrome/browser/ui/browser.h" #include "chrome/test/base/in_process_browser_test.h" #include "content/public/browser/notification_types.h" @@ -42,6 +44,11 @@ class BrowserNavigatorTest : public InProcessBrowserTest, const content::NotificationSource& source, const content::NotificationDetails& details) OVERRIDE; + bool OpenPOSTURLInNewForegroundTabAndGetTitle(const GURL& url, + const std::string& post_data, + bool is_browser_initiated, + base::string16* title); + size_t created_tab_contents_count_; }; diff --git a/content/public/browser/page_navigator.cc b/content/public/browser/page_navigator.cc index 3358bd9..d6eb79f 100644 --- a/content/public/browser/page_navigator.cc +++ b/content/public/browser/page_navigator.cc @@ -14,6 +14,7 @@ OpenURLParams::OpenURLParams( bool is_renderer_initiated) : url(url), referrer(referrer), + uses_post(false), source_frame_id(-1), disposition(disposition), transition(transition), @@ -31,6 +32,7 @@ OpenURLParams::OpenURLParams( bool is_renderer_initiated) : url(url), referrer(referrer), + uses_post(false), source_frame_id(source_frame_id), disposition(disposition), transition(transition), @@ -40,7 +42,8 @@ OpenURLParams::OpenURLParams( } OpenURLParams::OpenURLParams() - : source_frame_id(-1), + : uses_post(false), + source_frame_id(-1), disposition(UNKNOWN), transition(PageTransitionFromInt(0)), is_renderer_initiated(false), diff --git a/content/public/browser/page_navigator.h b/content/public/browser/page_navigator.h index 41e50b1..7a6e788 100644 --- a/content/public/browser/page_navigator.h +++ b/content/public/browser/page_navigator.h @@ -11,6 +11,8 @@ #include <string> +#include "base/memory/ref_counted.h" +#include "base/memory/ref_counted_memory.h" #include "content/common/content_export.h" #include "content/public/browser/global_request_id.h" #include "content/public/common/page_transition_types.h" @@ -40,6 +42,15 @@ struct CONTENT_EXPORT OpenURLParams { GURL url; Referrer referrer; + // Indicates whether this navigation will be sent using POST. + // The POST method is limited support for basic POST data by leveraging + // NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST. + // It is not for things like file uploads. + bool uses_post; + + // The post data when the navigation uses POST. + scoped_refptr<base::RefCountedMemory> browser_initiated_post_data; + // Extra headers to add to the request for this page. Headers are // represented as "<name>: <value>" and separated by \r\n. The entire string // is terminated by \r\n. May be empty if no extra headers are needed. @@ -87,6 +98,6 @@ class PageNavigator { virtual WebContents* OpenURL(const OpenURLParams& params) = 0; }; -} +} // namespace content #endif // CONTENT_PUBLIC_BROWSER_PAGE_NAVIGATOR_H_ |