This source file includes following definitions.
- RenderProcessHostCount
- host_destructions_
- RenderProcessExited
- RenderProcessHostDestroyed
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- logging_string_
- RenderProcessExited
- RenderProcessHostDestroyed
- host_destroyed_
- host_destroyed
- RenderProcessExited
- RenderProcessHostDestroyed
- IN_PROC_BROWSER_TEST_F
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/common/child_process_messages.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace content {
namespace {
int RenderProcessHostCount() {
content::RenderProcessHost::iterator hosts =
content::RenderProcessHost::AllHostsIterator();
int count = 0;
while (!hosts.IsAtEnd()) {
if (hosts.GetCurrentValue()->HasConnection())
count++;
hosts.Advance();
}
return count;
}
class RenderProcessHostTest : public ContentBrowserTest,
public RenderProcessHostObserver {
public:
RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {}
protected:
virtual void RenderProcessExited(RenderProcessHost* host,
base::ProcessHandle handle,
base::TerminationStatus status,
int exit_code) OVERRIDE {
++process_exits_;
}
virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE {
++host_destructions_;
}
int process_exits_;
int host_destructions_;
};
IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
ShutdownRequestFromActiveTabIgnored) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
NavigateToURL(shell(), test_url);
RenderProcessHost* rph =
shell()->web_contents()->GetRenderViewHost()->GetProcess();
host_destructions_ = 0;
process_exits_ = 0;
rph->AddObserver(this);
ChildProcessHostMsg_ShutdownRequest msg;
rph->OnMessageReceived(msg);
NavigateToURL(CreateBrowser(), test_url);
EXPECT_EQ(0, process_exits_);
if (!host_destructions_)
rph->RemoveObserver(this);
}
IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
GuestsAreNotSuitableHosts) {
content::RenderProcessHost::SetMaxRendererProcessCount(1);
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
NavigateToURL(shell(), test_url);
RenderProcessHost* rph =
shell()->web_contents()->GetRenderViewHost()->GetProcess();
reinterpret_cast<RenderProcessHostImpl*>(rph)->SetIsGuestForTesting(true);
EXPECT_EQ(1, RenderProcessHostCount());
GURL::Replacements replace_host;
std::string host_str("localhost");
replace_host.SetHostStr(host_str);
GURL another_url = embedded_test_server()->GetURL("/simple_page.html");
another_url = another_url.ReplaceComponents(replace_host);
NavigateToURL(CreateBrowser(), another_url);
EXPECT_EQ(2, RenderProcessHostCount());
}
class ShellCloser : public RenderProcessHostObserver {
public:
ShellCloser(Shell* shell, std::string* logging_string)
: shell_(shell), logging_string_(logging_string) {}
protected:
virtual void RenderProcessExited(RenderProcessHost* host,
base::ProcessHandle handle,
base::TerminationStatus status,
int exit_code) OVERRIDE {
logging_string_->append("ShellCloser::RenderProcessExited ");
shell_->Close();
}
virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE {
logging_string_->append("ShellCloser::RenderProcessHostDestroyed ");
}
Shell* shell_;
std::string* logging_string_;
};
class ObserverLogger : public RenderProcessHostObserver {
public:
explicit ObserverLogger(std::string* logging_string)
: logging_string_(logging_string), host_destroyed_(false) {}
bool host_destroyed() { return host_destroyed_; }
protected:
virtual void RenderProcessExited(RenderProcessHost* host,
base::ProcessHandle handle,
base::TerminationStatus status,
int exit_code) OVERRIDE {
logging_string_->append("ObserverLogger::RenderProcessExited ");
}
virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE {
logging_string_->append("ObserverLogger::RenderProcessHostDestroyed ");
host_destroyed_ = true;
}
std::string* logging_string_;
bool host_destroyed_;
};
IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
AllProcessExitedCallsBeforeAnyHostDestroyedCalls) {
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
NavigateToURL(shell(), test_url);
std::string logging_string;
ShellCloser shell_closer(shell(), &logging_string);
ObserverLogger observer_logger(&logging_string);
RenderProcessHost* rph =
shell()->web_contents()->GetRenderViewHost()->GetProcess();
rph->AddObserver(&shell_closer);
rph->AddObserver(&observer_logger);
NavigateToURL(shell(), GURL(kChromeUICrashURL));
EXPECT_EQ("ShellCloser::RenderProcessExited "
"ObserverLogger::RenderProcessExited "
"ShellCloser::RenderProcessHostDestroyed "
"ObserverLogger::RenderProcessHostDestroyed ", logging_string);
if (!observer_logger.host_destroyed()) {
rph->RemoveObserver(&shell_closer);
rph->RemoveObserver(&observer_logger);
}
}
}
}