1// Copyright (c) 2011 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// A wrapper around ResourceHandle and ResourceHandleClient that simplifies
6// the download of an HTTP object.  The interface is modeled after URLFetcher
7// in the /chrome/browser.
8//
9// ResourceFetcher::Delegate::OnURLFetchComplete will be called async after
10// the ResourceFetcher object is created.
11
12#ifndef WEBKIT_GLUE_RESOURCE_FETCHER_H_
13#define WEBKIT_GLUE_RESOURCE_FETCHER_H_
14
15#include <string>
16
17#include "base/basictypes.h"
18#include "base/callback.h"
19#include "base/memory/scoped_ptr.h"
20#include "base/timer.h"
21#include "googleurl/src/gurl.h"
22#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h"
23#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
24#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h"
25
26class GURL;
27
28namespace WebKit {
29class WebFrame;
30class WebURLLoader;
31struct WebURLError;
32}
33
34namespace webkit_glue {
35
36class ResourceFetcher : public WebKit::WebURLLoaderClient {
37 public:
38  // This will be called when the URL has been fetched, successfully or not.
39  // If there is a failure, response and data will both be empty.  |response|
40  // and |data| are both valid until the URLFetcher instance is destroyed.
41  typedef Callback2<const WebKit::WebURLResponse&,
42                    const std::string&>::Type Callback;
43
44  // We need a frame to make requests.
45  ResourceFetcher(
46      const GURL& url, WebKit::WebFrame* frame,
47      WebKit::WebURLRequest::TargetType target_type, Callback* callback);
48  ~ResourceFetcher();
49
50  // Stop the request and don't call the callback.
51  void Cancel();
52
53  bool completed() const { return completed_; }
54
55 protected:
56  // WebURLLoaderClient methods:
57  virtual void willSendRequest(
58      WebKit::WebURLLoader* loader, WebKit::WebURLRequest& new_request,
59      const WebKit::WebURLResponse& redirect_response);
60  virtual void didSendData(
61      WebKit::WebURLLoader* loader, unsigned long long bytes_sent,
62      unsigned long long total_bytes_to_be_sent);
63  virtual void didReceiveResponse(
64      WebKit::WebURLLoader* loader, const WebKit::WebURLResponse& response);
65  virtual void didReceiveCachedMetadata(
66      WebKit::WebURLLoader* loader, const char* data, int data_length);
67
68  virtual void didReceiveData(
69      WebKit::WebURLLoader* loader, const char* data, int data_length,
70      int encoded_data_length);
71  virtual void didFinishLoading(
72      WebKit::WebURLLoader* loader, double finishTime);
73  virtual void didFail(
74      WebKit::WebURLLoader* loader, const WebKit::WebURLError& error);
75
76  scoped_ptr<WebKit::WebURLLoader> loader_;
77
78  // URL we're fetching
79  GURL url_;
80
81  // Target type
82  WebKit::WebURLRequest::TargetType target_type_;
83
84  // A copy of the original resource response
85  WebKit::WebURLResponse response_;
86
87  // Set to true once the request is compelte.
88  bool completed_;
89
90 private:
91  // Start the actual download.
92  void Start(WebKit::WebFrame* frame);
93
94  void RunCallback(const WebKit::WebURLResponse& response,
95                   const std::string& data);
96
97  // Callback when we're done
98  scoped_ptr<Callback> callback_;
99
100  // Buffer to hold the content from the server.
101  std::string data_;
102
103  // Buffer to hold metadata from the cache.
104  std::string metadata_;
105};
106
107/////////////////////////////////////////////////////////////////////////////
108// A resource fetcher with a timeout
109class ResourceFetcherWithTimeout : public ResourceFetcher {
110 public:
111  ResourceFetcherWithTimeout(const GURL& url,
112                             WebKit::WebFrame* frame,
113                             WebKit::WebURLRequest::TargetType target_type,
114                             int timeout_secs,
115                             Callback* callback);
116  virtual ~ResourceFetcherWithTimeout();
117
118 private:
119  // Callback for timer that limits how long we wait for the alternate error
120  // page server.  If this timer fires and the request hasn't completed, we
121  // kill the request.
122  void TimeoutFired();
123
124  // Limit how long we wait for the alternate error page server.
125  base::OneShotTimer<ResourceFetcherWithTimeout> timeout_timer_;
126};
127
128}  // namespace webkit_glue
129
130#endif  // WEBKIT_GLUE_RESOURCE_FETCHER_H_
131