Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: content/browser/loader/intercepting_resource_handler.cc

Issue 2005273002: Move MimeTypeResourceHandler before ThrottlingResourceHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 first_read_buffer_size_(0) {}
22
23 InterceptingResourceHandler::~InterceptingResourceHandler() {}
24
25 void InterceptingResourceHandler::UseNewHandler(
26 std::unique_ptr<ResourceHandler> new_handler,
27 const std::string& payload_for_old_handler) {
28 new_handler_ = std::move(new_handler);
29 new_handler_->SetController(controller());
30 payload_for_old_handler_ = payload_for_old_handler;
31 }
32
33 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response,
34 bool* defer) {
35 // If there's no need to switch handlers, just start acting as a blind
36 // pass-through ResourceHandler.
37 if (!new_handler_) {
38 state_ = State::DONE;
39 first_read_buffer_ = nullptr;
40 return next_handler_->OnResponseStarted(response, defer);
41 }
42
43 // Otherwise, switch handlers. First, inform the original ResourceHandler
44 // that this will be handled entirely by the new ResourceHandler.
45 // TODO(clamy): We will probably need to check the return values of these for
46 // PlzNavigate.
47 bool defer_ignored = false;
48 next_handler_->OnResponseStarted(response, &defer_ignored);
49
50 // Although deferring OnResponseStarted is legal, the only downstream handler
51 // which does so is CrossSiteResourceHandler. Cross-site transitions should
52 // not trigger when switching handlers.
53 DCHECK(!defer_ignored);
54
55 // Make a copy of the data in the first read buffer. This data should be
56 // passed to the alternate ResourceHandler and not to to the current
57 // ResourceHandler.
58 if (first_read_buffer_) {
59 first_read_buffer_copy_ = new net::IOBuffer(first_read_buffer_size_);
60 memcpy(first_read_buffer_copy_->data(), first_read_buffer_->data(),
61 first_read_buffer_size_);
mmenke 2016/07/25 17:39:31 Do our memory tools complain if we end up copying
clamy 2016/07/26 16:24:15 Asan bot seems alright with it. To avoid that, the
62 }
63
64 // Send the payload to the old handler.
65 SendPayloadToOldHandler();
66 first_read_buffer_ = nullptr;
67
68 // This is now handled entirely within the new ResourceHandler, so just reset
69 // the original ResourceHandler.
70 next_handler_ = std::move(new_handler_);
71
72 state_ =
73 first_read_buffer_copy_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE;
74
75 return next_handler_->OnResponseStarted(response, defer);
76 }
77
78 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
79 int* buf_size,
80 int min_size) {
81 if (state_ == State::DONE)
82 return next_handler_->OnWillRead(buf, buf_size, min_size);
83
84 DCHECK_EQ(State::STARTING, state_);
85 DCHECK_EQ(-1, min_size);
86
87 if (!next_handler_->OnWillRead(buf, buf_size, min_size))
88 return false;
89
90 first_read_buffer_ = *buf;
91 first_read_buffer_size_ = *buf_size;
92 return true;
93 }
94
95 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
96 if (state_ == State::DONE)
97 return next_handler_->OnReadCompleted(bytes_read, defer);
98
99 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_);
100 state_ = State::DONE;
101
102 // Copy the data from the first read to the new ResourceHandler.
103 scoped_refptr<net::IOBuffer> buf;
104 int buf_len = 0;
105 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read))
106 return false;
107
108 CHECK((buf_len >= bytes_read) && (bytes_read >= 0));
109 CHECK_GE(first_read_buffer_size_, static_cast<size_t>(bytes_read));
110 memcpy(buf->data(), first_read_buffer_copy_->data(), bytes_read);
111
112 first_read_buffer_copy_ = nullptr;
113
114 return next_handler_->OnReadCompleted(bytes_read, defer);
115 }
116
117 void InterceptingResourceHandler::SendPayloadToOldHandler() {
118 bool defer_ignored = false;
119 if (payload_for_old_handler_.empty()) {
120 // If there is no payload, just finalize the request on the old handler.
121 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
122 net::ERR_ABORTED);
123 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
124 DCHECK(!defer_ignored);
125 return;
126 }
127
128 // Ensure the old ResourceHandler has a buffer that can store the payload.
129 scoped_refptr<net::IOBuffer> buf;
130 int size = 0;
131 if (first_read_buffer_ &&
132 first_read_buffer_size_ >= payload_for_old_handler_.length()) {
133 // The first read buffer can be reused. The data inside it has been copied
134 // before calling this function, so it can safely be overriden.
135 buf = first_read_buffer_;
136 size = first_read_buffer_size_;
137 } else if (first_read_buffer_ &&
138 first_read_buffer_size_ < payload_for_old_handler_.length()) {
139 // Simulate a read of size 0 to safely call OnWillRead with the right size
140 // on the old ResourceHandler.
mmenke 2016/07/25 17:39:31 Does this case ever help? first_read_buffer_ was
clamy 2016/07/26 16:24:15 Good point. Removed this case.
141 next_handler_->OnReadCompleted(0, &defer_ignored);
142 DCHECK(!defer_ignored);
143 }
144
145 // If the first read buffer cannot be reused, ask the old ResourceHandler to
146 // create a buffer that can contain payload.
147 if (!buf)
148 next_handler_->OnWillRead(&buf, &size, -1);
149
150 CHECK(buf);
151 CHECK_GE(size, static_cast<int>(payload_for_old_handler_.length()));
152 memcpy(buf->data(), payload_for_old_handler_.c_str(),
153 payload_for_old_handler_.length());
154 next_handler_->OnReadCompleted(payload_for_old_handler_.length(),
155 &defer_ignored);
156 DCHECK(!defer_ignored);
157
158 // Finalize the request.
159 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
160 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
161 DCHECK(!defer_ignored);
162 }
163
164 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698