Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / loader / cross_site_resource_handler.h
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(int request_id,
33                                    const GURL& new_url,
34                                    ResourceResponse* response,
35                                    bool* defer) OVERRIDE;
36   virtual bool OnResponseStarted(int request_id,
37                                  ResourceResponse* response,
38                                  bool* defer) OVERRIDE;
39   virtual bool OnReadCompleted(int request_id,
40                                int bytes_read,
41                                bool* defer) OVERRIDE;
42   virtual void OnResponseCompleted(int request_id,
43                                    const net::URLRequestStatus& status,
44                                    const std::string& security_info,
45                                    bool* defer) OVERRIDE;
46
47   // We can now send the response to the new renderer, which will cause
48   // WebContentsImpl to swap in the new renderer and destroy the old one.
49   void ResumeResponse();
50
51   // When set to true, requests are leaked when they can't be passed to a
52   // RenderViewHost, for unit tests.
53   CONTENT_EXPORT static void SetLeakRequestsForTesting(
54       bool leak_requests_for_testing);
55
56  private:
57   // Prepare to render the cross-site response in a new RenderViewHost, by
58   // telling the old RenderViewHost to run its onunload handler.
59   void StartCrossSiteTransition(int request_id,
60                                 ResourceResponse* response,
61                                 bool should_transfer);
62
63   // Defer the navigation to the UI thread to check whether transfer is required
64   // or not. Currently only used in --site-per-process.
65   bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info,
66                                      ResourceResponse* response,
67                                      bool* defer);
68
69   void ResumeOrTransfer(bool is_transfer);
70   void ResumeIfDeferred();
71
72   // Called when about to defer a request.  Sets |did_defer_| and logs the
73   // defferral
74   void OnDidDefer();
75
76   bool has_started_response_;
77   bool in_cross_site_transition_;
78   bool completed_during_transition_;
79   bool did_defer_;
80   net::URLRequestStatus completed_status_;
81   std::string completed_security_info_;
82   scoped_refptr<ResourceResponse> response_;
83
84   // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations
85   // are deferred to the UI thread and come back to IO thread via
86   // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI
87   // thread. This can be removed once the code is changed to only do one hop.
88   base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_;
89
90   DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler);
91 };
92
93 }  // namespace content
94
95 #endif  // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_