Chromium Code Reviews| Index: webkit/media/android/media_info_loader_android.cc |
| diff --git a/webkit/media/android/media_info_loader_android.cc b/webkit/media/android/media_info_loader_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..65c6731f086b7f84cc3edbec8b8b5c433b07b88c |
| --- /dev/null |
| +++ b/webkit/media/android/media_info_loader_android.cc |
| @@ -0,0 +1,184 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/media/android/media_info_loader_android.h" |
| + |
| +#include "base/bits.h" |
| +#include "base/callback_helpers.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebURLLoader.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| + |
| +using WebKit::WebFrame; |
| +using WebKit::WebURLError; |
| +using WebKit::WebURLLoader; |
| +using WebKit::WebURLLoaderOptions; |
| +using WebKit::WebURLRequest; |
| +using WebKit::WebURLResponse; |
| + |
| +namespace webkit_media { |
| + |
| +static const int kHttpOK = 200; |
| + |
| +MediaInfoLoaderAndroid::MediaInfoLoaderAndroid( |
| + const GURL& url, |
| + WebKit::WebMediaPlayer::CORSMode cors_mode, |
| + const ReadyCB& ready_cb) |
| + : loader_failed_(false), |
| + url_(url), |
| + cors_mode_(cors_mode), |
| + single_origin_(true), |
| + ready_cb_(ready_cb) {} |
| + |
| +MediaInfoLoaderAndroid::~MediaInfoLoaderAndroid() { |
| + ready_cb_.Reset(); |
| + active_loader_.reset(); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::Start(WebKit::WebFrame* frame) { |
| + // Make sure we have not started. |
| + DCHECK(!ready_cb_.is_null()); |
| + CHECK(frame); |
| + |
| + // Prepare the request. |
| + WebURLRequest request(url_); |
| + request.setTargetType(WebURLRequest::TargetIsMedia); |
| + frame->setReferrerForRequest(request, WebKit::WebURL()); |
| + |
| + scoped_ptr<WebURLLoader> loader; |
| + if (test_loader_) { |
| + loader = test_loader_.Pass(); |
| + } else { |
| + WebURLLoaderOptions options; |
| + if (cors_mode_ == WebKit::WebMediaPlayer::CORSModeUnspecified) { |
| + options.allowCredentials = true; |
| + options.crossOriginRequestPolicy = |
| + WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| + } else { |
| + options.exposeAllResponseHeaders = true; |
| + options.crossOriginRequestPolicy = |
| + WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
| + if (cors_mode_ == WebKit::WebMediaPlayer::CORSModeUseCredentials) |
| + options.allowCredentials = true; |
| + } |
| + loader.reset(frame->createAssociatedURLLoader(options)); |
| + } |
| + |
| + // Start the resource loading. |
| + loader->loadAsynchronously(request, this); |
| + active_loader_.reset(new ActiveLoader(loader.Pass())); |
| +} |
| + |
| +///////////////////////////////////////////////////////////////////////////// |
| +// WebKit::WebURLLoaderClient implementation. |
| +void MediaInfoLoaderAndroid::willSendRequest( |
| + WebURLLoader* loader, |
| + WebURLRequest& newRequest, |
| + const WebURLResponse& redirectResponse) { |
| + |
| + // The load may have been stopped and |ready_cb| is destroyed. |
| + // In this case we shouldn't do anything. |
| + if (ready_cb_.is_null()) { |
| + // Set the url in the request to an invalid value (empty url). |
| + newRequest.setURL(WebKit::WebURL()); |
| + return; |
| + } |
| + |
| + // Only allow |single_origin_| if we haven't seen a different origin yet. |
| + if (single_origin_) |
| + single_origin_ = url_.GetOrigin() == GURL(newRequest.url()).GetOrigin(); |
| + |
| + url_ = newRequest.url(); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didSendData( |
| + WebURLLoader* loader, |
| + unsigned long long bytes_sent, |
| + unsigned long long total_bytes_to_be_sent) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didReceiveResponse( |
| + WebURLLoader* loader, |
| + const WebURLResponse& response) { |
| + DVLOG(1) << "didReceiveResponse: HTTP/" |
| + << (response.httpVersion() == WebURLResponse::HTTP_0_9 ? "0.9" : |
| + response.httpVersion() == WebURLResponse::HTTP_1_0 ? "1.0" : |
| + response.httpVersion() == WebURLResponse::HTTP_1_1 ? "1.1" : |
| + "Unknown") |
| + << " " << response.httpStatusCode(); |
| + DCHECK(active_loader_.get()); |
| + if (response.httpStatusCode() == kHttpOK) { |
| + DidBecomeReady(kOk); |
| + } else { |
| + loader_failed_ = true; |
| + DidBecomeReady(kFailed); |
| + } |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didReceiveData( |
| + WebURLLoader* loader, |
| + const char* data, |
| + int data_length, |
| + int encoded_data_length) { |
| + // Ignored. |
|
scherkus (not reviewing)
2013/06/03 16:57:04
so if this was a 100MB media resource do we end up
Sami
2013/06/03 17:12:56
No, because as soon as we get a response from the
scherkus (not reviewing)
2013/06/03 17:37:39
OK .. but this is still an additional HTTP roundtr
|
| +} |
| + |
| +void MediaInfoLoaderAndroid::didDownloadData( |
| + WebKit::WebURLLoader* loader, |
| + int dataLength) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didReceiveCachedMetadata( |
| + WebURLLoader* loader, |
| + const char* data, |
| + int data_length) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didFinishLoading( |
| + WebURLLoader* loader, |
| + double finishTime) { |
| + DCHECK(active_loader_.get()); |
| + DidBecomeReady(kOk); |
| +} |
| + |
| +void MediaInfoLoaderAndroid::didFail( |
| + WebURLLoader* loader, |
| + const WebURLError& error) { |
| + DVLOG(1) << "didFail: reason=" << error.reason |
| + << ", isCancellation=" << error.isCancellation |
| + << ", domain=" << error.domain.utf8().data() |
| + << ", localizedDescription=" |
| + << error.localizedDescription.utf8().data(); |
| + DCHECK(active_loader_.get()); |
| + loader_failed_ = true; |
| + DidBecomeReady(kFailed); |
| +} |
| + |
| +bool MediaInfoLoaderAndroid::HasSingleOrigin() const { |
| + DCHECK(ready_cb_.is_null()) |
| + << "Must become ready before calling HasSingleOrigin()"; |
| + return single_origin_; |
| +} |
| + |
| +bool MediaInfoLoaderAndroid::DidPassCORSAccessCheck() const { |
| + DCHECK(ready_cb_.is_null()) |
| + << "Must become ready before calling DidPassCORSAccessCheck()"; |
| + return !loader_failed_ && |
| + cors_mode_ != WebKit::WebMediaPlayer::CORSModeUnspecified; |
| +} |
| + |
| +///////////////////////////////////////////////////////////////////////////// |
| +// Helper methods. |
| + |
| +void MediaInfoLoaderAndroid::DidBecomeReady(Status status) { |
| + active_loader_.reset(); |
| + if (!ready_cb_.is_null()) |
| + base::ResetAndReturn(&ready_cb_).Run(status); |
| +} |
| + |
| +} // namespace webkit_media |