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

Side by Side Diff: webkit/media/android/media_info_loader_android.cc

Issue 16327002: android: Implement single origin and CORS check for video (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added tests. 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/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/Platform/chromium/public/WebURLError.h"
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLLoader.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.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 ready_cb_.Reset();
37 active_loader_.reset();
38 }
39
40 void MediaInfoLoaderAndroid::Start(WebKit::WebFrame* frame) {
41 // Make sure we have not started.
42 DCHECK(!ready_cb_.is_null());
43 CHECK(frame);
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 MediaInfoLoaderAndroid::willSendRequest(
77 WebURLLoader* loader,
78 WebURLRequest& newRequest,
79 const WebURLResponse& redirectResponse) {
80
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 MediaInfoLoaderAndroid::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 MediaInfoLoaderAndroid::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 } else {
116 loader_failed_ = true;
117 DidBecomeReady(kFailed);
118 }
119 }
120
121 void MediaInfoLoaderAndroid::didReceiveData(
122 WebURLLoader* loader,
123 const char* data,
124 int data_length,
125 int encoded_data_length) {
126 // 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
127 }
128
129 void MediaInfoLoaderAndroid::didDownloadData(
130 WebKit::WebURLLoader* loader,
131 int dataLength) {
132 NOTIMPLEMENTED();
133 }
134
135 void MediaInfoLoaderAndroid::didReceiveCachedMetadata(
136 WebURLLoader* loader,
137 const char* data,
138 int data_length) {
139 NOTIMPLEMENTED();
140 }
141
142 void MediaInfoLoaderAndroid::didFinishLoading(
143 WebURLLoader* loader,
144 double finishTime) {
145 DCHECK(active_loader_.get());
146 DidBecomeReady(kOk);
147 }
148
149 void MediaInfoLoaderAndroid::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 MediaInfoLoaderAndroid::HasSingleOrigin() const {
163 DCHECK(ready_cb_.is_null())
164 << "Must become ready before calling HasSingleOrigin()";
165 return single_origin_;
166 }
167
168 bool MediaInfoLoaderAndroid::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 MediaInfoLoaderAndroid::DidBecomeReady(Status status) {
179 active_loader_.reset();
180 if (!ready_cb_.is_null())
181 base::ResetAndReturn(&ready_cb_).Run(status);
182 }
183
184 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698