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_
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"
22 class ResourceDispatcherHostImpl
;
25 // Used to buffer a request until enough data has been received.
26 class CONTENT_EXPORT BufferedResourceHandler
27 : public LayeredResourceHandler
,
28 public ResourceController
{
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
;
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
,
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();
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
);
78 // In this state, we are filling read_buffer_ with data for the purpose
79 // of sniffing the mime type of the response.
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.
88 // In this state, we are replaying buffered events (OnResponseStarted and
89 // OnReadCompleted) to the downstream ResourceHandler.
92 // In this state, we are just a blind pass-through ResourceHandler.
97 scoped_refptr
<ResourceResponse
> response_
;
98 ResourceDispatcherHostImpl
* host_
;
99 PluginService
* plugin_service_
;
100 scoped_refptr
<net::IOBuffer
> read_buffer_
;
101 int read_buffer_size_
;
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_