| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/renderer/media/android/media_info_loader_android.h" |
| 6 |
| 7 #include "base/bits.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 10 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 11 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
| 12 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 13 |
| 14 using WebKit::WebFrame; |
| 15 using WebKit::WebURLError; |
| 16 using WebKit::WebURLLoader; |
| 17 using WebKit::WebURLLoaderOptions; |
| 18 using WebKit::WebURLRequest; |
| 19 using WebKit::WebURLResponse; |
| 20 |
| 21 namespace webkit_media { |
| 22 |
| 23 static const int kHttpOK = 200; |
| 24 |
| 25 MediaInfoLoaderAndroid::MediaInfoLoaderAndroid( |
| 26 const GURL& url, |
| 27 WebKit::WebMediaPlayer::CORSMode cors_mode, |
| 28 const ReadyCB& ready_cb) |
| 29 : loader_failed_(false), |
| 30 url_(url), |
| 31 cors_mode_(cors_mode), |
| 32 single_origin_(true), |
| 33 ready_cb_(ready_cb) {} |
| 34 |
| 35 MediaInfoLoaderAndroid::~MediaInfoLoaderAndroid() {} |
| 36 |
| 37 void MediaInfoLoaderAndroid::Start(WebKit::WebFrame* frame) { |
| 38 // Make sure we have not started. |
| 39 DCHECK(!ready_cb_.is_null()); |
| 40 CHECK(frame); |
| 41 |
| 42 // Prepare the request. |
| 43 WebURLRequest request(url_); |
| 44 request.setTargetType(WebURLRequest::TargetIsMedia); |
| 45 frame->setReferrerForRequest(request, WebKit::WebURL()); |
| 46 |
| 47 scoped_ptr<WebURLLoader> loader; |
| 48 if (test_loader_) { |
| 49 loader = test_loader_.Pass(); |
| 50 } else { |
| 51 WebURLLoaderOptions options; |
| 52 if (cors_mode_ == WebKit::WebMediaPlayer::CORSModeUnspecified) { |
| 53 options.allowCredentials = true; |
| 54 options.crossOriginRequestPolicy = |
| 55 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 56 } else { |
| 57 options.exposeAllResponseHeaders = true; |
| 58 options.crossOriginRequestPolicy = |
| 59 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
| 60 if (cors_mode_ == WebKit::WebMediaPlayer::CORSModeUseCredentials) |
| 61 options.allowCredentials = true; |
| 62 } |
| 63 loader.reset(frame->createAssociatedURLLoader(options)); |
| 64 } |
| 65 |
| 66 // Start the resource loading. |
| 67 loader->loadAsynchronously(request, this); |
| 68 active_loader_.reset(new ActiveLoader(loader.Pass())); |
| 69 } |
| 70 |
| 71 ///////////////////////////////////////////////////////////////////////////// |
| 72 // WebKit::WebURLLoaderClient implementation. |
| 73 void MediaInfoLoaderAndroid::willSendRequest( |
| 74 WebURLLoader* loader, |
| 75 WebURLRequest& newRequest, |
| 76 const WebURLResponse& redirectResponse) { |
| 77 |
| 78 // The load may have been stopped and |ready_cb| is destroyed. |
| 79 // In this case we shouldn't do anything. |
| 80 if (ready_cb_.is_null()) { |
| 81 // Set the url in the request to an invalid value (empty url). |
| 82 newRequest.setURL(WebKit::WebURL()); |
| 83 return; |
| 84 } |
| 85 |
| 86 // Only allow |single_origin_| if we haven't seen a different origin yet. |
| 87 if (single_origin_) |
| 88 single_origin_ = url_.GetOrigin() == GURL(newRequest.url()).GetOrigin(); |
| 89 |
| 90 url_ = newRequest.url(); |
| 91 } |
| 92 |
| 93 void MediaInfoLoaderAndroid::didSendData( |
| 94 WebURLLoader* loader, |
| 95 unsigned long long bytes_sent, |
| 96 unsigned long long total_bytes_to_be_sent) { |
| 97 NOTIMPLEMENTED(); |
| 98 } |
| 99 |
| 100 void MediaInfoLoaderAndroid::didReceiveResponse( |
| 101 WebURLLoader* loader, |
| 102 const WebURLResponse& response) { |
| 103 DVLOG(1) << "didReceiveResponse: HTTP/" |
| 104 << (response.httpVersion() == WebURLResponse::HTTP_0_9 ? "0.9" : |
| 105 response.httpVersion() == WebURLResponse::HTTP_1_0 ? "1.0" : |
| 106 response.httpVersion() == WebURLResponse::HTTP_1_1 ? "1.1" : |
| 107 "Unknown") |
| 108 << " " << response.httpStatusCode(); |
| 109 DCHECK(active_loader_.get()); |
| 110 if (response.httpStatusCode() == kHttpOK) { |
| 111 DidBecomeReady(kOk); |
| 112 } else { |
| 113 loader_failed_ = true; |
| 114 DidBecomeReady(kFailed); |
| 115 } |
| 116 } |
| 117 |
| 118 void MediaInfoLoaderAndroid::didReceiveData( |
| 119 WebURLLoader* loader, |
| 120 const char* data, |
| 121 int data_length, |
| 122 int encoded_data_length) { |
| 123 // Ignored. |
| 124 } |
| 125 |
| 126 void MediaInfoLoaderAndroid::didDownloadData( |
| 127 WebKit::WebURLLoader* loader, |
| 128 int dataLength) { |
| 129 NOTIMPLEMENTED(); |
| 130 } |
| 131 |
| 132 void MediaInfoLoaderAndroid::didReceiveCachedMetadata( |
| 133 WebURLLoader* loader, |
| 134 const char* data, |
| 135 int data_length) { |
| 136 NOTIMPLEMENTED(); |
| 137 } |
| 138 |
| 139 void MediaInfoLoaderAndroid::didFinishLoading( |
| 140 WebURLLoader* loader, |
| 141 double finishTime) { |
| 142 DCHECK(active_loader_.get()); |
| 143 DidBecomeReady(kOk); |
| 144 } |
| 145 |
| 146 void MediaInfoLoaderAndroid::didFail( |
| 147 WebURLLoader* loader, |
| 148 const WebURLError& error) { |
| 149 DVLOG(1) << "didFail: reason=" << error.reason |
| 150 << ", isCancellation=" << error.isCancellation |
| 151 << ", domain=" << error.domain.utf8().data() |
| 152 << ", localizedDescription=" |
| 153 << error.localizedDescription.utf8().data(); |
| 154 DCHECK(active_loader_.get()); |
| 155 loader_failed_ = true; |
| 156 DidBecomeReady(kFailed); |
| 157 } |
| 158 |
| 159 bool MediaInfoLoaderAndroid::HasSingleOrigin() const { |
| 160 DCHECK(ready_cb_.is_null()) |
| 161 << "Must become ready before calling HasSingleOrigin()"; |
| 162 return single_origin_; |
| 163 } |
| 164 |
| 165 bool MediaInfoLoaderAndroid::DidPassCORSAccessCheck() const { |
| 166 DCHECK(ready_cb_.is_null()) |
| 167 << "Must become ready before calling DidPassCORSAccessCheck()"; |
| 168 return !loader_failed_ && |
| 169 cors_mode_ != WebKit::WebMediaPlayer::CORSModeUnspecified; |
| 170 } |
| 171 |
| 172 ///////////////////////////////////////////////////////////////////////////// |
| 173 // Helper methods. |
| 174 |
| 175 void MediaInfoLoaderAndroid::DidBecomeReady(Status status) { |
| 176 active_loader_.reset(); |
| 177 if (!ready_cb_.is_null()) |
| 178 base::ResetAndReturn(&ready_cb_).Run(status); |
| 179 } |
| 180 |
| 181 } // namespace webkit_media |
| OLD | NEW |