| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/callback_old.h" | 18 #include "base/callback.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/timer.h" | 20 #include "base/timer.h" |
| 21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 25 | 25 |
| 26 class GURL; | 26 class GURL; |
| 27 | 27 |
| 28 namespace WebKit { | 28 namespace WebKit { |
| 29 class WebFrame; | 29 class WebFrame; |
| 30 class WebURLLoader; | 30 class WebURLLoader; |
| 31 struct WebURLError; | 31 struct WebURLError; |
| 32 } | 32 } |
| 33 | 33 |
| 34 namespace webkit_glue { | 34 namespace webkit_glue { |
| 35 | 35 |
| 36 class ResourceFetcher : public WebKit::WebURLLoaderClient { | 36 class ResourceFetcher : public WebKit::WebURLLoaderClient { |
| 37 public: | 37 public: |
| 38 // This will be called when the URL has been fetched, successfully or not. | 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| | 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. | 40 // and |data| are both valid until the URLFetcher instance is destroyed. |
| 41 typedef Callback2<const WebKit::WebURLResponse&, | 41 typedef base::Callback<void(const WebKit::WebURLResponse&, |
| 42 const std::string&>::Type Callback; | 42 const std::string&)> Callback; |
| 43 | 43 |
| 44 // We need a frame to make requests. | 44 // We need a frame to make requests. |
| 45 ResourceFetcher( | 45 ResourceFetcher( |
| 46 const GURL& url, WebKit::WebFrame* frame, | 46 const GURL& url, WebKit::WebFrame* frame, |
| 47 WebKit::WebURLRequest::TargetType target_type, Callback* callback); | 47 WebKit::WebURLRequest::TargetType target_type, |
| 48 const Callback& callback); |
| 48 virtual ~ResourceFetcher(); | 49 virtual ~ResourceFetcher(); |
| 49 | 50 |
| 50 // Stop the request and don't call the callback. | 51 // Stop the request and don't call the callback. |
| 51 void Cancel(); | 52 void Cancel(); |
| 52 | 53 |
| 53 bool completed() const { return completed_; } | 54 bool completed() const { return completed_; } |
| 54 | 55 |
| 55 protected: | 56 protected: |
| 56 // WebURLLoaderClient methods: | 57 // WebURLLoaderClient methods: |
| 57 virtual void willSendRequest( | 58 virtual void willSendRequest( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 88 bool completed_; | 89 bool completed_; |
| 89 | 90 |
| 90 private: | 91 private: |
| 91 // Start the actual download. | 92 // Start the actual download. |
| 92 void Start(WebKit::WebFrame* frame); | 93 void Start(WebKit::WebFrame* frame); |
| 93 | 94 |
| 94 void RunCallback(const WebKit::WebURLResponse& response, | 95 void RunCallback(const WebKit::WebURLResponse& response, |
| 95 const std::string& data); | 96 const std::string& data); |
| 96 | 97 |
| 97 // Callback when we're done | 98 // Callback when we're done |
| 98 scoped_ptr<Callback> callback_; | 99 Callback callback_; |
| 99 | 100 |
| 100 // Buffer to hold the content from the server. | 101 // Buffer to hold the content from the server. |
| 101 std::string data_; | 102 std::string data_; |
| 102 | 103 |
| 103 // Buffer to hold metadata from the cache. | 104 // Buffer to hold metadata from the cache. |
| 104 std::string metadata_; | 105 std::string metadata_; |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 ///////////////////////////////////////////////////////////////////////////// | 108 ///////////////////////////////////////////////////////////////////////////// |
| 108 // A resource fetcher with a timeout | 109 // A resource fetcher with a timeout |
| 109 class ResourceFetcherWithTimeout : public ResourceFetcher { | 110 class ResourceFetcherWithTimeout : public ResourceFetcher { |
| 110 public: | 111 public: |
| 111 ResourceFetcherWithTimeout(const GURL& url, | 112 ResourceFetcherWithTimeout(const GURL& url, |
| 112 WebKit::WebFrame* frame, | 113 WebKit::WebFrame* frame, |
| 113 WebKit::WebURLRequest::TargetType target_type, | 114 WebKit::WebURLRequest::TargetType target_type, |
| 114 int timeout_secs, | 115 int timeout_secs, |
| 115 Callback* callback); | 116 const Callback& callback); |
| 116 virtual ~ResourceFetcherWithTimeout(); | 117 virtual ~ResourceFetcherWithTimeout(); |
| 117 | 118 |
| 118 private: | 119 private: |
| 119 // Callback for timer that limits how long we wait for the alternate error | 120 // 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 // page server. If this timer fires and the request hasn't completed, we |
| 121 // kill the request. | 122 // kill the request. |
| 122 void TimeoutFired(); | 123 void TimeoutFired(); |
| 123 | 124 |
| 124 // Limit how long we wait for the alternate error page server. | 125 // Limit how long we wait for the alternate error page server. |
| 125 base::OneShotTimer<ResourceFetcherWithTimeout> timeout_timer_; | 126 base::OneShotTimer<ResourceFetcherWithTimeout> timeout_timer_; |
| 126 }; | 127 }; |
| 127 | 128 |
| 128 } // namespace webkit_glue | 129 } // namespace webkit_glue |
| 129 | 130 |
| 130 #endif // WEBKIT_GLUE_RESOURCE_FETCHER_H_ | 131 #endif // WEBKIT_GLUE_RESOURCE_FETCHER_H_ |
| OLD | NEW |