Roll src/third_party/WebKit 3a0ba1e:fcd7003 (svn 191141:191149)
[chromium-blink-merge.git] / content / browser / loader / cross_site_resource_handler.h
blobf0410462996c07df3d703a2e023623c2adecdb19
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 #ifndef CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/browser/loader/layered_resource_handler.h"
11 #include "content/common/content_export.h"
12 #include "net/url_request/url_request_status.h"
14 namespace net {
15 class URLRequest;
18 namespace content {
20 struct TransitionLayerData;
22 // Ensures that responses are delayed for navigations that must be transferred
23 // to a different process. This handler wraps an AsyncEventHandler, and it sits
24 // inside SafeBrowsing and Buffered event handlers. This is important, so that
25 // it can intercept OnResponseStarted after we determine that a response is safe
26 // and not a download.
27 class CrossSiteResourceHandler : public LayeredResourceHandler {
28 public:
29 CrossSiteResourceHandler(scoped_ptr<ResourceHandler> next_handler,
30 net::URLRequest* request);
31 ~CrossSiteResourceHandler() override;
33 // ResourceHandler implementation:
34 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
35 ResourceResponse* response,
36 bool* defer) override;
37 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
38 bool OnReadCompleted(int bytes_read, bool* defer) override;
39 void OnResponseCompleted(const net::URLRequestStatus& status,
40 const std::string& security_info,
41 bool* defer) override;
43 // We can now send the response to the new renderer, which will cause
44 // WebContentsImpl to swap in the new renderer and destroy the old one.
45 void ResumeResponse();
47 // When set to true, requests are leaked when they can't be passed to a
48 // RenderViewHost, for unit tests.
49 CONTENT_EXPORT static void SetLeakRequestsForTesting(
50 bool leak_requests_for_testing);
52 // Navigations are deferred at OnResponseStarted to parse out any navigation
53 // transition link headers, and give the navigation transition (if it exists)
54 // a chance to run.
55 void ResumeResponseDeferredAtStart(int request_id);
57 // Returns whether the handler is deferred.
58 bool did_defer_for_testing() const { return did_defer_; }
60 private:
61 // Prepare to transfer the cross-site response to a new RenderFrameHost, by
62 // asking it to issue an identical request (on the UI thread).
63 void StartCrossSiteTransition(ResourceResponse* response);
65 // Defer the navigation to the UI thread to check whether transfer is required
66 // or not. Currently only used in --site-per-process.
67 bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info,
68 ResourceResponse* response,
69 bool* defer);
71 bool OnNavigationTransitionResponseStarted(
72 ResourceResponse* response,
73 bool* defer,
74 const TransitionLayerData& transition_data);
76 bool OnNormalResponseStarted(ResourceResponse* response,
77 bool* defer);
79 void ResumeOrTransfer(bool is_transfer);
80 void ResumeIfDeferred();
82 // Called when about to defer a request. Sets |did_defer_| and logs the
83 // defferral
84 void OnDidDefer();
86 bool has_started_response_;
87 bool in_cross_site_transition_;
88 bool completed_during_transition_;
89 bool did_defer_;
90 net::URLRequestStatus completed_status_;
91 std::string completed_security_info_;
92 scoped_refptr<ResourceResponse> response_;
94 // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations
95 // are deferred to the UI thread and come back to IO thread via
96 // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI
97 // thread. This can be removed once the code is changed to only do one hop.
98 base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_;
100 DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler);
103 } // namespace content
105 #endif // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_