Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / fetchers / resource_fetcher_impl.h
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 #ifndef CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_
6 #define CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/timer/timer.h"
15 #include "content/public/renderer/resource_fetcher.h"
16 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
17 #include "third_party/WebKit/public/platform/WebURLRequest.h"
18 #include "third_party/WebKit/public/platform/WebURLResponse.h"
19
20 class GURL;
21
22 namespace blink {
23 class WebFrame;
24 class WebURLLoader;
25 struct WebURLError;
26 }
27
28 namespace content {
29
30 class ResourceFetcherImpl : public ResourceFetcher,
31                             public blink::WebURLLoaderClient {
32  public:
33   // ResourceFetcher implementation:
34   virtual void SetMethod(const std::string& method) OVERRIDE;
35   virtual void SetBody(const std::string& body) OVERRIDE;
36   virtual void SetHeader(const std::string& header,
37                          const std::string& value) OVERRIDE;
38   virtual void Start(blink::WebFrame* frame,
39                      blink::WebURLRequest::TargetType target_type,
40                      const Callback& callback) OVERRIDE;
41   virtual void SetTimeout(const base::TimeDelta& timeout) OVERRIDE;
42
43  private:
44   friend class ResourceFetcher;
45
46   explicit ResourceFetcherImpl(const GURL& url);
47
48   virtual ~ResourceFetcherImpl();
49
50   void RunCallback(const blink::WebURLResponse& response,
51                    const std::string& data);
52
53   // Callback for timer that limits how long we wait for the server.  If this
54   // timer fires and the request hasn't completed, we kill the request.
55   void TimeoutFired();
56
57   // WebURLLoaderClient methods:
58   virtual void willSendRequest(
59       blink::WebURLLoader* loader, blink::WebURLRequest& new_request,
60       const blink::WebURLResponse& redirect_response);
61   virtual void didSendData(
62       blink::WebURLLoader* loader, unsigned long long bytes_sent,
63       unsigned long long total_bytes_to_be_sent);
64   virtual void didReceiveResponse(
65       blink::WebURLLoader* loader, const blink::WebURLResponse& response);
66   virtual void didReceiveCachedMetadata(
67       blink::WebURLLoader* loader, const char* data, int data_length);
68
69   virtual void didReceiveData(
70       blink::WebURLLoader* loader, const char* data, int data_length,
71       int encoded_data_length);
72   virtual void didFinishLoading(
73       blink::WebURLLoader* loader, double finishTime,
74       int64_t total_encoded_data_length);
75   virtual void didFail(
76       blink::WebURLLoader* loader, const blink::WebURLError& error);
77
78   scoped_ptr<blink::WebURLLoader> loader_;
79
80   // Request to send.  Released once Start() is called.
81   blink::WebURLRequest request_;
82
83   // Set to true once the request is complete.
84   bool completed_;
85
86   // Buffer to hold the content from the server.
87   std::string data_;
88
89   // A copy of the original resource response.
90   blink::WebURLResponse response_;
91
92   // Callback when we're done.
93   Callback callback_;
94
95   // Buffer to hold metadata from the cache.
96   std::string metadata_;
97
98   // Limit how long to wait for the server.
99   base::OneShotTimer<ResourceFetcherImpl> timeout_timer_;
100
101   DISALLOW_COPY_AND_ASSIGN(ResourceFetcherImpl);
102 };
103
104 }  // namespace content
105
106 #endif  // CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_