summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/buffered_resource_handler.h
blob: ab5393e7707a8239e3d4d853dc2754d576e52637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_BUFFERED_RESOURCE_HANDLER_H_
#define CONTENT_BROWSER_RENDERER_HOST_BUFFERED_RESOURCE_HANDLER_H_
#pragma once

#include <string>

#include "content/browser/renderer_host/resource_handler.h"

class MessageLoop;
class ResourceDispatcherHost;

namespace net {
class URLRequest;
}  // namespace net

// Used to buffer a request until enough data has been received.
class BufferedResourceHandler : public ResourceHandler {
 public:
  BufferedResourceHandler(ResourceHandler* handler,
                          ResourceDispatcherHost* host,
                          net::URLRequest* request);

  // ResourceHandler implementation:
  virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
  virtual bool OnRequestRedirected(int request_id, const GURL& new_url,
                                   ResourceResponse* response, bool* defer);
  virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
  virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
  virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
                          int min_size);
  virtual bool OnReadCompleted(int request_id, int* bytes_read);
  virtual bool OnResponseCompleted(int request_id,
                                   const net::URLRequestStatus& status,
                                   const std::string& security_info);
  virtual void OnRequestClosed();

 private:
  virtual ~BufferedResourceHandler();

  // Returns true if we should delay OnResponseStarted forwarding.
  bool DelayResponse();

  // Returns true if there will be a need to parse the DocType of the document
  // to determine the right way to handle it.
  bool ShouldBuffer(const GURL& url, const std::string& mime_type);

  // Returns true if there is enough information to process the DocType.
  bool DidBufferEnough(int bytes_read);

  // Returns true if we have to keep buffering data.
  bool KeepBuffering(int bytes_read);

  // Sends a pending OnResponseStarted notification. |in_complete| is true if
  // this is invoked from |OnResponseCompleted|.
  bool CompleteResponseStarted(int request_id, bool in_complete);

  // Returns true if we have to wait until the plugin list is generated.
  bool ShouldWaitForPlugins();

  // A test to determining whether the request should be forwarded to the
  // download thread.  If need_plugin_list was passed in and was set to true,
  // that means that the check couldn't be fully done because the plugins aren't
  // loaded.  The function should be called again after the plugin list is
  // loaded.
  bool ShouldDownload(bool* need_plugin_list);

  // Informs the original ResourceHandler |real_handler_| that the response will
  // be handled entirely by the new ResourceHandler |handler|.
  // A reference to |handler| is acquired.
  void UseAlternateResourceHandler(int request_id, ResourceHandler* handler);

  // Called on the file thread to load the list of plugins.
  void LoadPlugins();

  // Called on the IO thread once the list of plugins has been loaded.
  void OnPluginsLoaded();

  scoped_refptr<ResourceHandler> real_handler_;
  scoped_refptr<ResourceResponse> response_;
  ResourceDispatcherHost* host_;
  net::URLRequest* request_;
  scoped_refptr<net::IOBuffer> read_buffer_;
  scoped_refptr<net::IOBuffer> my_buffer_;
  int read_buffer_size_;
  int bytes_read_;
  bool sniff_content_;
  bool should_buffer_;
  bool wait_for_plugins_;
  bool buffering_;
  bool finished_;

  DISALLOW_COPY_AND_ASSIGN(BufferedResourceHandler);
};

#endif  // CONTENT_BROWSER_RENDERER_HOST_BUFFERED_RESOURCE_HANDLER_H_