cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / frame_host / navigator_impl.cc
blobc5b67b40ad6c2b7f8f9df41354ecff7b2155831a
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/navigation_params.h"
23 #include "content/common/view_messages.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/browser/content_browser_client.h"
26 #include "content/public/browser/global_request_id.h"
27 #include "content/public/browser/invalidate_type.h"
28 #include "content/public/browser/navigation_controller.h"
29 #include "content/public/browser/navigation_details.h"
30 #include "content/public/browser/page_navigator.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/stream_handle.h"
33 #include "content/public/common/bindings_policy.h"
34 #include "content/public/common/content_client.h"
35 #include "content/public/common/content_switches.h"
36 #include "content/public/common/resource_response.h"
37 #include "content/public/common/url_constants.h"
38 #include "content/public/common/url_utils.h"
39 #include "net/base/load_flags.h"
40 #include "net/http/http_response_headers.h"
42 namespace content {
44 namespace {
46 FrameMsg_Navigate_Type::Value GetNavigationType(
47 BrowserContext* browser_context, const NavigationEntryImpl& entry,
48 NavigationController::ReloadType reload_type) {
49 switch (reload_type) {
50 case NavigationControllerImpl::RELOAD:
51 return FrameMsg_Navigate_Type::RELOAD;
52 case NavigationControllerImpl::RELOAD_IGNORING_CACHE:
53 return FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE;
54 case NavigationControllerImpl::RELOAD_ORIGINAL_REQUEST_URL:
55 return FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL;
56 case NavigationControllerImpl::NO_RELOAD:
57 break; // Fall through to rest of function.
60 // |RenderViewImpl::PopulateStateFromPendingNavigationParams| differentiates
61 // between |RESTORE_WITH_POST| and |RESTORE|.
62 if (entry.restore_type() ==
63 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY) {
64 if (entry.GetHasPostData())
65 return FrameMsg_Navigate_Type::RESTORE_WITH_POST;
66 return FrameMsg_Navigate_Type::RESTORE;
69 return FrameMsg_Navigate_Type::NORMAL;
72 // PlzNavigate
73 // Returns the net load flags to use based on the navigation type.
74 // TODO(clamy): unify the code with what is happening on the renderer side.
75 int LoadFlagFromNavigationType(FrameMsg_Navigate_Type::Value navigation_type) {
76 int load_flags = net::LOAD_NORMAL;
77 switch (navigation_type) {
78 case FrameMsg_Navigate_Type::RELOAD:
79 case FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL:
80 load_flags |= net::LOAD_VALIDATE_CACHE;
81 break;
82 case FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE:
83 load_flags |= net::LOAD_BYPASS_CACHE;
84 break;
85 case FrameMsg_Navigate_Type::RESTORE:
86 load_flags |= net::LOAD_PREFERRING_CACHE;
87 break;
88 case FrameMsg_Navigate_Type::RESTORE_WITH_POST:
89 load_flags |= net::LOAD_ONLY_FROM_CACHE;
90 break;
91 case FrameMsg_Navigate_Type::NORMAL:
92 default:
93 break;
95 return load_flags;
98 // PlzNavigate
99 // Generates a default FrameHostMsg_BeginNavigation_Params to be used when there
100 // is no live renderer.
101 FrameHostMsg_BeginNavigation_Params MakeDefaultBeginNavigation(
102 const RequestNavigationParams& request_params,
103 FrameMsg_Navigate_Type::Value navigation_type) {
104 FrameHostMsg_BeginNavigation_Params begin_navigation_params;
105 begin_navigation_params.method = request_params.is_post ? "POST" : "GET";
106 begin_navigation_params.load_flags =
107 LoadFlagFromNavigationType(navigation_type);
109 // TODO(clamy): Post data from the browser should be put in the request body.
110 // Headers should be filled in as well.
112 begin_navigation_params.has_user_gesture = false;
113 return begin_navigation_params;
116 RenderFrameHostManager* GetRenderManager(RenderFrameHostImpl* rfh) {
117 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
118 switches::kSitePerProcess))
119 return rfh->frame_tree_node()->render_manager();
121 return rfh->frame_tree_node()->frame_tree()->root()->render_manager();
124 void MakeNavigateParams(const NavigationEntryImpl& entry,
125 NavigationControllerImpl* controller,
126 NavigationController::ReloadType reload_type,
127 base::TimeTicks navigation_start,
128 FrameMsg_Navigate_Params* params) {
129 params->common_params = CommonNavigationParams(
130 entry.GetURL(), entry.GetReferrer(), entry.GetTransitionType(),
131 GetNavigationType(controller->GetBrowserContext(), entry, reload_type),
132 !entry.IsViewSourceMode());
133 params->request_params = RequestNavigationParams(
134 entry.GetHasPostData(),
135 entry.extra_headers(),
136 entry.GetBrowserInitiatedPostData());
137 params->commit_params = CommitNavigationParams(
138 entry.GetPageState(), entry.GetIsOverridingUserAgent(), navigation_start);
139 if (!entry.GetBaseURLForDataURL().is_empty()) {
140 params->base_url_for_data_url = entry.GetBaseURLForDataURL();
141 params->history_url_for_data_url = entry.GetVirtualURL();
143 params->should_replace_current_entry = entry.should_replace_entry();
144 // This is used by the old performance infrastructure to set up DocumentState
145 // associated with the RenderView.
146 // TODO(ppi): make it go away.
147 params->request_time = base::Time::Now();
148 params->transferred_request_child_id =
149 entry.transferred_global_request_id().child_id;
150 params->transferred_request_request_id =
151 entry.transferred_global_request_id().request_id;
153 params->page_id = entry.GetPageID();
154 params->should_clear_history_list = entry.should_clear_history_list();
155 if (entry.should_clear_history_list()) {
156 // Set the history list related parameters to the same values a
157 // NavigationController would return before its first navigation. This will
158 // fully clear the RenderView's view of the session history.
159 params->pending_history_list_offset = -1;
160 params->current_history_list_offset = -1;
161 params->current_history_list_length = 0;
162 } else {
163 params->pending_history_list_offset = controller->GetIndexOfEntry(&entry);
164 params->current_history_list_offset =
165 controller->GetLastCommittedEntryIndex();
166 params->current_history_list_length = controller->GetEntryCount();
168 // Set the redirect chain to the navigation's redirects, unless we are
169 // returning to a completed navigation (whose previous redirects don't apply).
170 if (ui::PageTransitionIsNewNavigation(params->common_params.transition)) {
171 params->redirects = entry.GetRedirectChain();
172 } else {
173 params->redirects.clear();
176 params->can_load_local_resources = entry.GetCanLoadLocalResources();
177 params->frame_to_navigate = entry.GetFrameToNavigate();
180 } // namespace
182 struct NavigatorImpl::NavigationMetricsData {
183 NavigationMetricsData(base::TimeTicks start_time,
184 GURL url,
185 NavigationEntryImpl::RestoreType restore_type)
186 : start_time_(start_time), url_(url) {
187 is_restoring_from_last_session_ =
188 (restore_type ==
189 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY ||
190 restore_type == NavigationEntryImpl::RESTORE_LAST_SESSION_CRASHED);
193 base::TimeTicks start_time_;
194 GURL url_;
195 bool is_restoring_from_last_session_;
196 base::TimeTicks url_job_start_time_;
197 base::TimeDelta before_unload_delay_;
200 NavigatorImpl::NavigatorImpl(
201 NavigationControllerImpl* navigation_controller,
202 NavigatorDelegate* delegate)
203 : controller_(navigation_controller),
204 delegate_(delegate) {
207 NavigatorImpl::~NavigatorImpl() {}
209 NavigationController* NavigatorImpl::GetController() {
210 return controller_;
213 void NavigatorImpl::DidStartProvisionalLoad(
214 RenderFrameHostImpl* render_frame_host,
215 const GURL& url,
216 bool is_transition_navigation) {
217 bool is_error_page = (url.spec() == kUnreachableWebDataURL);
218 bool is_iframe_srcdoc = (url.spec() == kAboutSrcDocURL);
219 GURL validated_url(url);
220 RenderProcessHost* render_process_host = render_frame_host->GetProcess();
221 render_process_host->FilterURL(false, &validated_url);
223 bool is_main_frame = render_frame_host->frame_tree_node()->IsMainFrame();
224 NavigationEntryImpl* pending_entry =
225 NavigationEntryImpl::FromNavigationEntry(controller_->GetPendingEntry());
226 if (is_main_frame) {
227 // If there is no browser-initiated pending entry for this navigation and it
228 // is not for the error URL, create a pending entry using the current
229 // SiteInstance, and ensure the address bar updates accordingly. We don't
230 // know the referrer or extra headers at this point, but the referrer will
231 // be set properly upon commit.
232 bool has_browser_initiated_pending_entry = pending_entry &&
233 !pending_entry->is_renderer_initiated();
234 if (!has_browser_initiated_pending_entry && !is_error_page) {
235 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
236 controller_->CreateNavigationEntry(validated_url,
237 content::Referrer(),
238 ui::PAGE_TRANSITION_LINK,
239 true /* is_renderer_initiated */,
240 std::string(),
241 controller_->GetBrowserContext()));
242 entry->set_site_instance(
243 static_cast<SiteInstanceImpl*>(
244 render_frame_host->render_view_host()->GetSiteInstance()));
245 // TODO(creis): If there's a pending entry already, find a safe way to
246 // update it instead of replacing it and copying over things like this.
247 if (pending_entry) {
248 entry->set_transferred_global_request_id(
249 pending_entry->transferred_global_request_id());
250 entry->set_should_replace_entry(pending_entry->should_replace_entry());
251 entry->SetRedirectChain(pending_entry->GetRedirectChain());
253 controller_->SetPendingEntry(entry);
254 if (delegate_)
255 delegate_->NotifyChangedNavigationState(content::INVALIDATE_TYPE_URL);
258 if (delegate_ && is_transition_navigation)
259 delegate_->DidStartNavigationTransition(render_frame_host);
262 if (delegate_) {
263 // Notify the observer about the start of the provisional load.
264 delegate_->DidStartProvisionalLoad(
265 render_frame_host, validated_url, is_error_page, is_iframe_srcdoc);
270 void NavigatorImpl::DidFailProvisionalLoadWithError(
271 RenderFrameHostImpl* render_frame_host,
272 const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params) {
273 VLOG(1) << "Failed Provisional Load: " << params.url.possibly_invalid_spec()
274 << ", error_code: " << params.error_code
275 << ", error_description: " << params.error_description
276 << ", showing_repost_interstitial: " <<
277 params.showing_repost_interstitial
278 << ", frame_id: " << render_frame_host->GetRoutingID();
279 GURL validated_url(params.url);
280 RenderProcessHost* render_process_host = render_frame_host->GetProcess();
281 render_process_host->FilterURL(false, &validated_url);
283 if (net::ERR_ABORTED == params.error_code) {
284 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
285 // This means that the interstitial won't be torn down properly, which is
286 // bad. But if we have an interstitial, go back to another tab type, and
287 // then load the same interstitial again, we could end up getting the first
288 // interstitial's "failed" message (as a result of the cancel) when we're on
289 // the second one. We can't tell this apart, so we think we're tearing down
290 // the current page which will cause a crash later on.
292 // http://code.google.com/p/chromium/issues/detail?id=2855
293 // Because this will not tear down the interstitial properly, if "back" is
294 // back to another tab type, the interstitial will still be somewhat alive
295 // in the previous tab type. If you navigate somewhere that activates the
296 // tab with the interstitial again, you'll see a flash before the new load
297 // commits of the interstitial page.
298 FrameTreeNode* root =
299 render_frame_host->frame_tree_node()->frame_tree()->root();
300 if (root->render_manager()->interstitial_page() != NULL) {
301 LOG(WARNING) << "Discarding message during interstitial.";
302 return;
305 // We used to cancel the pending renderer here for cross-site downloads.
306 // However, it's not safe to do that because the download logic repeatedly
307 // looks for this WebContents based on a render ID. Instead, we just
308 // leave the pending renderer around until the next navigation event
309 // (Navigate, DidNavigate, etc), which will clean it up properly.
311 // TODO(creis): Find a way to cancel any pending RFH here.
314 // We usually clear the pending entry when it fails, so that an arbitrary URL
315 // isn't left visible above a committed page. This must be enforced when
316 // the pending entry isn't visible (e.g., renderer-initiated navigations) to
317 // prevent URL spoofs for in-page navigations that don't go through
318 // DidStartProvisionalLoadForFrame.
320 // However, we do preserve the pending entry in some cases, such as on the
321 // initial navigation of an unmodified blank tab. We also allow the delegate
322 // to say when it's safe to leave aborted URLs in the omnibox, to let the user
323 // edit the URL and try again. This may be useful in cases that the committed
324 // page cannot be attacker-controlled. In these cases, we still allow the
325 // view to clear the pending entry and typed URL if the user requests
326 // (e.g., hitting Escape with focus in the address bar).
328 // Note: don't touch the transient entry, since an interstitial may exist.
329 bool should_preserve_entry = controller_->IsUnmodifiedBlankTab() ||
330 delegate_->ShouldPreserveAbortedURLs();
331 if (controller_->GetPendingEntry() != controller_->GetVisibleEntry() ||
332 !should_preserve_entry) {
333 controller_->DiscardPendingEntry();
335 // Also force the UI to refresh.
336 controller_->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
339 if (delegate_)
340 delegate_->DidFailProvisionalLoadWithError(render_frame_host, params);
343 void NavigatorImpl::DidFailLoadWithError(
344 RenderFrameHostImpl* render_frame_host,
345 const GURL& url,
346 int error_code,
347 const base::string16& error_description) {
348 if (delegate_) {
349 delegate_->DidFailLoadWithError(
350 render_frame_host, url, error_code,
351 error_description);
355 bool NavigatorImpl::NavigateToEntry(
356 RenderFrameHostImpl* render_frame_host,
357 const NavigationEntryImpl& entry,
358 NavigationController::ReloadType reload_type) {
359 TRACE_EVENT0("browser,navigation", "NavigatorImpl::NavigateToEntry");
361 // The renderer will reject IPC messages with URLs longer than
362 // this limit, so don't attempt to navigate with a longer URL.
363 if (entry.GetURL().spec().size() > GetMaxURLChars()) {
364 LOG(WARNING) << "Refusing to load URL as it exceeds " << GetMaxURLChars()
365 << " characters.";
366 return false;
369 // This will be used to set the Navigation Timing API navigationStart
370 // parameter for browser navigations in new tabs (intents, tabs opened through
371 // "Open link in new tab"). We need to keep it above RFHM::Navigate() call to
372 // capture the time needed for the RenderFrameHost initialization.
373 base::TimeTicks navigation_start = base::TimeTicks::Now();
375 RenderFrameHostManager* manager =
376 render_frame_host->frame_tree_node()->render_manager();
378 // PlzNavigate: the RenderFrameHosts are no longer asked to navigate.
379 if (CommandLine::ForCurrentProcess()->HasSwitch(
380 switches::kEnableBrowserSideNavigation)) {
381 navigation_data_.reset(new NavigationMetricsData(
382 navigation_start, entry.GetURL(), entry.restore_type()));
383 return RequestNavigation(render_frame_host->frame_tree_node(),
384 entry,
385 reload_type,
386 navigation_start);
389 RenderFrameHostImpl* dest_render_frame_host = manager->Navigate(entry);
390 if (!dest_render_frame_host)
391 return false; // Unable to create the desired RenderFrameHost.
393 // Make sure no code called via RFHM::Navigate clears the pending entry.
394 CHECK_EQ(controller_->GetPendingEntry(), &entry);
396 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
397 // Double check that here.
398 CheckWebUIRendererDoesNotDisplayNormalURL(
399 dest_render_frame_host, entry.GetURL());
401 // Notify observers that we will navigate in this RenderFrame.
402 if (delegate_)
403 delegate_->AboutToNavigateRenderFrame(dest_render_frame_host);
405 // Create the navigation parameters.
406 // TODO(vitalybuka): Move this before AboutToNavigateRenderFrame once
407 // http://crbug.com/408684 is fixed.
408 FrameMsg_Navigate_Params navigate_params;
409 MakeNavigateParams(
410 entry, controller_, reload_type, navigation_start, &navigate_params);
412 // Navigate in the desired RenderFrameHost.
413 // We can skip this step in the rare case that this is a transfer navigation
414 // which began in the chosen RenderFrameHost, since the request has already
415 // been issued. In that case, simply resume the response.
416 bool is_transfer_to_same =
417 navigate_params.transferred_request_child_id != -1 &&
418 navigate_params.transferred_request_child_id ==
419 dest_render_frame_host->GetProcess()->GetID();
420 if (!is_transfer_to_same) {
421 navigation_data_.reset(new NavigationMetricsData(
422 navigation_start, entry.GetURL(), entry.restore_type()));
423 dest_render_frame_host->Navigate(navigate_params);
424 } else {
425 // No need to navigate again. Just resume the deferred request.
426 dest_render_frame_host->GetProcess()->ResumeDeferredNavigation(
427 GlobalRequestID(navigate_params.transferred_request_child_id,
428 navigate_params.transferred_request_request_id));
431 // Make sure no code called via RFH::Navigate clears the pending entry.
432 CHECK_EQ(controller_->GetPendingEntry(), &entry);
434 if (entry.GetPageID() == -1) {
435 // HACK!! This code suppresses javascript: URLs from being added to
436 // session history, which is what we want to do for javascript: URLs that
437 // do not generate content. What we really need is a message from the
438 // renderer telling us that a new page was not created. The same message
439 // could be used for mailto: URLs and the like.
440 if (entry.GetURL().SchemeIs(url::kJavaScriptScheme))
441 return false;
444 // Notify observers about navigation.
445 if (delegate_) {
446 delegate_->DidStartNavigationToPendingEntry(dest_render_frame_host,
447 entry.GetURL(),
448 reload_type);
451 return true;
454 bool NavigatorImpl::NavigateToPendingEntry(
455 RenderFrameHostImpl* render_frame_host,
456 NavigationController::ReloadType reload_type) {
457 return NavigateToEntry(
458 render_frame_host,
459 *NavigationEntryImpl::FromNavigationEntry(controller_->GetPendingEntry()),
460 reload_type);
463 void NavigatorImpl::DidNavigate(
464 RenderFrameHostImpl* render_frame_host,
465 const FrameHostMsg_DidCommitProvisionalLoad_Params& input_params) {
466 // PlzNavigate
467 // The navigation request has been committed so the browser process doesn't
468 // need to care about it anymore.
469 if (CommandLine::ForCurrentProcess()->HasSwitch(
470 switches::kEnableBrowserSideNavigation)) {
471 navigation_request_map_.erase(
472 render_frame_host->frame_tree_node()->frame_tree_node_id());
475 FrameHostMsg_DidCommitProvisionalLoad_Params params(input_params);
476 FrameTree* frame_tree = render_frame_host->frame_tree_node()->frame_tree();
477 bool use_site_per_process = base::CommandLine::ForCurrentProcess()->HasSwitch(
478 switches::kSitePerProcess);
480 if (use_site_per_process) {
481 // TODO(creis): Until we mirror the frame tree in the subframe's process,
482 // cross-process subframe navigations happen in a renderer's main frame.
483 // Correct the transition type here if we know it is for a subframe.
484 NavigationEntryImpl* pending_entry =
485 NavigationEntryImpl::FromNavigationEntry(
486 controller_->GetPendingEntry());
487 if (!render_frame_host->frame_tree_node()->IsMainFrame() &&
488 pending_entry &&
489 pending_entry->frame_tree_node_id() ==
490 render_frame_host->frame_tree_node()->frame_tree_node_id()) {
491 params.transition = ui::PAGE_TRANSITION_AUTO_SUBFRAME;
495 if (ui::PageTransitionIsMainFrame(params.transition)) {
496 if (delegate_) {
497 // When overscroll navigation gesture is enabled, a screenshot of the page
498 // in its current state is taken so that it can be used during the
499 // nav-gesture. It is necessary to take the screenshot here, before
500 // calling RenderFrameHostManager::DidNavigateMainFrame, because that can
501 // change WebContents::GetRenderViewHost to return the new host, instead
502 // of the one that may have just been swapped out.
503 if (delegate_->CanOverscrollContent()) {
504 // Don't take screenshots if we are staying on the same page. We want
505 // in-page navigations to be super fast, and taking a screenshot
506 // currently blocks GPU for a longer time than we are willing to
507 // tolerate in this use case.
508 if (!params.was_within_same_page)
509 controller_->TakeScreenshot();
512 // Run tasks that must execute just before the commit.
513 bool is_navigation_within_page = controller_->IsURLInPageNavigation(
514 params.url, params.was_within_same_page, render_frame_host);
515 delegate_->DidNavigateMainFramePreCommit(is_navigation_within_page);
518 if (!use_site_per_process)
519 frame_tree->root()->render_manager()->DidNavigateFrame(render_frame_host);
522 // When using --site-per-process, we notify the RFHM for all navigations,
523 // not just main frame navigations.
524 if (use_site_per_process) {
525 FrameTreeNode* frame = render_frame_host->frame_tree_node();
526 frame->render_manager()->DidNavigateFrame(render_frame_host);
529 // Update the site of the SiteInstance if it doesn't have one yet, unless
530 // assigning a site is not necessary for this URL. In that case, the
531 // SiteInstance can still be considered unused until a navigation to a real
532 // page.
533 SiteInstanceImpl* site_instance =
534 static_cast<SiteInstanceImpl*>(render_frame_host->GetSiteInstance());
535 if (!site_instance->HasSite() &&
536 ShouldAssignSiteForURL(params.url)) {
537 site_instance->SetSite(params.url);
540 // Need to update MIME type here because it's referred to in
541 // UpdateNavigationCommands() called by RendererDidNavigate() to
542 // determine whether or not to enable the encoding menu.
543 // It's updated only for the main frame. For a subframe,
544 // RenderView::UpdateURL does not set params.contents_mime_type.
545 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
546 // TODO(jungshik): Add a test for the encoding menu to avoid
547 // regressing it again.
548 // TODO(nasko): Verify the correctness of the above comment, since some of the
549 // code doesn't exist anymore. Also, move this code in the
550 // PageTransitionIsMainFrame code block above.
551 if (ui::PageTransitionIsMainFrame(params.transition) && delegate_)
552 delegate_->SetMainFrameMimeType(params.contents_mime_type);
554 LoadCommittedDetails details;
555 bool did_navigate = controller_->RendererDidNavigate(render_frame_host,
556 params, &details);
558 // For now, keep track of each frame's URL in its FrameTreeNode. This lets
559 // us estimate our process count for implementing OOP iframes.
560 // TODO(creis): Remove this when we track which pages commit in each frame.
561 render_frame_host->frame_tree_node()->set_current_url(params.url);
563 // Send notification about committed provisional loads. This notification is
564 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
565 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
566 if (details.type != NAVIGATION_TYPE_NAV_IGNORE && delegate_) {
567 DCHECK_EQ(!render_frame_host->GetParent(),
568 did_navigate ? details.is_main_frame : false);
569 ui::PageTransition transition_type = params.transition;
570 // Whether or not a page transition was triggered by going backward or
571 // forward in the history is only stored in the navigation controller's
572 // entry list.
573 if (did_navigate &&
574 (controller_->GetLastCommittedEntry()->GetTransitionType() &
575 ui::PAGE_TRANSITION_FORWARD_BACK)) {
576 transition_type = ui::PageTransitionFromInt(
577 params.transition | ui::PAGE_TRANSITION_FORWARD_BACK);
580 delegate_->DidCommitProvisionalLoad(render_frame_host,
581 params.url,
582 transition_type);
585 if (!did_navigate)
586 return; // No navigation happened.
588 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
589 // for the appropriate notification (best) or you can add it to
590 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
591 // necessary, please).
593 // TODO(carlosk): Move this out when PlzNavigate implementation properly calls
594 // the observer methods.
595 RecordNavigationMetrics(details, params, site_instance);
597 // Run post-commit tasks.
598 if (delegate_) {
599 if (details.is_main_frame)
600 delegate_->DidNavigateMainFramePostCommit(details, params);
602 delegate_->DidNavigateAnyFramePostCommit(
603 render_frame_host, details, params);
607 bool NavigatorImpl::ShouldAssignSiteForURL(const GURL& url) {
608 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
609 // still be used for a normal web site.
610 if (url == GURL(url::kAboutBlankURL))
611 return false;
613 // The embedder will then have the opportunity to determine if the URL
614 // should "use up" the SiteInstance.
615 return GetContentClient()->browser()->ShouldAssignSiteForURL(url);
618 void NavigatorImpl::RequestOpenURL(
619 RenderFrameHostImpl* render_frame_host,
620 const GURL& url,
621 const Referrer& referrer,
622 WindowOpenDisposition disposition,
623 bool should_replace_current_entry,
624 bool user_gesture) {
625 SiteInstance* current_site_instance =
626 GetRenderManager(render_frame_host)->current_frame_host()->
627 GetSiteInstance();
628 // If this came from a swapped out RenderFrameHost, we only allow the request
629 // if we are still in the same BrowsingInstance.
630 // TODO(creis): Move this to RenderFrameProxyHost::OpenURL.
631 if (render_frame_host->is_swapped_out() &&
632 !render_frame_host->GetSiteInstance()->IsRelatedSiteInstance(
633 current_site_instance)) {
634 return;
637 // Delegate to RequestTransferURL because this is just the generic
638 // case where |old_request_id| is empty.
639 // TODO(creis): Pass the redirect_chain into this method to support client
640 // redirects. http://crbug.com/311721.
641 std::vector<GURL> redirect_chain;
642 RequestTransferURL(render_frame_host,
643 url,
644 redirect_chain,
645 referrer,
646 ui::PAGE_TRANSITION_LINK,
647 disposition,
648 GlobalRequestID(),
649 should_replace_current_entry,
650 user_gesture);
653 void NavigatorImpl::RequestTransferURL(
654 RenderFrameHostImpl* render_frame_host,
655 const GURL& url,
656 const std::vector<GURL>& redirect_chain,
657 const Referrer& referrer,
658 ui::PageTransition page_transition,
659 WindowOpenDisposition disposition,
660 const GlobalRequestID& transferred_global_request_id,
661 bool should_replace_current_entry,
662 bool user_gesture) {
663 GURL dest_url(url);
664 SiteInstance* current_site_instance =
665 GetRenderManager(render_frame_host)->current_frame_host()->
666 GetSiteInstance();
667 if (!GetContentClient()->browser()->ShouldAllowOpenURL(
668 current_site_instance, url)) {
669 dest_url = GURL(url::kAboutBlankURL);
672 int64 frame_tree_node_id = -1;
673 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
674 switches::kSitePerProcess)) {
675 frame_tree_node_id =
676 render_frame_host->frame_tree_node()->frame_tree_node_id();
678 OpenURLParams params(
679 dest_url, referrer, frame_tree_node_id, disposition, page_transition,
680 true /* is_renderer_initiated */);
681 if (redirect_chain.size() > 0)
682 params.redirect_chain = redirect_chain;
683 params.transferred_global_request_id = transferred_global_request_id;
684 params.should_replace_current_entry = should_replace_current_entry;
685 params.user_gesture = user_gesture;
687 if (GetRenderManager(render_frame_host)->web_ui()) {
688 // Web UI pages sometimes want to override the page transition type for
689 // link clicks (e.g., so the new tab page can specify AUTO_BOOKMARK for
690 // automatically generated suggestions). We don't override other types
691 // like TYPED because they have different implications (e.g., autocomplete).
692 if (ui::PageTransitionCoreTypeIs(
693 params.transition, ui::PAGE_TRANSITION_LINK))
694 params.transition =
695 GetRenderManager(render_frame_host)->web_ui()->
696 GetLinkTransitionType();
698 // Note also that we hide the referrer for Web UI pages. We don't really
699 // want web sites to see a referrer of "chrome://blah" (and some
700 // chrome: URLs might have search terms or other stuff we don't want to
701 // send to the site), so we send no referrer.
702 params.referrer = Referrer();
704 // Navigations in Web UI pages count as browser-initiated navigations.
705 params.is_renderer_initiated = false;
708 if (delegate_)
709 delegate_->RequestOpenURL(render_frame_host, params);
712 // PlzNavigate
713 void NavigatorImpl::OnBeginNavigation(
714 FrameTreeNode* frame_tree_node,
715 const FrameHostMsg_BeginNavigation_Params& params,
716 const CommonNavigationParams& common_params) {
717 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
718 switches::kEnableBrowserSideNavigation));
719 DCHECK(frame_tree_node);
721 // TODO(clamy): In case of a renderer initiated navigation create a new
722 // NavigationRequest.
723 NavigationRequest* navigation_request =
724 navigation_request_map_.get(frame_tree_node->frame_tree_node_id());
725 DCHECK(navigation_request);
727 // Update the referrer with the one received from the renderer.
728 navigation_request->common_params().referrer = common_params.referrer;
730 scoped_ptr<NavigationRequestInfo> info(new NavigationRequestInfo(params));
732 info->first_party_for_cookies =
733 frame_tree_node->IsMainFrame()
734 ? navigation_request->common_params().url
735 : frame_tree_node->frame_tree()->root()->current_url();
736 info->is_main_frame = frame_tree_node->IsMainFrame();
737 info->parent_is_main_frame = !frame_tree_node->parent() ?
738 false : frame_tree_node->parent()->IsMainFrame();
740 // TODO(clamy): Inform the RenderFrameHostManager that a navigation is about
741 // to begin, so that it can speculatively spawn a new renderer if needed.
743 navigation_request->BeginNavigation(info.Pass(), params.request_body);
746 // PlzNavigate
747 void NavigatorImpl::CommitNavigation(FrameTreeNode* frame_tree_node,
748 ResourceResponse* response,
749 scoped_ptr<StreamHandle> body) {
750 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
751 switches::kEnableBrowserSideNavigation));
753 // HTTP 204 (No Content) and HTTP 205 (Reset Content) responses should not
754 // commit; they leave the frame showing the previous page.
755 if (response->head.headers.get() &&
756 (response->head.headers->response_code() == 204 ||
757 response->head.headers->response_code() == 205)) {
758 CancelNavigation(frame_tree_node);
759 return;
762 NavigationRequest* navigation_request =
763 navigation_request_map_.get(frame_tree_node->frame_tree_node_id());
764 DCHECK(navigation_request);
766 // Select an appropriate renderer to commit the navigation.
767 RenderFrameHostImpl* render_frame_host =
768 frame_tree_node->render_manager()->GetFrameHostForNavigation(
769 navigation_request->common_params().url,
770 navigation_request->common_params().transition);
771 CheckWebUIRendererDoesNotDisplayNormalURL(
772 render_frame_host, navigation_request->common_params().url);
774 render_frame_host->CommitNavigation(response, body.Pass(),
775 navigation_request->common_params(),
776 navigation_request->commit_params());
779 // PlzNavigate
780 void NavigatorImpl::CancelNavigation(FrameTreeNode* frame_tree_node) {
781 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
782 switches::kEnableBrowserSideNavigation));
783 navigation_request_map_.erase(frame_tree_node->frame_tree_node_id());
786 void NavigatorImpl::LogResourceRequestTime(
787 base::TimeTicks timestamp, const GURL& url) {
788 if (navigation_data_ && navigation_data_->url_ == url) {
789 navigation_data_->url_job_start_time_ = timestamp;
790 UMA_HISTOGRAM_TIMES(
791 "Navigation.TimeToURLJobStart",
792 navigation_data_->url_job_start_time_ - navigation_data_->start_time_);
796 void NavigatorImpl::LogBeforeUnloadTime(
797 const base::TimeTicks& renderer_before_unload_start_time,
798 const base::TimeTicks& renderer_before_unload_end_time) {
799 // Only stores the beforeunload delay if we're tracking a browser initiated
800 // navigation and it happened later than the navigation request.
801 if (navigation_data_ &&
802 renderer_before_unload_start_time > navigation_data_->start_time_) {
803 navigation_data_->before_unload_delay_ =
804 renderer_before_unload_end_time - renderer_before_unload_start_time;
808 void NavigatorImpl::CheckWebUIRendererDoesNotDisplayNormalURL(
809 RenderFrameHostImpl* render_frame_host,
810 const GURL& url) {
811 int enabled_bindings =
812 render_frame_host->render_view_host()->GetEnabledBindings();
813 bool is_allowed_in_web_ui_renderer =
814 WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
815 controller_->GetBrowserContext(), url);
816 if ((enabled_bindings & BINDINGS_POLICY_WEB_UI) &&
817 !is_allowed_in_web_ui_renderer) {
818 // Log the URL to help us diagnose any future failures of this CHECK.
819 GetContentClient()->SetActiveURL(url);
820 CHECK(0);
824 // PlzNavigate
825 bool NavigatorImpl::RequestNavigation(
826 FrameTreeNode* frame_tree_node,
827 const NavigationEntryImpl& entry,
828 NavigationController::ReloadType reload_type,
829 base::TimeTicks navigation_start) {
830 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
831 switches::kEnableBrowserSideNavigation));
832 DCHECK(frame_tree_node);
833 int64 frame_tree_node_id = frame_tree_node->frame_tree_node_id();
834 FrameMsg_Navigate_Type::Value navigation_type =
835 GetNavigationType(controller_->GetBrowserContext(), entry, reload_type);
836 scoped_ptr<NavigationRequest> navigation_request(new NavigationRequest(
837 frame_tree_node,
838 CommonNavigationParams(entry.GetURL(),
839 entry.GetReferrer(),
840 entry.GetTransitionType(),
841 navigation_type,
842 !entry.IsViewSourceMode()),
843 CommitNavigationParams(entry.GetPageState(),
844 entry.GetIsOverridingUserAgent(),
845 navigation_start)));
846 RequestNavigationParams request_params(entry.GetHasPostData(),
847 entry.extra_headers(),
848 entry.GetBrowserInitiatedPostData());
849 // TODO(clamy): Check if navigations are blocked and if so store the
850 // parameters.
852 // If there is an ongoing request, replace it.
853 navigation_request_map_.set(frame_tree_node_id, navigation_request.Pass());
855 if (frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
856 frame_tree_node->current_frame_host()->Send(new FrameMsg_RequestNavigation(
857 frame_tree_node->current_frame_host()->GetRoutingID(),
858 navigation_request_map_.get(frame_tree_node_id)->common_params(),
859 request_params));
860 return true;
863 // The navigation request is sent directly to the IO thread.
864 OnBeginNavigation(
865 frame_tree_node,
866 MakeDefaultBeginNavigation(request_params, navigation_type),
867 navigation_request_map_.get(frame_tree_node_id)->common_params());
868 return true;
871 void NavigatorImpl::RecordNavigationMetrics(
872 const LoadCommittedDetails& details,
873 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
874 SiteInstance* site_instance) {
875 DCHECK(site_instance->HasProcess());
876 if (!details.is_main_frame || !navigation_data_ ||
877 navigation_data_->url_ != params.original_request_url) {
878 return;
881 base::TimeDelta time_to_commit =
882 base::TimeTicks::Now() - navigation_data_->start_time_;
883 UMA_HISTOGRAM_TIMES("Navigation.TimeToCommit", time_to_commit);
885 time_to_commit -= navigation_data_->before_unload_delay_;
886 base::TimeDelta time_to_network = navigation_data_->url_job_start_time_ -
887 navigation_data_->start_time_ -
888 navigation_data_->before_unload_delay_;
889 if (navigation_data_->is_restoring_from_last_session_) {
890 UMA_HISTOGRAM_TIMES(
891 "Navigation.TimeToCommit_SessionRestored",
892 time_to_commit);
893 UMA_HISTOGRAM_TIMES(
894 "Navigation.TimeToURLJobStart_SessionRestored",
895 time_to_network);
896 navigation_data_.reset();
897 return;
899 bool navigation_created_new_renderer_process =
900 site_instance->GetProcess()->GetInitTimeForNavigationMetrics() >
901 navigation_data_->start_time_;
902 if (navigation_created_new_renderer_process) {
903 UMA_HISTOGRAM_TIMES(
904 "Navigation.TimeToCommit_NewRenderer_BeforeUnloadDiscounted",
905 time_to_commit);
906 UMA_HISTOGRAM_TIMES(
907 "Navigation.TimeToURLJobStart_NewRenderer_BeforeUnloadDiscounted",
908 time_to_network);
909 } else {
910 UMA_HISTOGRAM_TIMES(
911 "Navigation.TimeToCommit_ExistingRenderer_BeforeUnloadDiscounted",
912 time_to_commit);
913 UMA_HISTOGRAM_TIMES(
914 "Navigation.TimeToURLJobStart_ExistingRenderer_BeforeUnloadDiscounted",
915 time_to_network);
917 navigation_data_.reset();
920 } // namespace content