Enable plznavigate aka browser side navigation (#12535)

* enable plznavigate code path

* AtomBrowserClient::GetGeolocationApiKey returns the right default

* use IsLoadingToDifferentDocument to identify top level navigation in mainFrame

* use candidate site instance when available

* spec: don't test httpReferrer option for file origin

* update libcc ref

* affinity: only group same site in this mode

* plznavigate: don't emit did-get-response-details event for blob scheme
This commit is contained in:
Robo 2018-04-06 12:52:52 +05:30 committed by Cheng Zhao
parent f8b8dc1494
commit 65e8199a93
6 changed files with 61 additions and 59 deletions

View file

@ -128,11 +128,6 @@ bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
void AtomMainDelegate::PreSandboxStartup() { void AtomMainDelegate::PreSandboxStartup() {
brightray::MainDelegate::PreSandboxStartup(); brightray::MainDelegate::PreSandboxStartup();
// Set google API key.
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (!env->HasVar("GOOGLE_API_KEY"))
env->SetVar("GOOGLE_API_KEY", GOOGLEAPIS_API_KEY);
auto command_line = base::CommandLine::ForCurrentProcess(); auto command_line = base::CommandLine::ForCurrentProcess();
std::string process_type = command_line->GetSwitchValueASCII( std::string process_type = command_line->GetSwitchValueASCII(
::switches::kProcessType); ::switches::kProcessType);
@ -152,9 +147,6 @@ void AtomMainDelegate::PreSandboxStartup() {
} }
} }
// TODO(deepak1556): Fix and re-enable the plznavigation code path.
command_line->AppendSwitch(::switches::kDisableBrowserSideNavigation);
// Allow file:// URIs to read other file:// URIs by default. // Allow file:// URIs to read other file:// URIs by default.
command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles); command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);

View file

