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 private: | |
39 enum class State { | |
40 // In this state, the InterceptingResourceHandler is waiting for the mime | |
41 // type of the response to be identified, to check if the next handler | |
42 // should be replaced with an appropriate one. | |
43 STARTING, | |
44 | |
45 // In this state, the InterceptingResourceHandler is waiting to copy the | |
46 // read buffer to the next handler if it was replaced. This is needed | |
47 // because MimeTypeResourceHandler may call OnWillRead before calling | |
48 // OnResponseStarted, that is before the InterceptingResourceHandler | |
49 // replaces the original ResourceHandler of the request. Therefore, the | |
50 // data read at that time should be copied to the new ResourceHandler. | |
51 WAITING_FOR_BUFFER_COPY, | |
52 | |
53 // In this state, the InterceptingResourceHandler has replaced its next | |
54 // ResourceHandler if needed, and has ensured the buffered read data was | |
55 // properly transmitted to the new ResourceHandler. The | |
56 // InterceptingResourceHandler now acts as a pass-through ResourceHandler. | |
57 DONE, | |
58 }; | |
59 | |
60 // ResourceHandler implementation: | |
61 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | |
62 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
63 int* buf_size, | |
64 int min_size) override; | |
65 bool OnReadCompleted(int bytes_read, bool* defer) override; | |
66 | |
67 void SendPayloadToOldHandler(); | |
68 | |
69 State state_; | |
70 | |
71 std::unique_ptr<ResourceHandler> new_handler_; | |
72 std::string payload_for_old_handler_; | |
73 | |
74 // Used to copy data to the new next ResourceHandler. | |
75 scoped_refptr<net::IOBuffer> read_buffer_; | |
mmenke
2016/07/19 21:42:33
Maybe call this first_read_buffer_, and mention th
clamy
2016/07/25 16:58:25
Done.
| |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); | |
78 }; | |
79 | |
80 } // namespace content | |
81 | |
82 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ | |
OLD | NEW |