OLD | NEW |
1 // Copyright (c) 2012 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_MIME_TYPE_RESOURCE_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
6 #define CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ | 6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "content/browser/loader/layered_resource_handler.h" | 13 #include "content/browser/loader/layered_resource_handler.h" |
14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 #include "content/public/browser/resource_controller.h" | |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 class URLRequest; | 17 class URLRequest; |
19 } | 18 } |
20 | 19 |
21 namespace content { | 20 namespace content { |
22 class PluginService; | 21 class PluginService; |
23 class ResourceDispatcherHostImpl; | 22 class ResourceDispatcherHostImpl; |
24 struct WebPluginInfo; | 23 struct WebPluginInfo; |
25 | 24 |
26 // ResourceHandler that, if necessary, buffers a response body without passing | 25 // ResourceHandler that initiates special handling of the response if needed, |
27 // it to the next ResourceHandler until it can perform mime sniffing on it. | 26 // based on the response's MIME type (starts downloads, sends data to some |
28 // Once a response's MIME type is known, initiates special handling of the | 27 // plugin types via a special channel). |
29 // response if needed (starts downloads, sends data to some plugin types via a | 28 class CONTENT_EXPORT InterceptingResourceHandler |
30 // special channel). | 29 : public LayeredResourceHandler { |
31 // | |
32 // Uses the buffer provided by the original event handler for buffering, and | |
33 // continues to reuses it until it can determine the MIME type | |
34 // subsequent reads until it's done buffering. As a result, the buffer | |
35 // returned by the next ResourceHandler must have a capacity of at least | |
36 // net::kMaxBytesToSniff * 2. | |
37 // | |
38 // Before a request is sent, this ResourceHandler will also set an appropriate | |
39 // Accept header on the request based on its ResourceType, if one isn't already | |
40 // present. | |
41 class CONTENT_EXPORT MimeTypeResourceHandler | |
42 : public LayeredResourceHandler, | |
43 public ResourceController { | |
44 public: | 30 public: |
45 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. | 31 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL. |
46 MimeTypeResourceHandler(std::unique_ptr<ResourceHandler> next_handler, | 32 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, |
47 ResourceDispatcherHostImpl* host, | 33 ResourceDispatcherHostImpl* host, |
48 PluginService* plugin_service, | 34 PluginService* plugin_service, |
49 net::URLRequest* request); | 35 net::URLRequest* request); |
50 ~MimeTypeResourceHandler() override; | 36 ~InterceptingResourceHandler() override; |
51 | 37 |
52 private: | 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 for the plugin |
| 46 // info to be ready in order to possibly intercept the resource has a |
| 47 // stream. |
| 48 WAITING_FOR_PLUGINS, |
| 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 |
53 // ResourceHandler implementation: | 65 // ResourceHandler implementation: |
54 void SetController(ResourceController* controller) override; | |
55 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | 66 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
56 bool OnWillStart(const GURL&, bool* defer) override; | |
57 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 67 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
58 int* buf_size, | 68 int* buf_size, |
59 int min_size) override; | 69 int min_size) override; |
60 bool OnReadCompleted(int bytes_read, bool* defer) override; | 70 bool OnReadCompleted(int bytes_read, bool* defer) override; |
61 void OnResponseCompleted(const net::URLRequestStatus& status, | |
62 const std::string& security_info, | |
63 bool* defer) override; | |
64 | 71 |
65 // ResourceController implementation: | 72 bool MakeHandlerChoice(bool* defer); |
66 void Resume() override; | |
67 void Cancel() override; | |
68 void CancelAndIgnore() override; | |
69 void CancelWithError(int error_code) override; | |
70 | 73 |
71 bool ProcessResponse(bool* defer); | |
72 | |
73 bool ShouldSniffContent(); | |
74 bool DetermineMimeType(); | |
75 // Determines whether a plugin will handle the current request, and if so, | 74 // Determines whether a plugin will handle the current request, and if so, |
76 // sets up the handler to direct the response to that plugin. Returns false | 75 // sets up the handler to direct the response to that plugin. Returns false |
77 // if there is an error and the request should be cancelled and true | 76 // if there is an error and the request should be cancelled and true |
78 // otherwise. |defer| is set to true if plugin data is stale and needs to be | 77 // otherwise. |defer| is set to true if plugin data is stale and needs to be |
79 // refreshed before the request can be handled (in this case the function | 78 // refreshed before the request can be handled (in this case the function |
80 // still returns true). If the request is directed to a plugin, | 79 // still returns true). If the request is directed to a plugin, |
81 // |handled_by_plugin| is set to true. | 80 // |handled_by_plugin| is set to true. |
82 bool SelectPluginHandler(bool* defer, bool* handled_by_plugin); | 81 bool SelectPluginHandler(bool* defer, bool* handled_by_plugin); |
83 // Returns false if the request should be cancelled. | 82 // Returns false if the request should be cancelled. |
84 bool SelectNextHandler(bool* defer); | 83 bool SelectNextHandler(bool* defer); |
85 bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler, | 84 bool UseAlternateNextHandler(std::unique_ptr<ResourceHandler> handler, |
86 const std::string& payload_for_old_handler); | 85 const std::string& payload_for_old_handler); |
87 | 86 |
88 bool ReplayReadCompleted(bool* defer); | |
89 void CallReplayReadCompleted(); | |
90 | |
91 bool MustDownload(); | 87 bool MustDownload(); |
92 | 88 |
93 // Copies data from |read_buffer_| to |next_handler_|. | |
94 bool CopyReadBufferToNextHandler(); | |
95 | |
96 // Called on the IO thread once the list of plugins has been loaded. | 89 // Called on the IO thread once the list of plugins has been loaded. |
97 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); | 90 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins); |
98 | 91 |
99 enum State { | 92 bool CopyReadBufferToNextHandler(); |
100 STATE_STARTING, | |
101 | 93 |
102 // In this state, we are filling read_buffer_ with data for the purpose | |
103 // of sniffing the mime type of the response. | |
104 STATE_BUFFERING, | |
105 | |
106 // In this state, we are select an appropriate downstream ResourceHandler | |
107 // based on the mime type of the response. We are also potentially waiting | |
108 // for plugins to load so that we can determine if a plugin is available to | |
109 // handle the mime type. | |
110 STATE_PROCESSING, | |
111 | |
112 // In this state, we are replaying buffered events (OnResponseStarted and | |
113 // OnReadCompleted) to the downstream ResourceHandler. | |
114 STATE_REPLAYING, | |
115 | |
116 // In this state, we are just a blind pass-through ResourceHandler. | |
117 STATE_STREAMING | |
118 }; | |
119 State state_; | 94 State state_; |
120 | 95 |
121 scoped_refptr<ResourceResponse> response_; | |
122 ResourceDispatcherHostImpl* host_; | 96 ResourceDispatcherHostImpl* host_; |
123 #if defined(ENABLE_PLUGINS) | 97 #if defined(ENABLE_PLUGINS) |
124 PluginService* plugin_service_; | 98 PluginService* plugin_service_; |
125 #endif | 99 #endif |
126 scoped_refptr<net::IOBuffer> read_buffer_; | 100 |
127 int read_buffer_size_; | 101 bool switched_handler_; |
128 int bytes_read_; | |
129 | 102 |
130 bool must_download_; | 103 bool must_download_; |
131 bool must_download_is_set_; | 104 bool must_download_is_set_; |
132 | 105 |
133 base::WeakPtrFactory<MimeTypeResourceHandler> weak_ptr_factory_; | 106 // Used to copy data to the new next ResourceHandler. |
| 107 scoped_refptr<ResourceResponse> response_; |
| 108 scoped_refptr<net::IOBuffer> read_buffer_; |
| 109 int bytes_read_; |
134 | 110 |
135 DISALLOW_COPY_AND_ASSIGN(MimeTypeResourceHandler); | 111 base::WeakPtrFactory<InterceptingResourceHandler> weak_ptr_factory_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); |
136 }; | 114 }; |
137 | 115 |
138 } // namespace content | 116 } // namespace content |
139 | 117 |
140 #endif // CONTENT_BROWSER_LOADER_MIME_TYPE_RESOURCE_HANDLER_H_ | 118 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ |
OLD | NEW |