This source file includes following definitions.
- set_should_create_webui
- CreateWebUIControllerForURL
- GetWebUIType
- UseWebUIForURL
- UseWebUIBindingsForURL
- BeforeUnloadFired
- deleted_
- RenderViewDeleted
- deleted
- favicon_received_
- PluginCrashed
- DidUpdateFaviconURL
- plugin_crashed
- favicon_received
- SetUp
- TearDown
- set_should_create_webui
- StartCrossSiteTransition
- NavigateActiveAndCommit
- ShouldSwapProcesses
- CreateSwappedOutRenderViewHost
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- web_contents_
- RenderViewDeleted
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/frame_host/cross_site_transferring_request.h"
#include "content/browser/frame_host/navigation_controller_impl.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/frame_host/navigator.h"
#include "content/browser/frame_host/render_frame_host_manager.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/webui/web_ui_controller_factory_registry.h"
#include "content/common/frame_messages.h"
#include "content/common/view_messages.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/javascript_message_type.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_notification_tracker.h"
#include "content/test/test_content_browser_client.h"
#include "content/test/test_content_client.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
class RenderFrameHostManagerTestWebUIControllerFactory
: public WebUIControllerFactory {
public:
RenderFrameHostManagerTestWebUIControllerFactory()
: should_create_webui_(false) {
}
virtual ~RenderFrameHostManagerTestWebUIControllerFactory() {}
void set_should_create_webui(bool should_create_webui) {
should_create_webui_ = should_create_webui;
}
virtual WebUIController* CreateWebUIControllerForURL(
WebUI* web_ui, const GURL& url) const OVERRIDE {
if (!(should_create_webui_ && HasWebUIScheme(url)))
return NULL;
return new WebUIController(web_ui);
}
virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return WebUI::kNoWebUI;
}
virtual bool UseWebUIForURL(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return HasWebUIScheme(url);
}
virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return HasWebUIScheme(url);
}
private:
bool should_create_webui_;
DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManagerTestWebUIControllerFactory);
};
class BeforeUnloadFiredWebContentsDelegate : public WebContentsDelegate {
public:
BeforeUnloadFiredWebContentsDelegate() {}
virtual ~BeforeUnloadFiredWebContentsDelegate() {}
virtual void BeforeUnloadFired(WebContents* web_contents,
bool proceed,
bool* proceed_to_fire_unload) OVERRIDE {
*proceed_to_fire_unload = proceed;
}
private:
DISALLOW_COPY_AND_ASSIGN(BeforeUnloadFiredWebContentsDelegate);
};
class RenderViewHostDeletedObserver : public WebContentsObserver {
public:
RenderViewHostDeletedObserver(RenderViewHost* rvh)
: WebContentsObserver(WebContents::FromRenderViewHost(rvh)),
process_id_(rvh->GetProcess()->GetID()),
routing_id_(rvh->GetRoutingID()),
deleted_(false) {
}
virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE {
if (render_view_host->GetProcess()->GetID() == process_id_ &&
render_view_host->GetRoutingID() == routing_id_) {
deleted_ = true;
}
}
bool deleted() {
return deleted_;
}
private:
int process_id_;
int routing_id_;
bool deleted_;
DISALLOW_COPY_AND_ASSIGN(RenderViewHostDeletedObserver);
};
class PluginFaviconMessageObserver : public WebContentsObserver {
public:
PluginFaviconMessageObserver(WebContents* web_contents)
: WebContentsObserver(web_contents),
plugin_crashed_(false),
favicon_received_(false) { }
virtual void PluginCrashed(const base::FilePath& plugin_path,
base::ProcessId plugin_pid) OVERRIDE {
plugin_crashed_ = true;
}
virtual void DidUpdateFaviconURL(
int32 page_id,
const std::vector<FaviconURL>& candidates) OVERRIDE {
favicon_received_ = true;
}
bool plugin_crashed() {
return plugin_crashed_;
}
bool favicon_received() {
return favicon_received_;
}
private:
bool plugin_crashed_;
bool favicon_received_;
DISALLOW_COPY_AND_ASSIGN(PluginFaviconMessageObserver);
};
}
class RenderFrameHostManagerTest
: public RenderViewHostImplTestHarness {
public:
virtual void SetUp() OVERRIDE {
RenderViewHostImplTestHarness::SetUp();
WebUIControllerFactory::RegisterFactory(&factory_);
}
virtual void TearDown() OVERRIDE {
RenderViewHostImplTestHarness::TearDown();
WebUIControllerFactory::UnregisterFactoryForTesting(&factory_);
}
void set_should_create_webui(bool should_create_webui) {
factory_.set_should_create_webui(should_create_webui);
}
void StartCrossSiteTransition(TestWebContents* contents) {
std::vector<GURL> url_chain;
contents->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
EXPECT_TRUE(contents->cross_navigation_pending());
RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
contents->GetRenderViewHost());
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
rvh->rvh_state());
}
void NavigateActiveAndCommit(const GURL& url) {
controller().LoadURL(url, Referrer(), PAGE_TRANSITION_LINK, std::string());
TestRenderViewHost* old_rvh = test_rvh();
if (old_rvh != active_rvh()) {
old_rvh->SendBeforeUnloadACK(true);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, old_rvh->rvh_state());
}
int32 max_page_id = contents()->GetMaxPageIDForSiteInstance(
active_rvh()->GetSiteInstance());
if (old_rvh != active_rvh())
StartCrossSiteTransition(contents());
if (old_rvh != active_rvh()) {
old_rvh->OnSwappedOut(false);
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_COMMIT,
old_rvh->rvh_state());
}
RenderViewHostDeletedObserver rvh_observer(old_rvh);
active_test_rvh()->SendNavigate(max_page_id + 1, url);
if (old_rvh != active_rvh() && !rvh_observer.deleted())
EXPECT_TRUE(old_rvh->IsSwappedOut());
}
bool ShouldSwapProcesses(RenderFrameHostManager* manager,
const NavigationEntryImpl* current_entry,
const NavigationEntryImpl* new_entry) const {
return manager->ShouldSwapBrowsingInstancesForNavigation(current_entry,
new_entry);
}
TestRenderViewHost* CreateSwappedOutRenderViewHost() {
const GURL kChromeURL("chrome://foo");
const GURL kDestUrl("http://www.google.com/");
NavigateActiveAndCommit(kChromeURL);
TestRenderViewHost* ntp_rvh = static_cast<TestRenderViewHost*>(
contents()->GetRenderManagerForTesting()->current_host());
contents()->GetController().LoadURL(
kDestUrl, Referrer(), PAGE_TRANSITION_LINK, std::string());
EXPECT_TRUE(contents()->cross_navigation_pending());
static_cast<SiteInstanceImpl*>(ntp_rvh->GetSiteInstance())->
increment_active_view_count();
TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>(
contents()->GetRenderManagerForTesting()->pending_render_view_host());
CHECK(dest_rvh);
EXPECT_NE(ntp_rvh, dest_rvh);
ntp_rvh->SendBeforeUnloadACK(true);
dest_rvh->SendNavigate(101, kDestUrl);
ntp_rvh->OnSwappedOut(false);
EXPECT_TRUE(ntp_rvh->IsSwappedOut());
return ntp_rvh;
}
private:
RenderFrameHostManagerTestWebUIControllerFactory factory_;
};
TEST_F(RenderFrameHostManagerTest, NewTabPageProcesses) {
set_should_create_webui(true);
const GURL kChromeUrl("chrome://foo");
const GURL kDestUrl("http://www.google.com/");
NavigateActiveAndCommit(kChromeUrl);
EXPECT_TRUE(active_rvh()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
NavigateActiveAndCommit(kDestUrl);
scoped_ptr<TestWebContents> contents2(
TestWebContents::Create(browser_context(), NULL));
contents2->GetController().LoadURL(
kChromeUrl, Referrer(), PAGE_TRANSITION_LINK, std::string());
TestRenderViewHost* ntp_rvh2 = static_cast<TestRenderViewHost*>(
contents2->GetRenderManagerForTesting()->current_host());
EXPECT_FALSE(contents2->cross_navigation_pending());
ntp_rvh2->SendNavigate(100, kChromeUrl);
contents2->GetController().LoadURL(
kDestUrl, Referrer(), PAGE_TRANSITION_LINK, std::string());
EXPECT_TRUE(contents2->cross_navigation_pending());
TestRenderViewHost* dest_rvh2 = static_cast<TestRenderViewHost*>(
contents2->GetRenderManagerForTesting()->pending_render_view_host());
ASSERT_TRUE(dest_rvh2);
ntp_rvh2->SendBeforeUnloadACK(true);
StartCrossSiteTransition(contents2.get());
dest_rvh2->SendNavigate(101, kDestUrl);
EXPECT_NE(active_rvh()->GetProcess(), dest_rvh2->GetProcess());
EXPECT_NE(active_rvh()->GetSiteInstance(), dest_rvh2->GetSiteInstance());
EXPECT_FALSE(active_rvh()->GetSiteInstance()->IsRelatedSiteInstance(
dest_rvh2->GetSiteInstance()));
NavigateActiveAndCommit(kChromeUrl);
contents2->GetController().LoadURL(
kChromeUrl, Referrer(), PAGE_TRANSITION_LINK, std::string());
dest_rvh2->SendBeforeUnloadACK(true);
StartCrossSiteTransition(contents2.get());
static_cast<TestRenderViewHost*>(contents2->GetRenderManagerForTesting()->
pending_render_view_host())->SendNavigate(102, kChromeUrl);
EXPECT_NE(active_rvh()->GetSiteInstance(),
contents2->GetRenderViewHost()->GetSiteInstance());
EXPECT_EQ(active_rvh()->GetSiteInstance()->GetProcess(),
contents2->GetRenderViewHost()->GetSiteInstance()->GetProcess());
}
TEST_F(RenderFrameHostManagerTest, FilterMessagesWhileSwappedOut) {
const GURL kChromeURL("chrome://foo");
const GURL kDestUrl("http://www.google.com/");
std::vector<FaviconURL> icons;
NavigateActiveAndCommit(kChromeURL);
TestRenderViewHost* ntp_rvh = static_cast<TestRenderViewHost*>(
contents()->GetRenderManagerForTesting()->current_host());
const base::string16 ntp_title = base::ASCIIToUTF16("NTP Title");
{
PluginFaviconMessageObserver observer(contents());
EXPECT_TRUE(ntp_rvh->OnMessageReceived(
ViewHostMsg_UpdateFaviconURL(
rvh()->GetRoutingID(), 0, icons)));
EXPECT_TRUE(observer.favicon_received());
}
static_cast<SiteInstanceImpl*>(ntp_rvh->GetSiteInstance())->
increment_active_view_count();
NavigateActiveAndCommit(kDestUrl);
TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>(
contents()->GetRenderViewHost());
ASSERT_TRUE(dest_rvh);
EXPECT_NE(ntp_rvh, dest_rvh);
const base::string16 dest_title = base::ASCIIToUTF16("Google");
{
PluginFaviconMessageObserver observer(contents());
EXPECT_TRUE(
dest_rvh->OnMessageReceived(
ViewHostMsg_UpdateFaviconURL(rvh()->GetRoutingID(), 101, icons)));
EXPECT_TRUE(observer.favicon_received());
}
EXPECT_TRUE(ntp_rvh->IsSwappedOut());
{
PluginFaviconMessageObserver observer(contents());
EXPECT_TRUE(
ntp_rvh->OnMessageReceived(
ViewHostMsg_UpdateFaviconURL(rvh()->GetRoutingID(), 101, icons)));
EXPECT_FALSE(observer.favicon_received());
}
{
PluginFaviconMessageObserver observer(contents());
EXPECT_TRUE(ntp_rvh->main_render_frame_host()->OnMessageReceived(
FrameHostMsg_PluginCrashed(
main_rfh()->GetRoutingID(), base::FilePath(), 0)));
EXPECT_FALSE(observer.plugin_crashed());
}
MockRenderProcessHost* ntp_process_host =
static_cast<MockRenderProcessHost*>(ntp_rvh->GetProcess());
ntp_process_host->sink().ClearMessages();
const base::string16 msg = base::ASCIIToUTF16("Message");
bool result = false;
base::string16 unused;
ViewHostMsg_RunBeforeUnloadConfirm before_unload_msg(
rvh()->GetRoutingID(), kChromeURL, msg, false, &result, &unused);
before_unload_msg.EnableMessagePumping();
EXPECT_TRUE(ntp_rvh->OnMessageReceived(before_unload_msg));
EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID));
ntp_process_host->sink().ClearMessages();
ViewHostMsg_RunJavaScriptMessage js_msg(
rvh()->GetRoutingID(), msg, msg, kChromeURL,
JAVASCRIPT_MESSAGE_TYPE_CONFIRM, &result, &unused);
js_msg.EnableMessagePumping();
EXPECT_TRUE(ntp_rvh->OnMessageReceived(js_msg));
EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID));
}
TEST_F(RenderFrameHostManagerTest, WhiteListSwapCompositorFrame) {
TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
TestRenderWidgetHostView* swapped_out_rwhv =
static_cast<TestRenderWidgetHostView*>(swapped_out_rvh->GetView());
EXPECT_FALSE(swapped_out_rwhv->did_swap_compositor_frame());
MockRenderProcessHost* process_host =
static_cast<MockRenderProcessHost*>(swapped_out_rvh->GetProcess());
process_host->sink().ClearMessages();
cc::CompositorFrame frame;
ViewHostMsg_SwapCompositorFrame msg(rvh()->GetRoutingID(), 0, frame);
EXPECT_TRUE(swapped_out_rvh->OnMessageReceived(msg));
EXPECT_TRUE(swapped_out_rwhv->did_swap_compositor_frame());
}
TEST_F(RenderFrameHostManagerTest, WhiteListDidActivateAcceleratedCompositing) {
TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
MockRenderProcessHost* process_host =
static_cast<MockRenderProcessHost*>(swapped_out_rvh->GetProcess());
process_host->sink().ClearMessages();
ViewHostMsg_DidActivateAcceleratedCompositing msg(
rvh()->GetRoutingID(), true);
EXPECT_TRUE(swapped_out_rvh->OnMessageReceived(msg));
EXPECT_TRUE(swapped_out_rvh->is_accelerated_compositing_active());
}
TEST_F(RenderFrameHostManagerTest, GetRenderWidgetHostsReturnsActiveViews) {
TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
EXPECT_TRUE(swapped_out_rvh->IsSwappedOut());
scoped_ptr<RenderWidgetHostIterator> widgets(
RenderWidgetHost::GetRenderWidgetHosts());
RenderWidgetHost* widget = widgets->GetNextHost();
EXPECT_FALSE(widgets->GetNextHost());
RenderViewHost* rvh = RenderViewHost::From(widget);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT,
static_cast<RenderViewHostImpl*>(rvh)->rvh_state());
}
TEST_F(RenderFrameHostManagerTest,
GetRenderWidgetHostsWithinGetAllRenderWidgetHosts) {
TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
EXPECT_TRUE(swapped_out_rvh->IsSwappedOut());
scoped_ptr<RenderWidgetHostIterator> widgets(
RenderWidgetHost::GetRenderWidgetHosts());
while (RenderWidgetHost* w = widgets->GetNextHost()) {
bool found = false;
scoped_ptr<RenderWidgetHostIterator> all_widgets(
RenderWidgetHostImpl::GetAllRenderWidgetHosts());
while (RenderWidgetHost* widget = all_widgets->GetNextHost()) {
if (w == widget) {
found = true;
break;
}
}
EXPECT_TRUE(found);
}
}
TEST_F(RenderFrameHostManagerTest, ActiveViewCountWhileSwappingInandOut) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
SiteInstanceImpl* instance1 =
static_cast<SiteInstanceImpl*>(rvh1->GetSiteInstance());
EXPECT_EQ(instance1->active_view_count(), 1U);
scoped_ptr<TestWebContents> opener1(
TestWebContents::Create(browser_context(), instance1));
contents()->SetOpener(opener1.get());
scoped_ptr<TestWebContents> opener2(
TestWebContents::Create(browser_context(), instance1));
opener1->SetOpener(opener2.get());
EXPECT_EQ(instance1->active_view_count(), 3U);
contents()->NavigateAndCommit(kUrl2);
TestRenderViewHost* rvh2 = test_rvh();
SiteInstanceImpl* instance2 =
static_cast<SiteInstanceImpl*>(rvh2->GetSiteInstance());
EXPECT_EQ(instance2->active_view_count(), 1U);
EXPECT_EQ(instance1->active_view_count(), 2U);
contents()->NavigateAndCommit(kUrl1);
EXPECT_EQ(instance1->active_view_count(), 3U);
}
class RenderViewHostDestroyer : public WebContentsObserver {
public:
RenderViewHostDestroyer(RenderViewHost* render_view_host,
WebContents* web_contents)
: WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)),
render_view_host_(render_view_host),
web_contents_(web_contents) {}
virtual void RenderViewDeleted(
RenderViewHost* render_view_host) OVERRIDE {
if (render_view_host == render_view_host_)
delete web_contents_;
}
private:
RenderViewHost* render_view_host_;
WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(RenderViewHostDestroyer);
};
TEST_F(RenderFrameHostManagerTest,
DetectUseAfterFreeInShutdownRenderViewHostsInSiteInstance) {
const GURL kChromeURL("chrome://newtab");
const GURL kUrl1("http://www.google.com");
const GURL kUrl2("http://www.chromium.org");
NavigateActiveAndCommit(kChromeURL);
TestRenderViewHost* ntp_rvh = static_cast<TestRenderViewHost*>(
contents()->GetRenderManagerForTesting()->current_host());
TestWebContents* web_contents =
TestWebContents::Create(browser_context(), ntp_rvh->GetSiteInstance());
web_contents->NavigateAndCommit(kUrl1);
RenderViewHostDestroyer destroyer(ntp_rvh, web_contents);
contents()->NavigateAndCommit(kUrl2);
}
TEST_F(RenderFrameHostManagerTest, AlwaysSendEnableViewSourceMode) {
const GURL kChromeUrl("chrome://foo");
const GURL kUrl("view-source:http://foo");
NavigateActiveAndCommit(kChromeUrl);
controller().LoadURL(
kUrl, Referrer(), PAGE_TRANSITION_TYPED, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(FrameHostMsg_BeforeUnload_ACK(
main_test_rfh()->GetRoutingID(), true, now, now));
ASSERT_TRUE(pending_rvh());
RenderViewHost* last_rvh = pending_rvh();
int32 new_id = contents()->GetMaxPageIDForSiteInstance(
active_rvh()->GetSiteInstance()) + 1;
pending_test_rvh()->SendNavigate(new_id, kUrl);
EXPECT_EQ(controller().GetLastCommittedEntryIndex(), 1);
ASSERT_TRUE(controller().GetLastCommittedEntry());
EXPECT_TRUE(kUrl == controller().GetLastCommittedEntry()->GetURL());
EXPECT_FALSE(controller().GetPendingEntry());
process()->sink().ClearMessages();
controller().LoadURL(
kUrl, Referrer(), PAGE_TRANSITION_TYPED, std::string());
EXPECT_FALSE(pending_rvh());
EXPECT_TRUE(last_rvh == rvh());
test_rvh()->SendNavigate(new_id, kUrl);
EXPECT_EQ(controller().GetLastCommittedEntryIndex(), 1);
EXPECT_FALSE(controller().GetPendingEntry());
EXPECT_TRUE(process()->sink().GetUniqueMessageMatching(
ViewMsg_EnableViewSourceMode::ID));
}
TEST_F(RenderFrameHostManagerTest, Init) {
SiteInstanceImpl* instance =
static_cast<SiteInstanceImpl*>(SiteInstance::Create(browser_context()));
EXPECT_FALSE(instance->HasSite());
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
RenderViewHostImpl* rvh = manager->current_host();
RenderFrameHostImpl* rfh = manager->current_frame_host();
ASSERT_TRUE(rvh);
ASSERT_TRUE(rfh);
EXPECT_EQ(rvh, rfh->render_view_host());
EXPECT_EQ(instance, rvh->GetSiteInstance());
EXPECT_EQ(web_contents.get(), rvh->GetDelegate());
EXPECT_EQ(web_contents.get(), rfh->delegate());
EXPECT_TRUE(manager->GetRenderWidgetHostView());
EXPECT_FALSE(manager->pending_render_view_host());
}
TEST_F(RenderFrameHostManagerTest, Navigate) {
TestNotificationTracker notifications;
SiteInstance* instance = SiteInstance::Create(browser_context());
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
notifications.ListenFor(NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
Source<WebContents>(web_contents.get()));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
RenderFrameHostImpl* host;
const GURL kUrl1("http://www.google.com/");
NavigationEntryImpl entry1(
NULL , -1 , kUrl1, Referrer(),
base::string16() , PAGE_TRANSITION_TYPED,
false );
host = manager->Navigate(entry1);
EXPECT_TRUE(host == manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
manager->DidNavigateFrame(host);
EXPECT_TRUE(host == manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_FALSE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->SetSite(kUrl1);
const GURL kUrl2("http://www.google.com/foo");
NavigationEntryImpl entry2(
NULL , -1 , kUrl2,
Referrer(kUrl1, blink::WebReferrerPolicyDefault),
base::string16() , PAGE_TRANSITION_LINK,
true );
host = manager->Navigate(entry2);
EXPECT_TRUE(host == manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
manager->DidNavigateFrame(host);
EXPECT_TRUE(host == manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
const GURL kUrl3("http://webkit.org/");
NavigationEntryImpl entry3(
NULL , -1 , kUrl3,
Referrer(kUrl2, blink::WebReferrerPolicyDefault),
base::string16() , PAGE_TRANSITION_LINK,
false );
host = manager->Navigate(entry3);
EXPECT_TRUE(manager->pending_frame_host());
ASSERT_EQ(host, manager->pending_frame_host());
notifications.Reset();
manager->DidNavigateFrame(manager->pending_frame_host());
EXPECT_TRUE(host == manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_TRUE(
notifications.Check1AndReset(NOTIFICATION_RENDER_VIEW_HOST_CHANGED));
}
TEST_F(RenderFrameHostManagerTest, NavigateWithEarlyReNavigation) {
TestNotificationTracker notifications;
SiteInstance* instance = SiteInstance::Create(browser_context());
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
notifications.ListenFor(NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
Source<WebContents>(web_contents.get()));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
const GURL kUrl1("http://www.google.com/");
NavigationEntryImpl entry1(NULL , -1 , kUrl1,
Referrer(), base::string16() ,
PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host = manager->Navigate(entry1);
EXPECT_TRUE(host == manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_TRUE(
notifications.Check1AndReset(NOTIFICATION_RENDER_VIEW_HOST_CHANGED));
notifications.Reset();
manager->DidNavigateFrame(host);
EXPECT_TRUE(host == manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_FALSE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->SetSite(kUrl1);
const GURL kUrl2("http://www.example.com");
NavigationEntryImpl entry2(
NULL , -1 , kUrl2, Referrer(),
base::string16() , PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host2 = manager->Navigate(entry2);
int host2_process_id = host2->GetProcess()->GetID();
EXPECT_TRUE(manager->pending_frame_host());
ASSERT_EQ(host2, manager->pending_frame_host());
EXPECT_NE(host2, host);
EXPECT_TRUE(host2->render_view_host()->are_navigations_suspended());
MockRenderProcessHost* test_process_host2 =
static_cast<MockRenderProcessHost*>(host2->GetProcess());
test_process_host2->sink().ClearMessages();
host2->render_view_host()->NavigateToURL(kUrl2);
EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching(
FrameMsg_Navigate::ID));
TestRenderViewHost* test_host =
static_cast<TestRenderViewHost*>(host->render_view_host());
MockRenderProcessHost* test_process_host =
static_cast<MockRenderProcessHost*>(test_host->GetProcess());
EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
FrameMsg_BeforeUnload::ID));
test_host->SendBeforeUnloadACK(true);
manager->SwapOutOldPage();
EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
FrameMsg_SwapOut::ID));
test_host->OnSwappedOut(false);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
EXPECT_EQ(host2, manager->pending_frame_host());
EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching(
FrameMsg_Navigate::ID));
const GURL kUrl3("http://webkit.org/");
NavigationEntryImpl entry3(NULL , -1 , kUrl3,
Referrer(), base::string16() ,
PAGE_TRANSITION_TYPED,
false );
test_process_host->sink().ClearMessages();
RenderFrameHostImpl* host3 = manager->Navigate(entry3);
EXPECT_TRUE(manager->pending_frame_host());
ASSERT_EQ(host3, manager->pending_frame_host());
EXPECT_NE(host3, host);
EXPECT_NE(host3->GetProcess()->GetID(), host2_process_id);
EXPECT_TRUE(static_cast<RenderViewHostImpl*>(
host3->render_view_host())->are_navigations_suspended());
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
FrameMsg_BeforeUnload::ID));
test_host->SendBeforeUnloadACK(true);
manager->SwapOutOldPage();
EXPECT_FALSE(test_process_host->sink().GetUniqueMessageMatching(
FrameMsg_SwapOut::ID));
test_host->OnSwappedOut(false);
manager->DidNavigateFrame(host3);
EXPECT_TRUE(host3 == manager->current_frame_host());
ASSERT_TRUE(host3);
EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host3->GetSiteInstance())->
HasSite());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_TRUE(
notifications.Check1AndReset(NOTIFICATION_RENDER_VIEW_HOST_CHANGED));
}
TEST_F(RenderFrameHostManagerTest, WebUI) {
set_should_create_webui(true);
SiteInstance* instance = SiteInstance::Create(browser_context());
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
EXPECT_FALSE(manager->current_host()->IsRenderViewLive());
const GURL kUrl("chrome://foo");
NavigationEntryImpl entry(NULL , -1 , kUrl,
Referrer(), base::string16() ,
PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host = manager->Navigate(entry);
EXPECT_TRUE(host);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
EXPECT_EQ(kUrl, host->GetSiteInstance()->GetSiteURL());
EXPECT_FALSE(manager->pending_web_ui());
EXPECT_TRUE(manager->web_ui());
manager->DidNavigateFrame(host);
EXPECT_TRUE(
host->render_view_host()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
}
TEST_F(RenderFrameHostManagerTest, WebUIInNewTab) {
set_should_create_webui(true);
SiteInstance* blank_instance = SiteInstance::Create(browser_context());
scoped_ptr<TestWebContents> web_contents1(
TestWebContents::Create(browser_context(), blank_instance));
FrameTree tree1(web_contents1->GetFrameTree()->root()->navigator(),
web_contents1.get(), web_contents1.get(),
web_contents1.get(), web_contents1.get());
RenderFrameHostManager* manager1 = tree1.root()->render_manager();
manager1->Init(
browser_context(), blank_instance, MSG_ROUTING_NONE, MSG_ROUTING_NONE);
manager1->current_host()->CreateRenderView(base::string16(), -1, -1);
const GURL kUrl1("chrome://foo");
NavigationEntryImpl entry1(NULL , -1 , kUrl1,
Referrer(), base::string16() ,
PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host1 = manager1->Navigate(entry1);
EXPECT_EQ(host1, manager1->pending_frame_host());
EXPECT_NE(host1, manager1->current_frame_host());
EXPECT_TRUE(
host1->render_view_host()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
manager1->DidNavigateFrame(host1);
SiteInstance* webui_instance = host1->GetSiteInstance();
EXPECT_EQ(host1, manager1->current_frame_host());
EXPECT_TRUE(
host1->render_view_host()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
scoped_ptr<TestWebContents> web_contents2(
TestWebContents::Create(browser_context(), webui_instance));
FrameTree tree2(web_contents2->GetFrameTree()->root()->navigator(),
web_contents2.get(), web_contents2.get(),
web_contents2.get(), web_contents2.get());
RenderFrameHostManager* manager2 = tree2.root()->render_manager();
manager2->Init(
browser_context(), webui_instance, MSG_ROUTING_NONE, MSG_ROUTING_NONE);
manager2->current_host()->CreateRenderView(base::string16(), -1, -1);
const GURL kUrl2("chrome://foo/bar");
NavigationEntryImpl entry2(NULL , -1 , kUrl2,
Referrer(), base::string16() ,
PAGE_TRANSITION_LINK,
true );
RenderFrameHostImpl* host2 = manager2->Navigate(entry2);
EXPECT_EQ(host2, manager2->current_frame_host());
EXPECT_TRUE(
host2->render_view_host()->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
manager2->DidNavigateFrame(host2);
}
TEST_F(RenderFrameHostManagerTest, PageDoesBackAndReload) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.evil-site.com/");
contents()->NavigateAndCommit(kUrl1);
contents()->NavigateAndCommit(kUrl2);
RenderViewHost* evil_rvh = contents()->GetRenderViewHost();
contents()->OnGoToEntryAtOffset(-1);
EXPECT_NE(evil_rvh, contents()->GetRenderManagerForTesting()->
pending_render_view_host());
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.url = kUrl2;
params.transition = PAGE_TRANSITION_CLIENT_REDIRECT;
params.should_update_history = false;
params.gesture = NavigationGestureAuto;
params.was_within_same_page = false;
params.is_post = false;
params.page_state = PageState::CreateFromURL(kUrl2);
RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(evil_rvh);
RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(
rvh->GetProcess()->GetID(), rvh->main_frame_routing_id());
contents()->GetFrameTree()->root()->navigator()->DidNavigate(rfh, params);
EXPECT_TRUE(contents()->GetRenderManagerForTesting()->
pending_render_view_host() == NULL);
EXPECT_EQ(evil_rvh, contents()->GetRenderManagerForTesting()->current_host());
EXPECT_TRUE(contents()->GetController().GetPendingEntry() == NULL);
NavigationEntry* entry = contents()->GetController().GetVisibleEntry();
ASSERT_TRUE(entry != NULL);
EXPECT_EQ(kUrl2, entry->GetURL());
}
TEST_F(RenderFrameHostManagerTest, NavigateAfterMissingSwapOutACK) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
static_cast<SiteInstanceImpl*>(rvh1->GetSiteInstance())->
increment_active_view_count();
contents()->NavigateAndCommit(kUrl2);
TestRenderViewHost* rvh2 = test_rvh();
static_cast<SiteInstanceImpl*>(rvh2->GetSiteInstance())->
increment_active_view_count();
contents()->GetController().GoBack();
EXPECT_TRUE(rvh2->is_waiting_for_beforeunload_ack());
contents()->ProceedWithCrossSiteNavigation();
EXPECT_FALSE(rvh2->is_waiting_for_beforeunload_ack());
StartCrossSiteTransition(contents());
EXPECT_TRUE(rvh2->IsWaitingForUnloadACK());
const NavigationEntry* entry1 = contents()->GetController().GetPendingEntry();
rvh1->SendNavigate(entry1->GetPageID(), entry1->GetURL());
EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh2->rvh_state());
contents()->GetController().GoForward();
contents()->ProceedWithCrossSiteNavigation();
StartCrossSiteTransition(contents());
const NavigationEntry* entry2 = contents()->GetController().GetPendingEntry();
rvh2->SendNavigate(entry2->GetPageID(), entry2->GetURL());
EXPECT_EQ(rvh2, rvh());
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_TRUE(rvh1->IsSwappedOut());
}
TEST_F(RenderFrameHostManagerTest, CreateSwappedOutOpenerRVHs) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
const GURL kChromeUrl("chrome://foo");
contents()->NavigateAndCommit(kUrl1);
RenderFrameHostManager* manager = contents()->GetRenderManagerForTesting();
TestRenderViewHost* rvh1 = test_rvh();
scoped_ptr<TestWebContents> opener1(
TestWebContents::Create(browser_context(), rvh1->GetSiteInstance()));
RenderFrameHostManager* opener1_manager =
opener1->GetRenderManagerForTesting();
contents()->SetOpener(opener1.get());
scoped_ptr<TestWebContents> opener2(
TestWebContents::Create(browser_context(), rvh1->GetSiteInstance()));
RenderFrameHostManager* opener2_manager =
opener2->GetRenderManagerForTesting();
opener1->SetOpener(opener2.get());
contents()->NavigateAndCommit(kUrl2);
TestRenderViewHost* rvh2 = test_rvh();
EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance());
EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
rvh2->GetSiteInstance()));
EXPECT_TRUE(manager->IsRVHOnSwappedOutList(rvh1));
EXPECT_EQ(rvh1,
manager->GetSwappedOutRenderViewHost(rvh1->GetSiteInstance()));
TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>(
opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh));
EXPECT_TRUE(opener1_rvh->IsSwappedOut());
TestRenderViewHost* opener2_rvh = static_cast<TestRenderViewHost*>(
opener2_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
EXPECT_TRUE(opener2_manager->IsRVHOnSwappedOutList(opener2_rvh));
EXPECT_TRUE(opener2_rvh->IsSwappedOut());
contents()->NavigateAndCommit(kChromeUrl);
TestRenderViewHost* rvh3 = test_rvh();
EXPECT_NE(rvh1->GetSiteInstance(), rvh3->GetSiteInstance());
EXPECT_FALSE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
rvh3->GetSiteInstance()));
EXPECT_FALSE(opener1_manager->GetSwappedOutRenderViewHost(
rvh3->GetSiteInstance()));
EXPECT_FALSE(opener2_manager->GetSwappedOutRenderViewHost(
rvh3->GetSiteInstance()));
}
TEST_F(RenderFrameHostManagerTest, CleanUpSwappedOutRVHOnProcessCrash) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
scoped_ptr<TestWebContents> opener1(
TestWebContents::Create(browser_context(), rvh1->GetSiteInstance()));
RenderFrameHostManager* opener1_manager =
opener1->GetRenderManagerForTesting();
contents()->SetOpener(opener1.get());
opener1_manager->current_host()->CreateRenderView(base::string16(), -1, -1);
EXPECT_FALSE(opener1_manager->GetSwappedOutRenderViewHost(
rvh1->GetSiteInstance()));
opener1->NavigateAndCommit(kUrl2);
EXPECT_TRUE(opener1_manager->GetSwappedOutRenderViewHost(
rvh1->GetSiteInstance()));
RenderProcessHost::RendererClosedDetails details(
rvh1->GetProcess()->GetHandle(),
base::TERMINATION_STATUS_PROCESS_CRASHED,
0);
NotificationService::current()->Notify(
NOTIFICATION_RENDERER_PROCESS_CLOSED,
Source<RenderProcessHost>(rvh1->GetProcess()),
Details<RenderProcessHost::RendererClosedDetails>(&details));
rvh1->set_render_view_created(false);
EXPECT_FALSE(opener1_manager->GetSwappedOutRenderViewHost(
rvh1->GetSiteInstance()));
contents()->GetController().Reload(true);
EXPECT_EQ(opener1_manager->GetSwappedOutRenderViewHost(
rvh1->GetSiteInstance())->GetRoutingID(),
test_rvh()->opener_route_id());
}
TEST_F(RenderFrameHostManagerTest, EnableWebUIWithSwappedOutOpener) {
set_should_create_webui(true);
const GURL kSettingsUrl("chrome://chrome/settings");
const GURL kPluginUrl("chrome://plugins");
contents()->NavigateAndCommit(kSettingsUrl);
TestRenderViewHost* rvh1 = test_rvh();
EXPECT_TRUE(rvh1->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
scoped_ptr<TestWebContents> opener1(
TestWebContents::Create(browser_context(), rvh1->GetSiteInstance()));
RenderFrameHostManager* opener1_manager =
opener1->GetRenderManagerForTesting();
contents()->SetOpener(opener1.get());
contents()->NavigateAndCommit(kPluginUrl);
TestRenderViewHost* rvh2 = test_rvh();
EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance());
EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
rvh2->GetSiteInstance()));
TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>(
opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh));
EXPECT_TRUE(opener1_rvh->IsSwappedOut());
EXPECT_TRUE(rvh2->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
}
TEST_F(RenderFrameHostManagerTest, NoSwapOnGuestNavigations) {
TestNotificationTracker notifications;
GURL guest_url(std::string(kGuestScheme).append("://abc123"));
SiteInstance* instance =
SiteInstance::CreateForURL(browser_context(), guest_url);
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
RenderFrameHostImpl* host;
const GURL kUrl1("http://www.google.com/");
NavigationEntryImpl entry1(
NULL , -1 , kUrl1, Referrer(),
base::string16() , PAGE_TRANSITION_TYPED,
false );
host = manager->Navigate(entry1);
EXPECT_TRUE(host == manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_EQ(manager->current_frame_host()->GetSiteInstance(), instance);
manager->DidNavigateFrame(host);
EXPECT_EQ(host, manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
const GURL kUrl2("http://www.chromium.org");
NavigationEntryImpl entry2(
NULL , -1 , kUrl2,
Referrer(kUrl1, blink::WebReferrerPolicyDefault),
base::string16() , PAGE_TRANSITION_LINK,
true );
host = manager->Navigate(entry2);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
manager->DidNavigateFrame(host);
EXPECT_EQ(host, manager->current_frame_host());
ASSERT_TRUE(host);
EXPECT_EQ(static_cast<SiteInstanceImpl*>(host->GetSiteInstance()),
instance);
}
TEST_F(RenderFrameHostManagerTest, NavigateWithEarlyClose) {
TestNotificationTracker notifications;
SiteInstance* instance = SiteInstance::Create(browser_context());
BeforeUnloadFiredWebContentsDelegate delegate;
scoped_ptr<TestWebContents> web_contents(
TestWebContents::Create(browser_context(), instance));
web_contents->SetDelegate(&delegate);
notifications.ListenFor(NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
Source<WebContents>(web_contents.get()));
FrameTree tree(web_contents->GetFrameTree()->root()->navigator(),
web_contents.get(), web_contents.get(),
web_contents.get(), web_contents.get());
RenderFrameHostManager* manager = tree.root()->render_manager();
manager->Init(browser_context(), instance, MSG_ROUTING_NONE,
MSG_ROUTING_NONE);
const GURL kUrl1("http://www.google.com/");
NavigationEntryImpl entry1(NULL , -1 , kUrl1,
Referrer(), base::string16() ,
PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host = manager->Navigate(entry1);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_TRUE(
notifications.Check1AndReset(NOTIFICATION_RENDER_VIEW_HOST_CHANGED));
notifications.Reset();
manager->DidNavigateFrame(host);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->
HasSite());
static_cast<SiteInstanceImpl*>(host->GetSiteInstance())->SetSite(kUrl1);
const GURL kUrl2("http://www.example.com");
NavigationEntryImpl entry2(
NULL , -1 , kUrl2, Referrer(),
base::string16() , PAGE_TRANSITION_TYPED,
false );
RenderFrameHostImpl* host2 = manager->Navigate(entry2);
ASSERT_EQ(host2, manager->pending_frame_host());
EXPECT_NE(host2, host);
EXPECT_EQ(host, manager->current_frame_host());
EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
EXPECT_EQ(host2, manager->pending_frame_host());
notifications.ListenFor(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
Source<RenderWidgetHost>(host2->render_view_host()));
manager->OnBeforeUnloadACK(false, true, base::TimeTicks());
EXPECT_TRUE(
notifications.Check1AndReset(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED));
EXPECT_FALSE(manager->pending_frame_host());
EXPECT_EQ(host, manager->current_frame_host());
}
TEST_F(RenderFrameHostManagerTest,
SwapOutACKBeforeNewPageCommitsLeadsToDeletion) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
RenderViewHostDeletedObserver rvh_deleted_observer(rvh1);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(
FrameHostMsg_BeforeUnload_ACK(0, true, now, now));
EXPECT_TRUE(contents()->cross_navigation_pending());
TestRenderViewHost* rvh2 =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
std::vector<GURL> url_chain;
url_chain.push_back(GURL());
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
EXPECT_TRUE(contents()->cross_navigation_pending());
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_COMMIT, rvh1->rvh_state());
contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
EXPECT_FALSE(contents()->cross_navigation_pending());
EXPECT_EQ(rvh2, rvh());
EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_TRUE(rvh_deleted_observer.deleted());
rvh1 = NULL;
}
TEST_F(RenderFrameHostManagerTest,
SwapOutACKBeforeNewPageCommitsLeadsToSwapOut) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
RenderViewHostDeletedObserver rvh_deleted_observer(rvh1);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
static_cast<SiteInstanceImpl*>(
rvh1->GetSiteInstance())->increment_active_view_count();
controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(
FrameHostMsg_BeforeUnload_ACK(0, true, now, now));
EXPECT_TRUE(contents()->cross_navigation_pending());
TestRenderViewHost* rvh2 =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
std::vector<GURL> url_chain;
url_chain.push_back(GURL());
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
EXPECT_TRUE(contents()->cross_navigation_pending());
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_COMMIT, rvh1->rvh_state());
contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
EXPECT_FALSE(contents()->cross_navigation_pending());
EXPECT_EQ(rvh2, rvh());
EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_FALSE(rvh_deleted_observer.deleted());
EXPECT_TRUE(rvh1->IsSwappedOut());
}
TEST_F(RenderFrameHostManagerTest,
NewPageCommitsBeforeSwapOutACKLeadsToDeletion) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
RenderViewHostDeletedObserver rvh_deleted_observer(rvh1);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(
FrameHostMsg_BeforeUnload_ACK(0, true, now, now));
EXPECT_TRUE(contents()->cross_navigation_pending());
TestRenderViewHost* rvh2 =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
std::vector<GURL> url_chain;
url_chain.push_back(GURL());
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
EXPECT_TRUE(contents()->cross_navigation_pending());
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
rvh1->rvh_state());
contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
EXPECT_FALSE(contents()->cross_navigation_pending());
EXPECT_EQ(rvh2, rvh());
EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SHUTDOWN, rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_TRUE(rvh_deleted_observer.deleted());
rvh1 = NULL;
}
TEST_F(RenderFrameHostManagerTest,
NewPageCommitsBeforeSwapOutACKLeadsToSwapOut) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
RenderViewHostDeletedObserver rvh_deleted_observer(rvh1);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
static_cast<SiteInstanceImpl*>(
rvh1->GetSiteInstance())->increment_active_view_count();
controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(
FrameHostMsg_BeforeUnload_ACK(0, true, now, now));
EXPECT_TRUE(contents()->cross_navigation_pending());
TestRenderViewHost* rvh2 =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
std::vector<GURL> url_chain;
url_chain.push_back(GURL());
contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
contents()->GetRenderManagerForTesting()->pending_frame_host(),
GlobalRequestID(0, 0), scoped_ptr<CrossSiteTransferringRequest>(),
url_chain, Referrer(), PAGE_TRANSITION_TYPED, false);
EXPECT_TRUE(contents()->cross_navigation_pending());
EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
rvh1->rvh_state());
contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
EXPECT_FALSE(contents()->cross_navigation_pending());
EXPECT_EQ(rvh2, rvh());
EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_FALSE(rvh_deleted_observer.deleted());
EXPECT_TRUE(rvh1->IsSwappedOut());
}
TEST_F(RenderFrameHostManagerTest,
CommitNewNavigationBeforeSendingSwapOut) {
const GURL kUrl1("http://www.google.com/");
const GURL kUrl2("http://www.chromium.org/");
contents()->NavigateAndCommit(kUrl1);
TestRenderViewHost* rvh1 = test_rvh();
RenderViewHostDeletedObserver rvh_deleted_observer(rvh1);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
static_cast<SiteInstanceImpl*>(
rvh1->GetSiteInstance())->increment_active_view_count();
controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
base::TimeTicks now = base::TimeTicks::Now();
main_test_rfh()->OnMessageReceived(
FrameHostMsg_BeforeUnload_ACK(0, true, now, now));
EXPECT_TRUE(contents()->cross_navigation_pending());
TestRenderViewHost* rvh2 =
static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
EXPECT_FALSE(contents()->cross_navigation_pending());
EXPECT_EQ(rvh2, rvh());
EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
rvh1->OnSwappedOut(false);
EXPECT_FALSE(rvh_deleted_observer.deleted());
EXPECT_TRUE(rvh1->IsSwappedOut());
}
}