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

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

Issue 2327463002: Relax ad-hoc assumptions on InterceptingResourceHandler (Closed)
Patch Set: fix Created 4 years, 2 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 5 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/loader/layered_resource_handler.h" 14 #include "content/browser/loader/layered_resource_handler.h"
15 #include "content/browser/loader/resource_handler.h" 15 #include "content/browser/loader/resource_handler.h"
16 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
17 #include "content/public/browser/resource_controller.h"
17 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
19 #include "net/url_request/url_request_status.h"
18 20
19 namespace net { 21 namespace net {
20 class URLRequest; 22 class URLRequest;
21 } 23 }
22 24
23 namespace content { 25 namespace content {
24 26
25 // ResourceHandler that initiates special handling of the response if needed, 27 // ResourceHandler that initiates special handling of the response if needed,
26 // based on the response's MIME type (starts downloads, sends data to some 28 // based on the response's MIME type (starts downloads, sends data to some
27 // plugin types via a special channel). 29 // plugin types via a special channel).
30 //
31 // An InterceptingResourceHandler holds two handlers (|next_handler| and
32 // |new_handler|). It assumes the following:
33 // - OnResponseStarted on |next_handler| never sets |*defer|.
28 class CONTENT_EXPORT InterceptingResourceHandler 34 class CONTENT_EXPORT InterceptingResourceHandler
29 : public LayeredResourceHandler { 35 : public LayeredResourceHandler,
36 public ResourceController {
30 public: 37 public:
31 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, 38 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler,
32 net::URLRequest* request); 39 net::URLRequest* request);
33 ~InterceptingResourceHandler() override; 40 ~InterceptingResourceHandler() override;
34 41
35 // ResourceHandler implementation: 42 // ResourceHandler implementation:
43 void SetController(ResourceController* controller) override;
36 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; 44 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
37 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 45 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
38 int* buf_size, 46 int* buf_size,
39 int min_size) override; 47 int min_size) override;
40 bool OnReadCompleted(int bytes_read, bool* defer) override; 48 bool OnReadCompleted(int bytes_read, bool* defer) override;
49 void OnResponseCompleted(const net::URLRequestStatus& status,
50 bool* defer) override;
51
52 // ResourceController implementation:
53 void Cancel() override;
54 void CancelAndIgnore() override;
55 void CancelWithError(int error_code) override;
56 void Resume() override;
41 57
42 // Replaces the next handler with |new_handler|, sending 58 // Replaces the next handler with |new_handler|, sending
43 // |payload_for_old_handler| to the old handler. Must be called after 59 // |payload_for_old_handler| to the old handler. Must be called after
44 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One 60 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One
45 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and 61 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and
46 // OnRequestRedirected methods will not be called. 62 // OnRequestRedirected methods will not be called.
47 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler, 63 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler,
48 const std::string& payload_for_old_handler); 64 const std::string& payload_for_old_handler);
49 65
50 // Used in tests. 66 // Used in tests.
51 ResourceHandler* new_handler_for_testing() const { 67 ResourceHandler* new_handler_for_testing() const {
52 return new_handler_.get(); 68 return new_handler_.get();
53 } 69 }
54 70
55 private: 71 private:
56 enum class State { 72 enum class State {
57 // In this state, the InterceptingResourceHandler is waiting for the mime 73 // The InterceptingResourceHandler is waiting for the mime type of the
58 // type of the response to be identified, to check if the next handler 74 // response to be identified, to check if the next handler should be
59 // should be replaced with an appropriate one. 75 // replaced with an appropriate one.
60 STARTING, 76 STARTING,
61 77
62 // In this state, the InterceptingResourceHandler is waiting to copy the 78 // The InterceptingResourceHandler is sending the payload given via
63 // read buffer to the next handler if it was replaced. This is needed 79 // UseNewHandler to the old handler and waiting for its completion via
64 // because MimeTypeResourceHandler may call OnWillRead before calling 80 // Resume().
65 // OnResponseStarted, that is before the InterceptingResourceHandler 81 SENDING_PAYLOAD_TO_OLD_HANDLER,
66 // replaces the original ResourceHandler of the request. Therefore, the
67 // data read at that time should be copied to the new ResourceHandler.
68 WAITING_FOR_BUFFER_COPY,
69 82
70 // In this state, the InterceptingResourceHandler has replaced its next 83 // The InterceptingResourcHandler is notifying OnResponseCompleted to the
71 // ResourceHandler if needed, and has ensured the buffered read data was 84 // old handler as it is switching the handler. Note that this state is not
72 // properly transmitted to the new ResourceHandler. The 85 // used for InterceptingResourcHandler::OnResponseCompleted.
73 // InterceptingResourceHandler now acts as a pass-through ResourceHandler. 86 NOTIFYING_ON_RESPONSE_COMPLETED_TO_OLD_HANDLER,
74 DONE, 87
88 // The InterceptingResourcHandler is notifying OnResponseStarted to the new
89 // handler and waiting for its completion via Resume(). After the
90 // completion, the InterceptingResourcHandler will transit to
91 // WAITING_FOR_ON_READ_COMPLETED on success.
92 NOTIFYING_ON_RESPONSE_STARTED_TO_NEW_HANDLER,
93
94 // The InterceptingResourcHandler is waiting for OnReadCompleted to be
95 // called.
96 WAITING_FOR_ON_READ_COMPLETED,
97
98 // The InterceptingResourceHandler is sending the old handler's contents to
99 // the new handler and waiting for its completion via Resume().
100 SENDING_BUFFER_TO_NEW_HANDLER,
101
102 // The InterceptingResourceHandler has replaced its next ResourceHandler if
103 // needed, and has ensured the buffered read data was properly transmitted
104 // to the new ResourceHandler. The InterceptingResourceHandler now acts as
105 // a pass-through ResourceHandler.
106 PASS_THROUGH,
107
108 // The InterceptingResourceHandler is notifying OnResponseCompleted to the
109 // new handler. This is used in
110 // InterceptingResourceHandler::OnResponseCompleted. Note that this state is
111 // not related with NOTIFYING_ON_RESPONSE_COMPLETED_TO_OLD_HANDLER.
112 NOTIFYING_ON_RESPONSE_COMPLETED_TO_NEW_HANDLER,
75 }; 113 };
76 114
77 void SendPayloadToOldHandler(); 115 // Runs necessary operations depending on |state_|. Returns false when an
116 // error happens, and set |*defer| to true if the operation continues upon
117 // return.
118 bool DoLoop(bool* defer);
78 119
79 State state_; 120 // The return value and |defer| has the same meaning as DoLoop.
121 bool SendPayloadToOldHandler(bool* defer);
122 bool SendFirstReadBufferToNewHandler(bool* defer);
123 bool SendCompletionToOldHandler(bool* defer);
124
125 State state_ = State::STARTING;
80 126
81 std::unique_ptr<ResourceHandler> new_handler_; 127 std::unique_ptr<ResourceHandler> new_handler_;
82 std::string payload_for_old_handler_; 128 std::string payload_for_old_handler_;
129 size_t payload_bytes_written_ = 0;
83 130
84 // Result of the first read, that may have to be passed to an alternate 131 // Result of the first read, that may have to be passed to an alternate
85 // ResourceHandler instead of the original ResourceHandler. 132 // ResourceHandler instead of the original ResourceHandler.
86 scoped_refptr<net::IOBuffer> first_read_buffer_; 133 scoped_refptr<net::IOBuffer> first_read_buffer_;
87 size_t first_read_buffer_size_; 134 // Instead of |first_read_buffer_|, this handler creates a new IOBuffer with
88 scoped_refptr<net::IOBuffer> first_read_buffer_copy_; 135 // the same size and return it to the client.
136 scoped_refptr<net::IOBuffer> first_read_buffer_double_;
137 size_t first_read_buffer_size_ = 0;
138 size_t first_read_buffer_bytes_read_ = 0;
139 size_t first_read_buffer_bytes_written_ = 0;
140
141 scoped_refptr<ResourceResponse> response_;
142 net::URLRequestStatus completion_status_;
89 143
90 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); 144 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler);
91 }; 145 };
92 146
93 } // namespace content 147 } // namespace content
94 148
95 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 149 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698