Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / loader / cross_site_resource_handler.h
blobb73a5a1475b799e0493ea87bd0311e2ff7638f55
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 private:
53 // Prepare to transfer the cross-site response to a new RenderFrameHost, by
54 // asking it to issue an identical request (on the UI thread).
55 void StartCrossSiteTransition(ResourceResponse* response);
57 // Defer the navigation to the UI thread to check whether transfer is required
58 // or not. Currently only used in --site-per-process.
59 bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info,
60 ResourceResponse* response,
61 bool* defer);
63 bool OnNormalResponseStarted(ResourceResponse* response,
64 bool* defer);
66 void ResumeOrTransfer(bool is_transfer);
67 void ResumeIfDeferred();
69 // Called when about to defer a request. Sets |did_defer_| and logs the
70 // defferral
71 void OnDidDefer();
73 bool has_started_response_;
74 bool in_cross_site_transition_;
75 bool completed_during_transition_;
76 bool did_defer_;
77 net::URLRequestStatus completed_status_;
78 std::string completed_security_info_;
79 scoped_refptr<ResourceResponse> response_;
81 // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations
82 // are deferred to the UI thread and come back to IO thread via
83 // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI
84 // thread. This can be removed once the code is changed to only do one hop.
85 base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_;
87 DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler);
90 } // namespace content
92 #endif // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_