OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 NET_HTTP_PARTIAL_DATA_H_ |
| 6 #define NET_HTTP_PARTIAL_DATA_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "net/base/completion_callback.h" |
| 10 #include "net/http/http_byte_range.h" |
| 11 |
| 12 namespace disk_cache { |
| 13 class Entry; |
| 14 } |
| 15 |
| 16 namespace net { |
| 17 |
| 18 class IOBuffer; |
| 19 |
| 20 // This class provides support for dealing with range requests and the |
| 21 // subsequent partial-content responses. We use sparse cache entries to store |
| 22 // these requests. This class is tightly integrated with HttpCache::Transaction |
| 23 // and it is intended to allow a cleaner implementation of that class. |
| 24 // |
| 25 // In order to fulfill range requests, we may have to perform a sequence of |
| 26 // reads from the cache, interleaved with reads from the network / writes to the |
| 27 // cache. This class basically keeps track of the data required to perform each |
| 28 // of those individual network / cache requests. |
| 29 class PartialData { |
| 30 public: |
| 31 PartialData() : range_present_(false), final_range_(false) {} |
| 32 ~PartialData() {} |
| 33 |
| 34 // Performs initialization of the object by parsing the request |headers| |
| 35 // and verifying that we can process the requested range. Returns true if |
| 36 // we can process the requested range, and false otherwise. |new_headers| is |
| 37 // a subset of the request extra headers, with byte-range related headers |
| 38 // removed so that we can easily add any byte-range that we need. |
| 39 bool Init(const std::string& headers, const std::string& new_headers); |
| 40 |
| 41 // Restores the byte-range header that was removed during Init(), by appending |
| 42 // the data to the provided |headers|. |
| 43 void RestoreHeaders(std::string* headers) const; |
| 44 |
| 45 // Builds the required |headers| to perform the proper cache validation for |
| 46 // the next range to be fetched. Returns 0 when there is no need to perform |
| 47 // more operations because we reached the end of the request (so 0 bytes |
| 48 // should be actually returned to the user), a positive number to indicate |
| 49 // that |headers| should be used to validate the cache, or an appropriate |
| 50 // error code. |
| 51 int PrepareCacheValidation(disk_cache::Entry* entry, std::string* headers); |
| 52 |
| 53 // Returns true if the current range is stored in the cache. |
| 54 bool IsCurrentRangeCached() const; |
| 55 |
| 56 // Returns true if the current range is the last one needed to fulfill the |
| 57 // user's request. |
| 58 bool IsLastRange() const; |
| 59 |
| 60 // Reads up to |data_len| bytes from the cache and stores them in the provided |
| 61 // buffer (|data|). Basically, this is just a wrapper around the API of the |
| 62 // cache that provides the right arguments for the current range. When the IO |
| 63 // operation completes, OnCacheReadCompleted() must be called with the result |
| 64 // of the operation. |
| 65 int CacheRead(disk_cache::Entry* entry, IOBuffer* data, int data_len, |
| 66 CompletionCallback* callback); |
| 67 |
| 68 // Writes |data_len| bytes to cache. This is basically a wrapper around the |
| 69 // API of the cache that provides the right arguments for the current range. |
| 70 int CacheWrite(disk_cache::Entry* entry, IOBuffer* data, int data_len, |
| 71 CompletionCallback* callback); |
| 72 |
| 73 // This method should be called when CacheRead() finishes the read, to update |
| 74 // the internal state about the current range. |
| 75 void OnCacheReadCompleted(int result); |
| 76 |
| 77 // This method should be called after receiving data from the network, to |
| 78 // update the internal state about the current range. |
| 79 void OnNetworkReadCompleted(int result); |
| 80 |
| 81 private: |
| 82 static void AddRangeHeader(int64 start, int64 end, std::string* headers); |
| 83 |
| 84 int64 current_range_start_; |
| 85 int64 cached_start_; |
| 86 int cached_min_len_; |
| 87 HttpByteRange byte_range_; // The range requested by the user. |
| 88 std::string extra_headers_; // The clean set of extra headers (no ranges). |
| 89 bool range_present_; // True if next range entry is already stored. |
| 90 bool final_range_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(PartialData); |
| 93 }; |
| 94 |
| 95 } // namespace net |
| 96 |
| 97 #endif // NET_HTTP_PARTIAL_DATA_H_ |
OLD | NEW |