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