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

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, 3 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 "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "content/public/common/resource_response.h"
10 #include "net/base/io_buffer.h"
11
12 namespace content {
13
14 InterceptingResourceHandler::InterceptingResourceHandler(
15 std::unique_ptr<ResourceHandler> next_handler,
16 net::URLRequest* request)
17 : LayeredResourceHandler(request, std::move(next_handler)),
18 state_(State::STARTING),
19 first_read_buffer_size_(0) {}
20
21 InterceptingResourceHandler::~InterceptingResourceHandler() {}
22
23 bool InterceptingResourceHandler::OnResponseStarted(ResourceResponse* response,
24 bool* defer) {
25 // If there's no need to switch handlers, just start acting as a blind
26 // pass-through ResourceHandler.
27 if (!new_handler_) {
28 state_ = State::DONE;
29 first_read_buffer_ = nullptr;
30 return next_handler_->OnResponseStarted(response, defer);
31 }
32
33 // Otherwise, switch handlers. First, inform the original ResourceHandler
34 // that this will be handled entirely by the new ResourceHandler.
35 // TODO(clamy): We will probably need to check the return values of these for
36 // PlzNavigate.
37 bool defer_ignored = false;
38 next_handler_->OnResponseStarted(response, &defer_ignored);
39
40 // Although deferring OnResponseStarted is legal, the only downstream handler
41 // which does so is CrossSiteResourceHandler. Cross-site transitions should
42 // not trigger when switching handlers.
43 DCHECK(!defer_ignored);
44
45 // Make a copy of the data in the first read buffer. Despite not having been
46 // informed of any data being stored in first_read_buffer_, the
47 // MimeSniffingResourceHandler has read the data, it's just holding it back.
48 // This data should be passed to the alternate ResourceHandler and not to to
49 // the current ResourceHandler.
50 // TODO(clamy): see if doing the copy should be moved to the
51 // MimeSniffingResourceHandler.
52 if (first_read_buffer_) {
53 first_read_buffer_copy_ = new net::IOBuffer(first_read_buffer_size_);
54 memcpy(first_read_buffer_copy_->data(), first_read_buffer_->data(),
55 first_read_buffer_size_);
56 }
57
58 // Send the payload to the old handler.
59 SendPayloadToOldHandler();
60 first_read_buffer_ = nullptr;
61
62 // The original ResourceHandler is now no longer needed, so replace it with
63 // the new one, before sending the response to the new one.
64 next_handler_ = std::move(new_handler_);
65
66 state_ =
67 first_read_buffer_copy_ ? State::WAITING_FOR_BUFFER_COPY : State::DONE;
68
69 return next_handler_->OnResponseStarted(response, defer);
70 }
71
72 bool InterceptingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
73 int* buf_size,
74 int min_size) {
75 if (state_ == State::DONE)
76 return next_handler_->OnWillRead(buf, buf_size, min_size);
77
78 DCHECK_EQ(State::STARTING, state_);
79 DCHECK_EQ(-1, min_size);
80
81 if (!next_handler_->OnWillRead(buf, buf_size, min_size))
82 return false;
83
84 first_read_buffer_ = *buf;
85 first_read_buffer_size_ = *buf_size;
86 return true;
87 }
88
89 bool InterceptingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
90 if (state_ == State::DONE)
91 return next_handler_->OnReadCompleted(bytes_read, defer);
92
93 DCHECK_EQ(State::WAITING_FOR_BUFFER_COPY, state_);
94 state_ = State::DONE;
95
96 // Copy the data from the first read to the new ResourceHandler.
97 scoped_refptr<net::IOBuffer> buf;
98 int buf_len = 0;
99 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read))
100 return false;
101
102 CHECK(buf_len >= bytes_read);
103 CHECK(bytes_read >= 0);
mmenke 2016/09/02 19:22:05 This is checking input of this method - seems like
clamy 2016/09/06 11:50:48 Moved this one and converted it to a DCHECK.
104 CHECK_GE(first_read_buffer_size_, static_cast<size_t>(bytes_read));
mmenke 2016/09/02 19:22:05 The problem is CHECKs bloat up the binary, so they
clamy 2016/09/06 11:50:48 I'm keeping these two here as they are trying to p
105 memcpy(buf->data(), first_read_buffer_copy_->data(), bytes_read);
106
107 first_read_buffer_copy_ = nullptr;
108
109 // TODO(clamy): Add a unit test to check that the deferral value is properly
110 // passed to the caller.
111 return next_handler_->OnReadCompleted(bytes_read, defer);
112 }
113
114 void InterceptingResourceHandler::UseNewHandler(
115 std::unique_ptr<ResourceHandler> new_handler,
116 const std::string& payload_for_old_handler) {
117 new_handler_ = std::move(new_handler);
118 new_handler_->SetController(controller());
119 payload_for_old_handler_ = payload_for_old_handler;
120 }
121
122 void InterceptingResourceHandler::SendPayloadToOldHandler() {
123 bool defer_ignored = false;
124 if (payload_for_old_handler_.empty()) {
125 // If there is no payload, just finalize the request on the old handler.
126 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
127 net::ERR_ABORTED);
128 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
129 DCHECK(!defer_ignored);
130 return;
131 }
132
133 // Ensure the old ResourceHandler has a buffer that can store the payload.
134 scoped_refptr<net::IOBuffer> buf;
135 int size = 0;
136 if (first_read_buffer_) {
137 // The first read buffer can be reused. The data inside it has been copied
138 // before calling this function, so it can safely be overriden.
139 buf = first_read_buffer_;
140 size = first_read_buffer_size_;
141 }
142
143 // If there is no first read buffer, ask the old ResourceHandler to create a
144 // buffer that can contain payload.
145 if (!buf)
146 next_handler_->OnWillRead(&buf, &size, -1);
147
148 CHECK(buf);
149 CHECK_GE(size, static_cast<int>(payload_for_old_handler_.length()));
mmenke 2016/09/02 19:22:05 If the first CHECK fails, we're going to crash, so
clamy 2016/09/06 11:50:48 Converted it to a DCHECK.
150 memcpy(buf->data(), payload_for_old_handler_.c_str(),
151 payload_for_old_handler_.length());
152 next_handler_->OnReadCompleted(payload_for_old_handler_.length(),
153 &defer_ignored);
154 payload_for_old_handler_ = std::string();
155 DCHECK(!defer_ignored);
156
157 // Finalize the request.
158 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
159 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
160 DCHECK(!defer_ignored);
161 }
162
163 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698