| OLD | NEW |
| 1 // Copyright (c) 2010 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" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| (...skipping 17 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 const char* data, int dataLength); | 140 const char* data, int dataLength); |
| 135 virtual void didFinishLoading( | 141 virtual void didFinishLoading( |
| 136 WebKit::WebURLLoader* loader, | 142 WebKit::WebURLLoader* loader, |
| 137 double finishTime); | 143 double finishTime); |
| 138 virtual void didFail( | 144 virtual void didFail( |
| 139 WebKit::WebURLLoader* loader, | 145 WebKit::WebURLLoader* loader, |
| 140 const WebKit::WebURLError&); | 146 const WebKit::WebURLError&); |
| 141 | 147 |
| 142 bool HasSingleOrigin() const; | 148 bool HasSingleOrigin() const; |
| 143 | 149 |
| 150 // Sets the defer strategy to the given value. |
| 151 void UpdateDeferStrategy(DeferStrategy strategy); |
| 152 |
| 144 protected: | 153 protected: |
| 145 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; | 154 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; |
| 146 | |
| 147 virtual ~BufferedResourceLoader(); | 155 virtual ~BufferedResourceLoader(); |
| 148 | 156 |
| 149 private: | 157 private: |
| 150 friend class BufferedResourceLoaderTest; | 158 friend class BufferedResourceLoaderTest; |
| 151 | 159 |
| 152 // Defer the resource loading if the buffer is full. | 160 // Toggles whether the resource loading is deferred or not. |
| 153 void EnableDeferIfNeeded(); | 161 // Returns true if a network event was fired. |
| 162 bool ToggleDeferring(); |
| 154 | 163 |
| 155 // Disable defer loading if we are under-buffered. | 164 // Returns true if we should defer resource loading, based |
| 156 void DisableDeferIfNeeded(); | 165 // on current buffering scheme. |
| 166 bool ShouldEnableDefer(); |
| 167 |
| 168 // Returns true if we should enable resource loading, based |
| 169 // on current buffering scheme. |
| 170 bool ShouldDisableDefer(); |
| 171 |
| 172 // Updates deferring behavior based on current buffering scheme. |
| 173 void UpdateDeferBehavior(); |
| 157 | 174 |
| 158 // Returns true if the current read request can be fulfilled by what is in | 175 // Returns true if the current read request can be fulfilled by what is in |
| 159 // the buffer. | 176 // the buffer. |
| 160 bool CanFulfillRead(); | 177 bool CanFulfillRead(); |
| 161 | 178 |
| 162 // Returns true if the current read request will be fulfilled in the future. | 179 // Returns true if the current read request will be fulfilled in the future. |
| 163 bool WillFulfillRead(); | 180 bool WillFulfillRead(); |
| 164 | 181 |
| 165 // Method that does the actual read and calls the |read_callback_|, assuming | 182 // Method that does the actual read and calls the |read_callback_|, assuming |
| 166 // the request range is in |buffer_|. | 183 // the request range is in |buffer_|. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 189 void NotifyNetworkEvent(); | 206 void NotifyNetworkEvent(); |
| 190 | 207 |
| 191 bool HasPendingRead() { return read_callback_.get() != NULL; } | 208 bool HasPendingRead() { return read_callback_.get() != NULL; } |
| 192 | 209 |
| 193 // A sliding window of buffer. | 210 // A sliding window of buffer. |
| 194 scoped_ptr<media::SeekableBuffer> buffer_; | 211 scoped_ptr<media::SeekableBuffer> buffer_; |
| 195 | 212 |
| 196 // True if resource loading was deferred. | 213 // True if resource loading was deferred. |
| 197 bool deferred_; | 214 bool deferred_; |
| 198 | 215 |
| 199 // True if resource loader is allowed to defer, false otherwise. | 216 // Current buffering algorithm in place for resource loading. |
| 200 bool defer_allowed_; | 217 DeferStrategy defer_strategy_; |
| 201 | 218 |
| 202 // True if resource loading has completed. | 219 // True if resource loading has completed. |
| 203 bool completed_; | 220 bool completed_; |
| 204 | 221 |
| 205 // True if a range request was made. | 222 // True if a range request was made. |
| 206 bool range_requested_; | 223 bool range_requested_; |
| 207 | 224 |
| 208 // True if response data received is a partial range. | 225 // True if response data received is a partial range. |
| 209 bool partial_response_; | 226 bool partial_response_; |
| 210 | 227 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 239 | 256 |
| 240 // Used to ensure mocks for unittests are used instead of reset in Start(). | 257 // Used to ensure mocks for unittests are used instead of reset in Start(). |
| 241 bool keep_test_loader_; | 258 bool keep_test_loader_; |
| 242 | 259 |
| 243 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); | 260 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
| 244 }; | 261 }; |
| 245 | 262 |
| 246 } // namespace webkit_glue | 263 } // namespace webkit_glue |
| 247 | 264 |
| 248 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ | 265 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| OLD | NEW |