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