| 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 #include "webkit/glue/resource_fetcher.h" | 5 #include "webkit/glue/resource_fetcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitPlatformSupport
.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitPlatformSupport
.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 15 | 15 |
| 16 using base::TimeDelta; | 16 using base::TimeDelta; |
| 17 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 18 using WebKit::WebURLError; | 18 using WebKit::WebURLError; |
| 19 using WebKit::WebURLLoader; | 19 using WebKit::WebURLLoader; |
| 20 using WebKit::WebURLRequest; | 20 using WebKit::WebURLRequest; |
| 21 using WebKit::WebURLResponse; | 21 using WebKit::WebURLResponse; |
| 22 | 22 |
| 23 namespace webkit_glue { | 23 namespace webkit_glue { |
| 24 | 24 |
| 25 ResourceFetcher::ResourceFetcher(const GURL& url, WebFrame* frame, | 25 ResourceFetcher::ResourceFetcher(const GURL& url, WebFrame* frame, |
| 26 WebURLRequest::TargetType target_type, | 26 WebURLRequest::TargetType target_type, |
| 27 Callback* callback) | 27 const Callback& callback) |
| 28 : url_(url), | 28 : url_(url), |
| 29 target_type_(target_type), | 29 target_type_(target_type), |
| 30 completed_(false), | 30 completed_(false), |
| 31 callback_(callback) { | 31 callback_(callback) { |
| 32 // Can't do anything without a frame. However, delegate can be NULL (so we | 32 // Can't do anything without a frame. However, delegate can be NULL (so we |
| 33 // can do a http request and ignore the results). | 33 // can do a http request and ignore the results). |
| 34 DCHECK(frame); | 34 DCHECK(frame); |
| 35 Start(frame); | 35 Start(frame); |
| 36 } | 36 } |
| 37 | 37 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 WebURLRequest request(url_); | 51 WebURLRequest request(url_); |
| 52 request.setTargetType(target_type_); | 52 request.setTargetType(target_type_); |
| 53 frame->dispatchWillSendRequest(request); | 53 frame->dispatchWillSendRequest(request); |
| 54 | 54 |
| 55 loader_.reset(WebKit::webKitPlatformSupport()->createURLLoader()); | 55 loader_.reset(WebKit::webKitPlatformSupport()->createURLLoader()); |
| 56 loader_->loadAsynchronously(request, this); | 56 loader_->loadAsynchronously(request, this); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void ResourceFetcher::RunCallback(const WebURLResponse& response, | 59 void ResourceFetcher::RunCallback(const WebURLResponse& response, |
| 60 const std::string& data) { | 60 const std::string& data) { |
| 61 if (!callback_.get()) | 61 if (callback_.is_null()) |
| 62 return; | 62 return; |
| 63 | 63 |
| 64 // Take care to clear callback_ before running the callback as it may lead to | 64 // Take a reference to the callback as running the callback may lead to our |
| 65 // our destruction. | 65 // destruction. |
| 66 scoped_ptr<Callback> callback; | 66 Callback callback = callback_; |
| 67 callback.swap(callback_); | 67 callback.Run(response, data); |
| 68 callback->Run(response, data); | |
| 69 } | 68 } |
| 70 | 69 |
| 71 ///////////////////////////////////////////////////////////////////////////// | 70 ///////////////////////////////////////////////////////////////////////////// |
| 72 // WebURLLoaderClient methods | 71 // WebURLLoaderClient methods |
| 73 | 72 |
| 74 void ResourceFetcher::willSendRequest( | 73 void ResourceFetcher::willSendRequest( |
| 75 WebURLLoader* loader, WebURLRequest& new_request, | 74 WebURLLoader* loader, WebURLRequest& new_request, |
| 76 const WebURLResponse& redirect_response) { | 75 const WebURLResponse& redirect_response) { |
| 77 } | 76 } |
| 78 | 77 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 117 |
| 119 // Go ahead and tell our delegate that we're done. | 118 // Go ahead and tell our delegate that we're done. |
| 120 RunCallback(WebURLResponse(), std::string()); | 119 RunCallback(WebURLResponse(), std::string()); |
| 121 } | 120 } |
| 122 | 121 |
| 123 ///////////////////////////////////////////////////////////////////////////// | 122 ///////////////////////////////////////////////////////////////////////////// |
| 124 // A resource fetcher with a timeout | 123 // A resource fetcher with a timeout |
| 125 | 124 |
| 126 ResourceFetcherWithTimeout::ResourceFetcherWithTimeout( | 125 ResourceFetcherWithTimeout::ResourceFetcherWithTimeout( |
| 127 const GURL& url, WebFrame* frame, WebURLRequest::TargetType target_type, | 126 const GURL& url, WebFrame* frame, WebURLRequest::TargetType target_type, |
| 128 int timeout_secs, Callback* callback) | 127 int timeout_secs, const Callback& callback) |
| 129 : ResourceFetcher(url, frame, target_type, callback) { | 128 : ResourceFetcher(url, frame, target_type, callback) { |
| 130 timeout_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(timeout_secs), this, | 129 timeout_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(timeout_secs), this, |
| 131 &ResourceFetcherWithTimeout::TimeoutFired); | 130 &ResourceFetcherWithTimeout::TimeoutFired); |
| 132 } | 131 } |
| 133 | 132 |
| 134 ResourceFetcherWithTimeout::~ResourceFetcherWithTimeout() { | 133 ResourceFetcherWithTimeout::~ResourceFetcherWithTimeout() { |
| 135 } | 134 } |
| 136 | 135 |
| 137 void ResourceFetcherWithTimeout::TimeoutFired() { | 136 void ResourceFetcherWithTimeout::TimeoutFired() { |
| 138 if (!completed_) { | 137 if (!completed_) { |
| 139 loader_->cancel(); | 138 loader_->cancel(); |
| 140 didFail(NULL, WebURLError()); | 139 didFail(NULL, WebURLError()); |
| 141 } | 140 } |
| 142 } | 141 } |
| 143 | 142 |
| 144 } // namespace webkit_glue | 143 } // namespace webkit_glue |
| OLD | NEW |