Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / frame_host / navigator_impl.cc
bloba00f9723db6f8e4da50e66601a329096b15d4f47
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/frame_host/navigator_impl.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/time/time.h"
10 #include "content/browser/frame_host/frame_tree.h"
11 #include "content/browser/frame_host/frame_tree_node.h"
12 #include "content/browser/frame_host/navigation_controller_impl.h"
13 #include "content/browser/frame_host/navigation_entry_impl.h"
14 #include "content/browser/frame_host/navigation_request.h"
15 #include "content/browser/frame_host/navigation_request_info.h"
16 #include "content/browser/frame_host/navigator_delegate.h"
17 #include "content/browser/frame_host/render_frame_host_impl.h"
18 #include "content/browser/renderer_host/render_view_host_impl.h"
19 #include "content/browser/site_instance_impl.h"
20 #include "content/browser/webui/web_ui_controller_factory_registry.h"
21 #include "content/browser/webui/web_ui_impl.h"
22 #include "content/common/frame_messages.h"
23 #include "content/common/navigation_params.h"
24 #include "content/common/view_messages.h"
25 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/browser/global_request_id.h"
28 #include "content/public/browser/invalidate_type.h"
29 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/navigation_details.h"
31 #include "content/public/browser/page_navigator.h"
32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/stream_handle.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/common/bindings_policy.h"
36 #include "content/public/common/content_client.h"
37 #include "content/public/common/content_switches.h"
38 #include "content/public/common/resource_response.h"
39 #include "content/public/common/url_constants.h"
40 #include "content/public/common/url_utils.h"
41 #include "net/base/net_errors.h"
43 namespace content {
45 namespace {
47 FrameMsg_Navigate_Type::Value GetNavigationType(
48 BrowserContext* browser_context, const NavigationEntryImpl& entry,
49 NavigationController::ReloadType reload_type) {
50 switch (reload_type) {
51 case NavigationControllerImpl::RELOAD:
52 return FrameMsg_Navigate_Type::RELOAD;
53 case NavigationControllerImpl::RELOAD_IGNORING_CACHE:
54 return FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
55 case NavigationControllerImpl::RELOAD_ORIGINAL_REQUEST_URL:
56 return FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL;
57 case NavigationControllerImpl::NO_RELOAD:
58 break; // Fall through to rest of function.
61 // |RenderViewImpl::PopulateStateFromPendingNavigationParams| differentiates
62 // between |RESTORE_WITH_POST| and |RESTORE|.
63 if (entry.restore_type() ==
64 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY) {
65 if (entry.GetHasPostData())
66 return FrameMsg_Navigate_Type::RESTORE_WITH_POST;
67 return FrameMsg_Navigate_Type::RESTORE;
70 return FrameMsg_Navigate_Type::NORMAL;
73 RenderFrameHostManager* GetRenderManager(RenderFrameHostImpl* rfh) {
74 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
75 switches::kSitePerProcess))
76 return rfh->frame_tree_node()->render_manager();
78 return rfh->frame_tree_node()->frame_tree()->root()->render_manager();
81 } // namespace
83 struct NavigatorImpl::NavigationMetricsData {
84 NavigationMetricsData(base::TimeTicks start_time,
85 GURL url,
86 NavigationEntryImpl::RestoreType restore_type)
87 : start_time_(start_time), url_(url) {
88 is_restoring_from_last_session_ =
89 (restore_type ==
90 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY ||
91 restore_type == NavigationEntryImpl::RESTORE_LAST_SESSION_CRASHED);
94 base::TimeTicks start_time_;
95 GURL url_;
96 bool is_restoring_from_last_session_;
97 base::TimeTicks url_job_start_time_;
98 base::TimeDelta before_unload_delay_;
101 NavigatorImpl::NavigatorImpl(
102 NavigationControllerImpl* navigation_controller,
103 NavigatorDelegate* delegate)
104 : controller_(navigation_controller),
105 delegate_(delegate) {
108 NavigatorImpl::~NavigatorImpl() {}
110 NavigatorDelegate* NavigatorImpl::GetDelegate() {
111 return delegate_;
114 NavigationController* NavigatorImpl::GetController() {
115 return controller_;
118 void NavigatorImpl::DidStartProvisionalLoad(
119 RenderFrameHostImpl* render_frame_host,
120 const GURL& url,
121 bool is_transition_navigation) {
122 bool is_error_page = (url.spec() == kUnreachableWebDataURL);
123 bool is_iframe_srcdoc = (url.spec() == kAboutSrcDocURL);
124 GURL validated_url(url);
125 RenderProcessHost* render_process_host = render_frame_host->GetProcess();
126 render_process_host->FilterURL(false, &validated_url);
128 bool is_main_frame = render_frame_host->frame_tree_node()->IsMainFrame();
129 NavigationEntryImpl* pending_entry = controller_->GetPendingEntry();
130 if (is_main_frame) {
131 // If there is no browser-initiated pending entry for this navigation and it
132 // is not for the error URL, create a pending entry using the current
133 // SiteInstance, and ensure the address bar updates accordingly. We don't
134 // know the referrer or extra headers at this point, but the referrer will
135 // be set properly upon commit.
136 bool has_browser_initiated_pending_entry = pending_entry &&
137 !pending_entry->is_renderer_initiated();
138 if (!has_browser_initiated_pending_entry && !is_error_page) {
139 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
140 controller_->CreateNavigationEntry(validated_url,
141 content::Referrer(),
142 ui::PAGE_TRANSITION_LINK,
143 true /* is_renderer_initiated */,
144 std::string(),
145 controller_->GetBrowserContext()));
146 entry->set_site_instance(render_frame_host->GetSiteInstance());
147 // TODO(creis): If there's a pending entry already, find a safe way to
148 // update it instead of replacing it and copying over things like this.
149 if (pending_entry) {
150 entry->set_transferred_global_request_id(
151 pending_entry->transferred_global_request_id());
152 entry->set_should_replace_entry(pending_entry->should_replace_entry());
153 entry->SetRedirectChain(pending_entry->GetRedirectChain());
155 controller_->SetPendingEntry(entry);
156 if (delegate_)
157 delegate_->NotifyChangedNavigationState(content::INVALIDATE_TYPE_URL);
160 if (delegate_ && is_transition_navigation)
161 delegate_->DidStartNavigationTransition(render_frame_host);
164 if (delegate_) {
165 // Notify the observer about the start of the provisional load.
166 delegate_->DidStartProvisionalLoad(
167 render_frame_host, validated_url, is_error_page, is_iframe_srcdoc);
172 void NavigatorImpl::DidFailProvisionalLoadWithError(
173 RenderFrameHostImpl* render_frame_host,
174 const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params) {
175 VLOG(1) << "Failed Provisional Load: " << params.url.possibly_invalid_spec()
176 << ", error_code: " << params.error_code
177 << ", error_description: " << params.error_description
178 << ", showing_repost_interstitial: " <<
179 params.showing_repost_interstitial
180 << ", frame_id: " << render_frame_host->GetRoutingID();
181 GURL validated_url(params.url);
182 RenderProcessHost* render_process_host = render_frame_host->GetProcess();
183 render_process_host->FilterURL(false, &validated_url);
185 if (net::ERR_ABORTED == params.error_code) {
186 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
187 // This means that the interstitial won't be torn down properly, which is
188 // bad. But if we have an interstitial, go back to another tab type, and
189 // then load the same interstitial again, we could end up getting the first
190 // interstitial's "failed" message (as a result of the cancel) when we're on
191 // the second one. We can't tell this apart, so we think we're tearing down
192 // the current page which will cause a crash later on.
194 // http://code.google.com/p/chromium/issues/detail?id=2855
195 // Because this will not tear down the interstitial properly, if "back" is
196 // back to another tab type, the interstitial will still be somewhat alive
197 // in the previous tab type. If you navigate somewhere that activates the
198 // tab with the interstitial again, you'll see a flash before the new load
199 // commits of the interstitial page.
200 FrameTreeNode* root =
201 render_frame_host->frame_tree_node()->frame_tree()->root();
202 if (root->render_manager()->interstitial_page() != NULL) {
203 LOG(WARNING) << "Discarding message during interstitial.";
204 return;
207 // We used to cancel the pending renderer here for cross-site downloads.
208 // However, it's not safe to do that because the download logic repeatedly
209 // looks for this WebContents based on a render ID. Instead, we just
210 // leave the pending renderer around until the next navigation event
211 // (Navigate, DidNavigate, etc), which will clean it up properly.
213 // TODO(creis): Find a way to cancel any pending RFH here.
216 // We usually clear the pending entry when it fails, so that an arbitrary URL
217 // isn't left visible above a committed page. This must be enforced when
218 // the pending entry isn't visible (e.g., renderer-initiated navigations) to
219 // prevent URL spoofs for in-page navigations that don't go through
220 // DidStartProvisionalLoadForFrame.
222 // However, we do preserve the pending entry in some cases, such as on the
223 // initial navigation of an unmodified blank tab. We also allow the delegate
224 // to say when it's safe to leave aborted URLs in the omnibox, to let the user
225 // edit the URL and try again. This may be useful in cases that the committed
226 // page cannot be attacker-controlled. In these cases, we still allow the
227 // view to clear the pending entry and typed URL if the user requests
228 // (e.g., hitting Escape with focus in the address bar).
230 // Note: don't touch the transient entry, since an interstitial may exist.
231 bool should_preserve_entry = controller_->IsUnmodifiedBlankTab() ||
232 delegate_->ShouldPreserveAbortedURLs();
233 if (controller_->GetPendingEntry() != controller_->GetVisibleEntry() ||
234 !should_preserve_entry) {
235 controller_->DiscardPendingEntry(true);
237 // Also force the UI to refresh.
238 controller_->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
241 if (delegate_)
242 delegate_->DidFailProvisionalLoadWithError(render_frame_host, params);
245 void NavigatorImpl::DidFailLoadWithError(
246 RenderFrameHostImpl* render_frame_host,
247 const GURL& url,
248 int error_code,
249 const base::string16& error_description) {
250 if (delegate_) {
251 delegate_->DidFailLoadWithError(
252 render_frame_host, url, error_code,
253 error_description);
257 bool NavigatorImpl::NavigateToEntry(
258 FrameTreeNode* frame_tree_node,
259 const NavigationEntryImpl& entry,
260 NavigationController::ReloadType reload_type) {
261 TRACE_EVENT0("browser,navigation", "NavigatorImpl::NavigateToEntry");
263 // The renderer will reject IPC messages with URLs longer than
264 // this limit, so don't attempt to navigate with a longer URL.
265 if (entry.GetURL().spec().size() > GetMaxURLChars()) {
266 LOG(WARNING) << "Refusing to load URL as it exceeds " << GetMaxURLChars()
267 << " characters.";
268 return false;
271 // This will be used to set the Navigation Timing API navigationStart
272 // parameter for browser navigations in new tabs (intents, tabs opened through
273 // "Open link in new tab"). We need to keep it above RFHM::Navigate() call to
274 // capture the time needed for the RenderFrameHost initialization.
275 base::TimeTicks navigation_start = base::TimeTicks::Now();
277 RenderFrameHostManager* manager = frame_tree_node->render_manager();
279 // PlzNavigate: the RenderFrameHosts are no longer asked to navigate.
280 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
281 switches::kEnableBrowserSideNavigation)) {
282 navigation_data_.reset(new NavigationMetricsData(
283 navigation_start, entry.GetURL(), entry.restore_type()));
284 RequestNavigation(frame_tree_node, entry, reload_type, navigation_start);
286 // Notify observers about navigation.
287 if (delegate_)
288 delegate_->DidStartNavigationToPendingEntry(entry.GetURL(), reload_type);
290 return true;
293 RenderFrameHostImpl* dest_render_frame_host = manager->Navigate(entry);
294 if (!dest_render_frame_host)
295 return false; // Unable to create the desired RenderFrameHost.
297 // Make sure no code called via RFHM::Navigate clears the pending entry.
298 CHECK_EQ(controller_->GetPendingEntry(), &entry);
300 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
301 // Double check that here.
302 CheckWebUIRendererDoesNotDisplayNormalURL(
303 dest_render_frame_host, entry.GetURL());
305 // Notify observers that we will navigate in this RenderFrame.
306 if (delegate_) {
307 delegate_->AboutToNavigateRenderFrame(frame_tree_node->current_frame_host(),
308 dest_render_frame_host);
311 // Navigate in the desired RenderFrameHost.
312 // We can skip this step in the rare case that this is a transfer navigation
313 // which began in the chosen RenderFrameHost, since the request has already
314 // been issued. In that case, simply resume the response.
315 bool is_transfer_to_same =
316 entry.transferred_global_request_id().child_id != -1 &&
317 entry.transferred_global_request_id().child_id ==
318 dest_render_frame_host->GetProcess()->GetID();
319 if (!is_transfer_to_same) {
320 navigation_data_.reset(new NavigationMetricsData(
321 navigation_start, entry.GetURL(), entry.restore_type()));
322 // Create the navigation parameters.
323 // TODO(vitalybuka): Move this before AboutToNavigateRenderFrame once
324 // http://crbug.com/408684 is fixed.
325 FrameMsg_Navigate_Type::Value navigation_type =
326 GetNavigationType(controller_->GetBrowserContext(), entry, reload_type);
327 dest_render_frame_host->Navigate(
328 entry.ConstructCommonNavigationParams(navigation_type),
329 entry.ConstructStartNavigationParams(),
330 entry.ConstructRequestNavigationParams(
331 navigation_start, controller_->GetIndexOfEntry(&entry),
332 controller_->GetLastCommittedEntryIndex(),
333 controller_->GetEntryCount()));
334 } else {
335 // No need to navigate again. Just resume the deferred request.
336 dest_render_frame_host->GetProcess()->ResumeDeferredNavigation(
337 entry.transferred_global_request_id());
340 // Make sure no code called via RFH::Navigate clears the pending entry.
341 CHECK_EQ(controller_->GetPendingEntry(), &entry);
343 if (entry.GetPageID() == -1) {
344 // HACK!! This code suppresses javascript: URLs from being added to
345 // session history, which is what we want to do for javascript: URLs that
346 // do not generate content. What we really need is a message from the
347 // renderer telling us that a new page was not created. The same message
348 // could be used for mailto: URLs and the like.
349 if (entry.GetURL().SchemeIs(url::kJavaScriptScheme))
350 return false;
353 // Notify observers about navigation.
354 if (delegate_)
355 delegate_->DidStartNavigationToPendingEntry(entry.GetURL(), reload_type);
357 return true;
360 bool NavigatorImpl::NavigateToPendingEntry(
361 FrameTreeNode* frame_tree_node,
362 NavigationController::ReloadType reload_type) {
363 return NavigateToEntry(frame_tree_node, *controller_->GetPendingEntry(),
364 reload_type);
367 void NavigatorImpl::DidNavigate(
368 RenderFrameHostImpl* render_frame_host,
369 const FrameHostMsg_DidCommitProvisionalLoad_Params& input_params) {
370 // PlzNavigate
371 // The navigation request has been committed so the browser process doesn't
372 // need to care about it anymore.
373 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
374 switches::kEnableBrowserSideNavigation)) {
375 render_frame_host->frame_tree_node()->ResetNavigationRequest(true);
378 FrameHostMsg_DidCommitProvisionalLoad_Params params(input_params);
379 FrameTree* frame_tree = render_frame_host->frame_tree_node()->frame_tree();
380 bool use_site_per_process = base::CommandLine::ForCurrentProcess()->HasSwitch(
381 switches::kSitePerProcess);
383 if (ui::PageTransitionIsMainFrame(params.transition)) {
384 if (delegate_) {
385 // When overscroll navigation gesture is enabled, a screenshot of the page
386 // in its current state is taken so that it can be used during the
387 // nav-gesture. It is necessary to take the screenshot here, before
388 // calling RenderFrameHostManager::DidNavigateMainFrame, because that can
389 // change WebContents::GetRenderViewHost to return the new host, instead
390 // of the one that may have just been swapped out.
391 if (delegate_->CanOverscrollContent()) {
392 // Don't take screenshots if we are staying on the same page. We want
393 // in-page navigations to be super fast, and taking a screenshot
394 // currently blocks GPU for a longer time than we are willing to
395 // tolerate in this use case.
396 if (!params.was_within_same_page)
397 controller_->TakeScreenshot();
400 // Run tasks that must execute just before the commit.
401 bool is_navigation_within_page = controller_->IsURLInPageNavigation(
402 params.url, params.was_within_same_page, render_frame_host);
403 delegate_->DidNavigateMainFramePreCommit(is_navigation_within_page);
406 if (!use_site_per_process)
407 frame_tree->root()->render_manager()->DidNavigateFrame(
408 render_frame_host, params.gesture == NavigationGestureUser);
411 // Save the origin of the new page. Do this before calling
412 // DidNavigateFrame(), because the origin needs to be included in the SwapOut
413 // message, which is sent inside DidNavigateFrame(). SwapOut needs the
414 // origin because it creates a RenderFrameProxy that needs this to initialize
415 // its security context. This origin will also be sent to RenderFrameProxies
416 // created via ViewMsg_New and FrameMsg_NewFrameProxy.
417 render_frame_host->frame_tree_node()->set_current_origin(params.origin);
419 // When using --site-per-process, we notify the RFHM for all navigations,
420 // not just main frame navigations.
421 if (use_site_per_process) {
422 FrameTreeNode* frame = render_frame_host->frame_tree_node();
423 frame->render_manager()->DidNavigateFrame(
424 render_frame_host, params.gesture == NavigationGestureUser);
427 // Update the site of the SiteInstance if it doesn't have one yet, unless
428 // assigning a site is not necessary for this URL. In that case, the
429 // SiteInstance can still be considered unused until a navigation to a real
430 // page.
431 SiteInstanceImpl* site_instance = render_frame_host->GetSiteInstance();
432 if (!site_instance->HasSite() &&
433 ShouldAssignSiteForURL(params.url)) {
434 site_instance->SetSite(params.url);
437 // Need to update MIME type here because it's referred to in
438 // UpdateNavigationCommands() called by RendererDidNavigate() to
439 // determine whether or not to enable the encoding menu.
440 // It's updated only for the main frame. For a subframe,
441 // RenderView::UpdateURL does not set params.contents_mime_type.
442 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
443 // TODO(jungshik): Add a test for the encoding menu to avoid
444 // regressing it again.
445 // TODO(nasko): Verify the correctness of the above comment, since some of the
446 // code doesn't exist anymore. Also, move this code in the
447 // PageTransitionIsMainFrame code block above.
448 if (ui::PageTransitionIsMainFrame(params.transition) && delegate_)
449 delegate_->SetMainFrameMimeType(params.contents_mime_type);
451 LoadCommittedDetails details;
452 bool did_navigate = controller_->RendererDidNavigate(render_frame_host,
453 params, &details);
455 // For now, keep track of each frame's URL in its FrameTreeNode. This lets
456 // us estimate our process count for implementing OOP iframes.
457 // TODO(creis): Remove this when we track which pages commit in each frame.
458 render_frame_host->frame_tree_node()->set_current_url(params.url);
460 // Send notification about committed provisional loads. This notification is
461 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
462 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
463 if (details.type != NAVIGATION_TYPE_NAV_IGNORE && delegate_) {
464 DCHECK_EQ(!render_frame_host->GetParent(),
465 did_navigate ? details.is_main_frame : false);
466 ui::PageTransition transition_type = params.transition;
467 // Whether or not a page transition was triggered by going backward or
468 // forward in the history is only stored in the navigation controller's
469 // entry list.
470 if (did_navigate &&
471 (controller_->GetLastCommittedEntry()->GetTransitionType() &
472 ui::PAGE_TRANSITION_FORWARD_BACK)) {
473 transition_type = ui::PageTransitionFromInt(
474 params.transition | ui::PAGE_TRANSITION_FORWARD_BACK);
477 delegate_->DidCommitProvisionalLoad(render_frame_host,
478 params.url,
479 transition_type);
482 if (!did_navigate)
483 return; // No navigation happened.
485 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
486 // for the appropriate notification (best) or you can add it to
487 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
488 // necessary, please).
490 // TODO(carlosk): Move this out when PlzNavigate implementation properly calls
491 // the observer methods.
492 RecordNavigationMetrics(details, params, site_instance);
494 // Run post-commit tasks.
495 if (delegate_) {
496 if (details.is_main_frame) {
497 delegate_->DidNavigateMainFramePostCommit(render_frame_host,
498 details, params);
501 delegate_->DidNavigateAnyFramePostCommit(
502 render_frame_host, details, params);
506 bool NavigatorImpl::ShouldAssignSiteForURL(const GURL& url) {
507 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
508 // still be used for a normal web site.
509 if (url == GURL(url::kAboutBlankURL))
510 return false;
512 // The embedder will then have the opportunity to determine if the URL
513 // should "use up" the SiteInstance.
514 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
517 void NavigatorImpl::RequestOpenURL(RenderFrameHostImpl* render_frame_host,
518 const GURL& url,
519 SiteInstance* source_site_instance,
520 const Referrer& referrer,
521 ui::PageTransition page_transition,
522 WindowOpenDisposition disposition,
523 bool should_replace_current_entry,
524 bool user_gesture) {
525 SiteInstance* current_site_instance =
526 GetRenderManager(render_frame_host)->current_frame_host()->
527 GetSiteInstance();
528 // If this came from a swapped out RenderFrameHost, we only allow the request
529 // if we are still in the same BrowsingInstance.
530 // TODO(creis): Move this to RenderFrameProxyHost::OpenURL.
531 if (render_frame_host->is_swapped_out() &&
532 !render_frame_host->GetSiteInstance()->IsRelatedSiteInstance(
533 current_site_instance)) {
534 return;
537 // Delegate to RequestTransferURL because this is just the generic
538 // case where |old_request_id| is empty.
539 // TODO(creis): Pass the redirect_chain into this method to support client
540 // redirects. http://crbug.com/311721.
541 std::vector<GURL> redirect_chain;
542 RequestTransferURL(render_frame_host, url, source_site_instance,
543 redirect_chain, referrer, page_transition, disposition,
544 GlobalRequestID(), should_replace_current_entry,
545 user_gesture);
548 void NavigatorImpl::RequestTransferURL(
549 RenderFrameHostImpl* render_frame_host,
550 const GURL& url,
551 SiteInstance* source_site_instance,
552 const std::vector<GURL>& redirect_chain,
553 const Referrer& referrer,
554 ui::PageTransition page_transition,
555 WindowOpenDisposition disposition,
556 const GlobalRequestID& transferred_global_request_id,
557 bool should_replace_current_entry,
558 bool user_gesture) {
559 GURL dest_url(url);
560 SiteInstance* current_site_instance =
561 GetRenderManager(render_frame_host)->current_frame_host()->
562 GetSiteInstance();
563 if (!GetContentClient()->browser()->ShouldAllowOpenURL(
564 current_site_instance, url)) {
565 dest_url = GURL(url::kAboutBlankURL);
568 int64 frame_tree_node_id = -1;
570 // Send the navigation to the current FrameTreeNode if it's destined for a
571 // subframe in the current tab. We'll assume it's for the main frame
572 // (possibly of a new or different WebContents) otherwise.
573 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
574 switches::kSitePerProcess) &&
575 disposition == CURRENT_TAB &&
576 render_frame_host->GetParent()) {
577 frame_tree_node_id =
578 render_frame_host->frame_tree_node()->frame_tree_node_id();
581 OpenURLParams params(
582 dest_url, referrer, frame_tree_node_id, disposition, page_transition,
583 true /* is_renderer_initiated */);
584 params.source_site_instance = source_site_instance;
585 if (redirect_chain.size() > 0)
586 params.redirect_chain = redirect_chain;
587 params.transferred_global_request_id = transferred_global_request_id;
588 params.should_replace_current_entry = should_replace_current_entry;
589 params.user_gesture = user_gesture;
591 if (GetRenderManager(render_frame_host)->web_ui()) {
592 // Web UI pages sometimes want to override the page transition type for
593 // link clicks (e.g., so the new tab page can specify AUTO_BOOKMARK for
594 // automatically generated suggestions). We don't override other types
595 // like TYPED because they have different implications (e.g., autocomplete).
596 if (ui::PageTransitionCoreTypeIs(
597 params.transition, ui::PAGE_TRANSITION_LINK))
598 params.transition =
599 GetRenderManager(render_frame_host)->web_ui()->
600 GetLinkTransitionType();
602 // Note also that we hide the referrer for Web UI pages. We don't really
603 // want web sites to see a referrer of "chrome://blah" (and some
604 // chrome: URLs might have search terms or other stuff we don't want to
605 // send to the site), so we send no referrer.
606 params.referrer = Referrer();
608 // Navigations in Web UI pages count as browser-initiated navigations.
609 params.is_renderer_initiated = false;
612 if (delegate_)
613 delegate_->RequestOpenURL(render_frame_host, params);
616 // PlzNavigate
617 void NavigatorImpl::OnBeforeUnloadACK(FrameTreeNode* frame_tree_node,
618 bool proceed) {
619 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
620 switches::kEnableBrowserSideNavigation));
621 DCHECK(frame_tree_node);
623 NavigationRequest* navigation_request = frame_tree_node->navigation_request();
625 // The NavigationRequest may have been canceled while the renderer was
626 // executing the BeforeUnload event.
627 if (!navigation_request)
628 return;
630 DCHECK_EQ(NavigationRequest::WAITING_FOR_RENDERER_RESPONSE,
631 navigation_request->state());
633 if (proceed)
634 BeginNavigation(frame_tree_node);
635 else
636 CancelNavigation(frame_tree_node);
639 // PlzNavigate
640 void NavigatorImpl::OnBeginNavigation(
641 FrameTreeNode* frame_tree_node,
642 const CommonNavigationParams& common_params,
643 const BeginNavigationParams& begin_params,
644 scoped_refptr<ResourceRequestBody> body) {
645 // This is a renderer-initiated navigation.
646 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
647 switches::kEnableBrowserSideNavigation));
648 DCHECK(frame_tree_node);
650 NavigationRequest* ongoing_navigation_request =
651 frame_tree_node->navigation_request();
653 // The renderer-initiated navigation request is ignored iff a) there is an
654 // ongoing request b) which is browser or user-initiated and c) the renderer
655 // request is not user-initiated.
656 if (ongoing_navigation_request &&
657 (ongoing_navigation_request->browser_initiated() ||
658 ongoing_navigation_request->begin_params().has_user_gesture) &&
659 !begin_params.has_user_gesture) {
660 return;
663 // In all other cases the current navigation, if any, is canceled and a new
664 // NavigationRequest is created for the node.
665 scoped_ptr<NavigationRequest> navigation_request =
666 NavigationRequest::CreateRendererInitiated(
667 frame_tree_node, common_params, begin_params, body,
668 controller_->GetLastCommittedEntryIndex(),
669 controller_->GetEntryCount());
670 frame_tree_node->SetNavigationRequest(navigation_request.Pass());
672 if (frame_tree_node->IsMainFrame())
673 navigation_data_.reset();
675 BeginNavigation(frame_tree_node);
678 // PlzNavigate
679 void NavigatorImpl::CommitNavigation(FrameTreeNode* frame_tree_node,
680 ResourceResponse* response,
681 scoped_ptr<StreamHandle> body) {
682 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
683 switches::kEnableBrowserSideNavigation));
685 NavigationRequest* navigation_request = frame_tree_node->navigation_request();
686 DCHECK(navigation_request);
687 DCHECK(response ||
688 !NavigationRequest::ShouldMakeNetworkRequest(
689 navigation_request->common_params().url));
691 // HTTP 204 (No Content) and HTTP 205 (Reset Content) responses should not
692 // commit; they leave the frame showing the previous page.
693 if (response && response->head.headers.get() &&
694 (response->head.headers->response_code() == 204 ||
695 response->head.headers->response_code() == 205)) {
696 CancelNavigation(frame_tree_node);
697 return;
700 // Select an appropriate renderer to commit the navigation.
701 RenderFrameHostImpl* render_frame_host =
702 frame_tree_node->render_manager()->GetFrameHostForNavigation(
703 *navigation_request);
705 // The renderer can exit view source mode when any error or cancellation
706 // happen. When reusing the same renderer, overwrite to recover the mode.
707 if (navigation_request->is_view_source() &&
708 render_frame_host ==
709 frame_tree_node->render_manager()->current_frame_host()) {
710 DCHECK(!render_frame_host->GetParent());
711 render_frame_host->render_view_host()->Send(
712 new ViewMsg_EnableViewSourceMode(
713 render_frame_host->render_view_host()->GetRoutingID()));
716 CheckWebUIRendererDoesNotDisplayNormalURL(
717 render_frame_host, navigation_request->common_params().url);
719 render_frame_host->CommitNavigation(response, body.Pass(),
720 navigation_request->common_params(),
721 navigation_request->request_params());
724 // PlzNavigate
725 void NavigatorImpl::FailedNavigation(FrameTreeNode* frame_tree_node,
726 bool has_stale_copy_in_cache,
727 int error_code) {
728 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
729 switches::kEnableBrowserSideNavigation));
731 NavigationRequest* navigation_request = frame_tree_node->navigation_request();
732 DCHECK(navigation_request);
734 // If the request was canceled by the user do not show an error page.
735 if (error_code == net::ERR_ABORTED) {
736 frame_tree_node->ResetNavigationRequest(false);
737 return;
740 // Select an appropriate renderer to show the error page.
741 RenderFrameHostImpl* render_frame_host =
742 frame_tree_node->render_manager()->GetFrameHostForNavigation(
743 *navigation_request);
744 CheckWebUIRendererDoesNotDisplayNormalURL(
745 render_frame_host, navigation_request->common_params().url);
747 render_frame_host->FailedNavigation(navigation_request->common_params(),
748 navigation_request->request_params(),
749 has_stale_copy_in_cache, error_code);
752 // PlzNavigate
753 void NavigatorImpl::CancelNavigation(FrameTreeNode* frame_tree_node) {
754 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
755 switches::kEnableBrowserSideNavigation));
756 frame_tree_node->ResetNavigationRequest(false);
757 if (frame_tree_node->IsMainFrame())
758 navigation_data_.reset();
761 bool NavigatorImpl::IsWaitingForBeforeUnloadACK(
762 FrameTreeNode* frame_tree_node) {
763 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
764 switches::kEnableBrowserSideNavigation));
765 NavigationRequest* request = frame_tree_node->navigation_request();
766 if (!request)
767 return false;
768 return request->state() == NavigationRequest::WAITING_FOR_RENDERER_RESPONSE;
771 void NavigatorImpl::LogResourceRequestTime(
772 base::TimeTicks timestamp, const GURL& url) {
773 if (navigation_data_ && navigation_data_->url_ == url) {
774 navigation_data_->url_job_start_time_ = timestamp;
775 UMA_HISTOGRAM_TIMES(
776 "Navigation.TimeToURLJobStart",
777 navigation_data_->url_job_start_time_ - navigation_data_->start_time_);
781 void NavigatorImpl::LogBeforeUnloadTime(
782 const base::TimeTicks& renderer_before_unload_start_time,
783 const base::TimeTicks& renderer_before_unload_end_time) {
784 // Only stores the beforeunload delay if we're tracking a browser initiated
785 // navigation and it happened later than the navigation request.
786 if (navigation_data_ &&
787 renderer_before_unload_start_time > navigation_data_->start_time_) {
788 navigation_data_->before_unload_delay_ =
789 renderer_before_unload_end_time - renderer_before_unload_start_time;
793 void NavigatorImpl::CheckWebUIRendererDoesNotDisplayNormalURL(
794 RenderFrameHostImpl* render_frame_host,
795 const GURL& url) {
796 int enabled_bindings =
797 render_frame_host->render_view_host()->GetEnabledBindings();
798 bool is_allowed_in_web_ui_renderer =
799 WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
800 controller_->GetBrowserContext(), url);
801 if ((enabled_bindings & BINDINGS_POLICY_WEB_UI) &&
802 !is_allowed_in_web_ui_renderer) {
803 // Log the URL to help us diagnose any future failures of this CHECK.
804 GetContentClient()->SetActiveURL(url);
805 CHECK(0);
809 // PlzNavigate
810 void NavigatorImpl::RequestNavigation(
811 FrameTreeNode* frame_tree_node,
812 const NavigationEntryImpl& entry,
813 NavigationController::ReloadType reload_type,
814 base::TimeTicks navigation_start) {
815 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
816 switches::kEnableBrowserSideNavigation));
817 DCHECK(frame_tree_node);
818 FrameMsg_Navigate_Type::Value navigation_type =
819 GetNavigationType(controller_->GetBrowserContext(), entry, reload_type);
820 scoped_ptr<NavigationRequest> navigation_request =
821 NavigationRequest::CreateBrowserInitiated(frame_tree_node, entry,
822 navigation_type,
823 navigation_start, controller_);
824 frame_tree_node->SetNavigationRequest(navigation_request.Pass());
826 // Have the current renderer execute its beforeUnload event if needed. If it
827 // is not needed (eg. the renderer is not live), BeginNavigation should get
828 // called.
829 frame_tree_node->navigation_request()->SetWaitingForRendererResponse();
830 frame_tree_node->current_frame_host()->DispatchBeforeUnload(true);
833 void NavigatorImpl::BeginNavigation(FrameTreeNode* frame_tree_node) {
834 NavigationRequest* navigation_request = frame_tree_node->navigation_request();
836 // A browser-initiated navigation could have been cancelled while it was
837 // waiting for the BeforeUnload event to execute.
838 if (!navigation_request)
839 return;
841 // Start the request.
842 if (navigation_request->BeginNavigation()) {
843 // If the request was sent to the IO thread, notify the
844 // RenderFrameHostManager so it can speculatively create a RenderFrameHost
845 // (and potentially a new renderer process) in parallel.
846 frame_tree_node->render_manager()->BeginNavigation(*navigation_request);
850 void NavigatorImpl::RecordNavigationMetrics(
851 const LoadCommittedDetails& details,
852 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
853 SiteInstance* site_instance) {
854 DCHECK(site_instance->HasProcess());
856 if (!details.is_in_page)
857 RecordAction(base::UserMetricsAction("FrameLoad"));
859 if (!details.is_main_frame || !navigation_data_ ||
860 navigation_data_->url_job_start_time_.is_null() ||
861 navigation_data_->url_ != params.original_request_url) {
862 return;
865 base::TimeDelta time_to_commit =
866 base::TimeTicks::Now() - navigation_data_->start_time_;
867 UMA_HISTOGRAM_TIMES("Navigation.TimeToCommit", time_to_commit);
869 time_to_commit -= navigation_data_->before_unload_delay_;
870 base::TimeDelta time_to_network = navigation_data_->url_job_start_time_ -
871 navigation_data_->start_time_ -
872 navigation_data_->before_unload_delay_;
873 if (navigation_data_->is_restoring_from_last_session_) {
874 UMA_HISTOGRAM_TIMES(
875 "Navigation.TimeToCommit_SessionRestored_BeforeUnloadDiscounted",
876 time_to_commit);
877 UMA_HISTOGRAM_TIMES(
878 "Navigation.TimeToURLJobStart_SessionRestored_BeforeUnloadDiscounted",
879 time_to_network);
880 navigation_data_.reset();
881 return;
883 bool navigation_created_new_renderer_process =
884 site_instance->GetProcess()->GetInitTimeForNavigationMetrics() >
885 navigation_data_->start_time_;
886 if (navigation_created_new_renderer_process) {
887 UMA_HISTOGRAM_TIMES(
888 "Navigation.TimeToCommit_NewRenderer_BeforeUnloadDiscounted",
889 time_to_commit);
890 UMA_HISTOGRAM_TIMES(
891 "Navigation.TimeToURLJobStart_NewRenderer_BeforeUnloadDiscounted",
892 time_to_network);
893 } else {
894 UMA_HISTOGRAM_TIMES(
895 "Navigation.TimeToCommit_ExistingRenderer_BeforeUnloadDiscounted",
896 time_to_commit);
897 UMA_HISTOGRAM_TIMES(
898 "Navigation.TimeToURLJobStart_ExistingRenderer_BeforeUnloadDiscounted",
899 time_to_network);
901 navigation_data_.reset();
904 } // namespace content