Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / appcache / appcache_response.h
blob8d6c79060bdcf2eb76ac5c4870d90fcdc36e089d
1 // Copyright (c) 2012 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 CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/common/appcache_interfaces.h"
13 #include "content/common/content_export.h"
14 #include "net/base/completion_callback.h"
15 #include "net/http/http_response_info.h"
16 #include "url/gurl.h"
18 namespace net {
19 class IOBuffer;
22 namespace content {
23 class AppCacheStorage;
24 class MockAppCacheStorage;
26 static const int kUnkownResponseDataSize = -1;
28 // Response info for a particular response id. Instances are tracked in
29 // the working set.
30 class CONTENT_EXPORT AppCacheResponseInfo
31 : public base::RefCounted<AppCacheResponseInfo> {
32 public:
33 // AppCacheResponseInfo takes ownership of the http_info.
34 AppCacheResponseInfo(AppCacheStorage* storage, const GURL& manifest_url,
35 int64 response_id, net::HttpResponseInfo* http_info,
36 int64 response_data_size);
38 const GURL& manifest_url() const { return manifest_url_; }
39 int64 response_id() const { return response_id_; }
40 const net::HttpResponseInfo* http_response_info() const {
41 return http_response_info_.get();
43 int64 response_data_size() const { return response_data_size_; }
45 private:
46 friend class base::RefCounted<AppCacheResponseInfo>;
47 virtual ~AppCacheResponseInfo();
49 const GURL manifest_url_;
50 const int64 response_id_;
51 const scoped_ptr<net::HttpResponseInfo> http_response_info_;
52 const int64 response_data_size_;
53 AppCacheStorage* storage_;
56 // A refcounted wrapper for HttpResponseInfo so we can apply the
57 // refcounting semantics used with IOBuffer with these structures too.
58 struct CONTENT_EXPORT HttpResponseInfoIOBuffer
59 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> {
60 scoped_ptr<net::HttpResponseInfo> http_info;
61 int response_data_size;
63 HttpResponseInfoIOBuffer();
64 explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info);
66 protected:
67 friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>;
68 virtual ~HttpResponseInfoIOBuffer();
71 // Low level storage API used by the response reader and writer.
72 class CONTENT_EXPORT AppCacheDiskCacheInterface {
73 public:
74 class Entry {
75 public:
76 virtual int Read(int index, int64 offset, net::IOBuffer* buf, int buf_len,
77 const net::CompletionCallback& callback) = 0;
78 virtual int Write(int index, int64 offset, net::IOBuffer* buf, int buf_len,
79 const net::CompletionCallback& callback) = 0;
80 virtual int64 GetSize(int index) = 0;
81 virtual void Close() = 0;
82 protected:
83 virtual ~Entry() {}
86 virtual int CreateEntry(int64 key, Entry** entry,
87 const net::CompletionCallback& callback) = 0;
88 virtual int OpenEntry(int64 key, Entry** entry,
89 const net::CompletionCallback& callback) = 0;
90 virtual int DoomEntry(int64 key, const net::CompletionCallback& callback) = 0;
92 protected:
93 friend class base::RefCounted<AppCacheDiskCacheInterface>;
94 virtual ~AppCacheDiskCacheInterface() {}
97 // Common base class for response reader and writer.
98 class CONTENT_EXPORT AppCacheResponseIO {
99 public:
100 virtual ~AppCacheResponseIO();
101 int64 response_id() const { return response_id_; }
103 protected:
104 AppCacheResponseIO(int64 response_id,
105 int64 group_id,
106 AppCacheDiskCacheInterface* disk_cache);
108 virtual void OnIOComplete(int result) = 0;
109 virtual void OnOpenEntryComplete() {}
111 bool IsIOPending() { return !callback_.is_null(); }
112 void ScheduleIOCompletionCallback(int result);
113 void InvokeUserCompletionCallback(int result);
114 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
115 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
116 void OpenEntryIfNeeded();
118 const int64 response_id_;
119 const int64 group_id_;
120 AppCacheDiskCacheInterface* disk_cache_;
121 AppCacheDiskCacheInterface::Entry* entry_;
122 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
123 scoped_refptr<net::IOBuffer> buffer_;
124 int buffer_len_;
125 net::CompletionCallback callback_;
126 net::CompletionCallback open_callback_;
127 base::WeakPtrFactory<AppCacheResponseIO> weak_factory_;
129 private:
130 void OnRawIOComplete(int result);
131 void OpenEntryCallback(AppCacheDiskCacheInterface::Entry** entry, int rv);
134 // Reads existing response data from storage. If the object is deleted
135 // and there is a read in progress, the implementation will return
136 // immediately but will take care of any side effect of cancelling the
137 // operation. In other words, instances are safe to delete at will.
138 class CONTENT_EXPORT AppCacheResponseReader
139 : public AppCacheResponseIO {
140 public:
141 ~AppCacheResponseReader() override;
143 // Reads http info from storage. Always returns the result of the read
144 // asynchronously through the 'callback'. Returns the number of bytes read
145 // or a net:: error code. Guaranteed to not perform partial reads of
146 // the info data. The reader acquires a reference to the 'info_buf' until
147 // completion at which time the callback is invoked with either a negative
148 // error code or the number of bytes read. The 'info_buf' argument should
149 // contain a NULL http_info when ReadInfo is called. The 'callback' is a
150 // required parameter.
151 // Should only be called where there is no Read operation in progress.
152 // (virtual for testing)
153 virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
154 const net::CompletionCallback& callback);
156 // Reads data from storage. Always returns the result of the read
157 // asynchronously through the 'callback'. Returns the number of bytes read
158 // or a net:: error code. EOF is indicated with a return value of zero.
159 // The reader acquires a reference to the provided 'buf' until completion
160 // at which time the callback is invoked with either a negative error code
161 // or the number of bytes read. The 'callback' is a required parameter.
162 // Should only be called where there is no Read operation in progress.
163 // (virtual for testing)
164 virtual void ReadData(net::IOBuffer* buf, int buf_len,
165 const net::CompletionCallback& callback);
167 // Returns true if there is a read operation, for data or info, pending.
168 bool IsReadPending() { return IsIOPending(); }
170 // Used to support range requests. If not called, the reader will
171 // read the entire response body. If called, this must be called prior
172 // to the first call to the ReadData method.
173 void SetReadRange(int offset, int length);
175 protected:
176 friend class AppCacheStorageImpl;
177 friend class content::MockAppCacheStorage;
179 // Should only be constructed by the storage class and derivatives.
180 AppCacheResponseReader(int64 response_id,
181 int64 group_id,
182 AppCacheDiskCacheInterface* disk_cache);
184 void OnIOComplete(int result) override;
185 void OnOpenEntryComplete() override;
186 void ContinueReadInfo();
187 void ContinueReadData();
189 int range_offset_;
190 int range_length_;
191 int read_position_;
192 int reading_metadata_size_;
193 base::WeakPtrFactory<AppCacheResponseReader> weak_factory_;
196 // Writes new response data to storage. If the object is deleted
197 // and there is a write in progress, the implementation will return
198 // immediately but will take care of any side effect of cancelling the
199 // operation. In other words, instances are safe to delete at will.
200 class CONTENT_EXPORT AppCacheResponseWriter
201 : public AppCacheResponseIO {
202 public:
203 ~AppCacheResponseWriter() override;
205 // Writes the http info to storage. Always returns the result of the write
206 // asynchronously through the 'callback'. Returns the number of bytes written
207 // or a net:: error code. The writer acquires a reference to the 'info_buf'
208 // until completion at which time the callback is invoked with either a
209 // negative error code or the number of bytes written. The 'callback' is a
210 // required parameter. The contents of 'info_buf' are not modified.
211 // Should only be called where there is no Write operation in progress.
212 void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
213 const net::CompletionCallback& callback);
215 // Writes data to storage. Always returns the result of the write
216 // asynchronously through the 'callback'. Returns the number of bytes written
217 // or a net:: error code. Guaranteed to not perform partial writes.
218 // The writer acquires a reference to the provided 'buf' until completion at
219 // which time the callback is invoked with either a negative error code or
220 // the number of bytes written. The 'callback' is a required parameter.
221 // The contents of 'buf' are not modified.
222 // Should only be called where there is no Write operation in progress.
223 void WriteData(net::IOBuffer* buf, int buf_len,
224 const net::CompletionCallback& callback);
226 // Returns true if there is a write pending.
227 bool IsWritePending() { return IsIOPending(); }
229 // Returns the amount written, info and data.
230 int64 amount_written() { return info_size_ + write_position_; }
232 protected:
233 // Should only be constructed by the storage class and derivatives.
234 AppCacheResponseWriter(int64 response_id,
235 int64 group_id,
236 AppCacheDiskCacheInterface* disk_cache);
238 private:
239 friend class AppCacheStorageImpl;
240 friend class content::MockAppCacheStorage;
242 enum CreationPhase {
243 NO_ATTEMPT,
244 INITIAL_ATTEMPT,
245 DOOM_EXISTING,
246 SECOND_ATTEMPT
249 void OnIOComplete(int result) override;
250 void ContinueWriteInfo();
251 void ContinueWriteData();
252 void CreateEntryIfNeededAndContinue();
253 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
255 int info_size_;
256 int write_position_;
257 int write_amount_;
258 CreationPhase creation_phase_;
259 net::CompletionCallback create_callback_;
260 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
263 // Writes metadata of the existing response to storage. If the object is deleted
264 // and there is a write in progress, the implementation will return
265 // immediately but will take care of any side effect of cancelling the
266 // operation. In other words, instances are safe to delete at will.
267 class CONTENT_EXPORT AppCacheResponseMetadataWriter
268 : public AppCacheResponseIO {
269 public:
270 ~AppCacheResponseMetadataWriter() override;
272 // Writes metadata to storage. Always returns the result of the write
273 // asynchronously through the 'callback'. Returns the number of bytes written
274 // or a net:: error code. Guaranteed to not perform partial writes.
275 // The writer acquires a reference to the provided 'buf' until completion at
276 // which time the callback is invoked with either a negative error code or
277 // the number of bytes written. The 'callback' is a required parameter.
278 // The contents of 'buf' are not modified.
279 // Should only be called where there is no WriteMetadata operation in
280 // progress.
281 void WriteMetadata(net::IOBuffer* buf,
282 int buf_len,
283 const net::CompletionCallback& callback);
285 // Returns true if there is a write pending.
286 bool IsWritePending() { return IsIOPending(); }
288 protected:
289 friend class AppCacheStorageImpl;
290 friend class content::MockAppCacheStorage;
291 // Should only be constructed by the storage class and derivatives.
292 AppCacheResponseMetadataWriter(int64 response_id,
293 int64 group_id,
294 AppCacheDiskCacheInterface* disk_cache);
296 private:
297 void OnIOComplete(int result) override;
298 void OnOpenEntryComplete() override;
300 int write_amount_;
301 base::WeakPtrFactory<AppCacheResponseMetadataWriter> weak_factory_;
304 } // namespace content
306 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_