Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / loader / cross_site_resource_handler.cc
blobea69b318796c28e90b3956cbc71143e53282e0df
1 // Copyright (c) 2012 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/loader/cross_site_resource_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "content/browser/appcache/appcache_interceptor.h"
13 #include "content/browser/child_process_security_policy_impl.h"
14 #include "content/browser/frame_host/cross_site_transferring_request.h"
15 #include "content/browser/frame_host/render_frame_host_impl.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h"
17 #include "content/browser/loader/resource_request_info_impl.h"
18 #include "content/browser/site_instance_impl.h"
19 #include "content/browser/web_contents/web_contents_impl.h"
20 #include "content/common/site_isolation_policy.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/global_request_id.h"
24 #include "content/public/browser/resource_controller.h"
25 #include "content/public/browser/site_instance.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/resource_response.h"
28 #include "content/public/common/url_constants.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/url_request/url_request.h"
32 namespace content {
34 namespace {
36 bool leak_requests_for_testing_ = false;
38 // The parameters to OnCrossSiteResponseHelper exceed the number of arguments
39 // base::Bind supports.
40 struct CrossSiteResponseParams {
41 CrossSiteResponseParams(
42 int render_frame_id,
43 const GlobalRequestID& global_request_id,
44 const std::vector<GURL>& transfer_url_chain,
45 const Referrer& referrer,
46 ui::PageTransition page_transition,
47 bool should_replace_current_entry)
48 : render_frame_id(render_frame_id),
49 global_request_id(global_request_id),
50 transfer_url_chain(transfer_url_chain),
51 referrer(referrer),
52 page_transition(page_transition),
53 should_replace_current_entry(should_replace_current_entry) {
56 int render_frame_id;
57 GlobalRequestID global_request_id;
58 std::vector<GURL> transfer_url_chain;
59 Referrer referrer;
60 ui::PageTransition page_transition;
61 bool should_replace_current_entry;
64 void OnCrossSiteResponseHelper(const CrossSiteResponseParams& params) {
65 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request(
66 new CrossSiteTransferringRequest(params.global_request_id));
68 RenderFrameHostImpl* rfh =
69 RenderFrameHostImpl::FromID(params.global_request_id.child_id,
70 params.render_frame_id);
71 if (rfh) {
72 if (rfh->GetParent()) {
73 // We should only swap processes for subframes in --site-per-process mode.
74 // CrossSiteResourceHandler is not installed on subframe requests in
75 // default Chrome.
76 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
78 rfh->OnCrossSiteResponse(
79 params.global_request_id, cross_site_transferring_request.Pass(),
80 params.transfer_url_chain, params.referrer,
81 params.page_transition, params.should_replace_current_entry);
82 } else if (leak_requests_for_testing_ && cross_site_transferring_request) {
83 // Some unit tests expect requests to be leaked in this case, so they can
84 // pass them along manually.
85 cross_site_transferring_request->ReleaseRequest();
89 // Returns whether a transfer is needed by doing a check on the UI thread.
90 bool CheckNavigationPolicyOnUI(GURL url, int process_id, int render_frame_id) {
91 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
92 RenderFrameHostImpl* rfh =
93 RenderFrameHostImpl::FromID(process_id, render_frame_id);
94 if (!rfh)
95 return false;
97 // A transfer is not needed if the current SiteInstance doesn't yet have a
98 // site. This is the case for tests that use NavigateToURL.
99 if (!rfh->GetSiteInstance()->HasSite())
100 return false;
102 // For now, GuestViews never transfer on cross-site navigations.
103 WebContentsImpl* web_contents =
104 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(rfh));
105 if (web_contents->GetBrowserPluginGuest())
106 return false;
108 // TODO(nasko, nick): These following --site-per-process checks are
109 // overly simplistic. Update them to match all the cases
110 // considered by RenderFrameHostManager::DetermineSiteInstanceForURL.
111 if (SiteInstance::IsSameWebSite(rfh->GetSiteInstance()->GetBrowserContext(),
112 rfh->GetSiteInstance()->GetSiteURL(), url)) {
113 return false; // The same site, no transition needed.
116 // The sites differ. If either one requires a dedicated process,
117 // then a transfer is needed.
118 return rfh->GetSiteInstance()->RequiresDedicatedProcess() ||
119 SiteIsolationPolicy::DoesSiteRequireDedicatedProcess(url);
122 } // namespace
124 CrossSiteResourceHandler::CrossSiteResourceHandler(
125 scoped_ptr<ResourceHandler> next_handler,
126 net::URLRequest* request)
127 : LayeredResourceHandler(request, next_handler.Pass()),
128 has_started_response_(false),
129 in_cross_site_transition_(false),
130 completed_during_transition_(false),
131 did_defer_(false),
132 weak_ptr_factory_(this) {
135 CrossSiteResourceHandler::~CrossSiteResourceHandler() {
136 // Cleanup back-pointer stored on the request info.
137 GetRequestInfo()->set_cross_site_handler(NULL);
140 bool CrossSiteResourceHandler::OnRequestRedirected(
141 const net::RedirectInfo& redirect_info,
142 ResourceResponse* response,
143 bool* defer) {
144 // We should not have started the transition before being redirected.
145 DCHECK(!in_cross_site_transition_);
146 return next_handler_->OnRequestRedirected(redirect_info, response, defer);
149 bool CrossSiteResourceHandler::OnResponseStarted(
150 ResourceResponse* response,
151 bool* defer) {
152 response_ = response;
153 has_started_response_ = true;
155 // Store this handler on the ExtraRequestInfo, so that RDH can call our
156 // ResumeResponse method when we are ready to resume.
157 ResourceRequestInfoImpl* info = GetRequestInfo();
158 info->set_cross_site_handler(this);
160 return OnNormalResponseStarted(response, defer);
163 bool CrossSiteResourceHandler::OnNormalResponseStarted(
164 ResourceResponse* response,
165 bool* defer) {
166 // At this point, we know that the response is safe to send back to the
167 // renderer: it is not a download, and it has passed the SSL and safe
168 // browsing checks.
169 // We should not have already started the transition before now.
170 DCHECK(!in_cross_site_transition_);
172 ResourceRequestInfoImpl* info = GetRequestInfo();
174 // The content embedder can decide that a transfer to a different process is
175 // required for this URL. If so, pause the response now. Other cross process
176 // navigations can proceed immediately, since we run the unload handler at
177 // commit time. Note that a process swap may no longer be necessary if we
178 // transferred back into the original process due to a redirect.
179 bool definitely_transfer =
180 GetContentClient()->browser()->ShouldSwapProcessesForRedirect(
181 info->GetContext(), request()->original_url(), request()->url());
183 // If this is a download, just pass the response through without doing a
184 // cross-site check. The renderer will see it is a download and abort the
185 // request.
187 // Similarly, HTTP 204 (No Content) responses leave us showing the previous
188 // page. We should allow the navigation to finish without running the unload
189 // handler or swapping in the pending RenderFrameHost.
191 // In both cases, any pending RenderFrameHost (if one was created for this
192 // navigation) will stick around until the next cross-site navigation, since
193 // we are unable to tell when to destroy it.
194 // See RenderFrameHostManager::RendererAbortedProvisionalLoad.
196 // TODO(davidben): Unify IsDownload() and is_stream(). Several places need to
197 // check for both and remembering about streams is error-prone.
198 if (info->IsDownload() || info->is_stream() ||
199 (response->head.headers.get() &&
200 response->head.headers->response_code() == 204)) {
201 return next_handler_->OnResponseStarted(response, defer);
204 if (definitely_transfer) {
205 // Now that we know a transfer is needed and we have something to commit, we
206 // pause to let the UI thread set up the transfer.
207 StartCrossSiteTransition(response);
209 // Defer loading until after the new renderer process has issued a
210 // corresponding request.
211 *defer = true;
212 OnDidDefer();
213 return true;
216 // In the site-per-process model, we may also decide (independently from the
217 // content embedder's ShouldSwapProcessesForRedirect decision above) that a
218 // process transfer is needed. For that we need to consult the navigation
219 // policy on the UI thread, so pause the response. Process transfers are
220 // skipped for WebUI processes for now, since e.g. chrome://settings has
221 // multiple "cross-site" chrome:// frames, and that doesn't yet work cross-
222 // process.
223 if (SiteIsolationPolicy::AreCrossProcessFramesPossible() &&
224 !ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
225 info->GetChildID())) {
226 return DeferForNavigationPolicyCheck(info, response, defer);
229 // No deferral needed. Pass the response through.
230 return next_handler_->OnResponseStarted(response, defer);
233 void CrossSiteResourceHandler::ResumeOrTransfer(bool is_transfer) {
234 if (is_transfer) {
235 StartCrossSiteTransition(response_.get());
236 } else {
237 ResumeResponse();
241 bool CrossSiteResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
242 CHECK(!in_cross_site_transition_);
243 return next_handler_->OnReadCompleted(bytes_read, defer);
246 void CrossSiteResourceHandler::OnResponseCompleted(
247 const net::URLRequestStatus& status,
248 const std::string& security_info,
249 bool* defer) {
250 if (!in_cross_site_transition_) {
251 // If we're not transferring, then we should pass this through.
252 next_handler_->OnResponseCompleted(status, security_info, defer);
253 return;
256 // We have to buffer the call until after the transition completes.
257 completed_during_transition_ = true;
258 completed_status_ = status;
259 completed_security_info_ = security_info;
261 // Defer to tell RDH not to notify the world or clean up the pending request.
262 // We will do so in ResumeResponse.
263 *defer = true;
264 OnDidDefer();
267 // We can now send the response to the new renderer, which will cause
268 // WebContentsImpl to swap in the new renderer and destroy the old one.
269 void CrossSiteResourceHandler::ResumeResponse() {
270 TRACE_EVENT_ASYNC_END0(
271 "navigation", "CrossSiteResourceHandler transition", this);
272 DCHECK(request());
273 in_cross_site_transition_ = false;
274 ResourceRequestInfoImpl* info = GetRequestInfo();
276 if (has_started_response_) {
277 // Send OnResponseStarted to the new renderer.
278 DCHECK(response_.get());
279 bool defer = false;
280 if (!next_handler_->OnResponseStarted(response_.get(), &defer)) {
281 controller()->Cancel();
282 } else if (!defer) {
283 // Unpause the request to resume reading. Any further reads will be
284 // directed toward the new renderer.
285 ResumeIfDeferred();
289 // Remove ourselves from the ExtraRequestInfo.
290 info->set_cross_site_handler(NULL);
292 // If the response completed during the transition, notify the next
293 // event handler.
294 if (completed_during_transition_) {
295 bool defer = false;
296 next_handler_->OnResponseCompleted(completed_status_,
297 completed_security_info_,
298 &defer);
299 if (!defer)
300 ResumeIfDeferred();
304 // static
305 void CrossSiteResourceHandler::SetLeakRequestsForTesting(
306 bool leak_requests_for_testing) {
307 leak_requests_for_testing_ = leak_requests_for_testing;
310 // Prepare to transfer the response to a new RenderFrameHost.
311 void CrossSiteResourceHandler::StartCrossSiteTransition(
312 ResourceResponse* response) {
313 TRACE_EVENT_ASYNC_BEGIN0(
314 "navigation", "CrossSiteResourceHandler transition", this);
315 in_cross_site_transition_ = true;
316 response_ = response;
318 // Store this handler on the ExtraRequestInfo, so that RDH can call our
319 // ResumeResponse method when we are ready to resume.
320 ResourceRequestInfoImpl* info = GetRequestInfo();
321 info->set_cross_site_handler(this);
323 GlobalRequestID global_id(info->GetChildID(), info->GetRequestID());
325 // Tell the contents responsible for this request that a cross-site response
326 // is starting, so that it can tell its old renderer to run its onunload
327 // handler now. We will wait until the unload is finished and (if a transfer
328 // is needed) for the new renderer's request to arrive.
329 // The |transfer_url_chain| contains any redirect URLs that have already
330 // occurred, plus the destination URL at the end.
331 std::vector<GURL> transfer_url_chain;
332 Referrer referrer;
333 int render_frame_id = info->GetRenderFrameID();
334 transfer_url_chain = request()->url_chain();
335 referrer = Referrer(GURL(request()->referrer()), info->GetReferrerPolicy());
336 ResourceDispatcherHostImpl::Get()->MarkAsTransferredNavigation(global_id);
338 BrowserThread::PostTask(
339 BrowserThread::UI,
340 FROM_HERE,
341 base::Bind(
342 &OnCrossSiteResponseHelper,
343 CrossSiteResponseParams(render_frame_id,
344 global_id,
345 transfer_url_chain,
346 referrer,
347 info->GetPageTransition(),
348 info->should_replace_current_entry())));
351 bool CrossSiteResourceHandler::DeferForNavigationPolicyCheck(
352 ResourceRequestInfoImpl* info,
353 ResourceResponse* response,
354 bool* defer) {
355 // Store the response_ object internally, since the navigation is deferred
356 // regardless of whether it will be a transfer or not.
357 response_ = response;
359 // Always defer the navigation to the UI thread to make a policy decision.
360 // It will send the result back to the IO thread to either resume or
361 // transfer it to a new renderer.
362 // TODO(nasko): If the UI thread result is that transfer is required, the
363 // IO thread will defer to the UI thread again through
364 // StartCrossSiteTransition. This is unnecessary and the policy check on the
365 // UI thread should be refactored to avoid the extra hop.
366 BrowserThread::PostTaskAndReplyWithResult(
367 BrowserThread::UI,
368 FROM_HERE,
369 base::Bind(&CheckNavigationPolicyOnUI,
370 request()->url(),
371 info->GetChildID(),
372 info->GetRenderFrameID()),
373 base::Bind(&CrossSiteResourceHandler::ResumeOrTransfer,
374 weak_ptr_factory_.GetWeakPtr()));
376 // Defer loading until it is known whether the navigation will transfer
377 // to a new process or continue in the existing one.
378 *defer = true;
379 OnDidDefer();
380 return true;
383 void CrossSiteResourceHandler::ResumeIfDeferred() {
384 if (did_defer_) {
385 request()->LogUnblocked();
386 did_defer_ = false;
387 controller()->Resume();
391 void CrossSiteResourceHandler::OnDidDefer() {
392 did_defer_ = true;
393 request()->LogBlockedBy("CrossSiteResourceHandler");
396 } // namespace content