| OLD | NEW |
| 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 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 : public LayeredResourceHandler { | 38 : public LayeredResourceHandler { |
| 39 public: | 39 public: |
| 40 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, | 40 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
| 41 net::URLRequest* request); | 41 net::URLRequest* request); |
| 42 ~InterceptingResourceHandler() override; | 42 ~InterceptingResourceHandler() override; |
| 43 | 43 |
| 44 // ResourceHandler implementation: | 44 // ResourceHandler implementation: |
| 45 void OnResponseStarted( | 45 void OnResponseStarted( |
| 46 ResourceResponse* response, | 46 ResourceResponse* response, |
| 47 std::unique_ptr<ResourceController> controller) override; | 47 std::unique_ptr<ResourceController> controller) override; |
| 48 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 48 void OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 49 int* buf_size) override; | 49 int* buf_size, |
| 50 std::unique_ptr<ResourceController> controller) override; |
| 50 void OnReadCompleted(int bytes_read, | 51 void OnReadCompleted(int bytes_read, |
| 51 std::unique_ptr<ResourceController> controller) override; | 52 std::unique_ptr<ResourceController> controller) override; |
| 52 void OnResponseCompleted( | 53 void OnResponseCompleted( |
| 53 const net::URLRequestStatus& status, | 54 const net::URLRequestStatus& status, |
| 54 std::unique_ptr<ResourceController> controller) override; | 55 std::unique_ptr<ResourceController> controller) override; |
| 55 | 56 |
| 56 // Replaces the next handler with |new_handler|, sending | 57 // Replaces the next handler with |new_handler|, sending |
| 57 // |payload_for_old_handler| to the old handler. Must be called after | 58 // |payload_for_old_handler| to the old handler. Must be called after |
| 58 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One | 59 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One |
| 59 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and | 60 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and |
| (...skipping 14 matching lines...) Expand all Loading... |
| 74 enum class State { | 75 enum class State { |
| 75 // The InterceptingResourceHandler is waiting for the mime type of the | 76 // The InterceptingResourceHandler is waiting for the mime type of the |
| 76 // response to be identified, to check if the next handler should be | 77 // response to be identified, to check if the next handler should be |
| 77 // replaced with an appropriate one. | 78 // replaced with an appropriate one. |
| 78 STARTING, | 79 STARTING, |
| 79 | 80 |
| 80 // The InterceptingResourceHandler is starting the process of swapping | 81 // The InterceptingResourceHandler is starting the process of swapping |
| 81 // handlers. | 82 // handlers. |
| 82 SWAPPING_HANDLERS, | 83 SWAPPING_HANDLERS, |
| 83 | 84 |
| 85 // States where the InterceptingResourceHandler passes the initial |
| 86 // OnWillRead call to the old handler, and then waits for the resulting |
| 87 // buffer read buffer. |
| 88 SENDING_ON_WILL_READ_TO_OLD_HANDLER, |
| 89 WAITING_FOR_OLD_HANDLERS_BUFFER, |
| 90 |
| 84 // The InterceptingResourceHandler is sending the payload given via | 91 // The InterceptingResourceHandler is sending the payload given via |
| 85 // UseNewHandler to the old handler and waiting for its completion via | 92 // UseNewHandler to the old handler. The first state starts retrieving a |
| 86 // Resume(). | 93 // buffer from the old handler, the second state copies as much of the data |
| 94 // as possible to the received buffer and passes it to the old handler. |
| 87 SENDING_PAYLOAD_TO_OLD_HANDLER, | 95 SENDING_PAYLOAD_TO_OLD_HANDLER, |
| 96 RECEIVING_BUFFER_FROM_OLD_HANDLER, |
| 88 | 97 |
| 89 // The InterceptingResourcHandler is calling the new handler's | 98 // The InterceptingResourcHandler is calling the new handler's |
| 90 // OnResponseStarted method and waiting for its completion via Resume(). | 99 // OnResponseStarted method and waiting for its completion via Resume(). |
| 91 // After completion, the InterceptingResourceHandler will transition to | 100 // After completion, the InterceptingResourceHandler will transition to |
| 92 // SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER on success. | 101 // SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER on success. |
| 93 SENDING_ON_WILL_START_TO_NEW_HANDLER, | 102 SENDING_ON_WILL_START_TO_NEW_HANDLER, |
| 94 | 103 |
| 95 // The InterceptingResourcHandler is calling the new handler's | 104 // The InterceptingResourcHandler is calling the new handler's |
| 96 // OnResponseStarted method and waiting for its completion via Resume(). | 105 // OnResponseStarted method and waiting for its completion via Resume(). |
| 97 // After completion, the InterceptingResourceHandler will transition to | 106 // After completion, the InterceptingResourceHandler will transition to |
| 98 // WAITING_FOR_ON_READ_COMPLETED on success. | 107 // WAITING_FOR_ON_READ_COMPLETED on success. |
| 99 SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER, | 108 SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER, |
| 100 | 109 |
| 101 // The InterceptingResourcHandler is waiting for OnReadCompleted to be | 110 // The InterceptingResourcHandler is waiting for OnReadCompleted to be |
| 102 // called. | 111 // called. |
| 103 WAITING_FOR_ON_READ_COMPLETED, | 112 WAITING_FOR_ON_READ_COMPLETED, |
| 104 | 113 |
| 105 // The InterceptingResourceHandler is sending the old handler's contents to | 114 // The two phases of uploading previously received data stored in |
| 106 // the new handler and waiting for its completion via Resume(). | 115 // |first_read_buffer_double_| to the new handler, which is now stored in |
| 116 // |next_handler_|. The first state gets a buffer to write to, and the next |
| 117 // copies all the data it can to that buffer. |
| 107 SENDING_BUFFER_TO_NEW_HANDLER, | 118 SENDING_BUFFER_TO_NEW_HANDLER, |
| 119 SENDING_BUFFER_TO_NEW_HANDLER_WAITING_FOR_BUFFER, |
| 108 | 120 |
| 109 // The InterceptingResourceHandler has replaced its next ResourceHandler if | 121 // The InterceptingResourceHandler has replaced its next ResourceHandler if |
| 110 // needed, and has ensured the buffered read data was properly transmitted | 122 // needed, and has ensured the buffered read data was properly transmitted |
| 111 // to the new ResourceHandler. The InterceptingResourceHandler now acts as | 123 // to the new ResourceHandler. The InterceptingResourceHandler now acts as |
| 112 // a pass-through ResourceHandler. | 124 // a pass-through ResourceHandler. |
| 113 PASS_THROUGH, | 125 PASS_THROUGH, |
| 114 }; | 126 }; |
| 115 | 127 |
| 116 // Runs necessary operations depending on |state_|. | 128 // Runs necessary operations depending on |state_|. |
| 117 void DoLoop(); | 129 void DoLoop(); |
| 118 | 130 |
| 119 void ResumeInternal(); | 131 void ResumeInternal(); |
| 120 | 132 |
| 133 void SendOnWillReadToOldHandler(); |
| 134 void OnBufferReceived(); |
| 121 void SendOnResponseStartedToOldHandler(); | 135 void SendOnResponseStartedToOldHandler(); |
| 122 void SendPayloadToOldHandler(); | 136 void SendPayloadToOldHandler(); |
| 137 void ReceivedBufferFromOldHandler(); |
| 123 void SendFirstReadBufferToNewHandler(); | 138 void SendFirstReadBufferToNewHandler(); |
| 124 void SendOnResponseStartedToNewHandler(); | 139 void SendOnResponseStartedToNewHandler(); |
| 140 void ReceivedBufferFromNewHandler(); |
| 125 | 141 |
| 126 State state_ = State::STARTING; | 142 State state_ = State::STARTING; |
| 127 | 143 |
| 128 std::unique_ptr<ResourceHandler> new_handler_; | 144 std::unique_ptr<ResourceHandler> new_handler_; |
| 129 std::string payload_for_old_handler_; | 145 std::string payload_for_old_handler_; |
| 130 size_t payload_bytes_written_ = 0; | 146 size_t payload_bytes_written_ = 0; |
| 131 | 147 |
| 132 // Result of the first read, that may have to be passed to an alternate | 148 // Result of the first read, that may have to be passed to an alternate |
| 133 // ResourceHandler instead of the original ResourceHandler. | 149 // ResourceHandler instead of the original ResourceHandler. |
| 134 scoped_refptr<net::IOBuffer> first_read_buffer_; | 150 scoped_refptr<net::IOBuffer> first_read_buffer_; |
| 135 // Instead of |first_read_buffer_|, this handler creates a new IOBuffer with | 151 // Instead of |first_read_buffer_|, this handler creates a new IOBuffer with |
| 136 // the same size and return it to the client. | 152 // the same size and return it to the client. |
| 137 scoped_refptr<net::IOBuffer> first_read_buffer_double_; | 153 scoped_refptr<net::IOBuffer> first_read_buffer_double_; |
| 138 size_t first_read_buffer_size_ = 0; | 154 int first_read_buffer_size_ = 0; |
| 139 size_t first_read_buffer_bytes_read_ = 0; | 155 int first_read_buffer_bytes_read_ = 0; |
| 140 size_t first_read_buffer_bytes_written_ = 0; | 156 int first_read_buffer_bytes_written_ = 0; |
| 157 |
| 158 // Information about the new handler's buffer while copying data from |
| 159 // |first_read_buffer_double_| to the new handler's buffer. |
| 160 // Note that when these are used, the old handler has been destroyed, and |
| 161 // |next_handler_| is now the new one. |
| 162 scoped_refptr<net::IOBuffer> new_handler_read_buffer_; |
| 163 int new_handler_read_buffer_size_ = 0; |
| 164 |
| 165 // Pointers to parent-owned read buffer and its size. Only used for first |
| 166 // OnWillRead call. |
| 167 scoped_refptr<net::IOBuffer>* parent_read_buffer_ = nullptr; |
| 168 int* parent_read_buffer_size_ = nullptr; |
| 141 | 169 |
| 142 scoped_refptr<ResourceResponse> response_; | 170 scoped_refptr<ResourceResponse> response_; |
| 143 | 171 |
| 144 // Next two values are used to handle synchronous Resume calls without a | 172 // Next two values are used to handle synchronous Resume calls without a |
| 145 // PostTask. | 173 // PostTask. |
| 146 | 174 |
| 147 // True if the code is currently within a DoLoop. | 175 // True if the code is currently within a DoLoop. |
| 148 bool in_do_loop_ = false; | 176 bool in_do_loop_ = false; |
| 149 // True if the request was resumed while |in_do_loop_| was true; | 177 // True if the request was resumed while |in_do_loop_| was true; |
| 150 bool advance_to_next_state_ = false; | 178 bool advance_to_next_state_ = false; |
| 151 | 179 |
| 152 base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; | 180 base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; |
| 153 | 181 |
| 154 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); | 182 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); |
| 155 }; | 183 }; |
| 156 | 184 |
| 157 } // namespace content | 185 } // namespace content |
| 158 | 186 |
| 159 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ | 187 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
| OLD | NEW |