| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/fetchers/resource_fetcher_impl.h" | 5 #include "content/renderer/fetchers/resource_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "third_party/WebKit/public/platform/Platform.h" | 10 #include "third_party/WebKit/public/platform/Platform.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 using blink::WebURLResponse; | 28 using blink::WebURLResponse; |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 ResourceFetcher* ResourceFetcher::Create(const GURL& url) { | 33 ResourceFetcher* ResourceFetcher::Create(const GURL& url) { |
| 34 return new ResourceFetcherImpl(url); | 34 return new ResourceFetcherImpl(url); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url) | 37 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url) |
| 38 : request_(url), | 38 : request_(url) { |
| 39 completed_(false) { | |
| 40 } | 39 } |
| 41 | 40 |
| 42 ResourceFetcherImpl::~ResourceFetcherImpl() { | 41 ResourceFetcherImpl::~ResourceFetcherImpl() { |
| 43 if (!completed_ && loader_) | 42 if (!completed() && loader_) |
| 44 loader_->cancel(); | 43 Cancel(); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void ResourceFetcherImpl::SetMethod(const std::string& method) { | 46 void ResourceFetcherImpl::SetMethod(const std::string& method) { |
| 48 DCHECK(!request_.isNull()); | 47 DCHECK(!request_.isNull()); |
| 49 DCHECK(!loader_); | 48 DCHECK(!loader_); |
| 50 | 49 |
| 51 request_.setHTTPMethod(blink::WebString::fromUTF8(method)); | 50 request_.setHTTPMethod(blink::WebString::fromUTF8(method)); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void ResourceFetcherImpl::SetBody(const std::string& body) { | 53 void ResourceFetcherImpl::SetBody(const std::string& body) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 78 } | 77 } |
| 79 } | 78 } |
| 80 | 79 |
| 81 void ResourceFetcherImpl::Start(WebFrame* frame, | 80 void ResourceFetcherImpl::Start(WebFrame* frame, |
| 82 WebURLRequest::RequestContext request_context, | 81 WebURLRequest::RequestContext request_context, |
| 83 WebURLRequest::FrameType frame_type, | 82 WebURLRequest::FrameType frame_type, |
| 84 const Callback& callback) { | 83 const Callback& callback) { |
| 85 DCHECK(!loader_); | 84 DCHECK(!loader_); |
| 86 DCHECK(!request_.isNull()); | 85 DCHECK(!request_.isNull()); |
| 87 DCHECK(callback_.is_null()); | 86 DCHECK(callback_.is_null()); |
| 88 DCHECK(!completed_); | 87 DCHECK(!completed()); |
| 89 if (!request_.httpBody().isNull()) | 88 if (!request_.httpBody().isNull()) |
| 90 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies."; | 89 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies."; |
| 91 | 90 |
| 92 callback_ = callback; | 91 callback_ = callback; |
| 93 | 92 |
| 94 request_.setRequestContext(request_context); | 93 request_.setRequestContext(request_context); |
| 95 request_.setFrameType(frame_type); | 94 request_.setFrameType(frame_type); |
| 96 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies()); | 95 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies()); |
| 97 frame->dispatchWillSendRequest(request_); | 96 frame->dispatchWillSendRequest(request_); |
| 98 loader_.reset(blink::Platform::current()->createURLLoader()); | 97 loader_.reset(blink::Platform::current()->createURLLoader()); |
| 99 loader_->loadAsynchronously(request_, this); | 98 loader_->loadAsynchronously(request_, this); |
| 100 | 99 |
| 101 // No need to hold on to the request. | 100 // No need to hold on to the request. |
| 102 request_.reset(); | 101 request_.reset(); |
| 103 } | 102 } |
| 104 | 103 |
| 105 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) { | 104 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) { |
| 106 DCHECK(loader_); | 105 DCHECK(loader_); |
| 107 DCHECK(!completed_); | 106 DCHECK(!completed()); |
| 108 | 107 |
| 109 timeout_timer_.Start(FROM_HERE, timeout, this, | 108 timeout_timer_.Start(FROM_HERE, timeout, this, &ResourceFetcherImpl::Cancel); |
| 110 &ResourceFetcherImpl::TimeoutFired); | |
| 111 } | 109 } |
| 112 | 110 |
| 113 void ResourceFetcherImpl::RunCallback(const WebURLResponse& response, | 111 void ResourceFetcherImpl::OnLoadComplete() { |
| 114 const std::string& data) { | |
| 115 completed_ = true; | |
| 116 timeout_timer_.Stop(); | 112 timeout_timer_.Stop(); |
| 117 if (callback_.is_null()) | 113 if (callback_.is_null()) |
| 118 return; | 114 return; |
| 119 | 115 |
| 120 // Take a reference to the callback as running the callback may lead to our | 116 // Take a reference to the callback as running the callback may lead to our |
| 121 // destruction. | 117 // destruction. |
| 122 Callback callback = callback_; | 118 Callback callback = callback_; |
| 123 callback.Run(response, data); | 119 callback.Run(status() == LOAD_FAILED ? blink::WebURLResponse() : response(), |
| 120 status() == LOAD_FAILED ? std::string() : data()); |
| 124 } | 121 } |
| 125 | 122 |
| 126 void ResourceFetcherImpl::TimeoutFired() { | 123 void ResourceFetcherImpl::Cancel() { |
| 127 DCHECK(!completed_); | |
| 128 loader_->cancel(); | 124 loader_->cancel(); |
| 129 RunCallback(WebURLResponse(), std::string()); | 125 WebURLLoaderClientImpl::Cancel(); |
| 130 } | |
| 131 | |
| 132 ///////////////////////////////////////////////////////////////////////////// | |
| 133 // WebURLLoaderClient methods | |
| 134 | |
| 135 void ResourceFetcherImpl::willSendRequest( | |
| 136 WebURLLoader* loader, WebURLRequest& new_request, | |
| 137 const WebURLResponse& redirect_response) { | |
| 138 } | |
| 139 | |
| 140 void ResourceFetcherImpl::didSendData( | |
| 141 WebURLLoader* loader, unsigned long long bytes_sent, | |
| 142 unsigned long long total_bytes_to_be_sent) { | |
| 143 } | |
| 144 | |
| 145 void ResourceFetcherImpl::didReceiveResponse( | |
| 146 WebURLLoader* loader, const WebURLResponse& response) { | |
| 147 DCHECK(!completed_); | |
| 148 response_ = response; | |
| 149 } | |
| 150 | |
| 151 void ResourceFetcherImpl::didReceiveData( | |
| 152 WebURLLoader* loader, const char* data, int data_length, | |
| 153 int encoded_data_length) { | |
| 154 DCHECK(!completed_); | |
| 155 DCHECK(data_length > 0); | |
| 156 | |
| 157 data_.append(data, data_length); | |
| 158 } | |
| 159 | |
| 160 void ResourceFetcherImpl::didReceiveCachedMetadata( | |
| 161 WebURLLoader* loader, const char* data, int data_length) { | |
| 162 DCHECK(!completed_); | |
| 163 DCHECK(data_length > 0); | |
| 164 | |
| 165 metadata_.assign(data, data_length); | |
| 166 } | |
| 167 | |
| 168 void ResourceFetcherImpl::didFinishLoading( | |
| 169 WebURLLoader* loader, double finishTime, | |
| 170 int64_t total_encoded_data_length) { | |
| 171 DCHECK(!completed_); | |
| 172 | |
| 173 RunCallback(response_, data_); | |
| 174 } | |
| 175 | |
| 176 void ResourceFetcherImpl::didFail(WebURLLoader* loader, | |
| 177 const WebURLError& error) { | |
| 178 DCHECK(!completed_); | |
| 179 | |
| 180 // Go ahead and tell our delegate that we're done. | |
| 181 RunCallback(WebURLResponse(), std::string()); | |
| 182 } | 126 } |
| 183 | 127 |
| 184 } // namespace content | 128 } // namespace content |
| OLD | NEW |