OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
7 #include "media/base/filter_host.h" | 7 #include "media/base/filter_host.h" |
8 #include "net/base/load_flags.h" | 8 #include "net/base/load_flags.h" |
9 #include "net/base/data_url.h" | 9 #include "net/base/data_url.h" |
10 #include "net/http/http_response_headers.h" | |
11 #include "net/url_request/url_request_status.h" | 10 #include "net/url_request/url_request_status.h" |
11 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" | |
12 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" | |
12 #include "webkit/glue/media/simple_data_source.h" | 13 #include "webkit/glue/media/simple_data_source.h" |
13 #include "webkit/glue/resource_loader_bridge.h" | |
14 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char kHttpScheme[] = "http"; | 18 const char kHttpScheme[] = "http"; |
19 const char kHttpsScheme[] = "https"; | 19 const char kHttpsScheme[] = "https"; |
20 const char kDataScheme[] = "data"; | 20 const char kDataScheme[] = "data"; |
21 | 21 |
22 // A helper method that accepts only HTTP, HTTPS and FILE protocol. | 22 // A helper method that accepts only HTTP, HTTPS and FILE protocol. |
23 bool IsDataProtocol(const GURL& url) { | 23 bool IsDataProtocol(const GURL& url) { |
24 return url.SchemeIs(kDataScheme); | 24 return url.SchemeIs(kDataScheme); |
25 } | 25 } |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 namespace webkit_glue { | 29 namespace webkit_glue { |
30 | 30 |
31 SimpleDataSource::SimpleDataSource( | 31 SimpleDataSource::SimpleDataSource( |
32 MessageLoop* render_loop, | 32 MessageLoop* render_loop, |
33 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) | 33 WebKit::WebFrame* frame) |
34 : render_loop_(render_loop), | 34 : render_loop_(render_loop), |
35 bridge_factory_(bridge_factory), | 35 frame_(frame), |
36 size_(-1), | 36 size_(-1), |
37 single_origin_(true), | 37 single_origin_(true), |
38 state_(UNINITIALIZED) { | 38 state_(UNINITIALIZED), |
39 keep_test_loader(false) { | |
39 DCHECK(render_loop); | 40 DCHECK(render_loop); |
40 } | 41 } |
41 | 42 |
42 SimpleDataSource::~SimpleDataSource() { | 43 SimpleDataSource::~SimpleDataSource() { |
43 AutoLock auto_lock(lock_); | 44 AutoLock auto_lock(lock_); |
44 DCHECK(state_ == UNINITIALIZED || state_ == STOPPED); | 45 DCHECK(state_ == UNINITIALIZED || state_ == STOPPED); |
45 } | 46 } |
46 | 47 |
47 void SimpleDataSource::Stop(media::FilterCallback* callback) { | 48 void SimpleDataSource::Stop(media::FilterCallback* callback) { |
48 AutoLock auto_lock(lock_); | 49 AutoLock auto_lock(lock_); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 | 102 |
102 bool SimpleDataSource::GetSize(int64* size_out) { | 103 bool SimpleDataSource::GetSize(int64* size_out) { |
103 *size_out = size_; | 104 *size_out = size_; |
104 return true; | 105 return true; |
105 } | 106 } |
106 | 107 |
107 bool SimpleDataSource::IsStreaming() { | 108 bool SimpleDataSource::IsStreaming() { |
108 return false; | 109 return false; |
109 } | 110 } |
110 | 111 |
111 bool SimpleDataSource::OnReceivedRedirect( | 112 void SimpleDataSource::SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader) { |
112 const GURL& new_url, | 113 url_loader_.reset(mock_loader); |
113 const webkit_glue::ResourceResponseInfo& info, | 114 keep_test_loader = true; |
114 bool* has_new_first_party_for_cookies, | |
115 GURL* new_first_party_for_cookies) { | |
116 DCHECK(MessageLoop::current() == render_loop_); | |
117 single_origin_ = url_.GetOrigin() == new_url.GetOrigin(); | |
118 | |
119 // TODO(wtc): should we return a new first party for cookies URL? | |
120 *has_new_first_party_for_cookies = false; | |
121 return true; | |
122 } | 115 } |
123 | 116 |
124 void SimpleDataSource::OnReceivedResponse( | 117 void SimpleDataSource::willSendRequest( |
125 const webkit_glue::ResourceResponseInfo& info, | 118 WebKit::WebURLLoader* loader, |
126 bool content_filtered) { | 119 WebKit::WebURLRequest& newRequest, |
120 const WebKit::WebURLResponse& redirectResponse) { | |
127 DCHECK(MessageLoop::current() == render_loop_); | 121 DCHECK(MessageLoop::current() == render_loop_); |
128 size_ = info.content_length; | 122 single_origin_ = url_.GetOrigin() == GURL(newRequest.url()).GetOrigin(); |
123 | |
124 url_loader_.reset(loader); | |
125 url_ = newRequest.url(); | |
129 } | 126 } |
130 | 127 |
131 void SimpleDataSource::OnReceivedData(const char* data, int len) { | 128 void SimpleDataSource::didSendData( |
132 DCHECK(MessageLoop::current() == render_loop_); | 129 WebKit::WebURLLoader* loader, |
133 data_.append(data, len); | 130 unsigned long long bytesSent, |
131 unsigned long long totalBytesToBeSent) { | |
134 } | 132 } |
135 | 133 |
136 void SimpleDataSource::OnCompletedRequest(const URLRequestStatus& status, | 134 void SimpleDataSource::didReceiveResponse( |
137 const std::string& security_info, | 135 WebKit::WebURLLoader* loader, const WebKit::WebURLResponse& response) { |
138 const base::Time& completion_time) { | 136 DCHECK(MessageLoop::current() == render_loop_); |
137 size_ = response.expectedContentLength(); | |
138 } | |
139 | |
140 void SimpleDataSource::didDownloadData( | |
141 WebKit::WebURLLoader* loader, int dataLength) { | |
142 } | |
143 | |
144 void SimpleDataSource::didReceiveData( | |
145 WebKit::WebURLLoader* loader, const char* data, int data_length) { | |
146 DCHECK(MessageLoop::current() == render_loop_); | |
147 data_.append(data, data_length); | |
148 } | |
149 | |
150 void SimpleDataSource::didReceiveCachedMetadata( | |
151 WebKit::WebURLLoader* loader, const char* data, int dataLength) { | |
152 } | |
153 | |
154 void SimpleDataSource::didFinishLoading( | |
155 WebKit::WebURLLoader* loader, double finishTime) { | |
139 DCHECK(MessageLoop::current() == render_loop_); | 156 DCHECK(MessageLoop::current() == render_loop_); |
140 AutoLock auto_lock(lock_); | 157 AutoLock auto_lock(lock_); |
141 // It's possible this gets called after Stop(), in which case |host_| is no | 158 // It's possible this gets called after Stop(), in which case |host_| is no |
159 // longer valid. | |
160 if (state_ == STOPPED) | |
161 return; | |
162 | |
163 // Otherwise we should be initializing and have created a bridge. | |
Alpha Left Google
2010/11/19 22:53:42
nit: we don't use bridge any more.
| |
164 DCHECK_EQ(state_, INITIALIZING); | |
165 | |
166 // If we don't get a content length or the request has failed, report it | |
167 // as a network error. | |
168 if (size_ == -1) | |
169 size_ = data_.length(); | |
170 DCHECK(static_cast<size_t>(size_) == data_.length()); | |
171 | |
172 DoneInitialization_Locked(true); | |
173 } | |
174 | |
175 void SimpleDataSource::didFail( | |
176 WebKit::WebURLLoader* loader, | |
177 const WebKit::WebURLError& error) { | |
178 DCHECK(MessageLoop::current() == render_loop_); | |
179 AutoLock auto_lock(lock_); | |
180 // It's possible this gets called after Stop(), in which case |host_| is no | |
142 // longer valid. | 181 // longer valid. |
143 if (state_ == STOPPED) | 182 if (state_ == STOPPED) |
144 return; | 183 return; |
145 | 184 |
146 // Otherwise we should be initializing and have created a bridge. | 185 // Otherwise we should be initializing and have created a bridge. |
Alpha Left Google
2010/11/19 22:53:42
nit: we don't use bridge any more.
| |
147 DCHECK_EQ(state_, INITIALIZING); | 186 DCHECK_EQ(state_, INITIALIZING); |
148 DCHECK(bridge_.get()); | |
149 bridge_.reset(); | |
150 | 187 |
151 // If we don't get a content length or the request has failed, report it | 188 // If we don't get a content length or the request has failed, report it |
152 // as a network error. | 189 // as a network error. |
153 if (size_ == -1) | 190 if (size_ == -1) |
154 size_ = data_.length(); | 191 size_ = data_.length(); |
155 DCHECK(static_cast<size_t>(size_) == data_.length()); | 192 DCHECK(static_cast<size_t>(size_) == data_.length()); |
156 | 193 |
157 DoneInitialization_Locked(status.is_success()); | 194 DoneInitialization_Locked(false); |
158 } | 195 } |
159 | 196 |
160 bool SimpleDataSource::HasSingleOrigin() { | 197 bool SimpleDataSource::HasSingleOrigin() { |
161 DCHECK(MessageLoop::current() == render_loop_); | 198 DCHECK(MessageLoop::current() == render_loop_); |
162 return single_origin_; | 199 return single_origin_; |
163 } | 200 } |
164 | 201 |
165 void SimpleDataSource::Abort() { | 202 void SimpleDataSource::Abort() { |
166 DCHECK(MessageLoop::current() == render_loop_); | 203 DCHECK(MessageLoop::current() == render_loop_); |
167 NOTIMPLEMENTED(); | 204 frame_ = NULL; |
168 } | 205 } |
169 | 206 |
170 void SimpleDataSource::SetURL(const GURL& url) { | 207 void SimpleDataSource::SetURL(const GURL& url) { |
171 url_ = url; | 208 url_ = url; |
172 media_format_.Clear(); | 209 media_format_.Clear(); |
173 media_format_.SetAsString(media::MediaFormat::kMimeType, | 210 media_format_.SetAsString(media::MediaFormat::kMimeType, |
174 media::mime_type::kApplicationOctetStream); | 211 media::mime_type::kApplicationOctetStream); |
175 media_format_.SetAsString(media::MediaFormat::kURL, url.spec()); | 212 media_format_.SetAsString(media::MediaFormat::kURL, url.spec()); |
176 } | 213 } |
177 | 214 |
178 void SimpleDataSource::StartTask() { | 215 void SimpleDataSource::StartTask() { |
179 DCHECK(MessageLoop::current() == render_loop_); | 216 DCHECK(MessageLoop::current() == render_loop_); |
180 AutoLock auto_lock(lock_); | 217 AutoLock auto_lock(lock_); |
181 | 218 |
182 // We may have stopped. | 219 // We may have stopped. |
183 if (state_ == STOPPED) | 220 if (state_ == STOPPED) |
184 return; | 221 return; |
185 | 222 |
223 if (frame_ == NULL) | |
224 return; | |
225 | |
186 DCHECK_EQ(state_, INITIALIZING); | 226 DCHECK_EQ(state_, INITIALIZING); |
187 | 227 |
188 if (IsDataProtocol(url_)) { | 228 if (IsDataProtocol(url_)) { |
189 // If this using data protocol, we just need to decode it. | 229 // If this using data protocol, we just need to decode it. |
190 std::string mime_type, charset; | 230 std::string mime_type, charset; |
191 bool success = net::DataURL::Parse(url_, &mime_type, &charset, &data_); | 231 bool success = net::DataURL::Parse(url_, &mime_type, &charset, &data_); |
192 | 232 |
193 // Don't care about the mime-type just proceed if decoding was successful. | 233 // Don't care about the mime-type just proceed if decoding was successful. |
194 size_ = data_.length(); | 234 size_ = data_.length(); |
195 DoneInitialization_Locked(success); | 235 DoneInitialization_Locked(success); |
196 } else { | 236 } else { |
197 // Create our bridge and start loading the resource. | 237 // Prepare the request. |
198 bridge_.reset(bridge_factory_->CreateBridge( | 238 WebKit::WebURLRequest request(url_); |
199 url_, net::LOAD_BYPASS_CACHE, -1, -1)); | 239 |
200 bridge_->Start(this); | 240 frame_->setReferrerForRequest(request, WebKit::WebURL()); |
241 frame_->dispatchWillSendRequest(request); | |
242 | |
243 // This flag is for unittests as we don't want to reset |url_loader| | |
244 if (!keep_test_loader) | |
245 url_loader_.reset(WebKit::webKitClient()->createURLLoader()); | |
246 | |
247 // Start the resource loading. | |
248 url_loader_->loadAsynchronously(request, this); | |
201 } | 249 } |
202 } | 250 } |
203 | 251 |
204 void SimpleDataSource::CancelTask() { | 252 void SimpleDataSource::CancelTask() { |
205 DCHECK(MessageLoop::current() == render_loop_); | 253 DCHECK(MessageLoop::current() == render_loop_); |
206 AutoLock auto_lock(lock_); | 254 AutoLock auto_lock(lock_); |
207 DCHECK_EQ(state_, STOPPED); | 255 DCHECK_EQ(state_, STOPPED); |
208 | 256 |
209 // Cancel any pending requests. | 257 // Cancel any pending requests. |
210 if (bridge_.get()) { | 258 if (url_loader_.get()) { |
211 bridge_->Cancel(); | 259 url_loader_->cancel(); |
212 bridge_.reset(); | 260 url_loader_.reset(); |
213 } | 261 } |
214 } | 262 } |
215 | 263 |
216 void SimpleDataSource::DoneInitialization_Locked(bool success) { | 264 void SimpleDataSource::DoneInitialization_Locked(bool success) { |
217 lock_.AssertAcquired(); | 265 lock_.AssertAcquired(); |
218 if (success) { | 266 if (success) { |
219 state_ = INITIALIZED; | 267 state_ = INITIALIZED; |
220 host()->SetTotalBytes(size_); | 268 host()->SetTotalBytes(size_); |
221 host()->SetBufferedBytes(size_); | 269 host()->SetBufferedBytes(size_); |
222 // If scheme is file or data, say we are loaded. | 270 // If scheme is file or data, say we are loaded. |
223 host()->SetLoaded(url_.SchemeIsFile() || IsDataProtocol(url_)); | 271 host()->SetLoaded(url_.SchemeIsFile() || IsDataProtocol(url_)); |
224 } else { | 272 } else { |
225 host()->SetError(media::PIPELINE_ERROR_NETWORK); | 273 host()->SetError(media::PIPELINE_ERROR_NETWORK); |
226 } | 274 } |
227 initialize_callback_->Run(); | 275 initialize_callback_->Run(); |
228 initialize_callback_.reset(); | 276 initialize_callback_.reset(); |
229 } | 277 } |
230 | 278 |
231 } // namespace webkit_glue | 279 } // namespace webkit_glue |
OLD | NEW |