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
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_PARTIAL_DATA_H_
6 #define NET_HTTP_PARTIAL_DATA_H_
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/base/completion_callback.h"
11 #include "net/http/http_byte_range.h"
12 #include "net/http/http_request_headers.h"
14 namespace disk_cache
{
20 class HttpResponseHeaders
;
23 // This class provides support for dealing with range requests and the
24 // subsequent partial-content responses. We use sparse cache entries to store
25 // these requests. This class is tightly integrated with HttpCache::Transaction
26 // and it is intended to allow a cleaner implementation of that class.
28 // In order to fulfill range requests, we may have to perform a sequence of
29 // reads from the cache, interleaved with reads from the network / writes to the
30 // cache. This class basically keeps track of the data required to perform each
31 // of those individual network / cache requests.
37 // Performs initialization of the object by examining the request |headers|
38 // and verifying that we can process the requested range. Returns true if
39 // we can process the requested range, and false otherwise.
40 bool Init(const HttpRequestHeaders
& headers
);
42 // Sets the headers that we should use to make byte range requests. This is a
43 // subset of the request extra headers, with byte-range related headers
45 void SetHeaders(const HttpRequestHeaders
& headers
);
47 // Restores the byte-range headers, by appending the byte range to the headers
48 // provided to SetHeaders().
49 void RestoreHeaders(HttpRequestHeaders
* headers
) const;
51 // Starts the checks to perform a cache validation. Returns 0 when there is no
52 // need to perform more operations because we reached the end of the request
53 // (so 0 bytes should be actually returned to the user), a positive number to
54 // indicate that PrepareCacheValidation should be called, or an appropriate
55 // error code. If this method returns ERR_IO_PENDING, the |callback| will be
56 // notified when the result is ready.
57 int ShouldValidateCache(disk_cache::Entry
* entry
,
58 const CompletionCallback
& callback
);
60 // Builds the required |headers| to perform the proper cache validation for
61 // the next range to be fetched.
62 void PrepareCacheValidation(disk_cache::Entry
* entry
,
63 HttpRequestHeaders
* headers
);
65 // Returns true if the current range is stored in the cache.
66 bool IsCurrentRangeCached() const;
68 // Returns true if the current range is the last one needed to fulfill the
70 bool IsLastRange() const;
72 // Extracts info from headers already stored in the cache. Returns false if
73 // there is any problem with the headers. |truncated| should be true if we
74 // have an incomplete 200 entry.
75 bool UpdateFromStoredHeaders(const HttpResponseHeaders
* headers
,
76 disk_cache::Entry
* entry
, bool truncated
);
78 // Sets the byte current range to start again at zero (for a truncated entry).
79 void SetRangeToStartDownload();
81 // Returns true if the requested range is valid given the stored data.
82 bool IsRequestedRangeOK();
84 // Returns true if the response headers match what we expect, false otherwise.
85 bool ResponseHeadersOK(const HttpResponseHeaders
* headers
);
87 // Fixes the response headers to include the right content length and range.
88 // |success| is the result of the whole request so if it's false, we'll change
89 // the result code to be 416.
90 void FixResponseHeaders(HttpResponseHeaders
* headers
, bool success
);
92 // Fixes the content length that we want to store in the cache.
93 void FixContentLength(HttpResponseHeaders
* headers
);
95 // Reads up to |data_len| bytes from the cache and stores them in the provided
96 // buffer (|data|). Basically, this is just a wrapper around the API of the
97 // cache that provides the right arguments for the current range. When the IO
98 // operation completes, OnCacheReadCompleted() must be called with the result
100 int CacheRead(disk_cache::Entry
* entry
,
103 const CompletionCallback
& callback
);
105 // Writes |data_len| bytes to cache. This is basically a wrapper around the
106 // API of the cache that provides the right arguments for the current range.
107 int CacheWrite(disk_cache::Entry
* entry
,
110 const CompletionCallback
& callback
);
112 // This method should be called when CacheRead() finishes the read, to update
113 // the internal state about the current range.
114 void OnCacheReadCompleted(int result
);
116 // This method should be called after receiving data from the network, to
117 // update the internal state about the current range.
118 void OnNetworkReadCompleted(int result
);
120 bool initial_validation() const { return initial_validation_
; }
123 // Returns the length to use when scanning the cache.
124 int GetNextRangeLen();
126 // Completion routine for our callback.
127 void GetAvailableRangeCompleted(int64
* start
, int result
);
129 int64 current_range_start_
;
130 int64 current_range_end_
;
132 int64 resource_size_
;
134 HttpByteRange byte_range_
; // The range requested by the user.
135 // The clean set of extra headers (no ranges).
136 HttpRequestHeaders extra_headers_
;
137 bool range_present_
; // True if next range entry is already stored.
140 bool truncated_
; // We have an incomplete 200 stored.
141 bool initial_validation_
; // Only used for truncated entries.
142 CompletionCallback callback_
;
143 base::WeakPtrFactory
<PartialData
> weak_factory_
;
145 DISALLOW_COPY_AND_ASSIGN(PartialData
);
150 #endif // NET_HTTP_PARTIAL_DATA_H_