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 |
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_controller.h" | 15 #include "content/browser/loader/resource_controller.h" |
16 #include "content/browser/loader/resource_handler.h" | 16 #include "content/browser/loader/resource_handler.h" |
17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 class URLRequest; | 22 class URLRequest; |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
| 27 class ResourceController; |
| 28 |
27 // ResourceHandler that initiates special handling of the response if needed, | 29 // ResourceHandler that initiates special handling of the response if needed, |
28 // based on the response's MIME type (starts downloads, sends data to some | 30 // based on the response's MIME type (starts downloads, sends data to some |
29 // plugin types via a special channel). | 31 // plugin types via a special channel). |
30 // | 32 // |
31 // An InterceptingResourceHandler holds two handlers (|next_handler| and | 33 // An InterceptingResourceHandler holds two handlers (|next_handler| and |
32 // |new_handler|). It assumes the following: | 34 // |new_handler|). It assumes the following: |
33 // - OnResponseStarted on |next_handler| never sets |*defer|. | 35 // - OnResponseStarted on |next_handler| never sets |*defer|. |
34 // - OnResponseCompleted on |next_handler| never sets |*defer|. | 36 // - OnResponseCompleted on |next_handler| never sets |*defer|. |
35 class CONTENT_EXPORT InterceptingResourceHandler | 37 class CONTENT_EXPORT InterceptingResourceHandler |
36 : public LayeredResourceHandler, | 38 : public LayeredResourceHandler { |
37 public ResourceController { | |
38 public: | 39 public: |
39 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, | 40 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
40 net::URLRequest* request); | 41 net::URLRequest* request); |
41 ~InterceptingResourceHandler() override; | 42 ~InterceptingResourceHandler() override; |
42 | 43 |
43 // ResourceHandler implementation: | 44 // ResourceHandler implementation: |
44 void SetController(ResourceController* controller) override; | 45 void OnResponseStarted( |
45 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | 46 ResourceResponse* response, |
| 47 std::unique_ptr<ResourceController> controller) override; |
46 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 48 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
47 int* buf_size, | 49 int* buf_size, |
48 int min_size) override; | 50 int min_size) override; |
49 bool OnReadCompleted(int bytes_read, bool* defer) override; | 51 void OnReadCompleted(int bytes_read, |
50 void OnResponseCompleted(const net::URLRequestStatus& status, | 52 std::unique_ptr<ResourceController> controller) override; |
51 bool* defer) override; | 53 void OnResponseCompleted( |
52 | 54 const net::URLRequestStatus& status, |
53 // ResourceController implementation: | 55 std::unique_ptr<ResourceController> controller) override; |
54 void Cancel() override; | |
55 void CancelAndIgnore() override; | |
56 void CancelWithError(int error_code) override; | |
57 void Resume() override; | |
58 | 56 |
59 // Replaces the next handler with |new_handler|, sending | 57 // Replaces the next handler with |new_handler|, sending |
60 // |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 |
61 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One | 59 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One |
62 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and | 60 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and |
63 // OnRequestRedirected methods will not be called. | 61 // OnRequestRedirected methods will not be called. |
64 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler, | 62 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler, |
65 const std::string& payload_for_old_handler); | 63 const std::string& payload_for_old_handler); |
66 | 64 |
67 // Used in tests. | 65 // Used in tests. |
68 ResourceHandler* new_handler_for_testing() const { | 66 ResourceHandler* new_handler_for_testing() const { |
69 return new_handler_.get(); | 67 return new_handler_.get(); |
70 } | 68 } |
71 | 69 |
72 private: | 70 private: |
| 71 // ResourceController subclass that calls into the InterceptingResourceHandler |
| 72 // on cancel/resume. |
| 73 class Controller; |
| 74 |
73 enum class State { | 75 enum class State { |
74 // The InterceptingResourceHandler is waiting for the mime type of the | 76 // The InterceptingResourceHandler is waiting for the mime type of the |
75 // 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 |
76 // replaced with an appropriate one. | 78 // replaced with an appropriate one. |
77 STARTING, | 79 STARTING, |
78 | 80 |
| 81 // The InterceptingResourceHandler is starting the process of swapping |
| 82 // handlers. |
| 83 SWAPPING_HANDLERS, |
| 84 |
79 // The InterceptingResourceHandler is sending the payload given via | 85 // The InterceptingResourceHandler is sending the payload given via |
80 // UseNewHandler to the old handler and waiting for its completion via | 86 // UseNewHandler to the old handler and waiting for its completion via |
81 // Resume(). | 87 // Resume(). |
82 SENDING_PAYLOAD_TO_OLD_HANDLER, | 88 SENDING_PAYLOAD_TO_OLD_HANDLER, |
83 | 89 |
84 // The InterceptingResourcHandler is calling the new handler's | 90 // The InterceptingResourcHandler is calling the new handler's |
85 // OnResponseStarted method and waiting for its completion via Resume(). | 91 // OnResponseStarted method and waiting for its completion via Resume(). |
86 // After completion, the InterceptingResourceHandler will transition to | 92 // After completion, the InterceptingResourceHandler will transition to |
87 // SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER on success. | 93 // SENDING_ON_RESPONSE_STARTED_TO_NEW_HANDLER on success. |
88 SENDING_ON_WILL_START_TO_NEW_HANDLER, | 94 SENDING_ON_WILL_START_TO_NEW_HANDLER, |
(...skipping 12 matching lines...) Expand all Loading... |
101 // the new handler and waiting for its completion via Resume(). | 107 // the new handler and waiting for its completion via Resume(). |
102 SENDING_BUFFER_TO_NEW_HANDLER, | 108 SENDING_BUFFER_TO_NEW_HANDLER, |
103 | 109 |
104 // The InterceptingResourceHandler has replaced its next ResourceHandler if | 110 // The InterceptingResourceHandler has replaced its next ResourceHandler if |
105 // needed, and has ensured the buffered read data was properly transmitted | 111 // needed, and has ensured the buffered read data was properly transmitted |
106 // to the new ResourceHandler. The InterceptingResourceHandler now acts as | 112 // to the new ResourceHandler. The InterceptingResourceHandler now acts as |
107 // a pass-through ResourceHandler. | 113 // a pass-through ResourceHandler. |
108 PASS_THROUGH, | 114 PASS_THROUGH, |
109 }; | 115 }; |
110 | 116 |
111 // Runs necessary operations depending on |state_|. Returns false when an | 117 // Runs necessary operations depending on |state_|. |
112 // error happens, and set |*defer| to true if the operation continues upon | 118 void DoLoop(); |
113 // return. | |
114 bool DoLoop(bool* defer); | |
115 | 119 |
116 // The return value and |defer| has the same meaning as DoLoop. | 120 void ResumeInternal(); |
117 bool SendPayloadToOldHandler(bool* defer); | |
118 bool SendFirstReadBufferToNewHandler(bool* defer); | |
119 bool SendOnResponseStartedToNewHandler(bool* defer); | |
120 | 121 |
121 // Wraps calls to DoLoop. Resumes or Cancels underlying request, if needed. | 122 void SendOnResponseStartedToOldHandler(); |
122 void AdvanceState(); | 123 void SendPayloadToOldHandler(); |
| 124 void SendFirstReadBufferToNewHandler(); |
| 125 void SendOnResponseStartedToNewHandler(); |
123 | 126 |
124 State state_ = State::STARTING; | 127 State state_ = State::STARTING; |
125 | 128 |
126 std::unique_ptr<ResourceHandler> new_handler_; | 129 std::unique_ptr<ResourceHandler> new_handler_; |
127 std::string payload_for_old_handler_; | 130 std::string payload_for_old_handler_; |
128 size_t payload_bytes_written_ = 0; | 131 size_t payload_bytes_written_ = 0; |
129 | 132 |
130 // Result of the first read, that may have to be passed to an alternate | 133 // Result of the first read, that may have to be passed to an alternate |
131 // ResourceHandler instead of the original ResourceHandler. | 134 // ResourceHandler instead of the original ResourceHandler. |
132 scoped_refptr<net::IOBuffer> first_read_buffer_; | 135 scoped_refptr<net::IOBuffer> first_read_buffer_; |
133 // Instead of |first_read_buffer_|, this handler creates a new IOBuffer with | 136 // Instead of |first_read_buffer_|, this handler creates a new IOBuffer with |
134 // the same size and return it to the client. | 137 // the same size and return it to the client. |
135 scoped_refptr<net::IOBuffer> first_read_buffer_double_; | 138 scoped_refptr<net::IOBuffer> first_read_buffer_double_; |
136 size_t first_read_buffer_size_ = 0; | 139 size_t first_read_buffer_size_ = 0; |
137 size_t first_read_buffer_bytes_read_ = 0; | 140 size_t first_read_buffer_bytes_read_ = 0; |
138 size_t first_read_buffer_bytes_written_ = 0; | 141 size_t first_read_buffer_bytes_written_ = 0; |
139 | 142 |
140 scoped_refptr<ResourceResponse> response_; | 143 scoped_refptr<ResourceResponse> response_; |
141 | 144 |
| 145 // Next two values are used to handle synchronous Resume calls without a |
| 146 // PostTask. |
| 147 |
| 148 // True if the code is currently within a DoLoop. |
| 149 bool in_do_loop_ = false; |
| 150 // True if the request was resumed while |in_do_loop_| was true; |
| 151 bool advance_to_next_state_ = false; |
| 152 |
142 base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; | 153 base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; |
143 | 154 |
144 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); | 155 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); |
145 }; | 156 }; |
146 | 157 |
147 } // namespace content | 158 } // namespace content |
148 | 159 |
149 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ | 160 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
OLD | NEW |