ServiceWorker: Rename 'current' to 'controller'
[chromium-blink-merge.git] / net / http / partial_data.h
blob45bda39b43e954482be01ced96c8e04f81faf2dd
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 "net/base/completion_callback.h"
10 #include "net/http/http_byte_range.h"
11 #include "net/http/http_request_headers.h"
13 namespace disk_cache {
14 class Entry;
17 namespace net {
19 class HttpResponseHeaders;
20 class IOBuffer;
22 // This class provides support for dealing with range requests and the
23 // subsequent partial-content responses. We use sparse cache entries to store
24 // these requests. This class is tightly integrated with HttpCache::Transaction
25 // and it is intended to allow a cleaner implementation of that class.
27 // In order to fulfill range requests, we may have to perform a sequence of
28 // reads from the cache, interleaved with reads from the network / writes to the
29 // cache. This class basically keeps track of the data required to perform each
30 // of those individual network / cache requests.
31 class PartialData {
32 public:
33 PartialData();
34 ~PartialData();
36 // Performs initialization of the object by examining the request |headers|
37 // and verifying that we can process the requested range. Returns true if
38 // we can process the requested range, and false otherwise.
39 bool Init(const HttpRequestHeaders& headers);
41 // Sets the headers that we should use to make byte range requests. This is a
42 // subset of the request extra headers, with byte-range related headers
43 // removed.
44 void SetHeaders(const HttpRequestHeaders& headers);
46 // Restores the byte-range headers, by appending the byte range to the headers
47 // provided to SetHeaders().
48 void RestoreHeaders(HttpRequestHeaders* headers) const;
50 // Starts the checks to perform a cache validation. Returns 0 when there is no
51 // need to perform more operations because we reached the end of the request
52 // (so 0 bytes should be actually returned to the user), a positive number to
53 // indicate that PrepareCacheValidation should be called, or an appropriate
54 // error code. If this method returns ERR_IO_PENDING, the |callback| will be
55 // notified when the result is ready.
56 int ShouldValidateCache(disk_cache::Entry* entry,
57 const CompletionCallback& callback);
59 // Builds the required |headers| to perform the proper cache validation for
60 // the next range to be fetched.
61 void PrepareCacheValidation(disk_cache::Entry* entry,
62 HttpRequestHeaders* headers);
64 // Returns true if the current range is stored in the cache.
65 bool IsCurrentRangeCached() const;
67 // Returns true if the current range is the last one needed to fulfill the
68 // user's request.
69 bool IsLastRange() const;
71 // Extracts info from headers already stored in the cache. Returns false if
72 // there is any problem with the headers. |truncated| should be true if we
73 // have an incomplete 200 entry.
74 bool UpdateFromStoredHeaders(const HttpResponseHeaders* headers,
75 disk_cache::Entry* entry, bool truncated);
77 // Sets the byte current range to start again at zero (for a truncated entry).
78 void SetRangeToStartDownload();
80 // Returns true if the requested range is valid given the stored data.
81 bool IsRequestedRangeOK();
83 // Returns true if the response headers match what we expect, false otherwise.
84 bool ResponseHeadersOK(const HttpResponseHeaders* headers);
86 // Fixes the response headers to include the right content length and range.
87 // |success| is the result of the whole request so if it's false, we'll change
88 // the result code to be 416.
89 void FixResponseHeaders(HttpResponseHeaders* headers, bool success);
91 // Fixes the content length that we want to store in the cache.
92 void FixContentLength(HttpResponseHeaders* headers);
94 // Reads up to |data_len| bytes from the cache and stores them in the provided
95 // buffer (|data|). Basically, this is just a wrapper around the API of the
96 // cache that provides the right arguments for the current range. When the IO
97 // operation completes, OnCacheReadCompleted() must be called with the result
98 // of the operation.
99 int CacheRead(disk_cache::Entry* entry, IOBuffer* data, int data_len,
100 const net::CompletionCallback& callback);
102 // Writes |data_len| bytes to cache. This is basically a wrapper around the
103 // API of the cache that provides the right arguments for the current range.
104 int CacheWrite(disk_cache::Entry* entry, IOBuffer* data, int data_len,
105 const net::CompletionCallback& callback);
107 // This method should be called when CacheRead() finishes the read, to update
108 // the internal state about the current range.
109 void OnCacheReadCompleted(int result);
111 // This method should be called after receiving data from the network, to
112 // update the internal state about the current range.
113 void OnNetworkReadCompleted(int result);
115 bool initial_validation() const { return initial_validation_; }
117 private:
118 class Core;
119 // Returns the length to use when scanning the cache.
120 int GetNextRangeLen();
122 // Completion routine for our callback.
123 void GetAvailableRangeCompleted(int result, int64 start);
125 int64 current_range_start_;
126 int64 cached_start_;
127 int64 resource_size_;
128 int cached_min_len_;
129 HttpByteRange byte_range_; // The range requested by the user.
130 // The clean set of extra headers (no ranges).
131 HttpRequestHeaders extra_headers_;
132 bool range_present_; // True if next range entry is already stored.
133 bool final_range_;
134 bool sparse_entry_;
135 bool truncated_; // We have an incomplete 200 stored.
136 bool initial_validation_; // Only used for truncated entries.
137 Core* core_;
138 CompletionCallback callback_;
140 DISALLOW_COPY_AND_ASSIGN(PartialData);
143 } // namespace net
145 #endif // NET_HTTP_PARTIAL_DATA_H_