summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/cross_site_resource_handler.h
blob: 3c38fc758d465ab9f2a4ac51a7a5753ec69e8ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_
#define CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/loader/layered_resource_handler.h"
#include "content/common/content_export.h"
#include "net/url_request/url_request_status.h"

namespace net {
class URLRequest;
}

namespace content {

struct TransitionLayerData;

// Ensures that responses are delayed for navigations that must be transferred
// to a different process.  This handler wraps an AsyncEventHandler, and it sits
// inside SafeBrowsing and Buffered event handlers.  This is important, so that
// it can intercept OnResponseStarted after we determine that a response is safe
// and not a download.
class CrossSiteResourceHandler : public LayeredResourceHandler {
 public:
  enum class NavigationDecision {
    TRANSFER_REQUIRED,
    USE_EXISTING_RENDERER,
    CANCEL_REQUEST
  };

  CrossSiteResourceHandler(scoped_ptr<ResourceHandler> next_handler,
                           net::URLRequest* request);
  ~CrossSiteResourceHandler() override;

  // ResourceHandler implementation:
  bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
                           ResourceResponse* response,
                           bool* defer) override;
  bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
  bool OnReadCompleted(int bytes_read, bool* defer) override;
  void OnResponseCompleted(const net::URLRequestStatus& status,
                           const std::string& security_info,
                           bool* defer) override;

  // We can now send the response to the new renderer, which will cause
  // WebContentsImpl to swap in the new renderer and destroy the old one.
  void ResumeResponse();

  // When set to true, requests are leaked when they can't be passed to a
  // RenderViewHost, for unit tests.
  CONTENT_EXPORT static void SetLeakRequestsForTesting(
      bool leak_requests_for_testing);

 private:
  // Prepare to transfer the cross-site response to a new RenderFrameHost, by
  // asking it to issue an identical request (on the UI thread).
  void StartCrossSiteTransition(ResourceResponse* response);

  // Defer the navigation to the UI thread to check whether transfer is required
  // or not. Currently only used in --site-per-process.
  bool DeferForNavigationPolicyCheck(ResourceRequestInfoImpl* info,
                                     ResourceResponse* response,
                                     bool* defer);

  bool OnNormalResponseStarted(ResourceResponse* response,
                               bool* defer);

  void ResumeOrTransfer(NavigationDecision decision);
  void ResumeIfDeferred();

  // Called when about to defer a request.  Sets |did_defer_| and logs the
  // defferral
  void OnDidDefer();

  bool has_started_response_;
  bool in_cross_site_transition_;
  bool completed_during_transition_;
  bool did_defer_;
  net::URLRequestStatus completed_status_;
  std::string completed_security_info_;
  scoped_refptr<ResourceResponse> response_;

  // TODO(nasko): WeakPtr is needed in --site-per-process, since all navigations
  // are deferred to the UI thread and come back to IO thread via
  // PostTaskAndReplyWithResult. If a transfer is needed, it goes back to the UI
  // thread. This can be removed once the code is changed to only do one hop.
  base::WeakPtrFactory<CrossSiteResourceHandler> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(CrossSiteResourceHandler);
};

}  // namespace content

#endif  // CONTENT_BROWSER_LOADER_CROSS_SITE_RESOURCE_HANDLER_H_