chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / content / browser / frame_host / navigation_request.cc
blob8d9a20660ddd3fdd02e2eb963b7b5104524c4144
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"
22 namespace content {
24 namespace {
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;
34 break;
35 case FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE:
36 load_flags |= net::LOAD_BYPASS_CACHE;
37 break;
38 case FrameMsg_Navigate_Type::RESTORE:
39 load_flags |= net::LOAD_PREFERRING_CACHE;
40 break;
41 case FrameMsg_Navigate_Type::RESTORE_WITH_POST:
42 load_flags |= net::LOAD_ONLY_FROM_CACHE;
43 break;
44 case FrameMsg_Navigate_Type::NORMAL:
45 default:
46 break;
48 return load_flags;
51 } // namespace
53 // static
54 bool NavigationRequest::ShouldMakeNetworkRequest(const GURL& url) {
55 // Data and Javascript urls should not make network requests.
56 // TODO(clamy): same document navigations should not make network requests.
57 return !url.SchemeIs(url::kDataScheme) && url != GURL(url::kAboutBlankURL) &&
58 !url.SchemeIs(url::kJavaScriptScheme);
61 // static
62 scoped_ptr<NavigationRequest> NavigationRequest::CreateBrowserInitiated(
63 FrameTreeNode* frame_tree_node,
64 const NavigationEntryImpl& entry,
65 FrameMsg_Navigate_Type::Value navigation_type,
66 base::TimeTicks navigation_start,
67 NavigationControllerImpl* controller) {
68 std::string method = entry.GetHasPostData() ? "POST" : "GET";
70 // Copy existing headers and add necessary headers that may not be present
71 // in the RequestNavigationParams.
72 net::HttpRequestHeaders headers;
73 headers.AddHeadersFromString(entry.extra_headers());
74 headers.SetHeaderIfMissing(net::HttpRequestHeaders::kUserAgent,
75 GetContentClient()->GetUserAgent());
76 // TODO(clamy): match what blink is doing with accept headers.
77 headers.SetHeaderIfMissing("Accept", "*/*");
79 // Fill POST data from the browser in the request body.
80 scoped_refptr<ResourceRequestBody> request_body;
81 if (entry.GetHasPostData()) {
82 request_body = new ResourceRequestBody();
83 request_body->AppendBytes(
84 reinterpret_cast<const char *>(
85 entry.GetBrowserInitiatedPostData()->front()),
86 entry.GetBrowserInitiatedPostData()->size());
89 scoped_ptr<NavigationRequest> navigation_request(new NavigationRequest(
90 frame_tree_node, entry.ConstructCommonNavigationParams(navigation_type),
91 BeginNavigationParams(method, headers.ToString(),
92 LoadFlagFromNavigationType(navigation_type), false),
93 entry.ConstructRequestNavigationParams(
94 navigation_start, controller->HasCommittedRealLoad(frame_tree_node),
95 controller->GetPendingEntryIndex() == -1,
96 controller->GetIndexOfEntry(&entry),
97 controller->GetLastCommittedEntryIndex(),
98 controller->GetEntryCount()),
99 request_body, true, &entry));
100 return navigation_request.Pass();
103 // static
104 scoped_ptr<NavigationRequest> NavigationRequest::CreateRendererInitiated(
105 FrameTreeNode* frame_tree_node,
106 const CommonNavigationParams& common_params,
107 const BeginNavigationParams& begin_params,
108 scoped_refptr<ResourceRequestBody> body,
109 int current_history_list_offset,
110 int current_history_list_length) {
111 // TODO(clamy): Check if some PageState should be provided here.
112 // TODO(clamy): See how we should handle override of the user agent when the
113 // navigation may start in a renderer and commit in another one.
114 // TODO(clamy): See if the navigation start time should be measured in the
115 // renderer and sent to the browser instead of being measured here.
116 // TODO(clamy): The pending history list offset should be properly set.
117 // TODO(clamy): Set has_committed_real_load.
118 RequestNavigationParams request_params;
119 request_params.current_history_list_offset = current_history_list_offset;
120 request_params.current_history_list_length = current_history_list_length;
121 scoped_ptr<NavigationRequest> navigation_request(
122 new NavigationRequest(frame_tree_node, common_params, begin_params,
123 request_params, body, false, nullptr));
124 return navigation_request.Pass();
127 NavigationRequest::NavigationRequest(
128 FrameTreeNode* frame_tree_node,
129 const CommonNavigationParams& common_params,
130 const BeginNavigationParams& begin_params,
131 const RequestNavigationParams& request_params,
132 scoped_refptr<ResourceRequestBody> body,
133 bool browser_initiated,
134 const NavigationEntryImpl* entry)
135 : frame_tree_node_(frame_tree_node),
136 common_params_(common_params),
137 begin_params_(begin_params),
138 request_params_(request_params),
139 browser_initiated_(browser_initiated),
140 state_(NOT_STARTED),
141 restore_type_(NavigationEntryImpl::RESTORE_NONE),
142 is_view_source_(false),
143 bindings_(NavigationEntryImpl::kInvalidBindings) {
144 if (entry) {
145 source_site_instance_ = entry->source_site_instance();
146 dest_site_instance_ = entry->site_instance();
147 restore_type_ = entry->restore_type();
148 is_view_source_ = entry->IsViewSourceMode();
149 bindings_ = entry->bindings();
150 } else {
151 // This is needed to have about:blank and data URLs commit in the same
152 // SiteInstance as the initiating renderer.
153 source_site_instance_ =
154 frame_tree_node->current_frame_host()->GetSiteInstance();
157 const GURL& first_party_for_cookies =
158 frame_tree_node->IsMainFrame()
159 ? common_params.url
160 : frame_tree_node->frame_tree()->root()->current_url();
161 bool parent_is_main_frame = !frame_tree_node->parent() ?
162 false : frame_tree_node->parent()->IsMainFrame();
163 info_.reset(new NavigationRequestInfo(
164 common_params, begin_params, first_party_for_cookies,
165 frame_tree_node->IsMainFrame(), parent_is_main_frame,
166 frame_tree_node->frame_tree_node_id(), body));
169 NavigationRequest::~NavigationRequest() {
172 bool NavigationRequest::BeginNavigation() {
173 DCHECK(!loader_);
174 DCHECK(state_ == NOT_STARTED || state_ == WAITING_FOR_RENDERER_RESPONSE);
175 state_ = STARTED;
177 if (ShouldMakeNetworkRequest(common_params_.url)) {
178 loader_ = NavigationURLLoader::Create(
179 frame_tree_node_->navigator()->GetController()->GetBrowserContext(),
180 frame_tree_node_->frame_tree_node_id(), info_.Pass(), this);
181 return true;
184 // There is no need to make a network request for this navigation, so commit
185 // it immediately.
186 state_ = RESPONSE_STARTED;
187 frame_tree_node_->navigator()->CommitNavigation(
188 frame_tree_node_, nullptr, scoped_ptr<StreamHandle>());
189 return false;
191 // TODO(davidben): Fire (and add as necessary) observer methods such as
192 // DidStartProvisionalLoadForFrame for the navigation.
195 void NavigationRequest::OnRequestRedirected(
196 const net::RedirectInfo& redirect_info,
197 const scoped_refptr<ResourceResponse>& response) {
198 // TODO(davidben): Track other changes from redirects. These are important
199 // for, e.g., reloads.
200 common_params_.url = redirect_info.new_url;
202 // TODO(davidben): This where prerender and navigation_interceptor should be
203 // integrated. For now, just always follow all redirects.
204 loader_->FollowRedirect();
207 void NavigationRequest::OnResponseStarted(
208 const scoped_refptr<ResourceResponse>& response,
209 scoped_ptr<StreamHandle> body) {
210 DCHECK(state_ == STARTED);
211 state_ = RESPONSE_STARTED;
212 frame_tree_node_->navigator()->CommitNavigation(frame_tree_node_,
213 response.get(), body.Pass());
216 void NavigationRequest::OnRequestFailed(bool has_stale_copy_in_cache,
217 int net_error) {
218 DCHECK(state_ == STARTED);
219 state_ = FAILED;
220 frame_tree_node_->navigator()->FailedNavigation(
221 frame_tree_node_, has_stale_copy_in_cache, net_error);
224 void NavigationRequest::OnRequestStarted(base::TimeTicks timestamp) {
225 frame_tree_node_->navigator()->LogResourceRequestTime(timestamp,
226 common_params_.url);
229 } // namespace content