Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / content / browser / appcache / appcache_response.h
bloba4c87db3d562ac7b97b6ffa9ae53a35743b277c5
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 // (virtual for testing)
213 virtual void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
214 const net::CompletionCallback& callback);
216 // Writes data to storage. Always returns the result of the write
217 // asynchronously through the 'callback'. Returns the number of bytes written
218 // or a net:: error code. Guaranteed to not perform partial writes.
219 // The writer acquires a reference to the provided 'buf' until completion at
220 // which time the callback is invoked with either a negative error code or
221 // the number of bytes written. The 'callback' is a required parameter.
222 // The contents of 'buf' are not modified.
223 // Should only be called where there is no Write operation in progress.
224 // (virtual for testing)
225 virtual void WriteData(net::IOBuffer* buf,
226 int buf_len,
227 const net::CompletionCallback& callback);
229 // Returns true if there is a write pending.
230 bool IsWritePending() { return IsIOPending(); }
232 // Returns the amount written, info and data.
233 int64 amount_written() { return info_size_ + write_position_; }
235 protected:
236 // Should only be constructed by the storage class and derivatives.
237 AppCacheResponseWriter(int64 response_id,
238 int64 group_id,
239 AppCacheDiskCacheInterface* disk_cache);
241 private:
242 friend class AppCacheStorageImpl;
243 friend class content::MockAppCacheStorage;
245 enum CreationPhase {
246 NO_ATTEMPT,
247 INITIAL_ATTEMPT,
248 DOOM_EXISTING,
249 SECOND_ATTEMPT
252 void OnIOComplete(int result) override;
253 void ContinueWriteInfo();
254 void ContinueWriteData();
255 void CreateEntryIfNeededAndContinue();
256 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
258 int info_size_;
259 int write_position_;
260 int write_amount_;
261 CreationPhase creation_phase_;
262 net::CompletionCallback create_callback_;
263 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
266 // Writes metadata of the existing response to storage. If the object is deleted
267 // and there is a write in progress, the implementation will return
268 // immediately but will take care of any side effect of cancelling the
269 // operation. In other words, instances are safe to delete at will.
270 class CONTENT_EXPORT AppCacheResponseMetadataWriter
271 : public AppCacheResponseIO {
272 public:
273 ~AppCacheResponseMetadataWriter() override;
275 // Writes metadata to storage. Always returns the result of the write
276 // asynchronously through the 'callback'. Returns the number of bytes written
277 // or a net:: error code. Guaranteed to not perform partial writes.
278 // The writer acquires a reference to the provided 'buf' until completion at
279 // which time the callback is invoked with either a negative error code or
280 // the number of bytes written. The 'callback' is a required parameter.
281 // The contents of 'buf' are not modified.
282 // Should only be called where there is no WriteMetadata operation in
283 // progress.
284 void WriteMetadata(net::IOBuffer* buf,
285 int buf_len,
286 const net::CompletionCallback& callback);
288 // Returns true if there is a write pending.
289 bool IsWritePending() { return IsIOPending(); }
291 protected:
292 friend class AppCacheStorageImpl;
293 friend class content::MockAppCacheStorage;
294 // Should only be constructed by the storage class and derivatives.
295 AppCacheResponseMetadataWriter(int64 response_id,
296 int64 group_id,
297 AppCacheDiskCacheInterface* disk_cache);
299 private:
300 void OnIOComplete(int result) override;
301 void OnOpenEntryComplete() override;
303 int write_amount_;
304 base::WeakPtrFactory<AppCacheResponseMetadataWriter> weak_factory_;
307 } // namespace content
309 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_