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