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_RESOURCE_LOADER_H_
6#define CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_
7
8#include "base/gtest_prod_util.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "content/browser/loader/resource_handler.h"
12#include "content/browser/ssl/ssl_error_handler.h"
13#include "content/common/content_export.h"
14#include "content/public/browser/resource_controller.h"
15#include "content/public/common/signed_certificate_timestamp_id_and_status.h"
16#include "net/url_request/url_request.h"
17
18namespace content {
19class ResourceDispatcherHostLoginDelegate;
20class ResourceLoaderDelegate;
21class ResourceRequestInfoImpl;
22class SSLClientAuthHandler;
23
24// This class is responsible for driving the URLRequest (i.e., calling Start,
25// Read, and servicing events).  It has a ResourceHandler, which is typically a
26// chain of ResourceHandlers, and is the ResourceController for its handler.
27class CONTENT_EXPORT ResourceLoader : public net::URLRequest::Delegate,
28                                      public SSLErrorHandler::Delegate,
29                                      public ResourceController {
30 public:
31  ResourceLoader(scoped_ptr<net::URLRequest> request,
32                 scoped_ptr<ResourceHandler> handler,
33                 ResourceLoaderDelegate* delegate);
34  virtual ~ResourceLoader();
35
36  void StartRequest();
37  void CancelRequest(bool from_renderer);
38
39  void ReportUploadProgress();
40
41  bool is_transferring() const { return is_transferring_; }
42  void MarkAsTransferring(const GURL& target_url);
43  void CompleteTransfer();
44
45  net::URLRequest* request() { return request_.get(); }
46  ResourceRequestInfoImpl* GetRequestInfo();
47
48  void ClearLoginDelegate();
49  void ClearSSLClientAuthHandler();
50
51  // IPC message handlers:
52  void OnUploadProgressACK();
53
54 private:
55  FRIEND_TEST_ALL_PREFIXES(ResourceLoaderTest, ClientCertStoreLookup);
56  FRIEND_TEST_ALL_PREFIXES(ResourceLoaderTest, ClientCertStoreNull);
57
58  // net::URLRequest::Delegate implementation:
59  virtual void OnReceivedRedirect(net::URLRequest* request,
60                                  const GURL& new_url,
61                                  bool* defer) OVERRIDE;
62  virtual void OnAuthRequired(net::URLRequest* request,
63                              net::AuthChallengeInfo* info) OVERRIDE;
64  virtual void OnCertificateRequested(net::URLRequest* request,
65                                      net::SSLCertRequestInfo* info) OVERRIDE;
66  virtual void OnSSLCertificateError(net::URLRequest* request,
67                                     const net::SSLInfo& info,
68                                     bool fatal) OVERRIDE;
69  virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
70  virtual void OnReadCompleted(net::URLRequest* request,
71                               int bytes_read) OVERRIDE;
72
73  // SSLErrorHandler::Delegate implementation:
74  virtual void CancelSSLRequest(const GlobalRequestID& id,
75                                int error,
76                                const net::SSLInfo* ssl_info) OVERRIDE;
77  virtual void ContinueSSLRequest(const GlobalRequestID& id) OVERRIDE;
78
79  // ResourceController implementation:
80  virtual void Resume() OVERRIDE;
81  virtual void Cancel() OVERRIDE;
82  virtual void CancelAndIgnore() OVERRIDE;
83  virtual void CancelWithError(int error_code) OVERRIDE;
84
85  void StartRequestInternal();
86  void CancelRequestInternal(int error, bool from_renderer);
87  // Stores the SignedCertificateTimestamps held in |sct_list| in the
88  // SignedCertificateTimestampStore singleton, associated with |process_id|.
89  // On return, |sct_ids| contains the assigned ID and verification status of
90  // each SignedCertificateTimestamp.
91  void StoreSignedCertificateTimestamps(
92      const net::SignedCertificateTimestampAndStatusList& sct_list,
93      int process_id,
94      SignedCertificateTimestampIDStatusList* sct_ids);
95  void CompleteResponseStarted();
96  void StartReading(bool is_continuation);
97  void ResumeReading();
98  void ReadMore(int* bytes_read);
99  void CompleteRead(int bytes_read);
100  void ResponseCompleted();
101  void CallDidFinishLoading();
102  void RecordHistograms();
103
104  bool is_deferred() const { return deferred_stage_ != DEFERRED_NONE; }
105
106  // Used for categorizing loading of prefetches for reporting in histograms.
107  // NOTE: This enumeration is used in histograms, so please do not add entries
108  // in the middle.
109  enum PrefetchStatus {
110    STATUS_UNDEFINED,
111    STATUS_SUCCESS_FROM_CACHE,
112    STATUS_SUCCESS_FROM_NETWORK,
113    STATUS_CANCELED,
114    STATUS_MAX,
115  };
116
117  enum DeferredStage {
118    DEFERRED_NONE,
119    DEFERRED_START,
120    DEFERRED_REDIRECT,
121    DEFERRED_READ,
122    DEFERRED_FINISH
123  };
124  DeferredStage deferred_stage_;
125
126  scoped_ptr<net::URLRequest> request_;
127  scoped_ptr<ResourceHandler> handler_;
128  ResourceLoaderDelegate* delegate_;
129
130  scoped_refptr<ResourceDispatcherHostLoginDelegate> login_delegate_;
131  scoped_refptr<SSLClientAuthHandler> ssl_client_auth_handler_;
132
133  uint64 last_upload_position_;
134  bool waiting_for_upload_progress_ack_;
135  base::TimeTicks last_upload_ticks_;
136  base::TimeTicks read_deferral_start_time_;
137
138  // Indicates that we are in a state of being transferred to a new downstream
139  // consumer.  We are waiting for a notification to complete the transfer, at
140  // which point we'll receive a new ResourceHandler.
141  bool is_transferring_;
142
143  base::WeakPtrFactory<ResourceLoader> weak_ptr_factory_;
144
145  DISALLOW_COPY_AND_ASSIGN(ResourceLoader);
146};
147
148}  // namespace content
149
150#endif  // CONTENT_BROWSER_LOADER_RESOURCE_LOADER_H_
151