OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A wrapper around ResourceHandle and ResourceHandleClient that simplifies | 5 // A wrapper around ResourceHandle and ResourceHandleClient that simplifies |
6 // the download of an HTTP object. The interface is modeled after URLFetcher | 6 // the download of an HTTP object. The interface is modeled after URLFetcher |
7 // in the /chrome/browser. | 7 // in the /chrome/browser. |
8 // | 8 // |
9 // ResourceFetcher::Delegate::OnURLFetchComplete will be called async after | 9 // ResourceFetcher::Delegate::OnURLFetchComplete will be called async after |
10 // the ResourceFetcher object is created. | 10 // the ResourceFetcher object is created. |
11 | 11 |
12 #ifndef WEBKIT_GLUE_RESOURCE_FETCHER_H__ | 12 #ifndef WEBKIT_GLUE_RESOURCE_FETCHER_H_ |
13 #define WEBKIT_GLUE_RESOURCE_FETCHER_H__ | 13 #define WEBKIT_GLUE_RESOURCE_FETCHER_H_ |
14 | 14 |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/scoped_ptr.h" | 18 #include "base/scoped_ptr.h" |
| 19 #include "base/timer.h" |
19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
20 | 21 #include "webkit/api/public/WebURLLoaderClient.h" |
21 #include "base/compiler_specific.h" | 22 #include "webkit/api/public/WebURLResponse.h" |
22 | |
23 MSVC_PUSH_WARNING_LEVEL(0); | |
24 #include "Frame.h" | |
25 #include "Timer.h" | |
26 #include "ResourceHandleClient.h" | |
27 #include "ResourceResponse.h" | |
28 MSVC_POP_WARNING(); | |
29 | 23 |
30 class GURL; | 24 class GURL; |
| 25 class WebFrame; |
31 | 26 |
32 class ResourceFetcher : public WebCore::ResourceHandleClient { | 27 namespace WebKit { |
| 28 class WebURLLoader; |
| 29 class WebURLRequest; |
| 30 struct WebURLError; |
| 31 } |
| 32 |
| 33 namespace webkit_glue { |
| 34 |
| 35 class ResourceFetcher : public WebKit::WebURLLoaderClient { |
33 public: | 36 public: |
34 class Delegate { | 37 // This will be called when the URL has been fetched, successfully or not. |
35 public: | 38 // If there is a failure, response and data will both be empty. |response| |
36 // This will be called when the URL has been fetched, successfully or not. | 39 // and |data| are both valid until the URLFetcher instance is destroyed. |
37 // If there is a failure, response and data will both be empty. | 40 typedef Callback2<const WebKit::WebURLResponse&, |
38 // |response| and |data| are both valid until the URLFetcher instance is | 41 const std::string&>::Type Callback; |
39 // destroyed. | |
40 virtual void OnURLFetchComplete(const WebCore::ResourceResponse& response, | |
41 const std::string& data) = 0; | |
42 }; | |
43 | 42 |
44 // We need a frame and frame loader to make requests. | 43 // We need a frame to make requests. |
45 ResourceFetcher(const GURL& url, WebCore::Frame* frame, Delegate* d); | 44 ResourceFetcher(const GURL& url, WebFrame* frame, Callback* d); |
46 ~ResourceFetcher(); | 45 ~ResourceFetcher(); |
47 | 46 |
48 // Stop the request and don't call the callback. | 47 // Stop the request and don't call the callback. |
49 void Cancel(); | 48 void Cancel(); |
50 | 49 |
51 bool completed() const { return completed_; } | 50 bool completed() const { return completed_; } |
52 | 51 |
53 // ResourceHandleClient methods | 52 protected: |
54 virtual void didReceiveResponse(WebCore::ResourceHandle* resource_handle, | 53 // WebURLLoaderClient methods: |
55 const WebCore::ResourceResponse& response); | 54 virtual void willSendRequest( |
| 55 WebKit::WebURLLoader* loader, WebKit::WebURLRequest& new_request, |
| 56 const WebKit::WebURLResponse& redirect_response); |
| 57 virtual void didSendData( |
| 58 WebKit::WebURLLoader* loader, unsigned long long bytes_sent, |
| 59 unsigned long long total_bytes_to_be_sent); |
| 60 virtual void didReceiveResponse( |
| 61 WebKit::WebURLLoader* loader, const WebKit::WebURLResponse& response); |
| 62 virtual void didReceiveData( |
| 63 WebKit::WebURLLoader* loader, const char* data, int data_length, |
| 64 long long total_data_length); |
| 65 virtual void didFinishLoading(WebKit::WebURLLoader* loader); |
| 66 virtual void didFail( |
| 67 WebKit::WebURLLoader* loader, const WebKit::WebURLError& error); |
56 | 68 |
57 virtual void didReceiveData(WebCore::ResourceHandle* resource_handle, | 69 scoped_ptr<WebKit::WebURLLoader> loader_; |
58 const char* data, int length, int total_length); | |
59 | |
60 virtual void didFinishLoading(WebCore::ResourceHandle* resource_handle); | |
61 | |
62 virtual void didFail(WebCore::ResourceHandle* resource_handle, | |
63 const WebCore::ResourceError& error); | |
64 | |
65 protected: | |
66 // The parent ResourceHandle | |
67 RefPtr<WebCore::ResourceHandle> loader_; | |
68 | 70 |
69 // URL we're fetching | 71 // URL we're fetching |
70 GURL url_; | 72 GURL url_; |
71 | 73 |
72 // Callback when we're done | 74 // Callback when we're done |
73 Delegate* delegate_; | 75 Callback* callback_; |
74 | 76 |
75 // A copy of the original resource response | 77 // A copy of the original resource response |
76 WebCore::ResourceResponse response_; | 78 WebKit::WebURLResponse response_; |
77 | 79 |
78 // Set to true once the request is compelte. | 80 // Set to true once the request is compelte. |
79 bool completed_; | 81 bool completed_; |
80 | 82 |
81 private: | 83 private: |
82 // If we fail to start the request, we still want to finish async. | |
83 typedef WebCore::Timer<ResourceFetcher> StartFailedTimer; | |
84 | |
85 // Start the actual download. | 84 // Start the actual download. |
86 void Start(WebCore::Frame* frame); | 85 void Start(WebFrame* frame); |
87 | 86 |
88 // Buffer to hold the content from the server. | 87 // Buffer to hold the content from the server. |
89 std::string data_; | 88 std::string data_; |
90 }; | 89 }; |
91 | 90 |
92 ///////////////////////////////////////////////////////////////////////////// | 91 ///////////////////////////////////////////////////////////////////////////// |
93 // A resource fetcher with a timeout | 92 // A resource fetcher with a timeout |
94 class ResourceFetcherWithTimeout : public ResourceFetcher { | 93 class ResourceFetcherWithTimeout : public ResourceFetcher { |
95 public: | 94 public: |
96 ResourceFetcherWithTimeout(const GURL& url, WebCore::Frame* frame, double | 95 ResourceFetcherWithTimeout(const GURL& url, WebFrame* frame, |
97 timeout_secs, Delegate* d); | 96 int timeout_secs, Callback* c); |
98 virtual ~ResourceFetcherWithTimeout() {} | 97 virtual ~ResourceFetcherWithTimeout() {} |
99 | 98 |
100 private: | 99 private: |
101 typedef WebCore::Timer<ResourceFetcherWithTimeout> FetchTimer; | |
102 | |
103 // Callback for timer that limits how long we wait for the alternate error | 100 // Callback for timer that limits how long we wait for the alternate error |
104 // page server. If this timer fires and the request hasn't completed, we | 101 // page server. If this timer fires and the request hasn't completed, we |
105 // kill the request. | 102 // kill the request. |
106 void TimeoutFired(FetchTimer* timer); | 103 void TimeoutFired(); |
107 | 104 |
108 // Limit how long we wait for the alternate error page server. | 105 // Limit how long we wait for the alternate error page server. |
109 scoped_ptr<FetchTimer> timeout_timer_; | 106 base::OneShotTimer<ResourceFetcherWithTimeout> timeout_timer_; |
110 }; | 107 }; |
111 | 108 |
112 #endif // WEBKIT_GLUE_RESOURCE_FETCHER_H__ | 109 } // namespace webkit_glue |
| 110 |
| 111 #endif // WEBKIT_GLUE_RESOURCE_FETCHER_H_ |
OLD | NEW |