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