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 // Ensures that cross-site responses are delayed until the onunload handler of 21 // the previous page is allowed to run. This handler wraps an 22 // AsyncEventHandler, and it sits inside SafeBrowsing and Buffered event 23 // handlers. This is important, so that it can intercept OnResponseStarted 24 // after we determine that a response is safe and not a download. 25 class CrossSiteResourceHandler : public LayeredResourceHandler { 26 public: 27 CrossSiteResourceHandler(scoped_ptr<ResourceHandler> next_handler, 28 net::URLRequest* request); 29 virtual ~CrossSiteResourceHandler(); 30 31 // ResourceHandler implementation: 32 virtual bool OnRequestRedirected(const GURL& new_url, 33 ResourceResponse* response, 34 bool* defer) OVERRIDE; 35 virtual bool OnResponseStarted(ResourceResponse* response, 36 bool* defer) OVERRIDE; 37 virtual bool OnReadCompleted(int bytes_read, 38 bool* defer) OVERRIDE; 39 virtual void OnResponseCompleted(const net::URLRequestStatus& status, 40 const std::string& security_info, 41 bool* defer) OVERRIDE; 42 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(); 46 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); 51 52 private: 53 // Prepare to render the cross-site response in a new RenderViewHost, by 54 // telling the old RenderViewHost to run its onunload handler. 55 void StartCrossSiteTransition(ResourceResponse* response, 56 bool should_transfer); 57 58 // Defer the navigation to the UI thread to check whether transfer is required 59 // or not. Currently only used in --site-per-process. 60 bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info, 61 ResourceResponse* response, 62 bool* defer); 63 64 void ResumeOrTransfer(bool is_transfer); 65 void ResumeIfDeferred(); 66 67 // Called when about to defer a request. Sets |did_defer_| and logs the 68 // defferral 69 void OnDidDefer(); 70 71 bool has_started_response_; 72 bool in_cross_site_transition_; 73 bool completed_during_transition_; 74 bool did_defer_; 75 net::URLRequestStatus completed_status_; 76 std::string completed_security_info_; 77 scoped_refptr<ResourceResponse> response_; 78 79 // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations 80 // are deferred to the UI thread and come back to IO thread via 81 // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI 82 // thread. This can be removed once the code is changed to only do one hop. 83 base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_; 84 85 DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler); 86 }; 87 88 } // namespace content 89 90 #endif // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_ 91