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

Side by Side Diff: webkit/glue/media/buffered_data_source.h

Issue 3863002: Refactoring BufferedDataSource to work with WebURLLoader instead of a MediaResourceLoaderBridge. (Closed) Base URL: http://git.chromium.org/git/chromium.git
Patch Set: Removed unnecessary WebURLLoaders in the two unittests because windows compilation complains. Created 10 years, 1 month 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
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 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
6 #define WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 6 #define WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/lock.h" 11 #include "base/lock.h"
12 #include "base/scoped_ptr.h" 12 #include "base/scoped_ptr.h"
13 #include "base/timer.h" 13 #include "base/timer.h"
14 #include "base/condition_variable.h" 14 #include "base/condition_variable.h"
15 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
16 #include "media/base/filters.h" 16 #include "media/base/filters.h"
17 #include "media/base/media_format.h" 17 #include "media/base/media_format.h"
18 #include "media/base/pipeline.h" 18 #include "media/base/pipeline.h"
19 #include "media/base/seekable_buffer.h" 19 #include "media/base/seekable_buffer.h"
20 #include "net/base/completion_callback.h" 20 #include "net/base/completion_callback.h"
21 #include "net/base/file_stream.h" 21 #include "net/base/file_stream.h"
22 #include "webkit/glue/media/media_resource_loader_bridge_factory.h" 22 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
23 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h"
24 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoaderClient.h"
25 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h"
26 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h"
23 #include "webkit/glue/media/web_data_source.h" 27 #include "webkit/glue/media/web_data_source.h"
24 #include "webkit/glue/webmediaplayer_impl.h" 28 #include "webkit/glue/webmediaplayer_impl.h"
25 29
30 using WebKit::WebFrame;
31
26 namespace webkit_glue { 32 namespace webkit_glue {
33
27 ///////////////////////////////////////////////////////////////////////////// 34 /////////////////////////////////////////////////////////////////////////////
28 // BufferedResourceLoader 35 // BufferedResourceLoader
29 // This class works inside demuxer thread and render thread. It contains a 36 // This class works inside demuxer thread and render thread. It contains a
30 // resource loader bridge and does the actual resource loading. This object 37 // web url loader and does the actual resource loading. This object does
Alpha Left Google 2010/11/19 22:53:42 web url loader -> WebURLLoader remove the extra sp
31 // does buffering internally, it defers the resource loading if buffer is 38 // buffering internally, it defers the resource loading if buffer is full
32 // full and un-defers the resource loading if it is under buffered. 39 // and un-defers the resource loading if it is under buffered.
33 class BufferedResourceLoader : 40 class BufferedResourceLoader :
34 public base::RefCountedThreadSafe<BufferedResourceLoader>, 41 public base::RefCountedThreadSafe<BufferedResourceLoader>,
35 public webkit_glue::ResourceLoaderBridge::Peer { 42 public WebKit::WebURLLoaderClient {
36 public: 43 public:
37 typedef Callback0::Type NetworkEventCallback; 44 typedef Callback0::Type NetworkEventCallback;
38 45
39 // |bridge_factory| - Factory to create a ResourceLoaderBridge.
40 // |url| - URL for the resource to be loaded. 46 // |url| - URL for the resource to be loaded.
41 // |first_byte_position| - First byte to start loading from, -1 for not 47 // |first_byte_position| - First byte to start loading from, -1 for not
42 // specified. 48 // specified.
43 // |last_byte_position| - Last byte to be loaded, -1 for not specified. 49 // |last_byte_position| - Last byte to be loaded, -1 for not specified.
44 BufferedResourceLoader( 50 BufferedResourceLoader(
45 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory, 51 const GURL& url,
46 const GURL& url, 52 int64 first_byte_position,
47 int64 first_byte_position, 53 int64 last_byte_position);
48 int64 last_byte_position);
49 54
50 // Start the resource loading with the specified URL and range. 55 // Start the resource loading with the specified URL and range.
51 // This method operates in asynchronous mode. Once there's a response from the 56 // This method operates in asynchronous mode. Once there's a response from the
52 // server, success or fail |callback| is called with the result. 57 // server, success or fail |callback| is called with the result.
53 // |callback| is called with the following values: 58 // |callback| is called with the following values:
54 // - net::OK 59 // - net::OK
55 // The request has started successfully. 60 // The request has started successfully.
56 // - net::ERR_FAILED 61 // - net::ERR_FAILED
57 // The request has failed because of an error with the network. 62 // The request has failed because of an error with the network.
58 // - net::ERR_INVALID_RESPONSE 63 // - net::ERR_INVALID_RESPONSE
59 // An invalid response is received from the server. 64 // An invalid response is received from the server.
60 // - (Anything else) 65 // - (Anything else)
61 // An error code that indicates the request has failed. 66 // An error code that indicates the request has failed.
62 // |event_callback| is called when the response is completed, data is 67 // |event_callback| is called when the response is completed, data is
63 // received, the request is suspended or resumed. 68 // received, the request is suspended or resumed.
64 virtual void Start(net::CompletionCallback* callback, 69 virtual void Start(net::CompletionCallback* callback,
65 NetworkEventCallback* event_callback); 70 NetworkEventCallback* event_callback,
71 WebKit::WebFrame* frame);
66 72
67 // Stop this loader, cancels and request and release internal buffer. 73 // Stop this loader, cancels and request and release internal buffer.
68 virtual void Stop(); 74 virtual void Stop();
69 75
70 // Reads the specified |read_size| from |position| into |buffer| and when 76 // Reads the specified |read_size| from |position| into |buffer| and when
71 // the operation is done invoke |callback| with number of bytes read or an 77 // the operation is done invoke |callback| with number of bytes read or an
72 // error code. 78 // error code.
73 // |callback| is called with the following values: 79 // |callback| is called with the following values:
74 // - (Anything greater than or equal 0) 80 // - (Anything greater than or equal 0)
75 // Read was successful with the indicated number of bytes read. 81 // Read was successful with the indicated number of bytes read.
(...skipping 26 matching lines...) Expand all
102 // Returns true if the response for this loader is a partial response. 108 // Returns true if the response for this loader is a partial response.
103 // It means a 206 response in HTTP/HTTPS protocol. 109 // It means a 206 response in HTTP/HTTPS protocol.
104 virtual bool partial_response() { return partial_response_; } 110 virtual bool partial_response() { return partial_response_; }
105 111
106 // Returns true if network is currently active. 112 // Returns true if network is currently active.
107 virtual bool network_activity() { return !completed_ && !deferred_; } 113 virtual bool network_activity() { return !completed_ && !deferred_; }
108 114
109 // Returns resulting URL. 115 // Returns resulting URL.
110 virtual const GURL& url() { return url_; } 116 virtual const GURL& url() { return url_; }
111 117
118 // Used to inject a mock used for unittests.
119 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader);
120
112 ///////////////////////////////////////////////////////////////////////////// 121 /////////////////////////////////////////////////////////////////////////////
113 // webkit_glue::ResourceLoaderBridge::Peer implementations. 122 // WebKit::WebURLLoaderClient implementations.
114 virtual void OnUploadProgress(uint64 position, uint64 size) {} 123 virtual void willSendRequest(
115 virtual bool OnReceivedRedirect( 124 WebKit::WebURLLoader* loader,
116 const GURL& new_url, 125 WebKit::WebURLRequest& newRequest,
117 const webkit_glue::ResourceResponseInfo& info, 126 const WebKit::WebURLResponse& redirectResponse);
118 bool* has_new_first_party_for_cookies, 127 virtual void didSendData(
119 GURL* new_first_party_for_cookies); 128 WebKit::WebURLLoader* loader,
120 virtual void OnReceivedResponse( 129 unsigned long long bytesSent,
121 const webkit_glue::ResourceResponseInfo& info, 130 unsigned long long totalBytesToBeSent);
122 bool content_filtered); 131 virtual void didReceiveResponse(
123 virtual void OnDownloadedData(int len) {} 132 WebKit::WebURLLoader* loader,
124 virtual void OnReceivedData(const char* data, int len); 133 const WebKit::WebURLResponse& response);
125 virtual void OnCompletedRequest( 134 virtual void didDownloadData(
126 const URLRequestStatus& status, 135 WebKit::WebURLLoader* loader,
127 const std::string& security_info, 136 int dataLength);
128 const base::Time& completion_time); 137 virtual void didReceiveData(
138 WebKit::WebURLLoader* loader,
139 const char* data,
140 int dataLength);
141 virtual void didReceiveCachedMetadata(
142 WebKit::WebURLLoader* loader,
143 const char* data, int dataLength);
144 virtual void didFinishLoading(
145 WebKit::WebURLLoader* loader,
146 double finishTime);
147 virtual void didFail(
148 WebKit::WebURLLoader* loader,
149 const WebKit::WebURLError&);
129 150
130 protected: 151 protected:
131 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; 152 friend class base::RefCountedThreadSafe<BufferedResourceLoader>;
132 153
133 virtual ~BufferedResourceLoader(); 154 virtual ~BufferedResourceLoader();
134 155
135 private: 156 private:
136 friend class BufferedResourceLoaderTest; 157 friend class BufferedResourceLoaderTest;
137 158
138 // Defer the resource loading if the buffer is full. 159 // Defer the resource loading if the buffer is full.
139 void EnableDeferIfNeeded(); 160 void EnableDeferIfNeeded();
140 161
141 // Disable defer loading if we are under-buffered. 162 // Disable defer loading if we are under-buffered.
142 void DisableDeferIfNeeded(); 163 void DisableDeferIfNeeded();
143 164
144 // Returns true if the current read request can be fulfilled by what is in 165 // Returns true if the current read request can be fulfilled by what is in
145 // the buffer. 166 // the buffer.
146 bool CanFulfillRead(); 167 bool CanFulfillRead();
147 168
148 // Returns true if the current read request will be fulfilled in the future. 169 // Returns true if the current read request will be fulfilled in the future.
149 bool WillFulfillRead(); 170 bool WillFulfillRead();
150 171
151 // Method that does the actual read and calls the |read_callbac_|, assuming 172 // Method that does the actual read and calls the |read_callbac_|, assuming
152 // the request range is in |buffer_|. 173 // the request range is in |buffer_|.
153 void ReadInternal(); 174 void ReadInternal();
154 175
155 // If we have made a range request, verify the response from the server. 176 // If we have made a range request, verify the response from the server.
156 bool VerifyPartialResponse(const ResourceResponseInfo& info); 177 bool VerifyPartialResponse(const WebKit::WebURLResponse& response);
178
179 // Gets the lower and upper content ranges from the content-range or range
180 // header within a response. Returns true on success.
181 bool ReadContentRanges(const WebKit::WebURLResponse& response,
182 int* content_range_lower_bound,
183 int* content_range_upper_bound,
184 int* content_range_instance_size);
185
186 // Returns the value for a range request header using parameters
187 // |first_byte_position| and |last_byte_position|. Negative numbers other
188 // than -1 are not allowed for |first_byte_position| and |last_byte_position|.
189 // |first_byte_position| should always be less than or equal to
190 // |last_byte_position| if they are both not -1.
191 // Empty string is returned on invalid parameters.
192 std::string GenerateHeaders(int64 first_byte_position,
193 int64 last_byte_position);
157 194
158 // Done with read. Invokes the read callback and reset parameters for the 195 // Done with read. Invokes the read callback and reset parameters for the
159 // read request. 196 // read request.
160 void DoneRead(int error); 197 void DoneRead(int error);
161 198
162 // Done with start. Invokes the start callback and reset it. 199 // Done with start. Invokes the start callback and reset it.
163 void DoneStart(int error); 200 void DoneStart(int error);
164 201
165 // Calls |event_callback_| in terms of a network event. 202 // Calls |event_callback_| in terms of a network event.
166 void NotifyNetworkEvent(); 203 void NotifyNetworkEvent();
(...skipping 11 matching lines...) Expand all
178 215
179 // True if resource loading has completed. 216 // True if resource loading has completed.
180 bool completed_; 217 bool completed_;
181 218
182 // True if a range request was made. 219 // True if a range request was made.
183 bool range_requested_; 220 bool range_requested_;
184 221
185 // True if response data received is a partial range. 222 // True if response data received is a partial range.
186 bool partial_response_; 223 bool partial_response_;
187 224
188 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory_; 225 // Does the work of loading and sends data back to this client.
226 scoped_ptr<WebKit::WebURLLoader> url_loader_;
227
189 GURL url_; 228 GURL url_;
190 int64 first_byte_position_; 229 int64 first_byte_position_;
191 int64 last_byte_position_; 230 int64 last_byte_position_;
192 231
193 // Callback method that listens to network events. 232 // Callback method that listens to network events.
194 scoped_ptr<NetworkEventCallback> event_callback_; 233 scoped_ptr<NetworkEventCallback> event_callback_;
195 234
196 // Members used during request start. 235 // Members used during request start.
197 scoped_ptr<net::CompletionCallback> start_callback_; 236 scoped_ptr<net::CompletionCallback> start_callback_;
198 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_;
199 int64 offset_; 237 int64 offset_;
200 int64 content_length_; 238 int64 content_length_;
201 int64 instance_size_; 239 int64 instance_size_;
202 240
203 // Members used during a read operation. They should be reset after each 241 // Members used during a read operation. They should be reset after each
204 // read has completed or failed. 242 // read has completed or failed.
205 scoped_ptr<net::CompletionCallback> read_callback_; 243 scoped_ptr<net::CompletionCallback> read_callback_;
206 int64 read_position_; 244 int64 read_position_;
207 int read_size_; 245 int read_size_;
208 uint8* read_buffer_; 246 uint8* read_buffer_;
209 247
210 // Offsets of the requested first byte and last byte in |buffer_|. They are 248 // Offsets of the requested first byte and last byte in |buffer_|. They are
211 // written by VerifyRead(). 249 // written by VerifyRead().
212 int first_offset_; 250 int first_offset_;
213 int last_offset_; 251 int last_offset_;
214 252
253 // Used to ensure mocks for unittests are used instead of reset in Start().
254 bool keep_test_loader;
255
215 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); 256 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader);
216 }; 257 };
217 258
218 class BufferedDataSource : public WebDataSource { 259 class BufferedDataSource : public WebDataSource {
219 public: 260 public:
220 BufferedDataSource( 261 BufferedDataSource(
221 MessageLoop* render_loop, 262 MessageLoop* render_loop,
222 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory); 263 WebKit::WebFrame* frame);
223 264
224 virtual ~BufferedDataSource(); 265 virtual ~BufferedDataSource();
225 266
226 // media::MediaFilter implementation. 267 // media::MediaFilter implementation.
227 virtual void Initialize(const std::string& url, 268 virtual void Initialize(const std::string& url,
228 media::FilterCallback* callback); 269 media::FilterCallback* callback);
229 virtual bool IsUrlSupported(const std::string& url); 270 virtual bool IsUrlSupported(const std::string& url);
230 virtual void Stop(media::FilterCallback* callback); 271 virtual void Stop(media::FilterCallback* callback);
231 virtual void SetPlaybackRate(float playback_rate); 272 virtual void SetPlaybackRate(float playback_rate);
232 273
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // need to protect it. 372 // need to protect it.
332 int64 total_bytes_; 373 int64 total_bytes_;
333 374
334 // True if this data source is considered loaded. 375 // True if this data source is considered loaded.
335 bool loaded_; 376 bool loaded_;
336 377
337 // This value will be true if this data source can only support streaming. 378 // This value will be true if this data source can only support streaming.
338 // i.e. range request is not supported. 379 // i.e. range request is not supported.
339 bool streaming_; 380 bool streaming_;
340 381
382 // A webframe for loading.
383 WebKit::WebFrame* frame_;
384
341 // True if the media resource has a single origin. 385 // True if the media resource has a single origin.
342 bool single_origin_; 386 bool single_origin_;
343 387
344 // A factory object to produce ResourceLoaderBridge.
345 scoped_ptr<webkit_glue::MediaResourceLoaderBridgeFactory> bridge_factory_;
346
347 // A resource loader for the media resource. 388 // A resource loader for the media resource.
348 scoped_refptr<BufferedResourceLoader> loader_; 389 scoped_refptr<BufferedResourceLoader> loader_;
349 390
350 // True if network is active. 391 // True if network is active.
351 bool network_activity_; 392 bool network_activity_;
352 393
353 // Callback method from the pipeline for initialization. 394 // Callback method from the pipeline for initialization.
354 scoped_ptr<media::FilterCallback> initialize_callback_; 395 scoped_ptr<media::FilterCallback> initialize_callback_;
355 396
356 // Read parameters received from the Read() method call. 397 // Read parameters received from the Read() method call.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 // Keeps track of whether we used a Range header in the initialization 442 // Keeps track of whether we used a Range header in the initialization
402 // request. 443 // request.
403 bool using_range_request_; 444 bool using_range_request_;
404 445
405 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); 446 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource);
406 }; 447 };
407 448
408 } // namespace webkit_glue 449 } // namespace webkit_glue
409 450
410 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_ 451 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_DATA_SOURCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698