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 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "content/browser/loader/layered_resource_handler.h" |
| 15 #include "content/common/content_export.h" |
| 16 #include "net/base/io_buffer.h" |
| 17 |
| 18 namespace net { |
| 19 class URLRequest; |
| 20 } |
| 21 |
| 22 namespace content { |
| 23 |
| 24 // ResourceHandler that initiates special handling of the response if needed, |
| 25 // based on the response's MIME type (starts downloads, sends data to some |
| 26 // plugin types via a special channel). |
| 27 class CONTENT_EXPORT InterceptingResourceHandler |
| 28 : public LayeredResourceHandler { |
| 29 public: |
| 30 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. |
| 31 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
| 32 net::URLRequest* request); |
| 33 ~InterceptingResourceHandler() override; |
| 34 |
| 35 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler, |
| 36 const std::string& payload_for_old_handler); |
| 37 |
| 38 // Used in tests. |
| 39 ResourceHandler* new_handler_for_testing() const { |
| 40 return new_handler_.get(); |
| 41 } |
| 42 |
| 43 private: |
| 44 enum class State { |
| 45 // In this state, the InterceptingResourceHandler is waiting for the mime |
| 46 // type of the response to be identified, to check if the next handler |
| 47 // should be replaced with an appropriate one. |
| 48 STARTING, |
| 49 |
| 50 // In this state, the InterceptingResourceHandler is waiting to copy the |
| 51 // read buffer to the next handler if it was replaced. This is needed |
| 52 // because MimeTypeResourceHandler may call OnWillRead before calling |
| 53 // OnResponseStarted, that is before the InterceptingResourceHandler |
| 54 // replaces the original ResourceHandler of the request. Therefore, the |
| 55 // data read at that time should be copied to the new ResourceHandler. |
| 56 WAITING_FOR_BUFFER_COPY, |
| 57 |
| 58 // In this state, the InterceptingResourceHandler has replaced its next |
| 59 // ResourceHandler if needed, and has ensured the buffered read data was |
| 60 // properly transmitted to the new ResourceHandler. The |
| 61 // InterceptingResourceHandler now acts as a pass-through ResourceHandler. |
| 62 DONE, |
| 63 }; |
| 64 |
| 65 // ResourceHandler implementation: |
| 66 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
| 67 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 68 int* buf_size, |
| 69 int min_size) override; |
| 70 bool OnReadCompleted(int bytes_read, bool* defer) override; |
| 71 |
| 72 void SendPayloadToOldHandler(); |
| 73 |
| 74 State state_; |
| 75 |
| 76 std::unique_ptr<ResourceHandler> new_handler_; |
| 77 std::string payload_for_old_handler_; |
| 78 |
| 79 // Result of the first read, that may have to be passed to an alternate |
| 80 // ResourceHandler instead of the original ResourceHandler. |
| 81 scoped_refptr<net::IOBuffer> first_read_buffer_; |
| 82 size_t first_read_buffer_size_; |
| 83 scoped_refptr<net::IOBuffer> first_read_buffer_copy_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); |
| 86 }; |
| 87 |
| 88 } // namespace content |
| 89 |
| 90 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
OLD | NEW |