1 // Copyright 2014 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/navigation_request.h"
7 #include "content/browser/frame_host/frame_tree.h"
8 #include "content/browser/frame_host/frame_tree_node.h"
9 #include "content/browser/frame_host/navigation_controller_impl.h"
10 #include "content/browser/frame_host/navigation_request_info.h"
11 #include "content/browser/frame_host/navigator.h"
12 #include "content/browser/loader/navigation_url_loader.h"
13 #include "content/browser/site_instance_impl.h"
14 #include "content/common/resource_request_body.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/stream_handle.h"
17 #include "content/public/common/content_client.h"
18 #include "net/base/load_flags.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/url_request/redirect_info.h"
26 // Returns the net load flags to use based on the navigation type.
27 // TODO(clamy): unify the code with what is happening on the renderer side.
28 int LoadFlagFromNavigationType(FrameMsg_Navigate_Type::Value navigation_type
) {
29 int load_flags
= net::LOAD_NORMAL
;
30 switch (navigation_type
) {
31 case FrameMsg_Navigate_Type::RELOAD
:
32 case FrameMsg_Navigate_Type::RELOAD_ORIGINAL_REQUEST_URL
:
33 load_flags
|= net::LOAD_VALIDATE_CACHE
;
35 case FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE
:
36 load_flags
|= net::LOAD_BYPASS_CACHE
;
38 case FrameMsg_Navigate_Type::RESTORE
:
39 load_flags
|= net::LOAD_PREFERRING_CACHE
;
41 case FrameMsg_Navigate_Type::RESTORE_WITH_POST
:
42 load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
44 case FrameMsg_Navigate_Type::NORMAL
:
54 bool NavigationRequest::ShouldMakeNetworkRequest(const GURL
& url
) {
55 // Data urls should not make network requests.
56 // TODO(clamy): same document navigations should not make network requests.
57 return !url
.SchemeIs(url::kDataScheme
);
61 scoped_ptr
<NavigationRequest
> NavigationRequest::CreateBrowserInitiated(
62 FrameTreeNode
* frame_tree_node
,
63 const NavigationEntryImpl
& entry
,
64 FrameMsg_Navigate_Type::Value navigation_type
,
65 base::TimeTicks navigation_start
,
66 NavigationControllerImpl
* controller
) {
67 std::string method
= entry
.GetHasPostData() ? "POST" : "GET";
69 // Copy existing headers and add necessary headers that may not be present
70 // in the RequestNavigationParams.
71 net::HttpRequestHeaders headers
;
72 headers
.AddHeadersFromString(entry
.extra_headers());
73 headers
.SetHeaderIfMissing(net::HttpRequestHeaders::kUserAgent
,
74 GetContentClient()->GetUserAgent());
75 // TODO(clamy): match what blink is doing with accept headers.
76 headers
.SetHeaderIfMissing("Accept", "*/*");
78 // Fill POST data from the browser in the request body.
79 scoped_refptr
<ResourceRequestBody
> request_body
;
80 if (entry
.GetHasPostData()) {
81 request_body
= new ResourceRequestBody();
82 request_body
->AppendBytes(
83 reinterpret_cast<const char *>(
84 entry
.GetBrowserInitiatedPostData()->front()),
85 entry
.GetBrowserInitiatedPostData()->size());
88 scoped_ptr
<NavigationRequest
> navigation_request(new NavigationRequest(
89 frame_tree_node
, entry
.ConstructCommonNavigationParams(navigation_type
),
90 BeginNavigationParams(method
, headers
.ToString(),
91 LoadFlagFromNavigationType(navigation_type
), false),
92 entry
.ConstructRequestNavigationParams(
94 controller
->GetPendingEntryIndex() == -1,
95 controller
->GetIndexOfEntry(&entry
),
96 controller
->GetLastCommittedEntryIndex(),
97 controller
->GetEntryCount()),
98 request_body
, true, &entry
));
99 return navigation_request
.Pass();
103 scoped_ptr
<NavigationRequest
> NavigationRequest::CreateRendererInitiated(
104 FrameTreeNode
* frame_tree_node
,
105 const CommonNavigationParams
& common_params
,
106 const BeginNavigationParams
& begin_params
,
107 scoped_refptr
<ResourceRequestBody
> body
,
108 int current_history_list_offset
,
109 int current_history_list_length
) {
110 // TODO(clamy): Check if some PageState should be provided here.
111 // TODO(clamy): See how we should handle override of the user agent when the
112 // navigation may start in a renderer and commit in another one.
113 // TODO(clamy): See if the navigation start time should be measured in the
114 // renderer and sent to the browser instead of being measured here.
115 // TODO(clamy): The pending history list offset should be properly set.
116 RequestNavigationParams request_params
;
117 request_params
.current_history_list_offset
= current_history_list_offset
;
118 request_params
.current_history_list_length
= current_history_list_length
;
119 scoped_ptr
<NavigationRequest
> navigation_request(
120 new NavigationRequest(frame_tree_node
, common_params
, begin_params
,
121 request_params
, body
, false, nullptr));
122 return navigation_request
.Pass();
125 NavigationRequest::NavigationRequest(
126 FrameTreeNode
* frame_tree_node
,
127 const CommonNavigationParams
& common_params
,
128 const BeginNavigationParams
& begin_params
,
129 const RequestNavigationParams
& request_params
,
130 scoped_refptr
<ResourceRequestBody
> body
,
131 bool browser_initiated
,
132 const NavigationEntryImpl
* entry
)
133 : frame_tree_node_(frame_tree_node
),
134 common_params_(common_params
),
135 begin_params_(begin_params
),
136 request_params_(request_params
),
137 browser_initiated_(browser_initiated
),
139 restore_type_(NavigationEntryImpl::RESTORE_NONE
),
140 is_view_source_(false),
141 bindings_(NavigationEntryImpl::kInvalidBindings
) {
143 source_site_instance_
= entry
->source_site_instance();
144 dest_site_instance_
= entry
->site_instance();
145 restore_type_
= entry
->restore_type();
146 is_view_source_
= entry
->IsViewSourceMode();
147 bindings_
= entry
->bindings();
150 const GURL
& first_party_for_cookies
=
151 frame_tree_node
->IsMainFrame()
153 : frame_tree_node
->frame_tree()->root()->current_url();
154 bool parent_is_main_frame
= !frame_tree_node
->parent() ?
155 false : frame_tree_node
->parent()->IsMainFrame();
156 info_
.reset(new NavigationRequestInfo(
157 common_params
, begin_params
, first_party_for_cookies
,
158 frame_tree_node
->IsMainFrame(), parent_is_main_frame
, body
));
161 NavigationRequest::~NavigationRequest() {
164 bool NavigationRequest::BeginNavigation() {
166 DCHECK(state_
== NOT_STARTED
|| state_
== WAITING_FOR_RENDERER_RESPONSE
);
169 if (ShouldMakeNetworkRequest(common_params_
.url
)) {
170 loader_
= NavigationURLLoader::Create(
171 frame_tree_node_
->navigator()->GetController()->GetBrowserContext(),
172 frame_tree_node_
->frame_tree_node_id(), info_
.Pass(), this);
176 // There is no need to make a network request for this navigation, so commit
178 state_
= RESPONSE_STARTED
;
179 frame_tree_node_
->navigator()->CommitNavigation(
180 frame_tree_node_
, nullptr, scoped_ptr
<StreamHandle
>());
183 // TODO(davidben): Fire (and add as necessary) observer methods such as
184 // DidStartProvisionalLoadForFrame for the navigation.
187 void NavigationRequest::OnRequestRedirected(
188 const net::RedirectInfo
& redirect_info
,
189 const scoped_refptr
<ResourceResponse
>& response
) {
190 // TODO(davidben): Track other changes from redirects. These are important
191 // for, e.g., reloads.
192 common_params_
.url
= redirect_info
.new_url
;
194 // TODO(davidben): This where prerender and navigation_interceptor should be
195 // integrated. For now, just always follow all redirects.
196 loader_
->FollowRedirect();
199 void NavigationRequest::OnResponseStarted(
200 const scoped_refptr
<ResourceResponse
>& response
,
201 scoped_ptr
<StreamHandle
> body
) {
202 DCHECK(state_
== STARTED
);
203 state_
= RESPONSE_STARTED
;
204 frame_tree_node_
->navigator()->CommitNavigation(frame_tree_node_
,
205 response
.get(), body
.Pass());
208 void NavigationRequest::OnRequestFailed(bool has_stale_copy_in_cache
,
210 DCHECK(state_
== STARTED
);
212 frame_tree_node_
->navigator()->FailedNavigation(
213 frame_tree_node_
, has_stale_copy_in_cache
, net_error
);
216 void NavigationRequest::OnRequestStarted(base::TimeTicks timestamp
) {
217 frame_tree_node_
->navigator()->LogResourceRequestTime(timestamp
,
221 } // namespace content