Chromium Code Reviews| 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_BUFFERED_RESOURCE_HANDLER_H_ | |
| 6 #define CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "content/browser/loader/layered_resource_handler.h" | |
| 14 #include "content/common/content_export.h" | |
| 15 #include "content/public/browser/resource_controller.h" | |
| 16 | |
| 17 namespace net { | |
| 18 class URLRequest; | |
| 19 } | |
| 20 | |
| 21 namespace content { | |
| 22 class ResourceDispatcherHostImpl; | |
| 23 | |
| 24 // ResourceHandler that, if necessary, buffers a response body without passing | |
| 25 // it to the next ResourceHandler until asked to do so. | |
| 26 // | |
| 27 // Uses the buffer provided by the original event handler for buffering, and | |
| 28 // continues to reuses it until it's done buffering. As a result, the buffer | |
| 29 // returned by the next ResourceHandler must have a capacity of at least | |
| 30 // net::kMaxBytesToBuffer * 2. | |
| 31 class CONTENT_EXPORT BufferedResourceHandler : public LayeredResourceHandler, | |
|
mmenke
2016/05/26 17:30:22
What's the motivation for separating this out, and
mmenke
2016/05/26 20:01:41
My reason for asking is that I really don't want t
clamy
2016/05/30 15:08:17
As I explained in the review message, I split into
| |
| 32 public ResourceController { | |
| 33 public: | |
| 34 BufferedResourceHandler(std::unique_ptr<ResourceHandler> next_handler, | |
| 35 net::URLRequest* request, | |
| 36 int max_bytes_to_buffer); | |
| 37 ~BufferedResourceHandler() override; | |
| 38 | |
| 39 protected: | |
| 40 ResourceResponse* response() const { return response_.get(); } | |
| 41 int bytes_read() const { return bytes_read_; } | |
| 42 net::IOBuffer* read_buffer() const { return read_buffer_.get(); } | |
| 43 | |
| 44 // Called in OnResponseStarted and OnReadCompleted to see if the | |
| 45 // BufferedResourceHandler can go from buffering to replaying the buffered | |
| 46 // data. | |
| 47 virtual bool CanStartReplayInResponseStarted(); | |
| 48 virtual bool CanStartReplayInReadCompleted(); | |
| 49 | |
| 50 | |
| 51 // Called when starting to replay the buffered data to the downstream | |
| 52 // ResourceHandlers. If the return value is false, the request will be | |
| 53 // canceled. If |defer| is set to true, the replay attempt will stop until | |
| 54 // ProcessReplay is called (WillStartReplay will be called again by | |
| 55 // ProcessReplay). | |
| 56 virtual bool WillStartReplay(bool* defer); | |
| 57 | |
| 58 // Replays the buffered data to the downstream ResourceHandlers. This should | |
| 59 // only ba called directly when the previous replay attempt was paused by | |
| 60 // WillStartReplay. | |
| 61 bool ProcessReplay(bool* defer); | |
| 62 | |
| 63 // LayeredResourceHandler implementation: | |
| 64 void InstallNewLeafHandler( | |
| 65 std::unique_ptr<ResourceHandler> new_handler, | |
| 66 const std::string& payload_for_old_handler) override; | |
| 67 | |
| 68 private: | |
| 69 enum State { | |
| 70 // Starting state of the BufferedResourceHandler. In this state, it is | |
| 71 // acting as a blind pass-through ResourceHandler. | |
| 72 STATE_STARTING, | |
| 73 | |
| 74 // In this state, the BufferedResourceHandler is buffering the response | |
| 75 // data in read_buffer_. | |
| 76 STATE_BUFFERING, | |
| 77 | |
| 78 // In this state, the request is paused and the BufferedResourceHandler is | |
| 79 // waiting to start the replay of the response to the downstream | |
| 80 // ResourceHandlers. | |
| 81 STATE_PROCESSING, | |
| 82 | |
| 83 // In this state, the BufferedResourceHandler is replaying the buffered | |
| 84 // OnResponseStarted event to the downstream ResourceHandlers. | |
| 85 STATE_REPLAYING_RESPONSE_RECEIVED, | |
| 86 | |
| 87 // In this state, the BufferedResourceHandler is replaying the buffered | |
| 88 // OnResponseStarted event to the new leaf handler, if there is one. | |
| 89 STATE_REPLAYING_RESPONSE_RECEIVED_NEW_HANDLER, | |
| 90 | |
| 91 // In this state, the BufferedResourceHandler is just a blind pass-through | |
| 92 // ResourceHandler. | |
| 93 STATE_STREAMING, | |
| 94 }; | |
| 95 | |
| 96 // ResourceHandler implementation: | |
| 97 void SetController(ResourceController* controller) override; | |
| 98 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | |
| 99 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 100 int* buf_size, | |
| 101 int min_size) override; | |
| 102 bool OnReadCompleted(int bytes_read, bool* defer) override; | |
| 103 void OnResponseCompleted(const net::URLRequestStatus& status, | |
| 104 const std::string& security_info, | |
| 105 bool* defer) override; | |
| 106 | |
| 107 // ResourceController implementation: | |
| 108 void Resume() override; | |
| 109 void Cancel() override; | |
| 110 void CancelAndIgnore() override; | |
| 111 void CancelWithError(int error_code) override; | |
| 112 | |
| 113 bool MaybeStartReplay(bool* defer); | |
| 114 bool ReplayResponseReceived(bool* defer); | |
| 115 bool ReplayResponseReceivedNewHandler(bool* defer); | |
| 116 bool ReplayReadCompleted(bool* defer); | |
| 117 | |
| 118 State state_; | |
| 119 | |
| 120 const int max_bytes_to_buffer_; | |
| 121 | |
| 122 // Used to buffer the reponse received until replay. | |
| 123 scoped_refptr<ResourceResponse> response_; | |
| 124 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 125 int read_buffer_size_; | |
| 126 int bytes_read_; | |
| 127 | |
| 128 // Used to store a new leaf ResourceHandler until the actual one can be | |
| 129 // replaced during the replay of the reponse. | |
| 130 std::unique_ptr<ResourceHandler> new_leaf_handler_; | |
| 131 std::string payload_for_old_handler_; | |
| 132 | |
| 133 base::WeakPtrFactory<BufferedResourceHandler> weak_ptr_factory_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(BufferedResourceHandler); | |
| 136 }; | |
| 137 | |
| 138 } // namespace content | |
| 139 | |
| 140 #endif // CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_ | |
| OLD | NEW |