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.
5 #ifndef CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_
6 #define CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_
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"
30 class ResourceFetcherImpl
: public ResourceFetcher
,
31 public blink::WebURLLoaderClient
{
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::RequestContext request_context
,
40 blink::WebURLRequest::FrameType frame_type
,
41 const Callback
& callback
) OVERRIDE
;
42 virtual void SetTimeout(const base::TimeDelta
& timeout
) OVERRIDE
;
45 friend class ResourceFetcher
;
47 explicit ResourceFetcherImpl(const GURL
& url
);
49 virtual ~ResourceFetcherImpl();
51 void RunCallback(const blink::WebURLResponse
& response
,
52 const std::string
& data
);
54 // Callback for timer that limits how long we wait for the server. If this
55 // timer fires and the request hasn't completed, we kill the request.
58 // WebURLLoaderClient methods:
59 virtual void willSendRequest(
60 blink::WebURLLoader
* loader
, blink::WebURLRequest
& new_request
,
61 const blink::WebURLResponse
& redirect_response
);
62 virtual void didSendData(
63 blink::WebURLLoader
* loader
, unsigned long long bytes_sent
,
64 unsigned long long total_bytes_to_be_sent
);
65 virtual void didReceiveResponse(
66 blink::WebURLLoader
* loader
, const blink::WebURLResponse
& response
);
67 virtual void didReceiveCachedMetadata(
68 blink::WebURLLoader
* loader
, const char* data
, int data_length
);
70 virtual void didReceiveData(
71 blink::WebURLLoader
* loader
, const char* data
, int data_length
,
72 int encoded_data_length
);
73 virtual void didFinishLoading(
74 blink::WebURLLoader
* loader
, double finishTime
,
75 int64_t total_encoded_data_length
);
77 blink::WebURLLoader
* loader
, const blink::WebURLError
& error
);
79 scoped_ptr
<blink::WebURLLoader
> loader_
;
81 // Request to send. Released once Start() is called.
82 blink::WebURLRequest request_
;
84 // Set to true once the request is complete.
87 // Buffer to hold the content from the server.
90 // A copy of the original resource response.
91 blink::WebURLResponse response_
;
93 // Callback when we're done.
96 // Buffer to hold metadata from the cache.
97 std::string metadata_
;
99 // Limit how long to wait for the server.
100 base::OneShotTimer
<ResourceFetcherImpl
> timeout_timer_
;
102 DISALLOW_COPY_AND_ASSIGN(ResourceFetcherImpl
);
105 } // namespace content
107 #endif // CONTENT_RENDERER_FETCHERS_RESOURCE_FETCHER_H_