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. 4 5 #ifndef CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_ 7 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" 13 14 namespace net { 15 class URLRequest; 16 } 17 18 namespace content { 19 20 struct TransitionLayerData; 21 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 virtual ~CrossSiteResourceHandler(); 32 33 // ResourceHandler implementation: 34 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, 35 ResourceResponse* response, 36 bool* defer) OVERRIDE; 37 virtual bool OnResponseStarted(ResourceResponse* response, 38 bool* defer) OVERRIDE; 39 virtual bool OnReadCompleted(int bytes_read, 40 bool* defer) OVERRIDE; 41 virtual void OnResponseCompleted(const net::URLRequestStatus& status, 42 const std::string& security_info, 43 bool* defer) OVERRIDE; 44 45 // We can now send the response to the new renderer, which will cause 46 // WebContentsImpl to swap in the new renderer and destroy the old one. 47 void ResumeResponse(); 48 49 // When set to true, requests are leaked when they can't be passed to a 50 // RenderViewHost, for unit tests. 51 CONTENT_EXPORT static void SetLeakRequestsForTesting( 52 bool leak_requests_for_testing); 53 54 // Navigations are deferred at OnResponseStarted to parse out any navigation 55 // transition link headers, and give the navigation transition (if it exists) 56 // a chance to run. 57 void ResumeResponseDeferredAtStart(int request_id); 58 59 // Returns whether the handler is deferred. 60 bool did_defer_for_testing() const { return did_defer_; } 61 62 private: 63 // Prepare to transfer the cross-site response to a new RenderFrameHost, by 64 // asking it to issue an identical request (on the UI thread). 65 void StartCrossSiteTransition(ResourceResponse* response); 66 67 // Defer the navigation to the UI thread to check whether transfer is required 68 // or not. Currently only used in --site-per-process. 69 bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info, 70 ResourceResponse* response, 71 bool* defer); 72 73 bool OnNavigationTransitionResponseStarted( 74 ResourceResponse* response, 75 bool* defer, 76 const TransitionLayerData& transition_data); 77 78 bool OnNormalResponseStarted(ResourceResponse* response, 79 bool* defer); 80 81 void ResumeOrTransfer(bool is_transfer); 82 void ResumeIfDeferred(); 83 84 // Called when about to defer a request. Sets |did_defer_| and logs the 85 // defferral 86 void OnDidDefer(); 87 88 bool has_started_response_; 89 bool in_cross_site_transition_; 90 bool completed_during_transition_; 91 bool did_defer_; 92 net::URLRequestStatus completed_status_; 93 std::string completed_security_info_; 94 scoped_refptr<ResourceResponse> response_; 95 96 // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations 97 // are deferred to the UI thread and come back to IO thread via 98 // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI 99 // thread. This can be removed once the code is changed to only do one hop. 100 base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_; 101 102 DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler); 103 }; 104 105 } // namespace content 106 107 #endif // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_ 108