@ -849,6 +849,14 @@ void WebContents::DidStopLoading() {
void WebContents::DidGetResourceResponseStart( void WebContents::DidGetResourceResponseStart(
const content::ResourceRequestDetails& details) { const content::ResourceRequestDetails& details) {
// Plznavigate is using blob URLs to deliver the body
// of the main resource to the renderer process. This
// gets better in the future with kNavigationMojoResponse
// feature, which replaces this mechanism with Mojo Datapipe.
if (details.url.SchemeIsBlob() &&
(details.resource_type == content::RESOURCE_TYPE_MAIN_FRAME ||
details.resource_type == content::RESOURCE_TYPE_SUB_FRAME))
return;
Emit("did-get-response-details", Emit("did-get-response-details",
details.socket_address.IsEmpty(), details.socket_address.IsEmpty(),
details.url, details.url,
@ -1155,11 +1163,7 @@ bool WebContents::IsLoading() const {
} }
bool WebContents::IsLoadingMainFrame() const { bool WebContents::IsLoadingMainFrame() const {
// Comparing site instances works because Electron always creates a new site return web_contents()->IsLoadingToDifferentDocument();
// instance when navigating, regardless of origin. See AtomBrowserClient.
return (web_contents()->GetLastCommittedURL().is_empty() ||
web_contents()->GetSiteInstance() !=
web_contents()->GetPendingSiteInstance()) && IsLoading();
} }
bool WebContents::IsWaitingForResponse() const { bool WebContents::IsWaitingForResponse() const {

View file

@ -64,7 +64,16 @@ bool g_suppress_renderer_process_restart = false;
// Custom schemes to be registered to handle service worker. // Custom schemes to be registered to handle service worker.
std::string g_custom_service_worker_schemes = ""; std::string g_custom_service_worker_schemes = "";
void Noop(scoped_refptr<content::SiteInstance>) { bool IsSameWebSite(content::BrowserContext* browser_context,
const GURL& src_url,
const GURL& dest_url) {
return content::SiteInstance::IsSameWebSite(browser_context, src_url,
dest_url) ||
// `IsSameWebSite` doesn't seem to work for some URIs such as `file:`,
// handle these scenarios by comparing only the site as defined by
// `GetSiteForURL`.
content::SiteInstance::GetSiteForURL(browser_context, dest_url) ==
src_url;
} }
} // namespace } // namespace
@ -125,12 +134,7 @@ bool AtomBrowserClient::ShouldCreateNewSiteInstance(
// Create new a SiteInstance if navigating to a different site. // Create new a SiteInstance if navigating to a different site.
auto src_url = current_instance->GetSiteURL(); auto src_url = current_instance->GetSiteURL();
return return !IsSameWebSite(browser_context, src_url, url);
!content::SiteInstance::IsSameWebSite(browser_context, src_url, url) &&
// `IsSameWebSite` doesn't seem to work for some URIs such as `file:`,
// handle these scenarios by comparing only the site as defined by
// `GetSiteForURL`.
content::SiteInstance::GetSiteForURL(browser_context, url) != src_url;
} }
void AtomBrowserClient::AddProcessPreferences( void AtomBrowserClient::AddProcessPreferences(
@ -218,20 +222,19 @@ void AtomBrowserClient::OverrideWebkitPrefs(
void AtomBrowserClient::OverrideSiteInstanceForNavigation( void AtomBrowserClient::OverrideSiteInstanceForNavigation(
content::RenderFrameHost* rfh, content::RenderFrameHost* rfh,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
content::SiteInstance* current_instance,
const GURL& url, const GURL& url,
bool has_request_started,
content::SiteInstance* candidate_instance,
content::SiteInstance** new_instance) { content::SiteInstance** new_instance) {
if (g_suppress_renderer_process_restart) { if (g_suppress_renderer_process_restart) {
g_suppress_renderer_process_restart = false; g_suppress_renderer_process_restart = false;
return; return;
} }
content::SiteInstance* current_instance = rfh->GetSiteInstance();
if (!ShouldCreateNewSiteInstance(rfh, browser_context, current_instance, url)) if (!ShouldCreateNewSiteInstance(rfh, browser_context, current_instance, url))
return; return;
bool is_new_instance = true;
scoped_refptr<content::SiteInstance> site_instance;
// Do we have an affinity site to manage ? // Do we have an affinity site to manage ?
std::string affinity; std::string affinity;
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh); auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
@ -241,35 +244,35 @@ void AtomBrowserClient::OverrideSiteInstanceForNavigation(
!affinity.empty()) { !affinity.empty()) {
affinity = base::ToLowerASCII(affinity); affinity = base::ToLowerASCII(affinity);
auto iter = site_per_affinities.find(affinity); auto iter = site_per_affinities.find(affinity);
if (iter != site_per_affinities.end()) { GURL dest_site = content::SiteInstance::GetSiteForURL(browser_context, url);
site_instance = iter->second; if (iter != site_per_affinities.end() &&
is_new_instance = false; IsSameWebSite(browser_context, iter->second->GetSiteURL(), dest_site)) {
*new_instance = iter->second;
} else { } else {
// We must not provide the url. site_per_affinities[affinity] = candidate_instance;
// This site is "isolated" and must not be taken into account *new_instance = candidate_instance;
// when Chromium looking at a candidate for an url. // Remember the original web contents for the pending renderer process.
site_instance = content::SiteInstance::Create( auto pending_process = candidate_instance->GetProcess();
browser_context); pending_processes_[pending_process->GetID()] = web_contents;
site_per_affinities[affinity] = site_instance.get();
} }
} else { } else {
site_instance = content::SiteInstance::CreateForURL( // OverrideSiteInstanceForNavigation will be called more than once during a
browser_context, // navigation (currently twice, on request and when it's about to commit in
url); // the renderer), look at RenderFrameHostManager::GetFrameHostForNavigation.
} // In the default mode we should resuse the same site instance until the
*new_instance = site_instance.get(); // request commits otherwise it will get destroyed. Currently there is no
// unique lifetime tracker for a navigation request during site instance
if (is_new_instance) { // creation. We check for the state of the request, which should be one of
// Make sure the |site_instance| is not freed // (WAITING_FOR_RENDERER_RESPONSE, STARTED, RESPONSE_STARTED, FAILED) along
// when this function returns. // with the availability of a speculative render frame host.
// FIXME(zcbenz): We should adjust if (has_request_started) {
// OverrideSiteInstanceForNavigation's interface to solve this. *new_instance = current_instance;
BrowserThread::PostTask( return;
BrowserThread::UI, FROM_HERE, }
base::Bind(&Noop, base::RetainedRef(site_instance)));
*new_instance = candidate_instance;
// Remember the original web contents for the pending renderer process. // Remember the original web contents for the pending renderer process.
auto pending_process = site_instance->GetProcess(); auto pending_process = candidate_instance->GetProcess();
pending_processes_[pending_process->GetID()] = web_contents; pending_processes_[pending_process->GetID()] = web_contents;
} }
} }

View file

@ -56,8 +56,9 @@ class AtomBrowserClient : public brightray::BrowserClient,
void OverrideSiteInstanceForNavigation( void OverrideSiteInstanceForNavigation(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
content::SiteInstance* current_instance,
const GURL& dest_url, const GURL& dest_url,
bool has_request_started,
content::SiteInstance* candidate_instance,
content::SiteInstance** new_instance) override; content::SiteInstance** new_instance) override;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line, void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) override; int child_process_id) override;

View file

@ -285,15 +285,17 @@ describe('<webview> tag', function () {
describe('httpreferrer attribute', () => { describe('httpreferrer attribute', () => {
it('sets the referrer url', (done) => { it('sets the referrer url', (done) => {
const referrer = 'http://github.com/' const referrer = 'http://github.com/'
const listener = (e) => { const server = http.createServer((req, res) => {
assert.equal(e.message, referrer) res.end()
webview.removeEventListener('console-message', listener) server.close()
assert.equal(req.headers.referer, referrer)
done() done()
} }).listen(0, '127.0.0.1', () => {
webview.addEventListener('console-message', listener) const port = server.address().port
webview.setAttribute('httpreferrer', referrer) webview.setAttribute('httpreferrer', referrer)
webview.src = `file://${fixtures}/pages/referrer.html` webview.src = 'http://127.0.0.1:' + port
document.body.appendChild(webview) document.body.appendChild(webview)
})
}) })
}) })
@ -643,7 +645,7 @@ describe('<webview> tag', function () {
}) })
}) })
describe('setDevToolsWebCotnents() API', () => { describe('setDevToolsWebContents() API', () => {
it('sets webContents of webview as devtools', (done) => { it('sets webContents of webview as devtools', (done) => {
const webview2 = new WebView() const webview2 = new WebView()
webview2.addEventListener('did-attach', () => { webview2.addEventListener('did-attach', () => {

@ -1 +1 @@
Subproject commit 76a718409b7b859c6b708492e89aab0389fe447f Subproject commit f1fc5cfb3d17d73687574b6ee4636dd1a9dbb7b5