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_ASYNC_RESOURCE_HANDLER_H_
6#define CONTENT_BROWSER_LOADER_ASYNC_RESOURCE_HANDLER_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/time/time.h"
12#include "content/browser/loader/resource_handler.h"
13#include "content/browser/loader/resource_message_delegate.h"
14#include "url/gurl.h"
15
16namespace net {
17class URLRequest;
18}
19
20namespace content {
21class ResourceBuffer;
22class ResourceContext;
23class ResourceDispatcherHostImpl;
24class ResourceMessageFilter;
25class SharedIOBuffer;
26
27// Used to complete an asynchronous resource request in response to resource
28// load events from the resource dispatcher host.
29class AsyncResourceHandler : public ResourceHandler,
30                             public ResourceMessageDelegate {
31 public:
32  AsyncResourceHandler(net::URLRequest* request,
33                       ResourceDispatcherHostImpl* rdh);
34  virtual ~AsyncResourceHandler();
35
36  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
37
38  // ResourceHandler implementation:
39  virtual bool OnUploadProgress(uint64 position, uint64 size) OVERRIDE;
40  virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
41                                   ResourceResponse* response,
42                                   bool* defer) OVERRIDE;
43  virtual bool OnResponseStarted(ResourceResponse* response,
44                                 bool* defer) OVERRIDE;
45  virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE;
46  virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) OVERRIDE;
47  virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
48                          int* buf_size,
49                          int min_size) OVERRIDE;
50  virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE;
51  virtual void OnResponseCompleted(const net::URLRequestStatus& status,
52                                   const std::string& security_info,
53                                   bool* defer) OVERRIDE;
54  virtual void OnDataDownloaded(int bytes_downloaded) OVERRIDE;
55
56 private:
57  // IPC message handlers:
58  void OnFollowRedirect(int request_id);
59  void OnDataReceivedACK(int request_id);
60
61  bool EnsureResourceBufferIsInitialized();
62  void ResumeIfDeferred();
63  void OnDefer();
64
65  scoped_refptr<ResourceBuffer> buffer_;
66  ResourceDispatcherHostImpl* rdh_;
67
68  // Number of messages we've sent to the renderer that we haven't gotten an
69  // ACK for. This allows us to avoid having too many messages in flight.
70  int pending_data_count_;
71
72  int allocation_size_;
73
74  bool did_defer_;
75
76  bool has_checked_for_sufficient_resources_;
77  bool sent_received_response_msg_;
78  bool sent_first_data_msg_;
79
80  int64_t reported_transfer_size_;
81
82  base::TimeTicks redirect_start_time_;
83
84  DISALLOW_COPY_AND_ASSIGN(AsyncResourceHandler);
85};
86
87}  // namespace content
88
89#endif  // CONTENT_BROWSER_LOADER_ASYNC_RESOURCE_HANDLER_H_
90