OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| 6 #define WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/lock.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/timer.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "media/base/seekable_buffer.h" |
| 16 #include "net/base/file_stream.h" |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h" |
| 19 #include "third_party/WebKit/WebKit/chromium/public/WebURLLoaderClient.h" |
| 20 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
| 21 #include "webkit/glue/media/web_data_source.h" |
| 22 #include "webkit/glue/webmediaplayer_impl.h" |
| 23 |
| 24 namespace webkit_glue { |
| 25 |
| 26 // This class works inside demuxer thread and render thread. It contains a |
| 27 // WebURLLoader and does the actual resource loading. This object does |
| 28 // buffering internally, it defers the resource loading if buffer is full |
| 29 // and un-defers the resource loading if it is under buffered. |
| 30 class BufferedResourceLoader : |
| 31 public base::RefCountedThreadSafe<BufferedResourceLoader>, |
| 32 public WebKit::WebURLLoaderClient { |
| 33 public: |
| 34 typedef Callback0::Type NetworkEventCallback; |
| 35 |
| 36 // |url| - URL for the resource to be loaded. |
| 37 // |first_byte_position| - First byte to start loading from, -1 for not |
| 38 // specified. |
| 39 // |last_byte_position| - Last byte to be loaded, -1 for not specified. |
| 40 BufferedResourceLoader(const GURL& url, |
| 41 int64 first_byte_position, |
| 42 int64 last_byte_position); |
| 43 |
| 44 // Start the resource loading with the specified URL and range. |
| 45 // This method operates in asynchronous mode. Once there's a response from the |
| 46 // server, success or fail |callback| is called with the result. |
| 47 // |callback| is called with the following values: |
| 48 // - net::OK |
| 49 // The request has started successfully. |
| 50 // - net::ERR_FAILED |
| 51 // The request has failed because of an error with the network. |
| 52 // - net::ERR_INVALID_RESPONSE |
| 53 // An invalid response is received from the server. |
| 54 // - (Anything else) |
| 55 // An error code that indicates the request has failed. |
| 56 // |event_callback| is called when the response is completed, data is |
| 57 // received, the request is suspended or resumed. |
| 58 virtual void Start(net::CompletionCallback* callback, |
| 59 NetworkEventCallback* event_callback, |
| 60 WebKit::WebFrame* frame); |
| 61 |
| 62 // Stop this loader, cancels and request and release internal buffer. |
| 63 virtual void Stop(); |
| 64 |
| 65 // Reads the specified |read_size| from |position| into |buffer| and when |
| 66 // the operation is done invoke |callback| with number of bytes read or an |
| 67 // error code. |
| 68 // |callback| is called with the following values: |
| 69 // - (Anything greater than or equal 0) |
| 70 // Read was successful with the indicated number of bytes read. |
| 71 // - net::ERR_FAILED |
| 72 // The read has failed because of an error with the network. |
| 73 // - net::ERR_CACHE_MISS |
| 74 // The read was made too far away from the current buffered position. |
| 75 virtual void Read(int64 position, int read_size, |
| 76 uint8* buffer, net::CompletionCallback* callback); |
| 77 |
| 78 // Returns the position of the first byte buffered. Returns -1 if such value |
| 79 // is not available. |
| 80 virtual int64 GetBufferedFirstBytePosition(); |
| 81 |
| 82 // Returns the position of the last byte buffered. Returns -1 if such value |
| 83 // is not available. |
| 84 virtual int64 GetBufferedLastBytePosition(); |
| 85 |
| 86 // Sets whether deferring data is allowed or disallowed. |
| 87 virtual void SetAllowDefer(bool is_allowed); |
| 88 |
| 89 // Gets the content length in bytes of the instance after this loader has been |
| 90 // started. If this value is -1, then content length is unknown. |
| 91 virtual int64 content_length() { return content_length_; } |
| 92 |
| 93 // Gets the original size of the file requested. If this value is -1, then |
| 94 // the size is unknown. |
| 95 virtual int64 instance_size() { return instance_size_; } |
| 96 |
| 97 // Returns true if the response for this loader is a partial response. |
| 98 // It means a 206 response in HTTP/HTTPS protocol. |
| 99 virtual bool partial_response() { return partial_response_; } |
| 100 |
| 101 // Returns true if network is currently active. |
| 102 virtual bool network_activity() { return !completed_ && !deferred_; } |
| 103 |
| 104 // Returns resulting URL. |
| 105 virtual const GURL& url() { return url_; } |
| 106 |
| 107 // Used to inject a mock used for unittests. |
| 108 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); |
| 109 |
| 110 ///////////////////////////////////////////////////////////////////////////// |
| 111 // WebKit::WebURLLoaderClient implementations. |
| 112 virtual void willSendRequest( |
| 113 WebKit::WebURLLoader* loader, |
| 114 WebKit::WebURLRequest& newRequest, |
| 115 const WebKit::WebURLResponse& redirectResponse); |
| 116 virtual void didSendData( |
| 117 WebKit::WebURLLoader* loader, |
| 118 unsigned long long bytesSent, |
| 119 unsigned long long totalBytesToBeSent); |
| 120 virtual void didReceiveResponse( |
| 121 WebKit::WebURLLoader* loader, |
| 122 const WebKit::WebURLResponse& response); |
| 123 virtual void didDownloadData( |
| 124 WebKit::WebURLLoader* loader, |
| 125 int dataLength); |
| 126 virtual void didReceiveData( |
| 127 WebKit::WebURLLoader* loader, |
| 128 const char* data, |
| 129 int dataLength); |
| 130 virtual void didReceiveCachedMetadata( |
| 131 WebKit::WebURLLoader* loader, |
| 132 const char* data, int dataLength); |
| 133 virtual void didFinishLoading( |
| 134 WebKit::WebURLLoader* loader, |
| 135 double finishTime); |
| 136 virtual void didFail( |
| 137 WebKit::WebURLLoader* loader, |
| 138 const WebKit::WebURLError&); |
| 139 |
| 140 protected: |
| 141 friend class base::RefCountedThreadSafe<BufferedResourceLoader>; |
| 142 |
| 143 virtual ~BufferedResourceLoader(); |
| 144 |
| 145 private: |
| 146 friend class BufferedResourceLoaderTest; |
| 147 |
| 148 // Defer the resource loading if the buffer is full. |
| 149 void EnableDeferIfNeeded(); |
| 150 |
| 151 // Disable defer loading if we are under-buffered. |
| 152 void DisableDeferIfNeeded(); |
| 153 |
| 154 // Returns true if the current read request can be fulfilled by what is in |
| 155 // the buffer. |
| 156 bool CanFulfillRead(); |
| 157 |
| 158 // Returns true if the current read request will be fulfilled in the future. |
| 159 bool WillFulfillRead(); |
| 160 |
| 161 // Method that does the actual read and calls the |read_callbac_|, assuming |
| 162 // the request range is in |buffer_|. |
| 163 void ReadInternal(); |
| 164 |
| 165 // If we have made a range request, verify the response from the server. |
| 166 bool VerifyPartialResponse(const WebKit::WebURLResponse& response); |
| 167 |
| 168 // Returns the value for a range request header using parameters |
| 169 // |first_byte_position| and |last_byte_position|. Negative numbers other |
| 170 // than -1 are not allowed for |first_byte_position| and |last_byte_position|. |
| 171 // |first_byte_position| should always be less than or equal to |
| 172 // |last_byte_position| if they are both not -1. |
| 173 // Empty string is returned on invalid parameters. |
| 174 std::string GenerateHeaders(int64 first_byte_position, |
| 175 int64 last_byte_position); |
| 176 |
| 177 // Done with read. Invokes the read callback and reset parameters for the |
| 178 // read request. |
| 179 void DoneRead(int error); |
| 180 |
| 181 // Done with start. Invokes the start callback and reset it. |
| 182 void DoneStart(int error); |
| 183 |
| 184 // Calls |event_callback_| in terms of a network event. |
| 185 void NotifyNetworkEvent(); |
| 186 |
| 187 bool HasPendingRead() { return read_callback_.get() != NULL; } |
| 188 |
| 189 // A sliding window of buffer. |
| 190 scoped_ptr<media::SeekableBuffer> buffer_; |
| 191 |
| 192 // True if resource loading was deferred. |
| 193 bool deferred_; |
| 194 |
| 195 // True if resource loader is allowed to defer, false otherwise. |
| 196 bool defer_allowed_; |
| 197 |
| 198 // True if resource loading has completed. |
| 199 bool completed_; |
| 200 |
| 201 // True if a range request was made. |
| 202 bool range_requested_; |
| 203 |
| 204 // True if response data received is a partial range. |
| 205 bool partial_response_; |
| 206 |
| 207 // Does the work of loading and sends data back to this client. |
| 208 scoped_ptr<WebKit::WebURLLoader> url_loader_; |
| 209 |
| 210 GURL url_; |
| 211 int64 first_byte_position_; |
| 212 int64 last_byte_position_; |
| 213 |
| 214 // Callback method that listens to network events. |
| 215 scoped_ptr<NetworkEventCallback> event_callback_; |
| 216 |
| 217 // Members used during request start. |
| 218 scoped_ptr<net::CompletionCallback> start_callback_; |
| 219 int64 offset_; |
| 220 int64 content_length_; |
| 221 int64 instance_size_; |
| 222 |
| 223 // Members used during a read operation. They should be reset after each |
| 224 // read has completed or failed. |
| 225 scoped_ptr<net::CompletionCallback> read_callback_; |
| 226 int64 read_position_; |
| 227 int read_size_; |
| 228 uint8* read_buffer_; |
| 229 |
| 230 // Offsets of the requested first byte and last byte in |buffer_|. They are |
| 231 // written by VerifyRead(). |
| 232 int first_offset_; |
| 233 int last_offset_; |
| 234 |
| 235 // Used to ensure mocks for unittests are used instead of reset in Start(). |
| 236 bool keep_test_loader_; |
| 237 |
| 238 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
| 239 }; |
| 240 |
| 241 } // namespace webkit_glue |
| 242 |
| 243 #endif // WEBKIT_GLUE_MEDIA_BUFFERED_RESOURCE_LOADER_H_ |
OLD | NEW |