Add new CefRenderProcessHandler::OnBeforeNavigation callback (issue #722).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@904 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2012-11-09 18:47:09 +00:00
parent 255fdad295
commit 79f3683beb
15 changed files with 472 additions and 54 deletions

View File

@ -1189,6 +1189,7 @@
'tests/unittests/client_app_delegates.cc',
'tests/unittests/cookie_unittest.cc',
'tests/unittests/dom_unittest.cc',
'tests/unittests/navigation_unittest.cc',
'tests/unittests/process_message_unittest.cc',
'tests/unittests/scheme_handler_unittest.cc',
'tests/unittests/urlrequest_unittest.cc',

View File

@ -81,6 +81,17 @@ typedef struct _cef_render_process_handler_t {
struct _cef_render_process_handler_t* self,
struct _cef_browser_t* browser);
///
// Called before browser navigation. Return true (1) to cancel the navigation
// or false (0) to allow the navigation to proceed. The |request| object
// cannot be modified in this callback.
///
int (CEF_CALLBACK *on_before_navigation)(
struct _cef_render_process_handler_t* self,
struct _cef_browser_t* browser, struct _cef_frame_t* frame,
struct _cef_request_t* request,
enum cef_navigation_type_t navigation_type, int is_redirect);
///
// Called immediately after the V8 context for a frame has been created. To
// retrieve the JavaScript 'window' object use the

View File

@ -52,6 +52,8 @@
/*--cef(source=client)--*/
class CefRenderProcessHandler : public virtual CefBase {
public:
typedef cef_navigation_type_t NavigationType;
///
// Called after the render process main thread has been created.
///
@ -76,6 +78,18 @@ class CefRenderProcessHandler : public virtual CefBase {
/*--cef()--*/
virtual void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) {}
///
// Called before browser navigation. Return true to cancel the navigation or
// false to allow the navigation to proceed. The |request| object cannot be
// modified in this callback.
///
/*--cef()--*/
virtual bool OnBeforeNavigation(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
NavigationType navigation_type,
bool is_redirect) { return false; }
///
// Called immediately after the V8 context for a frame has been created. To
// retrieve the JavaScript 'window' object use the CefV8Context::GetGlobal()

View File

@ -1188,6 +1188,18 @@ enum cef_focus_source_t {
FOCUS_SOURCE_SYSTEM,
};
///
// Navigation types.
///
enum cef_navigation_type_t {
NAVIGATION_LINK_CLICKED = 0,
NAVIGATION_FORM_SUBMITTED,
NAVIGATION_BACK_FORWARD,
NAVIGATION_RELOAD,
NAVIGATION_FORM_RESUBMITTED,
NAVIGATION_OTHER,
};
///
// Supported XML encoding types. The parser supports ASCII, ISO-8859-1, and
// UTF16 (LE and BE) by default. All other types must be translated to UTF8

View File

@ -15,6 +15,7 @@ MSVC_POP_WARNING();
#include "libcef/common/cef_messages.h"
#include "libcef/common/content_client.h"
#include "libcef/common/request_impl.h"
#include "libcef/renderer/browser_impl.h"
#include "libcef/renderer/chrome_bindings.h"
#include "libcef/renderer/render_message_filter.h"
@ -201,6 +202,62 @@ void CefContentRendererClient::RenderViewCreated(
new CefPrerendererClient(render_view);
}
bool CefContentRendererClient::HandleNavigation(
WebKit::WebFrame* frame,
const WebKit::WebURLRequest& request,
WebKit::WebNavigationType type,
WebKit::WebNavigationPolicy default_policy,
bool is_redirect) {
CefRefPtr<CefApp> application = CefContentClient::Get()->application();
if (application.get()) {
CefRefPtr<CefRenderProcessHandler> handler =
application->GetRenderProcessHandler();
if (handler.get()) {
CefRefPtr<CefBrowserImpl> browserPtr =
CefBrowserImpl::GetBrowserForMainFrame(frame->top());
DCHECK(browserPtr.get());
if (browserPtr.get()) {
CefRefPtr<CefFrameImpl> framePtr = browserPtr->GetWebFrameImpl(frame);
CefRefPtr<CefRequest> requestPtr(CefRequest::Create());
CefRequestImpl* requestImpl =
static_cast<CefRequestImpl*>(requestPtr.get());
requestImpl->Set(request);
requestImpl->SetReadOnly(true);
cef_navigation_type_t navigation_type;
switch (type) {
case WebKit::WebNavigationTypeLinkClicked:
navigation_type = NAVIGATION_LINK_CLICKED;
break;
case WebKit::WebNavigationTypeFormSubmitted:
navigation_type = NAVIGATION_FORM_SUBMITTED;
break;
case WebKit::WebNavigationTypeBackForward:
navigation_type = NAVIGATION_BACK_FORWARD;
break;
case WebKit::WebNavigationTypeReload:
navigation_type = NAVIGATION_RELOAD;
break;
case WebKit::WebNavigationTypeFormResubmitted:
navigation_type = NAVIGATION_FORM_RESUBMITTED;
break;
case WebKit::WebNavigationTypeOther:
navigation_type = NAVIGATION_OTHER;
break;
}
if (handler->OnBeforeNavigation(browserPtr.get(), framePtr.get(),
requestPtr.get(), navigation_type,
is_redirect)) {
return true;
}
}
}
}
return false;
}
void CefContentRendererClient::DidCreateScriptContext(
WebKit::WebFrame* frame, v8::Handle<v8::Context> context,
int extension_group, int world_id) {

View File

@ -56,6 +56,11 @@ class CefContentRendererClient : public content::ContentRendererClient {
// ContentRendererClient implementation.
virtual void RenderThreadStarted() OVERRIDE;
virtual void RenderViewCreated(content::RenderView* render_view) OVERRIDE;
virtual bool HandleNavigation(WebKit::WebFrame* frame,
const WebKit::WebURLRequest& request,
WebKit::WebNavigationType type,
WebKit::WebNavigationPolicy default_policy,
bool is_redirect) OVERRIDE;
virtual void DidCreateScriptContext(WebKit::WebFrame* frame,
v8::Handle<v8::Context> context,
int extension_group,

View File

@ -15,6 +15,7 @@
#include "libcef_dll/ctocpp/domnode_ctocpp.h"
#include "libcef_dll/ctocpp/frame_ctocpp.h"
#include "libcef_dll/ctocpp/process_message_ctocpp.h"
#include "libcef_dll/ctocpp/request_ctocpp.h"
#include "libcef_dll/ctocpp/v8context_ctocpp.h"
#include "libcef_dll/ctocpp/v8exception_ctocpp.h"
#include "libcef_dll/ctocpp/v8stack_trace_ctocpp.h"
@ -80,6 +81,40 @@ void CEF_CALLBACK render_process_handler_on_browser_destroyed(
CefBrowserCToCpp::Wrap(browser));
}
int CEF_CALLBACK render_process_handler_on_before_navigation(
struct _cef_render_process_handler_t* self, cef_browser_t* browser,
cef_frame_t* frame, struct _cef_request_t* request,
enum cef_navigation_type_t navigation_type, int is_redirect) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
DCHECK(self);
if (!self)
return 0;
// Verify param: browser; type: refptr_diff
DCHECK(browser);
if (!browser)
return 0;
// Verify param: frame; type: refptr_diff
DCHECK(frame);
if (!frame)
return 0;
// Verify param: request; type: refptr_diff
DCHECK(request);
if (!request)
return 0;
// Execute
bool _retval = CefRenderProcessHandlerCppToC::Get(self)->OnBeforeNavigation(
CefBrowserCToCpp::Wrap(browser),
CefFrameCToCpp::Wrap(frame),
CefRequestCToCpp::Wrap(request),
navigation_type,
is_redirect?true:false);
// Return type: bool
return _retval;
}
void CEF_CALLBACK render_process_handler_on_context_created(
struct _cef_render_process_handler_t* self, cef_browser_t* browser,
cef_frame_t* frame, struct _cef_v8context_t* context) {
@ -240,6 +275,8 @@ CefRenderProcessHandlerCppToC::CefRenderProcessHandlerCppToC(
render_process_handler_on_browser_created;
struct_.struct_.on_browser_destroyed =
render_process_handler_on_browser_destroyed;
struct_.struct_.on_before_navigation =
render_process_handler_on_before_navigation;
struct_.struct_.on_context_created =
render_process_handler_on_context_created;
struct_.struct_.on_context_released =

View File

@ -14,6 +14,7 @@
#include "libcef_dll/cpptoc/domnode_cpptoc.h"
#include "libcef_dll/cpptoc/frame_cpptoc.h"
#include "libcef_dll/cpptoc/process_message_cpptoc.h"
#include "libcef_dll/cpptoc/request_cpptoc.h"
#include "libcef_dll/cpptoc/v8context_cpptoc.h"
#include "libcef_dll/cpptoc/v8exception_cpptoc.h"
#include "libcef_dll/cpptoc/v8stack_trace_cpptoc.h"
@ -76,6 +77,40 @@ void CefRenderProcessHandlerCToCpp::OnBrowserDestroyed(
CefBrowserCppToC::Wrap(browser));
}
bool CefRenderProcessHandlerCToCpp::OnBeforeNavigation(
CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, NavigationType navigation_type,
bool is_redirect) {
if (CEF_MEMBER_MISSING(struct_, on_before_navigation))
return false;
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: browser; type: refptr_diff
DCHECK(browser.get());
if (!browser.get())
return false;
// Verify param: frame; type: refptr_diff
DCHECK(frame.get());
if (!frame.get())
return false;
// Verify param: request; type: refptr_diff
DCHECK(request.get());
if (!request.get())
return false;
// Execute
int _retval = struct_->on_before_navigation(struct_,
CefBrowserCppToC::Wrap(browser),
CefFrameCppToC::Wrap(frame),
CefRequestCppToC::Wrap(request),
navigation_type,
is_redirect);
// Return type: bool
return _retval?true:false;
}
void CefRenderProcessHandlerCToCpp::OnContextCreated(
CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {

View File

@ -38,6 +38,9 @@ class CefRenderProcessHandlerCToCpp
virtual void OnWebKitInitialized() OVERRIDE;
virtual void OnBrowserCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool OnBeforeNavigation(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request,
NavigationType navigation_type, bool is_redirect) OVERRIDE;
virtual void OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE;
virtual void OnContextReleased(CefRefPtr<CefBrowser> browser,

View File

@ -26,6 +26,11 @@ patches = [
'name': 'zlib',
'path': '../third_party/zlib/',
},
{
# https://codereview.chromium.org/11293157/
'name': 'content_navigation',
'path': '../content/',
},
{
# http://code.google.com/p/chromiumembedded/issues/detail?id=364
'name': 'spi_webcore_364',

View File

@ -0,0 +1,66 @@
Index: public/renderer/content_renderer_client.cc
===================================================================
--- public/renderer/content_renderer_client.cc (revision 165669)
+++ public/renderer/content_renderer_client.cc (working copy)
@@ -56,6 +56,15 @@
return false;
}
+bool ContentRendererClient::HandleNavigation(
+ WebKit::WebFrame* frame,
+ const WebKit::WebURLRequest& request,
+ WebKit::WebNavigationType type,
+ WebKit::WebNavigationPolicy default_policy,
+ bool is_redirect) {
+ return false;
+}
+
bool ContentRendererClient::ShouldFork(WebKit::WebFrame* frame,
const GURL& url,
bool is_initial_navigation,
Index: public/renderer/content_renderer_client.h
===================================================================
--- public/renderer/content_renderer_client.h (revision 165669)
+++ public/renderer/content_renderer_client.h (working copy)
@@ -12,6 +12,8 @@
#include "ipc/ipc_message.h"
#include "content/public/common/content_client.h"
#include "content/public/common/page_transition_types.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebNavigationPolicy.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebNavigationType.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPageVisibilityState.h"
#include "v8/include/v8.h"
@@ -130,6 +132,14 @@
// Returns true if the given url can create popup windows.
virtual bool AllowPopup(const GURL& creator);
+ // Returns true if the navigation was handled by the embedder and should be
+ // ignored by WebKit. This method is used by CEF.
+ virtual bool HandleNavigation(WebKit::WebFrame* frame,
+ const WebKit::WebURLRequest& request,
+ WebKit::WebNavigationType type,
+ WebKit::WebNavigationPolicy default_policy,
+ bool is_redirect);
+
// Returns true if we should fork a new process for the given navigation.
virtual bool ShouldFork(WebKit::WebFrame* frame,
const GURL& url,
Index: renderer/render_view_impl.cc
===================================================================
--- renderer/render_view_impl.cc (revision 165669)
+++ renderer/render_view_impl.cc (working copy)
@@ -2674,6 +2674,13 @@
WebNavigationPolicy RenderViewImpl::decidePolicyForNavigation(
WebFrame* frame, const WebURLRequest& request, WebNavigationType type,
const WebNode&, WebNavigationPolicy default_policy, bool is_redirect) {
+ if (request.url() != GURL(kSwappedOutURL) &&
+ GetContentClient()->renderer()->HandleNavigation(frame, request, type,
+ default_policy,
+ is_redirect)) {
+ return WebKit::WebNavigationPolicyIgnore;
+ }
+
Referrer referrer(
GURL(request.httpHeaderField(WebString::fromUTF8("Referer"))),
GetReferrerPolicyFromRequest(frame, request));

View File

@ -272,6 +272,13 @@ void ClientApp::OnWebKitInitialized() {
(*it)->OnWebKitInitialized(this);
}
void ClientApp::OnBrowserCreated(CefRefPtr<CefBrowser> browser) {
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it)
(*it)->OnBrowserCreated(this, browser);
}
void ClientApp::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) {
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
@ -279,6 +286,23 @@ void ClientApp::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) {
(*it)->OnBrowserDestroyed(this, browser);
}
bool ClientApp::OnBeforeNavigation(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
NavigationType navigation_type,
bool is_redirect) {
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it) {
if ((*it)->OnBeforeNavigation(this, browser, frame, request,
navigation_type, is_redirect)) {
return true;
}
}
return false;
}
void ClientApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {

View File

@ -52,11 +52,28 @@ class ClientApp : public CefApp,
virtual void OnWebKitInitialized(CefRefPtr<ClientApp> app) {
}
// Called after a browser has been created.
virtual void OnBrowserCreated(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser) {
}
// Called before a browser is destroyed.
virtual void OnBrowserDestroyed(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser) {
}
// Called before browser navigation. Return true to cancel the navigation or
// false to allow the navigation to proceed. The |request| object cannot be
// modified in this callback.
virtual bool OnBeforeNavigation(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
cef_navigation_type_t navigation_type,
bool is_redirect) {
return false;
}
// Called when a V8 context is created. Used to create V8 window bindings
// and set message callbacks. RenderDelegates should check for unique URLs
// to avoid interfering with each other.
@ -168,7 +185,13 @@ class ClientApp : public CefApp,
// CefRenderProcessHandler methods.
virtual void OnRenderThreadCreated() OVERRIDE;
virtual void OnWebKitInitialized() OVERRIDE;
virtual void OnBrowserCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool OnBeforeNavigation(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
NavigationType navigation_type,
bool is_redirect) OVERRIDE;
virtual void OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) OVERRIDE;

View File

@ -29,6 +29,10 @@ void ClientApp::CreateRenderDelegates(RenderDelegateSet& delegates) {
// Bring in the URLRequest tests.
extern void CreateURLRequestRendererTests(RenderDelegateSet& delegates);
CreateURLRequestRendererTests(delegates);
// Bring in the Navigation tests.
extern void CreateNavigationRendererTests(RenderDelegateSet& delegates);
CreateNavigationRendererTests(delegates);
}
// static

View File

@ -4,15 +4,18 @@
#include "include/cef_callback.h"
#include "include/cef_scheme.h"
#include "tests/cefclient/client_app.h"
#include "tests/unittests/test_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
static const char* kNav1 = "http://tests/nav1.html";
static const char* kNav2 = "http://tests/nav2.html";
static const char* kNav3 = "http://tests/nav3.html";
static const char* kNav4 = "http://tests/nav4.html";
const char kHNavDomain[] = "http://tests-hnav/";
const char kHNav1[] = "http://tests-hnav/nav1.html";
const char kHNav2[] = "http://tests-hnav/nav2.html";
const char kHNav3[] = "http://tests-hnav/nav3.html";
const char kHistoryNavMsg[] = "NavigationTest.HistoryNav";
enum NavAction {
NA_LOAD = 1,
@ -29,29 +32,95 @@ typedef struct {
} NavListItem;
// Array of navigation actions: X = current page, . = history exists
static NavListItem kNavList[] = {
// kNav1 | kNav2 | kNav3
{NA_LOAD, kNav1, false, false}, // X
{NA_LOAD, kNav2, true, false}, // . X
{NA_BACK, kNav1, false, true}, // X .
{NA_FORWARD, kNav2, true, false}, // . X
{NA_LOAD, kNav3, true, false}, // . . X
{NA_BACK, kNav2, true, true}, // . X .
static NavListItem kHNavList[] = {
// kHNav1 | kHNav2 | kHNav3
{NA_LOAD, kHNav1, false, false}, // X
{NA_LOAD, kHNav2, true, false}, // . X
{NA_BACK, kHNav1, false, true}, // X .
{NA_FORWARD, kHNav2, true, false}, // . X
{NA_LOAD, kHNav3, true, false}, // . . X
{NA_BACK, kHNav2, true, true}, // . X .
// TODO(cef): Enable once ClearHistory is implemented
// {NA_CLEAR, kNav2, false, false}, // X
// {NA_CLEAR, kHNav2, false, false}, // X
};
#define NAV_LIST_SIZE() (sizeof(kNavList) / sizeof(NavListItem))
#define NAV_LIST_SIZE() (sizeof(kHNavList) / sizeof(NavListItem))
// Renderer side.
class HistoryNavRendererTest : public ClientApp::RenderDelegate {
public:
HistoryNavRendererTest()
: nav_(0) {}
virtual bool OnBeforeNavigation(CefRefPtr<ClientApp> app,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
cef_navigation_type_t navigation_type,
bool is_redirect) OVERRIDE {
std::string url = request->GetURL();
// Don't leak into other tests.
if (url.find(kHNavDomain) != 0)
return false;
const NavListItem& item = kHNavList[nav_];
EXPECT_STREQ(item.target, url.c_str());
if (item.action == NA_LOAD)
EXPECT_EQ(NAVIGATION_OTHER, navigation_type);
else if (item.action == NA_BACK || item.action == NA_FORWARD)
EXPECT_EQ(NAVIGATION_BACK_FORWARD, navigation_type);
if (nav_ > 0) {
const NavListItem& last_item = kHNavList[nav_ - 1];
EXPECT_EQ(last_item.can_go_back, browser->CanGoBack());
EXPECT_EQ(last_item.can_go_forward, browser->CanGoForward());
} else {
EXPECT_FALSE(browser->CanGoBack());
EXPECT_FALSE(browser->CanGoForward());
}
SendTestResults(browser);
nav_++;
return false;
}
protected:
// Send the test results.
void SendTestResults(CefRefPtr<CefBrowser> browser) {
// Check if the test has failed.
bool result = !TestFailed();
// Return the result to the browser process.
CefRefPtr<CefProcessMessage> return_msg =
CefProcessMessage::Create(kHistoryNavMsg);
CefRefPtr<CefListValue> args = return_msg->GetArgumentList();
EXPECT_TRUE(args.get());
EXPECT_TRUE(args->SetInt(0, nav_));
EXPECT_TRUE(args->SetBool(1, result));
EXPECT_TRUE(browser->SendProcessMessage(PID_BROWSER, return_msg));
}
int nav_;
IMPLEMENT_REFCOUNTING(HistoryNavRendererTest);
};
// Browser side.
class HistoryNavTestHandler : public TestHandler {
public:
HistoryNavTestHandler() : nav_(0) {}
HistoryNavTestHandler()
: nav_(0),
load_end_confirmation_(false),
renderer_confirmation_(false) {}
virtual void RunTest() OVERRIDE {
// Add the resources that we will navigate to/from.
AddResource(kNav1, "<html>Nav1</html>", "text/html");
AddResource(kNav2, "<html>Nav2</html>", "text/html");
AddResource(kNav3, "<html>Nav3</html>", "text/html");
AddResource(kHNav1, "<html>Nav1</html>", "text/html");
AddResource(kHNav2, "<html>Nav2</html>", "text/html");
AddResource(kHNav3, "<html>Nav3</html>", "text/html");
// Create the browser.
CreateBrowser(CefString());
@ -64,7 +133,7 @@ class HistoryNavTestHandler : public TestHandler {
return;
}
const NavListItem& item = kNavList[nav_];
const NavListItem& item = kHNavList[nav_];
// Perform the action.
switch (item.action) {
@ -89,6 +158,15 @@ class HistoryNavTestHandler : public TestHandler {
}
}
void RunNextNavIfReady(CefRefPtr<CefBrowser> browser) {
if (load_end_confirmation_ && renderer_confirmation_) {
load_end_confirmation_ = false;
renderer_confirmation_ = false;
nav_++;
RunNav(browser);
}
}
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE {
TestHandler::OnAfterCreated(browser);
@ -98,7 +176,7 @@ class HistoryNavTestHandler : public TestHandler {
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request) OVERRIDE {
const NavListItem& item = kNavList[nav_];
const NavListItem& item = kHNavList[nav_];
got_before_resource_load_[nav_].yes();
@ -113,7 +191,7 @@ class HistoryNavTestHandler : public TestHandler {
bool isLoading,
bool canGoBack,
bool canGoForward) OVERRIDE {
const NavListItem& item = kNavList[nav_];
const NavListItem& item = kHNavList[nav_];
got_loading_state_change_[nav_].yes();
@ -128,7 +206,7 @@ class HistoryNavTestHandler : public TestHandler {
if(browser->IsPopup() || !frame->IsMain())
return;
const NavListItem& item = kNavList[nav_];
const NavListItem& item = kHNavList[nav_];
got_load_start_[nav_].yes();
@ -144,7 +222,7 @@ class HistoryNavTestHandler : public TestHandler {
if (browser->IsPopup() || !frame->IsMain())
return;
const NavListItem& item = kNavList[nav_];
const NavListItem& item = kHNavList[nav_];
got_load_end_[nav_].yes();
@ -158,12 +236,37 @@ class HistoryNavTestHandler : public TestHandler {
if (item.can_go_forward == browser->CanGoForward())
got_correct_can_go_forward2_[nav_].yes();
nav_++;
RunNav(browser);
load_end_confirmation_ = true;
RunNextNavIfReady(browser);
}
virtual bool OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) OVERRIDE {
if (message->GetName().ToString() == kHistoryNavMsg) {
got_before_navigation_[nav_].yes();
// Test that the renderer side succeeded.
CefRefPtr<CefListValue> args = message->GetArgumentList();
EXPECT_TRUE(args.get());
EXPECT_EQ(nav_, args->GetInt(0));
EXPECT_TRUE(args->GetBool(1));
renderer_confirmation_ = true;
RunNextNavIfReady(browser);
return true;
}
// Message not handled.
return false;
}
int nav_;
bool load_end_confirmation_;
bool renderer_confirmation_;
TrackCallback got_before_navigation_[NAV_LIST_SIZE()];
TrackCallback got_before_resource_load_[NAV_LIST_SIZE()];
TrackCallback got_correct_target_[NAV_LIST_SIZE()];
TrackCallback got_loading_state_change_[NAV_LIST_SIZE()];
@ -186,7 +289,8 @@ TEST(NavigationTest, History) {
handler->ExecuteTest();
for (size_t i = 0; i < NAV_LIST_SIZE(); ++i) {
if (kNavList[i].action != NA_CLEAR) {
if (kHNavList[i].action != NA_CLEAR) {
ASSERT_TRUE(handler->got_before_navigation_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_before_resource_load_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_correct_target_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_load_start_[i]) << "i = " << i;
@ -197,7 +301,7 @@ TEST(NavigationTest, History) {
ASSERT_TRUE(handler->got_correct_can_go_back_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_correct_can_go_forward_[i]) << "i = " << i;
if (kNavList[i].action != NA_CLEAR) {
if (kHNavList[i].action != NA_CLEAR) {
ASSERT_TRUE(handler->got_load_end_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_correct_load_end_url_[i]) << "i = " << i;
ASSERT_TRUE(handler->got_correct_can_go_back2_[i]) << "i = " << i;
@ -209,6 +313,10 @@ TEST(NavigationTest, History) {
namespace {
const char kFNav1[] = "http://tests/nav1.html";
const char kFNav2[] = "http://tests/nav2.html";
const char kFNav3[] = "http://tests/nav3.html";
class FrameNameIdentNavTestHandler : public TestHandler {
public:
FrameNameIdentNavTestHandler() : browse_ct_(0) {}
@ -218,19 +326,19 @@ class FrameNameIdentNavTestHandler : public TestHandler {
std::stringstream ss;
// Page with named frame
ss << "<html>Nav1<iframe src=\"" << kNav2 << "\" name=\"nav2\"></html>";
AddResource(kNav1, ss.str(), "text/html");
ss << "<html>Nav1<iframe src=\"" << kFNav2 << "\" name=\"nav2\"></html>";
AddResource(kFNav1, ss.str(), "text/html");
ss.str("");
// Page with unnamed frame
ss << "<html>Nav2<iframe src=\"" << kNav3 << "\"></html>";
AddResource(kNav2, ss.str(), "text/html");
ss << "<html>Nav2<iframe src=\"" << kFNav3 << "\"></html>";
AddResource(kFNav2, ss.str(), "text/html");
ss.str("");
AddResource(kNav3, "<html>Nav3</html>", "text/html");
AddResource(kFNav3, "<html>Nav3</html>", "text/html");
// Create the browser.
CreateBrowser(kNav1);
CreateBrowser(kFNav1);
}
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
@ -240,7 +348,7 @@ class FrameNameIdentNavTestHandler : public TestHandler {
CefRefPtr<CefFrame> parent = frame->GetParent();
std::string url = request->GetURL();
if (url == kNav1) {
if (url == kFNav1) {
frame1_ident_ = frame->GetIdentifier();
if (name == "") {
frame1_name_ = name;
@ -248,7 +356,7 @@ class FrameNameIdentNavTestHandler : public TestHandler {
}
if (!parent.get())
got_frame1_ident_parent_before_.yes();
} else if (url == kNav2) {
} else if (url == kFNav2) {
frame2_ident_ = frame->GetIdentifier();
if (name == "nav2") {
frame2_name_ = name;
@ -256,7 +364,7 @@ class FrameNameIdentNavTestHandler : public TestHandler {
}
if (parent.get() && frame1_ident_ == parent->GetIdentifier())
got_frame2_ident_parent_before_.yes();
} else if (url == kNav3) {
} else if (url == kFNav3) {
frame3_ident_ = frame->GetIdentifier();
if (name == "<!--framePath //nav2/<!--frame0-->-->") {
frame3_name_ = name;
@ -275,17 +383,17 @@ class FrameNameIdentNavTestHandler : public TestHandler {
std::string url = frame->GetURL();
CefRefPtr<CefFrame> parent = frame->GetParent();
if (url == kNav1) {
if (url == kFNav1) {
if (frame1_ident_ == frame->GetIdentifier())
got_frame1_ident_.yes();
if (!parent.get())
got_frame1_ident_parent_after_.yes();
} else if (url == kNav2) {
} else if (url == kFNav2) {
if (frame2_ident_ == frame->GetIdentifier())
got_frame2_ident_.yes();
if (parent.get() && frame1_ident_ == parent->GetIdentifier())
got_frame2_ident_parent_after_.yes();
} else if (url == kNav3) {
} else if (url == kFNav3) {
if (frame3_ident_ == frame->GetIdentifier())
got_frame3_ident_.yes();
if (parent.get() && frame2_ident_ == parent->GetIdentifier())
@ -364,6 +472,11 @@ TEST(NavigationTest, FrameNameIdent) {
namespace {
const char kRNav1[] = "http://tests/nav1.html";
const char kRNav2[] = "http://tests/nav2.html";
const char kRNav3[] = "http://tests/nav3.html";
const char kRNav4[] = "http://tests/nav4.html";
bool g_got_nav1_request = false;
bool g_got_nav3_request = false;
bool g_got_nav4_request = false;
@ -378,19 +491,19 @@ class RedirectSchemeHandler : public CefResourceHandler {
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
std::string url = request->GetURL();
if (url == kNav1) {
if (url == kRNav1) {
// Redirect using HTTP 302
g_got_nav1_request = true;
status_ = 302;
location_ = kNav2;
location_ = kRNav2;
content_ = "<html><body>Redirected Nav1</body></html>";
} else if (url == kNav3) {
} else if (url == kRNav3) {
// Redirect using redirectUrl
g_got_nav3_request = true;
status_ = -1;
location_ = kNav4;
location_ = kRNav4;
content_ = "<html><body>Redirected Nav3</body></html>";
} else if (url == kNav4) {
} else if (url == kRNav4) {
g_got_nav4_request = true;
status_ = 200;
content_ = "<html><body>Nav4</body></html>";
@ -486,7 +599,7 @@ class RedirectTestHandler : public TestHandler {
virtual void RunTest() OVERRIDE {
// Create the browser.
CreateBrowser(kNav1);
CreateBrowser(kRNav1);
}
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
@ -495,11 +608,11 @@ class RedirectTestHandler : public TestHandler {
// Should be called for all but the second URL.
std::string url = request->GetURL();
if (url == kNav1) {
if (url == kRNav1) {
got_nav1_before_resource_load_.yes();
} else if (url == kNav3) {
} else if (url == kRNav3) {
got_nav3_before_resource_load_.yes();
} else if (url == kNav4) {
} else if (url == kRNav4) {
got_nav4_before_resource_load_.yes();
} else {
got_invalid_before_resource_load_.yes();
@ -514,16 +627,16 @@ class RedirectTestHandler : public TestHandler {
CefString& new_url) OVERRIDE {
// Should be called for each redirected URL.
if (old_url == kNav1 && new_url == kNav2) {
if (old_url == kRNav1 && new_url == kRNav2) {
// Called due to the nav1 redirect response.
got_nav1_redirect_.yes();
// Change the redirect to the 3rd URL.
new_url = kNav3;
} else if (old_url == kNav1 && new_url == kNav3) {
new_url = kRNav3;
} else if (old_url == kRNav1 && new_url == kRNav3) {
// Called due to the redirect change above.
got_nav2_redirect_.yes();
} else if (old_url == kNav3 && new_url == kNav4) {
} else if (old_url == kRNav3 && new_url == kRNav4) {
// Called due to the nav3 redirect response.
got_nav3_redirect_.yes();
} else {
@ -536,7 +649,7 @@ class RedirectTestHandler : public TestHandler {
// Should only be called for the final loaded URL.
std::string url = frame->GetURL();
if (url == kNav4) {
if (url == kRNav4) {
got_nav4_load_start_.yes();
} else {
got_invalid_load_start_.yes();
@ -549,7 +662,7 @@ class RedirectTestHandler : public TestHandler {
// Should only be called for the final loaded URL.
std::string url = frame->GetURL();
if (url == kNav4) {
if (url == kRNav4) {
got_nav4_load_end_.yes();
DestroyTest();
} else {
@ -603,3 +716,11 @@ TEST(NavigationTest, Redirect) {
ASSERT_TRUE(g_got_nav4_request);
ASSERT_FALSE(g_got_invalid_request);
}
// Entry point for creating navigation renderer test objects.
// Called from client_app_delegates.cc.
void CreateNavigationRendererTests(
ClientApp::RenderDelegateSet& delegates) {
delegates.insert(new HistoryNavRendererTest);
}