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/url_constants.h"
37 #include "content/public/common/url_utils.h"
38 #include "net/base/load_flags.h"
44 FrameMsg_Navigate_Type::Value
GetNavigationType(
45 BrowserContext
* browser_context
, const NavigationEntryImpl
& entry
,
46 NavigationController::ReloadType reload_type
) {
47 switch (reload_type
) {
48 case NavigationControllerImpl::RELOAD
:
49 return FrameMsg_Navigate_Type::RELOAD
;
50 case NavigationControllerImpl::RELOAD_IGNORING_CACHE
:
51 return FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE
;
52 case NavigationControllerImpl::RELOAD_ORIGINAL_REQUEST_URL
:
53 return FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL
;
54 case NavigationControllerImpl::NO_RELOAD
:
55 break; // Fall through to rest of function.
58 // |RenderViewImpl::PopulateStateFromPendingNavigationParams| differentiates
59 // between |RESTORE_WITH_POST| and |RESTORE|.
60 if (entry
.restore_type() ==
61 NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY
) {
62 if (entry
.GetHasPostData())
63 return FrameMsg_Navigate_Type::RESTORE_WITH_POST
;
64 return FrameMsg_Navigate_Type::RESTORE
;
67 return FrameMsg_Navigate_Type::NORMAL
;
71 // Returns the net load flags to use based on the navigation type.
72 // TODO(clamy): unify the code with what is happening on the renderer side.
73 int LoadFlagFromNavigationType(FrameMsg_Navigate_Type::Value navigation_type
) {
74 int load_flags
= net::LOAD_NORMAL
;
75 switch (navigation_type
) {
76 case FrameMsg_Navigate_Type::RELOAD
:
77 case FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL
:
78 load_flags
|= net::LOAD_VALIDATE_CACHE
;
80 case FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE
:
81 load_flags
|= net::LOAD_BYPASS_CACHE
;
83 case FrameMsg_Navigate_Type::RESTORE
:
84 load_flags
|= net::LOAD_PREFERRING_CACHE
;
86 case FrameMsg_Navigate_Type::RESTORE_WITH_POST
:
87 load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
89 case FrameMsg_Navigate_Type::NORMAL
:
97 // Generates a default FrameHostMsg_BeginNavigation_Params to be used when there
98 // is no live renderer.
99 FrameHostMsg_BeginNavigation_Params
MakeDefaultBeginNavigation(
100 const RequestNavigationParams
& request_params
,
101 FrameMsg_Navigate_Type::Value navigation_type
) {
102 FrameHostMsg_BeginNavigation_Params begin_navigation_params
;
103 begin_navigation_params
.method
= request_params
.is_post
? "POST" : "GET";
104 begin_navigation_params
.load_flags
=
105 LoadFlagFromNavigationType(navigation_type
);
107 // TODO(clamy): Post data from the browser should be put in the request body.
108 // Headers should be filled in as well.
110 begin_navigation_params
.has_user_gesture
= false;
111 return begin_navigation_params
;
114 RenderFrameHostManager
* GetRenderManager(RenderFrameHostImpl
* rfh
) {
115 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kSitePerProcess
))
117 return rfh
->frame_tree_node()->render_manager();
119 return rfh
->frame_tree_node()->frame_tree()->root()->render_manager();
122 void MakeNavigateParams(const NavigationEntryImpl
& entry
,
123 NavigationControllerImpl
* controller
,
124 NavigationController::ReloadType reload_type
,
125 base::TimeTicks navigation_start
,
126 FrameMsg_Navigate_Params
* params
) {
127 params
->common_params
= CommonNavigationParams(
128 entry
.GetURL(), entry
.GetReferrer(), entry
.GetTransitionType(),
129 GetNavigationType(controller
->GetBrowserContext(), entry
, reload_type
),
130 !entry
.IsViewSourceMode());
131 params
->request_params
= RequestNavigationParams(
132 entry
.GetHasPostData(),
133 entry
.extra_headers(),
134 entry
.GetBrowserInitiatedPostData());
135 params
->commit_params
= CommitNavigationParams(
136 entry
.GetPageState(), entry
.GetIsOverridingUserAgent(), navigation_start
);
137 if (!entry
.GetBaseURLForDataURL().is_empty()) {
138 params
->base_url_for_data_url
= entry
.GetBaseURLForDataURL();
139 params
->history_url_for_data_url
= entry
.GetVirtualURL();
141 params
->should_replace_current_entry
= entry
.should_replace_entry();
142 // This is used by the old performance infrastructure to set up DocumentState
143 // associated with the RenderView.
144 // TODO(ppi): make it go away.
145 params
->request_time
= base::Time::Now();
146 params
->transferred_request_child_id
=
147 entry
.transferred_global_request_id().child_id
;
148 params
->transferred_request_request_id
=
149 entry
.transferred_global_request_id().request_id
;
151 params
->page_id
= entry
.GetPageID();
152 params
->should_clear_history_list
= entry
.should_clear_history_list();
153 if (entry
.should_clear_history_list()) {
154 // Set the history list related parameters to the same values a
155 // NavigationController would return before its first navigation. This will
156 // fully clear the RenderView's view of the session history.
157 params
->pending_history_list_offset
= -1;
158 params
->current_history_list_offset
= -1;
159 params
->current_history_list_length
= 0;
161 params
->pending_history_list_offset
= controller
->GetIndexOfEntry(&entry
);
162 params
->current_history_list_offset
=
163 controller
->GetLastCommittedEntryIndex();
164 params
->current_history_list_length
= controller
->GetEntryCount();
166 // Set the redirect chain to the navigation's redirects, unless we are
167 // returning to a completed navigation (whose previous redirects don't apply).
168 if (ui::PageTransitionIsNewNavigation(params
->common_params
.transition
)) {
169 params
->redirects
= entry
.GetRedirectChain();
171 params
->redirects
.clear();
174 params
->can_load_local_resources
= entry
.GetCanLoadLocalResources();
175 params
->frame_to_navigate
= entry
.GetFrameToNavigate();
181 NavigatorImpl::NavigatorImpl(
182 NavigationControllerImpl
* navigation_controller
,
183 NavigatorDelegate
* delegate
)
184 : controller_(navigation_controller
),
185 delegate_(delegate
) {
188 NavigatorImpl::~NavigatorImpl() {}
190 NavigationController
* NavigatorImpl::GetController() {
194 void NavigatorImpl::DidStartProvisionalLoad(
195 RenderFrameHostImpl
* render_frame_host
,
197 bool is_transition_navigation
) {
198 bool is_error_page
= (url
.spec() == kUnreachableWebDataURL
);
199 bool is_iframe_srcdoc
= (url
.spec() == kAboutSrcDocURL
);
200 GURL
validated_url(url
);
201 RenderProcessHost
* render_process_host
= render_frame_host
->GetProcess();
202 render_process_host
->FilterURL(false, &validated_url
);
204 bool is_main_frame
= render_frame_host
->frame_tree_node()->IsMainFrame();
205 NavigationEntryImpl
* pending_entry
=
206 NavigationEntryImpl::FromNavigationEntry(controller_
->GetPendingEntry());
208 // If there is no browser-initiated pending entry for this navigation and it
209 // is not for the error URL, create a pending entry using the current
210 // SiteInstance, and ensure the address bar updates accordingly. We don't
211 // know the referrer or extra headers at this point, but the referrer will
212 // be set properly upon commit.
213 bool has_browser_initiated_pending_entry
= pending_entry
&&
214 !pending_entry
->is_renderer_initiated();
215 if (!has_browser_initiated_pending_entry
&& !is_error_page
) {
216 NavigationEntryImpl
* entry
= NavigationEntryImpl::FromNavigationEntry(
217 controller_
->CreateNavigationEntry(validated_url
,
219 ui::PAGE_TRANSITION_LINK
,
220 true /* is_renderer_initiated */,
222 controller_
->GetBrowserContext()));
223 entry
->set_site_instance(
224 static_cast<SiteInstanceImpl
*>(
225 render_frame_host
->render_view_host()->GetSiteInstance()));
226 // TODO(creis): If there's a pending entry already, find a safe way to
227 // update it instead of replacing it and copying over things like this.
229 entry
->set_transferred_global_request_id(
230 pending_entry
->transferred_global_request_id());
231 entry
->set_should_replace_entry(pending_entry
->should_replace_entry());
232 entry
->SetRedirectChain(pending_entry
->GetRedirectChain());
234 controller_
->SetPendingEntry(entry
);
236 delegate_
->NotifyChangedNavigationState(content::INVALIDATE_TYPE_URL
);
239 if (delegate_
&& is_transition_navigation
)
240 delegate_
->DidStartNavigationTransition(render_frame_host
);
244 // Notify the observer about the start of the provisional load.
245 delegate_
->DidStartProvisionalLoad(
246 render_frame_host
, validated_url
, is_error_page
, is_iframe_srcdoc
);
251 void NavigatorImpl::DidFailProvisionalLoadWithError(
252 RenderFrameHostImpl
* render_frame_host
,
253 const FrameHostMsg_DidFailProvisionalLoadWithError_Params
& params
) {
254 VLOG(1) << "Failed Provisional Load: " << params
.url
.possibly_invalid_spec()
255 << ", error_code: " << params
.error_code
256 << ", error_description: " << params
.error_description
257 << ", showing_repost_interstitial: " <<
258 params
.showing_repost_interstitial
259 << ", frame_id: " << render_frame_host
->GetRoutingID();
260 GURL
validated_url(params
.url
);
261 RenderProcessHost
* render_process_host
= render_frame_host
->GetProcess();
262 render_process_host
->FilterURL(false, &validated_url
);
264 if (net::ERR_ABORTED
== params
.error_code
) {
265 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
266 // This means that the interstitial won't be torn down properly, which is
267 // bad. But if we have an interstitial, go back to another tab type, and
268 // then load the same interstitial again, we could end up getting the first
269 // interstitial's "failed" message (as a result of the cancel) when we're on
270 // the second one. We can't tell this apart, so we think we're tearing down
271 // the current page which will cause a crash later on.
273 // http://code.google.com/p/chromium/issues/detail?id=2855
274 // Because this will not tear down the interstitial properly, if "back" is
275 // back to another tab type, the interstitial will still be somewhat alive
276 // in the previous tab type. If you navigate somewhere that activates the
277 // tab with the interstitial again, you'll see a flash before the new load
278 // commits of the interstitial page.
279 FrameTreeNode
* root
=
280 render_frame_host
->frame_tree_node()->frame_tree()->root();
281 if (root
->render_manager()->interstitial_page() != NULL
) {
282 LOG(WARNING
) << "Discarding message during interstitial.";
286 // We used to cancel the pending renderer here for cross-site downloads.
287 // However, it's not safe to do that because the download logic repeatedly
288 // looks for this WebContents based on a render ID. Instead, we just
289 // leave the pending renderer around until the next navigation event
290 // (Navigate, DidNavigate, etc), which will clean it up properly.
292 // TODO(creis): Find a way to cancel any pending RFH here.
295 // We usually clear the pending entry when it fails, so that an arbitrary URL
296 // isn't left visible above a committed page. This must be enforced when
297 // the pending entry isn't visible (e.g., renderer-initiated navigations) to
298 // prevent URL spoofs for in-page navigations that don't go through
299 // DidStartProvisionalLoadForFrame.
301 // However, we do preserve the pending entry in some cases, such as on the
302 // initial navigation of an unmodified blank tab. We also allow the delegate
303 // to say when it's safe to leave aborted URLs in the omnibox, to let the user
304 // edit the URL and try again. This may be useful in cases that the committed
305 // page cannot be attacker-controlled. In these cases, we still allow the
306 // view to clear the pending entry and typed URL if the user requests
307 // (e.g., hitting Escape with focus in the address bar).
309 // Note: don't touch the transient entry, since an interstitial may exist.
310 bool should_preserve_entry
= controller_
->IsUnmodifiedBlankTab() ||
311 delegate_
->ShouldPreserveAbortedURLs();
312 if (controller_
->GetPendingEntry() != controller_
->GetVisibleEntry() ||
313 !should_preserve_entry
) {
314 controller_
->DiscardPendingEntry();
316 // Also force the UI to refresh.
317 controller_
->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL
);
321 delegate_
->DidFailProvisionalLoadWithError(render_frame_host
, params
);
324 void NavigatorImpl::DidFailLoadWithError(
325 RenderFrameHostImpl
* render_frame_host
,
328 const base::string16
& error_description
) {
330 delegate_
->DidFailLoadWithError(
331 render_frame_host
, url
, error_code
,
336 bool NavigatorImpl::NavigateToEntry(
337 RenderFrameHostImpl
* render_frame_host
,
338 const NavigationEntryImpl
& entry
,
339 NavigationController::ReloadType reload_type
) {
340 TRACE_EVENT0("browser,navigation", "NavigatorImpl::NavigateToEntry");
342 // The renderer will reject IPC messages with URLs longer than
343 // this limit, so don't attempt to navigate with a longer URL.
344 if (entry
.GetURL().spec().size() > GetMaxURLChars()) {
345 LOG(WARNING
) << "Refusing to load URL as it exceeds " << GetMaxURLChars()
350 // This will be used to set the Navigation Timing API navigationStart
351 // parameter for browser navigations in new tabs (intents, tabs opened through
352 // "Open link in new tab"). We need to keep it above RFHM::Navigate() call to
353 // capture the time needed for the RenderFrameHost initialization.
354 base::TimeTicks navigation_start
= base::TimeTicks::Now();
356 RenderFrameHostManager
* manager
=
357 render_frame_host
->frame_tree_node()->render_manager();
359 // PlzNavigate: the RenderFrameHosts are no longer asked to navigate.
360 if (CommandLine::ForCurrentProcess()->HasSwitch(
361 switches::kEnableBrowserSideNavigation
)) {
362 navigation_start_time_and_url
= MakeTuple(navigation_start
, entry
.GetURL());
363 return RequestNavigation(render_frame_host
->frame_tree_node(),
369 RenderFrameHostImpl
* dest_render_frame_host
= manager
->Navigate(entry
);
370 if (!dest_render_frame_host
)
371 return false; // Unable to create the desired RenderFrameHost.
373 // Make sure no code called via RFHM::Navigate clears the pending entry.
374 CHECK_EQ(controller_
->GetPendingEntry(), &entry
);
376 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
377 // Double check that here.
378 CheckWebUIRendererDoesNotDisplayNormalURL(
379 dest_render_frame_host
, entry
.GetURL());
381 // Notify observers that we will navigate in this RenderFrame.
383 delegate_
->AboutToNavigateRenderFrame(dest_render_frame_host
);
385 // Create the navigation parameters.
386 // TODO(vitalybuka): Move this before AboutToNavigateRenderFrame once
387 // http://crbug.com/408684 is fixed.
388 FrameMsg_Navigate_Params navigate_params
;
390 entry
, controller_
, reload_type
, navigation_start
, &navigate_params
);
392 // Navigate in the desired RenderFrameHost.
393 // We can skip this step in the rare case that this is a transfer navigation
394 // which began in the chosen RenderFrameHost, since the request has already
395 // been issued. In that case, simply resume the response.
396 bool is_transfer_to_same
=
397 navigate_params
.transferred_request_child_id
!= -1 &&
398 navigate_params
.transferred_request_child_id
==
399 dest_render_frame_host
->GetProcess()->GetID();
400 if (!is_transfer_to_same
) {
401 navigation_start_time_and_url
= MakeTuple(navigation_start
, entry
.GetURL());
402 dest_render_frame_host
->Navigate(navigate_params
);
404 // No need to navigate again. Just resume the deferred request.
405 dest_render_frame_host
->GetProcess()->ResumeDeferredNavigation(
406 GlobalRequestID(navigate_params
.transferred_request_child_id
,
407 navigate_params
.transferred_request_request_id
));
410 // Make sure no code called via RFH::Navigate clears the pending entry.
411 CHECK_EQ(controller_
->GetPendingEntry(), &entry
);
413 if (entry
.GetPageID() == -1) {
414 // HACK!! This code suppresses javascript: URLs from being added to
415 // session history, which is what we want to do for javascript: URLs that
416 // do not generate content. What we really need is a message from the
417 // renderer telling us that a new page was not created. The same message
418 // could be used for mailto: URLs and the like.
419 if (entry
.GetURL().SchemeIs(url::kJavaScriptScheme
))
423 // Notify observers about navigation.
425 delegate_
->DidStartNavigationToPendingEntry(dest_render_frame_host
,
433 bool NavigatorImpl::NavigateToPendingEntry(
434 RenderFrameHostImpl
* render_frame_host
,
435 NavigationController::ReloadType reload_type
) {
436 return NavigateToEntry(
438 *NavigationEntryImpl::FromNavigationEntry(controller_
->GetPendingEntry()),
442 void NavigatorImpl::DidNavigate(
443 RenderFrameHostImpl
* render_frame_host
,
444 const FrameHostMsg_DidCommitProvisionalLoad_Params
& input_params
) {
446 // The navigation request has been committed so the browser process doesn't
447 // need to care about it anymore.
448 if (CommandLine::ForCurrentProcess()->HasSwitch(
449 switches::kEnableBrowserSideNavigation
)) {
450 navigation_request_map_
.erase(
451 render_frame_host
->frame_tree_node()->frame_tree_node_id());
454 FrameHostMsg_DidCommitProvisionalLoad_Params
params(input_params
);
455 FrameTree
* frame_tree
= render_frame_host
->frame_tree_node()->frame_tree();
456 bool use_site_per_process
= base::CommandLine::ForCurrentProcess()->HasSwitch(
457 switches::kSitePerProcess
);
459 if (use_site_per_process
) {
460 // TODO(creis): Until we mirror the frame tree in the subframe's process,
461 // cross-process subframe navigations happen in a renderer's main frame.
462 // Correct the transition type here if we know it is for a subframe.
463 NavigationEntryImpl
* pending_entry
=
464 NavigationEntryImpl::FromNavigationEntry(
465 controller_
->GetPendingEntry());
466 if (!render_frame_host
->frame_tree_node()->IsMainFrame() &&
468 pending_entry
->frame_tree_node_id() ==
469 render_frame_host
->frame_tree_node()->frame_tree_node_id()) {
470 params
.transition
= ui::PAGE_TRANSITION_AUTO_SUBFRAME
;
474 if (ui::PageTransitionIsMainFrame(params
.transition
)) {
476 // When overscroll navigation gesture is enabled, a screenshot of the page
477 // in its current state is taken so that it can be used during the
478 // nav-gesture. It is necessary to take the screenshot here, before
479 // calling RenderFrameHostManager::DidNavigateMainFrame, because that can
480 // change WebContents::GetRenderViewHost to return the new host, instead
481 // of the one that may have just been swapped out.
482 if (delegate_
->CanOverscrollContent()) {
483 // Don't take screenshots if we are staying on the same page. We want
484 // in-page navigations to be super fast, and taking a screenshot
485 // currently blocks GPU for a longer time than we are willing to
486 // tolerate in this use case.
487 if (!params
.was_within_same_page
)
488 controller_
->TakeScreenshot();
491 // Run tasks that must execute just before the commit.
492 bool is_navigation_within_page
= controller_
->IsURLInPageNavigation(
493 params
.url
, params
.was_within_same_page
, render_frame_host
);
494 delegate_
->DidNavigateMainFramePreCommit(is_navigation_within_page
);
497 if (!use_site_per_process
)
498 frame_tree
->root()->render_manager()->DidNavigateFrame(render_frame_host
);
501 // When using --site-per-process, we notify the RFHM for all navigations,
502 // not just main frame navigations.
503 if (use_site_per_process
) {
504 FrameTreeNode
* frame
= render_frame_host
->frame_tree_node();
505 frame
->render_manager()->DidNavigateFrame(render_frame_host
);
508 // Update the site of the SiteInstance if it doesn't have one yet, unless
509 // assigning a site is not necessary for this URL. In that case, the
510 // SiteInstance can still be considered unused until a navigation to a real
512 SiteInstanceImpl
* site_instance
=
513 static_cast<SiteInstanceImpl
*>(render_frame_host
->GetSiteInstance());
514 if (!site_instance
->HasSite() &&
515 ShouldAssignSiteForURL(params
.url
)) {
516 site_instance
->SetSite(params
.url
);
519 // Need to update MIME type here because it's referred to in
520 // UpdateNavigationCommands() called by RendererDidNavigate() to
521 // determine whether or not to enable the encoding menu.
522 // It's updated only for the main frame. For a subframe,
523 // RenderView::UpdateURL does not set params.contents_mime_type.
524 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
525 // TODO(jungshik): Add a test for the encoding menu to avoid
526 // regressing it again.
527 // TODO(nasko): Verify the correctness of the above comment, since some of the
528 // code doesn't exist anymore. Also, move this code in the
529 // PageTransitionIsMainFrame code block above.
530 if (ui::PageTransitionIsMainFrame(params
.transition
) && delegate_
)
531 delegate_
->SetMainFrameMimeType(params
.contents_mime_type
);
533 LoadCommittedDetails details
;
534 bool did_navigate
= controller_
->RendererDidNavigate(render_frame_host
,
537 // For now, keep track of each frame's URL in its FrameTreeNode. This lets
538 // us estimate our process count for implementing OOP iframes.
539 // TODO(creis): Remove this when we track which pages commit in each frame.
540 render_frame_host
->frame_tree_node()->set_current_url(params
.url
);
542 // Send notification about committed provisional loads. This notification is
543 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
544 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
545 if (details
.type
!= NAVIGATION_TYPE_NAV_IGNORE
&& delegate_
) {
546 DCHECK_EQ(!render_frame_host
->GetParent(),
547 did_navigate
? details
.is_main_frame
: false);
548 ui::PageTransition transition_type
= params
.transition
;
549 // Whether or not a page transition was triggered by going backward or
550 // forward in the history is only stored in the navigation controller's
553 (controller_
->GetLastCommittedEntry()->GetTransitionType() &
554 ui::PAGE_TRANSITION_FORWARD_BACK
)) {
555 transition_type
= ui::PageTransitionFromInt(
556 params
.transition
| ui::PAGE_TRANSITION_FORWARD_BACK
);
559 delegate_
->DidCommitProvisionalLoad(render_frame_host
,
565 return; // No navigation happened.
567 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
568 // for the appropriate notification (best) or you can add it to
569 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
570 // necessary, please).
572 // TODO(carlosk): Move this out when PlzNavigate implementation properly calls
573 // the observer methods.
574 if (details
.is_main_frame
&&
575 navigation_start_time_and_url
.a
.ToInternalValue() != 0
576 && navigation_start_time_and_url
.b
== params
.original_request_url
) {
577 base::TimeDelta time_to_commit
=
578 base::TimeTicks::Now() - navigation_start_time_and_url
.a
;
579 UMA_HISTOGRAM_TIMES("Navigation.TimeToCommit", time_to_commit
);
580 navigation_start_time_and_url
= MakeTuple(base::TimeTicks(), GURL());
583 // Run post-commit tasks.
585 if (details
.is_main_frame
)
586 delegate_
->DidNavigateMainFramePostCommit(details
, params
);
588 delegate_
->DidNavigateAnyFramePostCommit(
589 render_frame_host
, details
, params
);
593 bool NavigatorImpl::ShouldAssignSiteForURL(const GURL
& url
) {
594 // about:blank should not "use up" a new SiteInstance. The SiteInstance can
595 // still be used for a normal web site.
596 if (url
== GURL(url::kAboutBlankURL
))
599 // The embedder will then have the opportunity to determine if the URL
600 // should "use up" the SiteInstance.
601 return GetContentClient()->browser()->ShouldAssignSiteForURL(url
);
604 void NavigatorImpl::RequestOpenURL(
605 RenderFrameHostImpl
* render_frame_host
,
607 const Referrer
& referrer
,
608 WindowOpenDisposition disposition
,
609 bool should_replace_current_entry
,
611 SiteInstance
* current_site_instance
=
612 GetRenderManager(render_frame_host
)->current_frame_host()->
614 // If this came from a swapped out RenderFrameHost, we only allow the request
615 // if we are still in the same BrowsingInstance.
616 // TODO(creis): Move this to RenderFrameProxyHost::OpenURL.
617 if (render_frame_host
->is_swapped_out() &&
618 !render_frame_host
->GetSiteInstance()->IsRelatedSiteInstance(
619 current_site_instance
)) {
623 // Delegate to RequestTransferURL because this is just the generic
624 // case where |old_request_id| is empty.
625 // TODO(creis): Pass the redirect_chain into this method to support client
626 // redirects. http://crbug.com/311721.
627 std::vector
<GURL
> redirect_chain
;
628 RequestTransferURL(render_frame_host
,
632 ui::PAGE_TRANSITION_LINK
,
635 should_replace_current_entry
,
639 void NavigatorImpl::RequestTransferURL(
640 RenderFrameHostImpl
* render_frame_host
,
642 const std::vector
<GURL
>& redirect_chain
,
643 const Referrer
& referrer
,
644 ui::PageTransition page_transition
,
645 WindowOpenDisposition disposition
,
646 const GlobalRequestID
& transferred_global_request_id
,
647 bool should_replace_current_entry
,
650 SiteInstance
* current_site_instance
=
651 GetRenderManager(render_frame_host
)->current_frame_host()->
653 if (!GetContentClient()->browser()->ShouldAllowOpenURL(
654 current_site_instance
, url
)) {
655 dest_url
= GURL(url::kAboutBlankURL
);
658 int64 frame_tree_node_id
= -1;
659 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
660 switches::kSitePerProcess
)) {
662 render_frame_host
->frame_tree_node()->frame_tree_node_id();
664 OpenURLParams
params(
665 dest_url
, referrer
, frame_tree_node_id
, disposition
, page_transition
,
666 true /* is_renderer_initiated */);
667 if (redirect_chain
.size() > 0)
668 params
.redirect_chain
= redirect_chain
;
669 params
.transferred_global_request_id
= transferred_global_request_id
;
670 params
.should_replace_current_entry
= should_replace_current_entry
;
671 params
.user_gesture
= user_gesture
;
673 if (GetRenderManager(render_frame_host
)->web_ui()) {
674 // Web UI pages sometimes want to override the page transition type for
675 // link clicks (e.g., so the new tab page can specify AUTO_BOOKMARK for
676 // automatically generated suggestions). We don't override other types
677 // like TYPED because they have different implications (e.g., autocomplete).
678 if (ui::PageTransitionCoreTypeIs(
679 params
.transition
, ui::PAGE_TRANSITION_LINK
))
681 GetRenderManager(render_frame_host
)->web_ui()->
682 GetLinkTransitionType();
684 // Note also that we hide the referrer for Web UI pages. We don't really
685 // want web sites to see a referrer of "chrome://blah" (and some
686 // chrome: URLs might have search terms or other stuff we don't want to
687 // send to the site), so we send no referrer.
688 params
.referrer
= Referrer();
690 // Navigations in Web UI pages count as browser-initiated navigations.
691 params
.is_renderer_initiated
= false;
695 delegate_
->RequestOpenURL(render_frame_host
, params
);
699 void NavigatorImpl::OnBeginNavigation(
700 FrameTreeNode
* frame_tree_node
,
701 const FrameHostMsg_BeginNavigation_Params
& params
,
702 const CommonNavigationParams
& common_params
) {
703 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
704 switches::kEnableBrowserSideNavigation
));
705 DCHECK(frame_tree_node
);
707 // TODO(clamy): In case of a renderer initiated navigation create a new
708 // NavigationRequest.
709 NavigationRequest
* navigation_request
=
710 navigation_request_map_
.get(frame_tree_node
->frame_tree_node_id());
711 DCHECK(navigation_request
);
713 // Update the referrer with the one received from the renderer.
714 navigation_request
->common_params().referrer
= common_params
.referrer
;
716 scoped_ptr
<NavigationRequestInfo
> info(new NavigationRequestInfo(params
));
718 info
->first_party_for_cookies
=
719 frame_tree_node
->IsMainFrame()
720 ? navigation_request
->common_params().url
721 : frame_tree_node
->frame_tree()->root()->current_url();
722 info
->is_main_frame
= frame_tree_node
->IsMainFrame();
723 info
->parent_is_main_frame
= !frame_tree_node
->parent() ?
724 false : frame_tree_node
->parent()->IsMainFrame();
726 // TODO(clamy): Inform the RenderFrameHostManager that a navigation is about
727 // to begin, so that it can speculatively spawn a new renderer if needed.
729 navigation_request
->BeginNavigation(info
.Pass(), params
.request_body
);
733 void NavigatorImpl::CommitNavigation(FrameTreeNode
* frame_tree_node
,
734 ResourceResponse
* response
,
735 scoped_ptr
<StreamHandle
> body
) {
736 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
737 switches::kEnableBrowserSideNavigation
));
738 NavigationRequest
* navigation_request
=
739 navigation_request_map_
.get(frame_tree_node
->frame_tree_node_id());
740 DCHECK(navigation_request
);
742 // Select an appropriate renderer to commit the navigation.
743 RenderFrameHostImpl
* render_frame_host
=
744 frame_tree_node
->render_manager()->GetFrameHostForNavigation(
745 navigation_request
->common_params().url
,
746 navigation_request
->common_params().transition
);
747 CheckWebUIRendererDoesNotDisplayNormalURL(
748 render_frame_host
, navigation_request
->common_params().url
);
750 render_frame_host
->CommitNavigation(response
, body
.Pass(),
751 navigation_request
->common_params(),
752 navigation_request
->commit_params());
756 void NavigatorImpl::CancelNavigation(FrameTreeNode
* frame_tree_node
) {
757 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
758 switches::kEnableBrowserSideNavigation
));
759 navigation_request_map_
.erase(frame_tree_node
->frame_tree_node_id());
762 void NavigatorImpl::LogResourceRequestTime(
763 base::TimeTicks timestamp
, const GURL
& url
) {
764 if (navigation_start_time_and_url
.a
.ToInternalValue() != 0
765 && navigation_start_time_and_url
.b
== url
) {
766 base::TimeDelta time_to_network
=
767 timestamp
- navigation_start_time_and_url
.a
;
768 UMA_HISTOGRAM_TIMES("Navigation.TimeToURLJobStart", time_to_network
);
772 void NavigatorImpl::CheckWebUIRendererDoesNotDisplayNormalURL(
773 RenderFrameHostImpl
* render_frame_host
,
775 int enabled_bindings
=
776 render_frame_host
->render_view_host()->GetEnabledBindings();
777 bool is_allowed_in_web_ui_renderer
=
778 WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
779 controller_
->GetBrowserContext(), url
);
780 if ((enabled_bindings
& BINDINGS_POLICY_WEB_UI
) &&
781 !is_allowed_in_web_ui_renderer
) {
782 // Log the URL to help us diagnose any future failures of this CHECK.
783 GetContentClient()->SetActiveURL(url
);
789 bool NavigatorImpl::RequestNavigation(
790 FrameTreeNode
* frame_tree_node
,
791 const NavigationEntryImpl
& entry
,
792 NavigationController::ReloadType reload_type
,
793 base::TimeTicks navigation_start
) {
794 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(
795 switches::kEnableBrowserSideNavigation
));
796 DCHECK(frame_tree_node
);
797 int64 frame_tree_node_id
= frame_tree_node
->frame_tree_node_id();
798 FrameMsg_Navigate_Type::Value navigation_type
=
799 GetNavigationType(controller_
->GetBrowserContext(), entry
, reload_type
);
800 scoped_ptr
<NavigationRequest
> navigation_request(new NavigationRequest(
802 CommonNavigationParams(entry
.GetURL(),
804 entry
.GetTransitionType(),
806 !entry
.IsViewSourceMode()),
807 CommitNavigationParams(entry
.GetPageState(),
808 entry
.GetIsOverridingUserAgent(),
810 RequestNavigationParams
request_params(entry
.GetHasPostData(),
811 entry
.extra_headers(),
812 entry
.GetBrowserInitiatedPostData());
813 // TODO(clamy): Check if navigations are blocked and if so store the
816 // If there is an ongoing request, replace it.
817 navigation_request_map_
.set(frame_tree_node_id
, navigation_request
.Pass());
819 if (frame_tree_node
->current_frame_host()->IsRenderFrameLive()) {
820 // TODO(clamy): send a RequestNavigation IPC.
825 // The navigation request is sent directly to the IO thread.
828 MakeDefaultBeginNavigation(request_params
, navigation_type
),
829 navigation_request_map_
.get(frame_tree_node_id
)->common_params());
833 } // namespace content