| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 5 #ifndef WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| 6 #define WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 6 #define WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 kFailed, | 61 kFailed, |
| 62 | 62 |
| 63 // The loader will never be able to satisfy the read request. Please stop, | 63 // The loader will never be able to satisfy the read request. Please stop, |
| 64 // delete, create a new loader, and try again. | 64 // delete, create a new loader, and try again. |
| 65 kCacheMiss, | 65 kCacheMiss, |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Keep in sync with WebMediaPlayer::CORSMode. | 68 // Keep in sync with WebMediaPlayer::CORSMode. |
| 69 enum CORSMode { kUnspecified, kAnonymous, kUseCredentials }; | 69 enum CORSMode { kUnspecified, kAnonymous, kUseCredentials }; |
| 70 | 70 |
| 71 enum LoadingState { |
| 72 kLoading, // Actively attempting to download data. |
| 73 kLoadingDeferred, // Loading intentionally deferred. |
| 74 kLoadingFinished, // Loading finished normally; no more data will arrive. |
| 75 kLoadingFailed, // Loading finished abnormally; no more data will arrive. |
| 76 }; |
| 77 |
| 71 // |url| - URL for the resource to be loaded. | 78 // |url| - URL for the resource to be loaded. |
| 72 // |cors_mode| - HTML media element's crossorigin attribute. | 79 // |cors_mode| - HTML media element's crossorigin attribute. |
| 73 // |first_byte_position| - First byte to start loading from, | 80 // |first_byte_position| - First byte to start loading from, |
| 74 // |kPositionNotSpecified| for not specified. | 81 // |kPositionNotSpecified| for not specified. |
| 75 // |last_byte_position| - Last byte to be loaded, | 82 // |last_byte_position| - Last byte to be loaded, |
| 76 // |kPositionNotSpecified| for not specified. | 83 // |kPositionNotSpecified| for not specified. |
| 77 // |strategy| is the initial loading strategy to use. | 84 // |strategy| is the initial loading strategy to use. |
| 78 // |bitrate| is the bitrate of the media, 0 if unknown. | 85 // |bitrate| is the bitrate of the media, 0 if unknown. |
| 79 // |playback_rate| is the current playback rate of the media. | 86 // |playback_rate| is the current playback rate of the media. |
| 80 BufferedResourceLoader( | 87 BufferedResourceLoader( |
| 81 const GURL& url, | 88 const GURL& url, |
| 82 CORSMode cors_mode, | 89 CORSMode cors_mode, |
| 83 int64 first_byte_position, | 90 int64 first_byte_position, |
| 84 int64 last_byte_position, | 91 int64 last_byte_position, |
| 85 DeferStrategy strategy, | 92 DeferStrategy strategy, |
| 86 int bitrate, | 93 int bitrate, |
| 87 float playback_rate, | 94 float playback_rate, |
| 88 media::MediaLog* media_log); | 95 media::MediaLog* media_log); |
| 89 virtual ~BufferedResourceLoader(); | 96 virtual ~BufferedResourceLoader(); |
| 90 | 97 |
| 91 // Start the resource loading with the specified URL and range. | 98 // Start the resource loading with the specified URL and range. |
| 92 // | 99 // |
| 93 // |event_cb| is called to notify the client of network activity in the | 100 // |loading_cb| is executed when the loading state has changed. |
| 94 // following situations: | 101 // |progress_cb| is executed when additional data has arrived. |
| 95 // - Data was received | |
| 96 // - Reading was suspended/resumed | |
| 97 // - Loading completed | |
| 98 // - Loading failed | |
| 99 typedef base::Callback<void(Status)> StartCB; | 102 typedef base::Callback<void(Status)> StartCB; |
| 103 typedef base::Callback<void(LoadingState)> LoadingStateChangedCB; |
| 104 typedef base::Callback<void(int64)> ProgressCB; |
| 100 void Start(const StartCB& start_cb, | 105 void Start(const StartCB& start_cb, |
| 101 const base::Closure& event_cb, | 106 const LoadingStateChangedCB& loading_cb, |
| 107 const ProgressCB& progress_cb, |
| 102 WebKit::WebFrame* frame); | 108 WebKit::WebFrame* frame); |
| 103 | 109 |
| 104 // Stops everything associated with this loader, including active URL loads | 110 // Stops everything associated with this loader, including active URL loads |
| 105 // and pending callbacks. | 111 // and pending callbacks. |
| 106 // | 112 // |
| 107 // It is safe to delete a BufferedResourceLoader after calling Stop(). | 113 // It is safe to delete a BufferedResourceLoader after calling Stop(). |
| 108 void Stop(); | 114 void Stop(); |
| 109 | 115 |
| 110 // Copies |read_size| bytes from |position| into |buffer|, executing |read_cb| | 116 // Copies |read_size| bytes from |position| into |buffer|, executing |read_cb| |
| 111 // when the operation has completed. | 117 // when the operation has completed. |
| 112 // | 118 // |
| 113 // The callback will contain the number of bytes read iff the status is kOk, | 119 // The callback will contain the number of bytes read iff the status is kOk, |
| 114 // zero otherwise. | 120 // zero otherwise. |
| 115 // | 121 // |
| 116 // If necessary will temporarily increase forward capacity of buffer to | 122 // If necessary will temporarily increase forward capacity of buffer to |
| 117 // accomodate an unusually large read. | 123 // accomodate an unusually large read. |
| 118 typedef base::Callback<void(Status, int)> ReadCB; | 124 typedef base::Callback<void(Status, int)> ReadCB; |
| 119 void Read(int64 position, int read_size, | 125 void Read(int64 position, int read_size, |
| 120 uint8* buffer, const ReadCB& read_cb); | 126 uint8* buffer, const ReadCB& read_cb); |
| 121 | 127 |
| 122 // Returns the position of the last byte buffered. Returns | |
| 123 // |kPositionNotSpecified| if such value is not available. | |
| 124 int64 GetBufferedPosition(); | |
| 125 | |
| 126 // Gets the content length in bytes of the instance after this loader has been | 128 // Gets the content length in bytes of the instance after this loader has been |
| 127 // started. If this value is |kPositionNotSpecified|, then content length is | 129 // started. If this value is |kPositionNotSpecified|, then content length is |
| 128 // unknown. | 130 // unknown. |
| 129 int64 content_length(); | 131 int64 content_length(); |
| 130 | 132 |
| 131 // Gets the original size of the file requested. If this value is | 133 // Gets the original size of the file requested. If this value is |
| 132 // |kPositionNotSpecified|, then the size is unknown. | 134 // |kPositionNotSpecified|, then the size is unknown. |
| 133 int64 instance_size(); | 135 int64 instance_size(); |
| 134 | 136 |
| 135 // Returns true if the server supports byte range requests. | 137 // Returns true if the server supports byte range requests. |
| 136 bool range_supported(); | 138 bool range_supported(); |
| 137 | 139 |
| 138 // Returns true if the resource loader is currently downloading data. | |
| 139 bool is_downloading_data(); | |
| 140 | |
| 141 // Returns resulting URL. | 140 // Returns resulting URL. |
| 142 const GURL& url(); | 141 const GURL& url(); |
| 143 | 142 |
| 144 // WebKit::WebURLLoaderClient implementation. | 143 // WebKit::WebURLLoaderClient implementation. |
| 145 virtual void willSendRequest( | 144 virtual void willSendRequest( |
| 146 WebKit::WebURLLoader* loader, | 145 WebKit::WebURLLoader* loader, |
| 147 WebKit::WebURLRequest& newRequest, | 146 WebKit::WebURLRequest& newRequest, |
| 148 const WebKit::WebURLResponse& redirectResponse); | 147 const WebKit::WebURLResponse& redirectResponse); |
| 149 virtual void didSendData( | 148 virtual void didSendData( |
| 150 WebKit::WebURLLoader* loader, | 149 WebKit::WebURLLoader* loader, |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 std::string GenerateHeaders(int64 first_byte_position, | 251 std::string GenerateHeaders(int64 first_byte_position, |
| 253 int64 last_byte_position); | 252 int64 last_byte_position); |
| 254 | 253 |
| 255 // Done with read. Invokes the read callback and reset parameters for the | 254 // Done with read. Invokes the read callback and reset parameters for the |
| 256 // read request. | 255 // read request. |
| 257 void DoneRead(Status status, int bytes_read); | 256 void DoneRead(Status status, int bytes_read); |
| 258 | 257 |
| 259 // Done with start. Invokes the start callback and reset it. | 258 // Done with start. Invokes the start callback and reset it. |
| 260 void DoneStart(Status status); | 259 void DoneStart(Status status); |
| 261 | 260 |
| 262 // Calls |event_cb_| in terms of a network event. | |
| 263 void NotifyNetworkEvent(); | |
| 264 | |
| 265 bool HasPendingRead() { return !read_cb_.is_null(); } | 261 bool HasPendingRead() { return !read_cb_.is_null(); } |
| 266 | 262 |
| 267 // Helper function that returns true if a range request was specified. | 263 // Helper function that returns true if a range request was specified. |
| 268 bool IsRangeRequest() const; | 264 bool IsRangeRequest() const; |
| 269 | 265 |
| 270 // Log everything interesting to |media_log_|. | 266 // Log everything interesting to |media_log_|. |
| 271 void Log(); | 267 void Log(); |
| 272 | 268 |
| 273 // A sliding window of buffer. | 269 // A sliding window of buffer. |
| 274 media::SeekableBuffer buffer_; | 270 media::SeekableBuffer buffer_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 292 | 288 |
| 293 // Forward capacity to reset to after an extension. | 289 // Forward capacity to reset to after an extension. |
| 294 size_t saved_forward_capacity_; | 290 size_t saved_forward_capacity_; |
| 295 | 291 |
| 296 GURL url_; | 292 GURL url_; |
| 297 CORSMode cors_mode_; | 293 CORSMode cors_mode_; |
| 298 const int64 first_byte_position_; | 294 const int64 first_byte_position_; |
| 299 const int64 last_byte_position_; | 295 const int64 last_byte_position_; |
| 300 bool single_origin_; | 296 bool single_origin_; |
| 301 | 297 |
| 302 // Closure that listens to network events. | 298 // Executed whenever the state of resource loading has changed. |
| 303 base::Closure event_cb_; | 299 LoadingStateChangedCB loading_cb_; |
| 300 |
| 301 // Executed whenever additional data has been downloaded and reports the |
| 302 // zero-indexed file offset of the furthest buffered byte. |
| 303 ProgressCB progress_cb_; |
| 304 | 304 |
| 305 // Members used during request start. | 305 // Members used during request start. |
| 306 StartCB start_cb_; | 306 StartCB start_cb_; |
| 307 int64 offset_; | 307 int64 offset_; |
| 308 int64 content_length_; | 308 int64 content_length_; |
| 309 int64 instance_size_; | 309 int64 instance_size_; |
| 310 | 310 |
| 311 // Members used during a read operation. They should be reset after each | 311 // Members used during a read operation. They should be reset after each |
| 312 // read has completed or failed. | 312 // read has completed or failed. |
| 313 ReadCB read_cb_; | 313 ReadCB read_cb_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 330 float playback_rate_; | 330 float playback_rate_; |
| 331 | 331 |
| 332 scoped_refptr<media::MediaLog> media_log_; | 332 scoped_refptr<media::MediaLog> media_log_; |
| 333 | 333 |
| 334 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | 334 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
| 335 }; | 335 }; |
| 336 | 336 |
| 337 } // namespace webkit_media | 337 } // namespace webkit_media |
| 338 | 338 |
| 339 #endif // WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 339 #endif // WEBKIT_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| OLD | NEW |