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