Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: webkit/renderer/media/media_info_loader.cc

Issue 16327002: android: Implement single origin and CORS check for video (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fetch media info in parallel. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/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/Source/WebKit/chromium/public/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
scherkus (not reviewing) 2013/06/12 01:37:58 nit: remove extra blank line
Sami 2013/06/12 13:59:31 Done.
81 // The load may have been stopped and |ready_cb| is destroyed.
82 // In this case we shouldn't do anything.
83 if (ready_cb_.is_null()) {
84 // Set the url in the request to an invalid value (empty url).
85 newRequest.setURL(WebKit::WebURL());
86 return;
87 }
88
89 // Only allow |single_origin_| if we haven't seen a different origin yet.
90 if (single_origin_)
91 single_origin_ = url_.GetOrigin() == GURL(newRequest.url()).GetOrigin();
92
93 url_ = newRequest.url();
94 }
95
96 void MediaInfoLoader::didSendData(
97 WebURLLoader* loader,
98 unsigned long long bytes_sent,
99 unsigned long long total_bytes_to_be_sent) {
100 NOTIMPLEMENTED();
101 }
102
103 void MediaInfoLoader::didReceiveResponse(
104 WebURLLoader* loader,
105 const WebURLResponse& response) {
106 DVLOG(1) << "didReceiveResponse: HTTP/"
107 << (response.httpVersion() == WebURLResponse::HTTP_0_9 ? "0.9" :
108 response.httpVersion() == WebURLResponse::HTTP_1_0 ? "1.0" :
109 response.httpVersion() == WebURLResponse::HTTP_1_1 ? "1.1" :
110 "Unknown")
111 << " " << response.httpStatusCode();
112 DCHECK(active_loader_.get());
113 if (response.httpStatusCode() == kHttpOK) {
114 DidBecomeReady(kOk);
115 return;
116 }
117 loader_failed_ = true;
118 DidBecomeReady(kFailed);
119 }
120
121 void MediaInfoLoader::didReceiveData(
122 WebURLLoader* loader,
123 const char* data,
124 int data_length,
125 int encoded_data_length) {
126 // Ignored.
127 }
128
129 void MediaInfoLoader::didDownloadData(
130 WebKit::WebURLLoader* loader,
131 int dataLength) {
132 NOTIMPLEMENTED();
133 }
134
135 void MediaInfoLoader::didReceiveCachedMetadata(
136 WebURLLoader* loader,
137 const char* data,
138 int data_length) {
139 NOTIMPLEMENTED();
140 }
141
142 void MediaInfoLoader::didFinishLoading(
143 WebURLLoader* loader,
144 double finishTime) {
145 DCHECK(active_loader_.get());
146 DidBecomeReady(kOk);
147 }
148
149 void MediaInfoLoader::didFail(
150 WebURLLoader* loader,
151 const WebURLError& error) {
152 DVLOG(1) << "didFail: reason=" << error.reason
153 << ", isCancellation=" << error.isCancellation
154 << ", domain=" << error.domain.utf8().data()
155 << ", localizedDescription="
156 << error.localizedDescription.utf8().data();
157 DCHECK(active_loader_.get());
158 loader_failed_ = true;
159 DidBecomeReady(kFailed);
160 }
161
162 bool MediaInfoLoader::HasSingleOrigin() const {
163 DCHECK(ready_cb_.is_null())
164 << "Must become ready before calling HasSingleOrigin()";
165 return single_origin_;
166 }
167
168 bool MediaInfoLoader::DidPassCORSAccessCheck() const {
169 DCHECK(ready_cb_.is_null())
170 << "Must become ready before calling DidPassCORSAccessCheck()";
171 return !loader_failed_ &&
172 cors_mode_ != WebKit::WebMediaPlayer::CORSModeUnspecified;
173 }
174
175 /////////////////////////////////////////////////////////////////////////////
176 // Helper methods.
177
178 void MediaInfoLoader::DidBecomeReady(Status status) {
179 UMA_HISTOGRAM_TIMES("MediaInfoLoader.ReadyDelayTime",
scherkus (not reviewing) 2013/06/12 01:37:58 don't forget the change in src/tools/metrics/histo
Sami 2013/06/12 13:59:31 Thanks, I wasn't aware of that file.
180 base::TimeTicks::Now() - start_time_);
181 active_loader_.reset();
182 if (!ready_cb_.is_null())
183 base::ResetAndReturn(&ready_cb_).Run(status);
184 }
185
186 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698