OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #include "content/browser/loader/intercepting_resource_handler.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "content/public/common/resource_response.h" | |
12 #include "net/base/io_buffer.h" | |
13 | |
14 namespace content { | |
15 | |
16 InterceptingResourceHandler::InterceptingResourceHandler( | |
17 std::unique_ptr<ResourceHandler> next_handler, | |
18 net::URLRequest* request) | |
19 : LayeredResourceHandler(request, std::move(next_handler)), | |
20 state_(State::STARTING) {} | |
21 | |
22 InterceptingResourceHandler::~InterceptingResourceHandler() {} | |
23 | |
24 void InterceptingResourceHandler::UseNewHandler( | |
25 std::unique_ptr<ResourceHandler> new_handler, | |
26 const std::string& payload_for_old_handler) { | |
27 new_handler_ = std::move(new_handler); | |
28 new_handler_->SetController(controller()); | |
29 payload_for_old_handler_ = payload_for_old_handler; | |
30 } | |
31 | |
32 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response, | |
33 bool* defer) { | |
34 // If there's no need to switch handlers, just start acting as a blind | |
35 // pass-through ResourceHandler. | |
36 if (!new_handler_) { | |
37 state_ = State::DONE; | |
38 return next_handler_->OnResponseStarted(response, defer); | |
39 } | |
40 | |
41 // Otherwise, switch handlers. First, inform the original ResourceHandler | |
42 // that this will be handled entirely by the new ResourceHandler. | |
43 // TODO(clamy): We will probably need to check the return values of these for | |
44 // PlzNavigate. | |
45 bool defer_ignored = false; | |
46 next_handler_->OnResponseStarted(response, &defer_ignored); | |
47 // Although deferring OnResponseStarted is legal, the only downstream handler | |
48 // which does so is CrossSiteResourceHandler. Cross-site transitions should | |
49 // not trigger when switching handlers. | |
50 DCHECK(!defer_ignored); | |
51 | |
52 // Send the payload to the old handler. | |
53 SendPayloadToOldHandler(); | |
54 | |
55 // This is now handled entirely within the new ResourceHandler, so just reset | |
56 // the original ResourceHandler. | |
57 next_handler_ = std::move(new_handler_); | |
58 | |
59 state_ = read_buffer_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE; | |
60 | |
61 return next_handler_->OnResponseStarted(response, defer); | |
62 } | |
63 | |
64 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
65 int* buf_size, | |
66 int min_size) { | |
67 if (state_ == State::DONE) | |
68 return next_handler_->OnWillRead(buf, buf_size, min_size); | |
69 | |
70 DCHECK_EQ(State::STARTING, state_); | |
71 DCHECK_EQ(-1, min_size); | |
72 | |
73 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) | |
74 return false; | |
75 | |
76 read_buffer_ = *buf; | |
77 return true; | |
78 } | |
79 | |
80 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
81 if (state_ == State::DONE) | |
82 return next_handler_->OnReadCompleted(bytes_read, defer); | |
83 | |
84 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_); | |
85 state_ = State::DONE; | |
86 | |
87 // Copy the data in the read buffer to the new ResourceHandler. | |
88 DCHECK(read_buffer_.get()); | |
89 scoped_refptr<net::IOBuffer> buf; | |
90 int buf_len = 0; | |
91 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read)) | |
92 return false; | |
93 | |
94 CHECK((buf_len >= bytes_read) && (bytes_read >= 0)); | |
95 memcpy(buf->data(), read_buffer_->data(), bytes_read); | |
96 read_buffer_ = nullptr; | |
97 | |
98 return next_handler_->OnReadCompleted(bytes_read, defer); | |
99 } | |
100 | |
101 void InterceptingResourceHandler::SendPayloadToOldHandler() { | |
102 bool defer_ignored = false; | |
103 if (payload_for_old_handler_.empty()) { | |
104 // If there is no payload, just finalize the request on the old handler. | |
105 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, | |
106 net::ERR_ABORTED); | |
107 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
108 DCHECK(!defer_ignored); | |
109 return; | |
110 } | |
111 | |
112 // Pass the payload to the old handler. The IOBuffer which is created in | |
113 // OnWillRead can contain data that needs to be passed to the new handler, so | |
114 // it cannot be used to store the payload for the old handler. Instead, | |
115 // complete the read operation that may have created the IOBuffer. The old | |
116 // handler will release it, but this InterceptingResourceHandler keeps a | |
117 // pointer to the IOBuffer, so it won't actually be reset. This will allow | |
118 // the old handler to create a new buffer to store the payload. | |
mmenke
2016/07/19 17:43:45
This seems like a problem. There doesn't seem to
clamy
2016/07/25 16:58:25
I'm now making a copy of the buffer before sending
| |
119 if (read_buffer_) { | |
120 next_handler_->OnReadCompleted(0, &defer_ignored); | |
121 DCHECK(!defer_ignored); | |
122 } | |
123 | |
124 // Get a new buffer and use it to send |payload_for_old_handler_| to the | |
125 // original ResourceHandler. | |
126 scoped_refptr<net::IOBuffer> buf; | |
127 int size = 0; | |
128 next_handler_->OnWillRead(&buf, &size, -1); | |
129 CHECK_GE(size, static_cast<int>(payload_for_old_handler_.length())); | |
130 memcpy(buf->data(), payload_for_old_handler_.c_str(), | |
131 payload_for_old_handler_.length()); | |
132 next_handler_->OnReadCompleted(payload_for_old_handler_.length(), | |
133 &defer_ignored); | |
134 DCHECK(!defer_ignored); | |
135 | |
136 // Finalize the request. | |
137 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | |
138 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
139 DCHECK(!defer_ignored); | |
140 } | |
141 | |
142 } // namespace content | |
OLD | NEW |