OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_RESOURCE_LOADER_H_ | 5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
6 #define WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 6 #define WEBKIT_GLUE_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 18 matching lines...) Expand all Loading... |
29 const char kDataScheme[] = "data"; | 29 const char kDataScheme[] = "data"; |
30 | 30 |
31 // This class works inside demuxer thread and render thread. It contains a | 31 // This class works inside demuxer thread and render thread. It contains a |
32 // WebURLLoader and does the actual resource loading. This object does | 32 // WebURLLoader and does the actual resource loading. This object does |
33 // buffering internally, it defers the resource loading if buffer is full | 33 // buffering internally, it defers the resource loading if buffer is full |
34 // and un-defers the resource loading if it is under buffered. | 34 // and un-defers the resource loading if it is under buffered. |
35 class BufferedResourceLoader : | 35 class BufferedResourceLoader : |
36 public base::RefCountedThreadSafe<BufferedResourceLoader>, | 36 public base::RefCountedThreadSafe<BufferedResourceLoader>, |
37 public WebKit::WebURLLoaderClient { | 37 public WebKit::WebURLLoaderClient { |
38 public: | 38 public: |
| 39 // kNeverDefer - Aggresively buffer; never defer loading while paused. |
| 40 // kReadThenDefer - Request only enough data to fulfill read requests. |
| 41 // kThresholdDefer - Try to keep amount of buffered data at a threshold. |
| 42 enum DeferStrategy { |
| 43 kNeverDefer, |
| 44 kReadThenDefer, |
| 45 kThresholdDefer, |
| 46 }; |
| 47 |
39 typedef Callback0::Type NetworkEventCallback; | 48 typedef Callback0::Type NetworkEventCallback; |
40 | 49 |
41 // |url| - URL for the resource to be loaded. | 50 // |url| - URL for the resource to be loaded. |
42 // |first_byte_position| - First byte to start loading from, | 51 // |first_byte_position| - First byte to start loading from, |
43 // |kPositionNotSpecified| for not specified. | 52 // |kPositionNotSpecified| for not specified. |
44 // |last_byte_position| - Last byte to be loaded, | 53 // |last_byte_position| - Last byte to be loaded, |
45 // |kPositionNotSpecified| for not specified. | 54 // |kPositionNotSpecified| for not specified. |
46 BufferedResourceLoader(const GURL& url, | 55 BufferedResourceLoader(const GURL& url, |
47 int64 first_byte_position, | 56 int64 first_byte_position, |
48 int64 last_byte_position); | 57 int64 last_byte_position); |
(...skipping 29 matching lines...) Expand all Loading... |
78 // The read has failed because of an error with the network. | 87 // The read has failed because of an error with the network. |
79 // - net::ERR_CACHE_MISS | 88 // - net::ERR_CACHE_MISS |
80 // The read was made too far away from the current buffered position. | 89 // The read was made too far away from the current buffered position. |
81 virtual void Read(int64 position, int read_size, | 90 virtual void Read(int64 position, int read_size, |
82 uint8* buffer, net::CompletionCallback* callback); | 91 uint8* buffer, net::CompletionCallback* callback); |
83 | 92 |
84 // Returns the position of the last byte buffered. Returns | 93 // Returns the position of the last byte buffered. Returns |
85 // |kPositionNotSpecified| if such value is not available. | 94 // |kPositionNotSpecified| if such value is not available. |
86 virtual int64 GetBufferedPosition(); | 95 virtual int64 GetBufferedPosition(); |
87 | 96 |
88 // Sets whether deferring data is allowed or disallowed. | |
89 virtual void SetAllowDefer(bool is_allowed); | |
90 | |
91 // Gets the content length in bytes of the instance after this loader has been | 97 // Gets the content length in bytes of the instance after this loader has been |
92 // started. If this value is |kPositionNotSpecified|, then content length is | 98 // started. If this value is |kPositionNotSpecified|, then content length is |
93 // unknown. | 99 // unknown. |
94 virtual int64 content_length(); | 100 virtual int64 content_length(); |
95 | 101 |
96 // Gets the original size of the file requested. If this value is | 102 // Gets the original size of the file requested. If this value is |
97 // |kPositionNotSpecified|, then the size is unknown. | 103 // |kPositionNotSpecified|, then the size is unknown. |
98 virtual int64 instance_size(); | 104 virtual int64 instance_size(); |
99 | 105 |
100 // Returns true if the response for this loader is a partial response. | 106 // Returns true if the response for this loader is a partial response. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 const char* data, int dataLength); | 148 const char* data, int dataLength); |
143 virtual void didFinishLoading( | 149 virtual void didFinishLoading( |
144 WebKit::WebURLLoader* loader, | 150 WebKit::WebURLLoader* loader, |
145 double finishTime); | 151 double finishTime); |
146 virtual void didFail( | 152 virtual void didFail( |
147 WebKit::WebURLLoader* loader, | 153 WebKit::WebURLLoader* loader, |
148 const WebKit::WebURLError&); | 154 const WebKit::WebURLError&); |
149 | 155 |
150 bool HasSingleOrigin() const; | 156 bool HasSingleOrigin() const; |
151 | 157 |
| 158 // Sets the defer strategy to the given value. |
| 159 void UpdateDeferStrategy(DeferStrategy strategy); |
| 160 |
152 protected: | 161 protected: |
153 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | 162 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; |
154 | |
155 virtual ~BufferedResourceLoader(); | 163 virtual ~BufferedResourceLoader(); |
156 | 164 |
157 private: | 165 private: |
158 friend class BufferedResourceLoaderTest; | 166 friend class BufferedResourceLoaderTest; |
159 | 167 |
160 // Defer the resource loading if the buffer is full. | 168 // Toggles whether the resource loading is deferred or not. |
161 void EnableDeferIfNeeded(); | 169 // Returns true if a network event was fired. |
| 170 bool ToggleDeferring(); |
162 | 171 |
163 // Disable defer loading if we are under-buffered. | 172 // Returns true if we should defer resource loading, based |
164 void DisableDeferIfNeeded(); | 173 // on current buffering scheme. |
| 174 bool ShouldEnableDefer(); |
| 175 |
| 176 // Returns true if we should enable resource loading, based |
| 177 // on current buffering scheme. |
| 178 bool ShouldDisableDefer(); |
| 179 |
| 180 // Updates deferring behavior based on current buffering scheme. |
| 181 void UpdateDeferBehavior(); |
165 | 182 |
166 // Returns true if the current read request can be fulfilled by what is in | 183 // Returns true if the current read request can be fulfilled by what is in |
167 // the buffer. | 184 // the buffer. |
168 bool CanFulfillRead(); | 185 bool CanFulfillRead(); |
169 | 186 |
170 // Returns true if the current read request will be fulfilled in the future. | 187 // Returns true if the current read request will be fulfilled in the future. |
171 bool WillFulfillRead(); | 188 bool WillFulfillRead(); |
172 | 189 |
173 // Method that does the actual read and calls the |read_callback_|, assuming | 190 // Method that does the actual read and calls the |read_callback_|, assuming |
174 // the request range is in |buffer_|. | 191 // the request range is in |buffer_|. |
(...skipping 22 matching lines...) Expand all Loading... |
197 void NotifyNetworkEvent(); | 214 void NotifyNetworkEvent(); |
198 | 215 |
199 bool HasPendingRead() { return read_callback_.get() != NULL; } | 216 bool HasPendingRead() { return read_callback_.get() != NULL; } |
200 | 217 |
201 // A sliding window of buffer. | 218 // A sliding window of buffer. |
202 scoped_ptr<media::SeekableBuffer> buffer_; | 219 scoped_ptr<media::SeekableBuffer> buffer_; |
203 | 220 |
204 // True if resource loading was deferred. | 221 // True if resource loading was deferred. |
205 bool deferred_; | 222 bool deferred_; |
206 | 223 |
207 // True if resource loader is allowed to defer, false otherwise. | 224 // Current buffering algorithm in place for resource loading. |
208 bool defer_allowed_; | 225 DeferStrategy defer_strategy_; |
209 | 226 |
210 // True if resource loading has completed. | 227 // True if resource loading has completed. |
211 bool completed_; | 228 bool completed_; |
212 | 229 |
213 // True if a range request was made. | 230 // True if a range request was made. |
214 bool range_requested_; | 231 bool range_requested_; |
215 | 232 |
216 // True if response data received is a partial range. | 233 // True if response data received is a partial range. |
217 bool partial_response_; | 234 bool partial_response_; |
218 | 235 |
(...skipping 28 matching lines...) Expand all Loading... |
247 | 264 |
248 // Used to ensure mocks for unittests are used instead of reset in Start(). | 265 // Used to ensure mocks for unittests are used instead of reset in Start(). |
249 bool keep_test_loader_; | 266 bool keep_test_loader_; |
250 | 267 |
251 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | 268 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
252 }; | 269 }; |
253 | 270 |
254 } // namespace webkit_glue | 271 } // namespace webkit_glue |
255 | 272 |
256 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 273 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
OLD | NEW |