Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
| 7 #include "media/base/filter_host.h" | 7 #include "media/base/filter_host.h" |
| 8 #include "net/base/load_flags.h" | 8 #include "net/base/load_flags.h" |
| 9 #include "net/base/data_url.h" | 9 #include "net/base/data_url.h" |
| 10 #include "net/http/http_response_headers.h" | |
| 11 #include "net/url_request/url_request_status.h" | 10 #include "net/url_request/url_request_status.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" | |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" | |
| 12 #include "webkit/glue/media/simple_data_source.h" | 13 #include "webkit/glue/media/simple_data_source.h" |
| 13 #include "webkit/glue/resource_loader_bridge.h" | |
| 14 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const char kHttpScheme[] = "http"; | 18 const char kHttpScheme[] = "http"; |
| 19 const char kHttpsScheme[] = "https"; | 19 const char kHttpsScheme[] = "https"; |
| 20 const char kDataScheme[] = "data"; | 20 const char kDataScheme[] = "data"; |
| 21 | 21 |
| 22 // A helper method that accepts only HTTP, HTTPS and FILE protocol. | 22 // A helper method that accepts only HTTP, HTTPS and FILE protocol. |
| 23 bool IsDataProtocol(const GURL& url) { | 23 bool IsDataProtocol(const GURL& url) { |
| 24 return url.SchemeIs(kDataScheme); | 24 return url.SchemeIs(kDataScheme); |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 namespace webkit_glue { | 29 namespace webkit_glue { |
| 30 | 30 |
| 31 SimpleDataSource::SimpleDataSource( | 31 SimpleDataSource::SimpleDataSource( |
| 32 MessageLoop* render_loop, | 32 MessageLoop* render_loop, |
| 33 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) | 33 WebKit::WebFrame* frame) |
| 34 : render_loop_(render_loop), | 34 : render_loop_(render_loop), |
| 35 bridge_factory_(bridge_factory), | 35 frame_(frame), |
| 36 size_(-1), | 36 size_(-1), |
| 37 single_origin_(true), | 37 single_origin_(true), |
| 38 state_(UNINITIALIZED) { | 38 state_(UNINITIALIZED), |
| 39 keep_test_loader_(false) { | |
| 39 DCHECK(render_loop); | 40 DCHECK(render_loop); |
| 40 } | 41 } |
| 41 | 42 |
| 42 SimpleDataSource::~SimpleDataSource() { | 43 SimpleDataSource::~SimpleDataSource() { |
| 43 AutoLock auto_lock(lock_); | 44 AutoLock auto_lock(lock_); |
| 44 DCHECK(state_ == UNINITIALIZED || state_ == STOPPED); | 45 DCHECK(state_ == UNINITIALIZED || state_ == STOPPED); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void SimpleDataSource::Stop(media::FilterCallback* callback) { | 48 void SimpleDataSource::Stop(media::FilterCallback* callback) { |
| 48 AutoLock auto_lock(lock_); | 49 AutoLock auto_lock(lock_); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 102 |
| 102 bool SimpleDataSource::GetSize(int64* size_out) { | 103 bool SimpleDataSource::GetSize(int64* size_out) { |
| 103 *size_out = size_; | 104 *size_out = size_; |
| 104 return true; | 105 return true; |
| 105 } | 106 } |
| 106 | 107 |
| 107 bool SimpleDataSource::IsStreaming() { | 108 bool SimpleDataSource::IsStreaming() { |
| 108 return false; | 109 return false; |
| 109 } | 110 } |
| 110 | 111 |
| 111 bool SimpleDataSource::OnReceivedRedirect( | 112 void SimpleDataSource::SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader) { |
| 112 const GURL& new_url, | 113 url_loader_.reset(mock_loader); |
| 113 const webkit_glue::ResourceResponseInfo& info, | 114 keep_test_loader_ = true; |
| 114 bool* has_new_first_party_for_cookies, | |
| 115 GURL* new_first_party_for_cookies) { | |
| 116 DCHECK(MessageLoop::current() == render_loop_); | |
| 117 single_origin_ = url_.GetOrigin() == new_url.GetOrigin(); | |
| 118 | |
| 119 // TODO(wtc): should we return a new first party for cookies URL? | |
| 120 *has_new_first_party_for_cookies = false; | |
| 121 return true; | |
| 122 } | 115 } |
| 123 | 116 |
| 124 void SimpleDataSource::OnReceivedResponse( | 117 void SimpleDataSource::willSendRequest( |
| 125 const webkit_glue::ResourceResponseInfo& info, | 118 WebKit::WebURLLoader* loader, |
| 126 bool content_filtered) { | 119 WebKit::WebURLRequest& newRequest, |
| 120 const WebKit::WebURLResponse& redirectResponse) { | |
| 127 DCHECK(MessageLoop::current() == render_loop_); | 121 DCHECK(MessageLoop::current() == render_loop_); |
| 128 size_ = info.content_length; | 122 single_origin_ = url_.GetOrigin() == GURL(newRequest.url()).GetOrigin(); |
| 123 | |
| 124 url_loader_.reset(loader); | |
|
scherkus (not reviewing)
2010/11/30 20:05:39
do we have to worry about keeping the test loader
annacc
2010/11/30 21:51:45
Probably not. I've removed this line.
| |
| 125 url_ = newRequest.url(); | |
| 129 } | 126 } |
| 130 | 127 |
| 131 void SimpleDataSource::OnReceivedData(const char* data, int len) { | 128 void SimpleDataSource::didSendData( |
| 132 DCHECK(MessageLoop::current() == render_loop_); | 129 WebKit::WebURLLoader* loader, |
| 133 data_.append(data, len); | 130 unsigned long long bytesSent, |
| 131 unsigned long long totalBytesToBeSent) { | |
| 134 } | 132 } |
| 135 | 133 |
| 136 void SimpleDataSource::OnCompletedRequest(const URLRequestStatus& status, | 134 void SimpleDataSource::didReceiveResponse( |
| 137 const std::string& security_info, | 135 WebKit::WebURLLoader* loader, |
| 138 const base::Time& completion_time) { | 136 const WebKit::WebURLResponse& response) { |
| 137 DCHECK(MessageLoop::current() == render_loop_); | |
| 138 size_ = response.expectedContentLength(); | |
| 139 } | |
| 140 | |
| 141 void SimpleDataSource::didDownloadData( | |
| 142 WebKit::WebURLLoader* loader, | |
| 143 int dataLength) { | |
| 144 } | |
|
scherkus (not reviewing)
2010/11/30 20:05:39
NOTIMPLEMENTED() for these methods?
do we expect
annacc
2010/11/30 21:51:45
No. didSendData() is only used for uploading and d
| |
| 145 | |
| 146 void SimpleDataSource::didReceiveData( | |
| 147 WebKit::WebURLLoader* loader, | |
| 148 const char* data, | |
| 149 int data_length) { | |
| 150 DCHECK(MessageLoop::current() == render_loop_); | |
| 151 data_.append(data, data_length); | |
| 152 } | |
| 153 | |
| 154 void SimpleDataSource::didReceiveCachedMetadata( | |
| 155 WebKit::WebURLLoader* loader, | |
| 156 const char* data, | |
| 157 int dataLength) { | |
| 158 } | |
| 159 | |
| 160 void SimpleDataSource::didFinishLoading( | |
| 161 WebKit::WebURLLoader* loader, | |
| 162 double finishTime) { | |
| 139 DCHECK(MessageLoop::current() == render_loop_); | 163 DCHECK(MessageLoop::current() == render_loop_); |
| 140 AutoLock auto_lock(lock_); | 164 AutoLock auto_lock(lock_); |
| 141 // It's possible this gets called after Stop(), in which case |host_| is no | 165 // It's possible this gets called after Stop(), in which case |host_| is no |
| 142 // longer valid. | 166 // longer valid. |
| 143 if (state_ == STOPPED) | 167 if (state_ == STOPPED) |
| 144 return; | 168 return; |
| 145 | 169 |
| 146 // Otherwise we should be initializing and have created a bridge. | 170 // Otherwise we should be initializing and have created a WebURLLoader. |
| 147 DCHECK_EQ(state_, INITIALIZING); | 171 DCHECK_EQ(state_, INITIALIZING); |
| 148 DCHECK(bridge_.get()); | |
| 149 bridge_.reset(); | |
| 150 | 172 |
| 151 // If we don't get a content length or the request has failed, report it | 173 // If we don't get a content length or the request has failed, report it |
| 152 // as a network error. | 174 // as a network error. |
| 153 if (size_ == -1) | 175 if (size_ == -1) |
| 154 size_ = data_.length(); | 176 size_ = data_.length(); |
| 155 DCHECK(static_cast<size_t>(size_) == data_.length()); | 177 DCHECK(static_cast<size_t>(size_) == data_.length()); |
| 156 | 178 |
| 157 DoneInitialization_Locked(status.is_success()); | 179 DoneInitialization_Locked(true); |
| 180 } | |
| 181 | |
| 182 void SimpleDataSource::didFail( | |
| 183 WebKit::WebURLLoader* loader, | |
| 184 const WebKit::WebURLError& error) { | |
| 185 DCHECK(MessageLoop::current() == render_loop_); | |
| 186 AutoLock auto_lock(lock_); | |
| 187 // It's possible this gets called after Stop(), in which case |host_| is no | |
| 188 // longer valid. | |
| 189 if (state_ == STOPPED) | |
| 190 return; | |
| 191 | |
| 192 // Otherwise we should be initializing and have created a WebURLLoader. | |
| 193 DCHECK_EQ(state_, INITIALIZING); | |
| 194 | |
| 195 // If we don't get a content length or the request has failed, report it | |
| 196 // as a network error. | |
| 197 if (size_ == -1) | |
| 198 size_ = data_.length(); | |
| 199 DCHECK(static_cast<size_t>(size_) == data_.length()); | |
| 200 | |
| 201 DoneInitialization_Locked(false); | |
| 158 } | 202 } |
| 159 | 203 |
| 160 bool SimpleDataSource::HasSingleOrigin() { | 204 bool SimpleDataSource::HasSingleOrigin() { |
| 161 DCHECK(MessageLoop::current() == render_loop_); | 205 DCHECK(MessageLoop::current() == render_loop_); |
| 162 return single_origin_; | 206 return single_origin_; |
| 163 } | 207 } |
| 164 | 208 |
| 165 void SimpleDataSource::Abort() { | 209 void SimpleDataSource::Abort() { |
| 166 DCHECK(MessageLoop::current() == render_loop_); | 210 DCHECK(MessageLoop::current() == render_loop_); |
| 167 NOTIMPLEMENTED(); | 211 frame_ = NULL; |
| 168 } | 212 } |
| 169 | 213 |
| 170 void SimpleDataSource::SetURL(const GURL& url) { | 214 void SimpleDataSource::SetURL(const GURL& url) { |
| 171 url_ = url; | 215 url_ = url; |
| 172 media_format_.Clear(); | 216 media_format_.Clear(); |
| 173 media_format_.SetAsString(media::MediaFormat::kMimeType, | 217 media_format_.SetAsString(media::MediaFormat::kMimeType, |
| 174 media::mime_type::kApplicationOctetStream); | 218 media::mime_type::kApplicationOctetStream); |
| 175 media_format_.SetAsString(media::MediaFormat::kURL, url.spec()); | 219 media_format_.SetAsString(media::MediaFormat::kURL, url.spec()); |
| 176 } | 220 } |
| 177 | 221 |
| 178 void SimpleDataSource::StartTask() { | 222 void SimpleDataSource::StartTask() { |
| 179 DCHECK(MessageLoop::current() == render_loop_); | 223 DCHECK(MessageLoop::current() == render_loop_); |
| 180 AutoLock auto_lock(lock_); | 224 AutoLock auto_lock(lock_); |
| 181 | 225 |
| 182 // We may have stopped. | 226 // We may have stopped. |
| 183 if (state_ == STOPPED) | 227 if (state_ == STOPPED) |
| 184 return; | 228 return; |
| 185 | 229 |
| 230 if (frame_ == NULL) | |
| 231 return; | |
| 232 | |
| 186 DCHECK_EQ(state_, INITIALIZING); | 233 DCHECK_EQ(state_, INITIALIZING); |
| 187 | 234 |
| 188 if (IsDataProtocol(url_)) { | 235 if (IsDataProtocol(url_)) { |
| 189 // If this using data protocol, we just need to decode it. | 236 // If this using data protocol, we just need to decode it. |
| 190 std::string mime_type, charset; | 237 std::string mime_type, charset; |
| 191 bool success = net::DataURL::Parse(url_, &mime_type, &charset, &data_); | 238 bool success = net::DataURL::Parse(url_, &mime_type, &charset, &data_); |
| 192 | 239 |
| 193 // Don't care about the mime-type just proceed if decoding was successful. | 240 // Don't care about the mime-type just proceed if decoding was successful. |
| 194 size_ = data_.length(); | 241 size_ = data_.length(); |
| 195 DoneInitialization_Locked(success); | 242 DoneInitialization_Locked(success); |
| 196 } else { | 243 } else { |
| 197 // Create our bridge and start loading the resource. | 244 // Prepare the request. |
| 198 bridge_.reset(bridge_factory_->CreateBridge( | 245 WebKit::WebURLRequest request(url_); |
| 199 url_, net::LOAD_BYPASS_CACHE, -1, -1)); | 246 |
| 200 bridge_->Start(this); | 247 frame_->setReferrerForRequest(request, WebKit::WebURL()); |
| 248 frame_->dispatchWillSendRequest(request); | |
| 249 | |
| 250 // This flag is for unittests as we don't want to reset |url_loader| | |
| 251 if (!keep_test_loader_) | |
| 252 url_loader_.reset(WebKit::webKitClient()->createURLLoader()); | |
| 253 | |
| 254 // Start the resource loading. | |
| 255 url_loader_->loadAsynchronously(request, this); | |
| 201 } | 256 } |
| 202 } | 257 } |
| 203 | 258 |
| 204 void SimpleDataSource::CancelTask() { | 259 void SimpleDataSource::CancelTask() { |
| 205 DCHECK(MessageLoop::current() == render_loop_); | 260 DCHECK(MessageLoop::current() == render_loop_); |
| 206 AutoLock auto_lock(lock_); | 261 AutoLock auto_lock(lock_); |
| 207 DCHECK_EQ(state_, STOPPED); | 262 DCHECK_EQ(state_, STOPPED); |
| 208 | 263 |
| 209 // Cancel any pending requests. | 264 // Cancel any pending requests. |
| 210 if (bridge_.get()) { | 265 if (url_loader_.get()) { |
| 211 bridge_->Cancel(); | 266 url_loader_->cancel(); |
| 212 bridge_.reset(); | 267 url_loader_.reset(); |
| 213 } | 268 } |
| 214 } | 269 } |
| 215 | 270 |
| 216 void SimpleDataSource::DoneInitialization_Locked(bool success) { | 271 void SimpleDataSource::DoneInitialization_Locked(bool success) { |
| 217 lock_.AssertAcquired(); | 272 lock_.AssertAcquired(); |
| 218 if (success) { | 273 if (success) { |
| 219 state_ = INITIALIZED; | 274 state_ = INITIALIZED; |
| 220 host()->SetTotalBytes(size_); | 275 host()->SetTotalBytes(size_); |
| 221 host()->SetBufferedBytes(size_); | 276 host()->SetBufferedBytes(size_); |
| 222 // If scheme is file or data, say we are loaded. | 277 // If scheme is file or data, say we are loaded. |
| 223 host()->SetLoaded(url_.SchemeIsFile() || IsDataProtocol(url_)); | 278 host()->SetLoaded(url_.SchemeIsFile() || IsDataProtocol(url_)); |
| 224 } else { | 279 } else { |
| 225 host()->SetError(media::PIPELINE_ERROR_NETWORK); | 280 host()->SetError(media::PIPELINE_ERROR_NETWORK); |
| 226 } | 281 } |
| 227 initialize_callback_->Run(); | 282 initialize_callback_->Run(); |
| 228 initialize_callback_.reset(); | 283 initialize_callback_.reset(); |
| 229 } | 284 } |
| 230 | 285 |
| 231 } // namespace webkit_glue | 286 } // namespace webkit_glue |
| OLD | NEW |