Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / loader / buffered_resource_handler.h
blob4e80ec53bd58e9c731037c14fc10ac3af05b7099
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/weak_ptr.h"
12 #include "content/browser/loader/layered_resource_handler.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/resource_controller.h"
16 namespace net {
17 class URLRequest;
20 namespace content {
21 class PluginService;
22 class ResourceDispatcherHostImpl;
23 struct WebPluginInfo;
25 // Used to buffer a request until enough data has been received.
26 class CONTENT_EXPORT BufferedResourceHandler
27 : public LayeredResourceHandler,
28 public ResourceController {
29 public:
30 // If ENABLE_PLUGINS is defined, |plugin_service| must not be NULL.
31 BufferedResourceHandler(scoped_ptr<ResourceHandler> next_handler,
32 ResourceDispatcherHostImpl* host,
33 PluginService* plugin_service,
34 net::URLRequest* request);
35 ~BufferedResourceHandler() override;
37 private:
38 // ResourceHandler implementation:
39 void SetController(ResourceController* controller) override;
40 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
41 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
42 int* buf_size,
43 int min_size) override;
44 bool OnReadCompleted(int bytes_read, bool* defer) override;
45 void OnResponseCompleted(const net::URLRequestStatus& status,
46 const std::string& security_info,
47 bool* defer) override;
49 // ResourceController implementation:
50 void Resume() override;
51 void Cancel() override;
52 void CancelAndIgnore() override;
53 void CancelWithError(int error_code) override;
55 bool ProcessResponse(bool* defer);
57 bool ShouldSniffContent();
58 bool DetermineMimeType();
59 bool SelectNextHandler(bool* defer);
60 bool UseAlternateNextHandler(scoped_ptr<ResourceHandler> handler,
61 const std::string& payload_for_old_handler);
63 bool ReplayReadCompleted(bool* defer);
64 void CallReplayReadCompleted();
66 bool MustDownload();
67 bool HasSupportingPlugin(bool* is_stale);
69 // Copies data from |read_buffer_| to |next_handler_|.
70 bool CopyReadBufferToNextHandler();
72 // Called on the IO thread once the list of plugins has been loaded.
73 void OnPluginsLoaded(const std::vector<WebPluginInfo>& plugins);
75 enum State {
76 STATE_STARTING,
78 // In this state, we are filling read_buffer_ with data for the purpose
79 // of sniffing the mime type of the response.
80 STATE_BUFFERING,
82 // In this state, we are select an appropriate downstream ResourceHandler
83 // based on the mime type of the response. We are also potentially waiting
84 // for plugins to load so that we can determine if a plugin is available to
85 // handle the mime type.
86 STATE_PROCESSING,
88 // In this state, we are replaying buffered events (OnResponseStarted and
89 // OnReadCompleted) to the downstream ResourceHandler.
90 STATE_REPLAYING,
92 // In this state, we are just a blind pass-through ResourceHandler.
93 STATE_STREAMING
95 State state_;
97 scoped_refptr<ResourceResponse> response_;
98 ResourceDispatcherHostImpl* host_;
99 PluginService* plugin_service_;
100 scoped_refptr<net::IOBuffer> read_buffer_;
101 int read_buffer_size_;
102 int bytes_read_;
104 bool must_download_;
105 bool must_download_is_set_;
107 base::WeakPtrFactory<BufferedResourceHandler> weak_ptr_factory_;
109 DISALLOW_COPY_AND_ASSIGN(BufferedResourceHandler);
112 } // namespace content
114 #endif // CONTENT_BROWSER_LOADER_BUFFERED_RESOURCE_HANDLER_H_