Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / appcache / appcache_response.h
blob7d5db3a60a23636eb4921001e0330914f28d0ec4
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 WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_
6 #define WEBKIT_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 "googleurl/src/gurl.h"
13 #include "net/base/completion_callback.h"
14 #include "net/http/http_response_info.h"
15 #include "webkit/appcache/appcache_interfaces.h"
17 namespace net {
18 class IOBuffer;
21 namespace appcache {
23 class AppCacheService;
25 static const int kUnkownResponseDataSize = -1;
27 // Response info for a particular response id. Instances are tracked in
28 // the working set.
29 class WEBKIT_STORAGE_EXPORT AppCacheResponseInfo
30 : public base::RefCounted<AppCacheResponseInfo> {
31 public:
32 // AppCacheResponseInfo takes ownership of the http_info.
33 AppCacheResponseInfo(AppCacheService* service, const GURL& manifest_url,
34 int64 response_id, net::HttpResponseInfo* http_info,
35 int64 response_data_size);
37 const GURL& manifest_url() const { return manifest_url_; }
38 int64 response_id() const { return response_id_; }
39 const net::HttpResponseInfo* http_response_info() const {
40 return http_response_info_.get();
42 int64 response_data_size() const { return response_data_size_; }
44 private:
45 friend class base::RefCounted<AppCacheResponseInfo>;
46 virtual ~AppCacheResponseInfo();
48 const GURL manifest_url_;
49 const int64 response_id_;
50 const scoped_ptr<net::HttpResponseInfo> http_response_info_;
51 const int64 response_data_size_;
52 const AppCacheService* service_;
55 // A refcounted wrapper for HttpResponseInfo so we can apply the
56 // refcounting semantics used with IOBuffer with these structures too.
57 struct WEBKIT_STORAGE_EXPORT HttpResponseInfoIOBuffer
58 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> {
59 scoped_ptr<net::HttpResponseInfo> http_info;
60 int response_data_size;
62 HttpResponseInfoIOBuffer();
63 explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info);
65 private:
66 friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>;
67 virtual ~HttpResponseInfoIOBuffer();
70 // Low level storage API used by the response reader and writer.
71 class WEBKIT_STORAGE_EXPORT AppCacheDiskCacheInterface {
72 public:
73 class Entry {
74 public:
75 virtual int Read(int index, int64 offset, net::IOBuffer* buf, int buf_len,
76 const net::CompletionCallback& callback) = 0;
77 virtual int Write(int index, int64 offset, net::IOBuffer* buf, int buf_len,
78 const net::CompletionCallback& callback) = 0;
79 virtual int64 GetSize(int index) = 0;
80 virtual void Close() = 0;
81 protected:
82 virtual ~Entry() {}
85 virtual int CreateEntry(int64 key, Entry** entry,
86 const net::CompletionCallback& callback) = 0;
87 virtual int OpenEntry(int64 key, Entry** entry,
88 const net::CompletionCallback& callback) = 0;
89 virtual int DoomEntry(int64 key, const net::CompletionCallback& callback) = 0;
91 protected:
92 friend class base::RefCounted<AppCacheDiskCacheInterface>;
93 virtual ~AppCacheDiskCacheInterface() {}
96 // Common base class for response reader and writer.
97 class WEBKIT_STORAGE_EXPORT AppCacheResponseIO {
98 public:
99 virtual ~AppCacheResponseIO();
100 int64 response_id() const { return response_id_; }
102 protected:
103 AppCacheResponseIO(int64 response_id,
104 int64 group_id,
105 AppCacheDiskCacheInterface* disk_cache);
107 virtual void OnIOComplete(int result) = 0;
109 bool IsIOPending() { return !callback_.is_null(); }
110 void ScheduleIOCompletionCallback(int result);
111 void InvokeUserCompletionCallback(int result);
112 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
113 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
115 const int64 response_id_;
116 const int64 group_id_;
117 AppCacheDiskCacheInterface* disk_cache_;
118 AppCacheDiskCacheInterface::Entry* entry_;
119 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
120 scoped_refptr<net::IOBuffer> buffer_;
121 int buffer_len_;
122 net::CompletionCallback callback_;
123 base::WeakPtrFactory<AppCacheResponseIO> weak_factory_;
125 private:
126 void OnRawIOComplete(int result);
129 // Reads existing response data from storage. If the object is deleted
130 // and there is a read in progress, the implementation will return
131 // immediately but will take care of any side effect of cancelling the
132 // operation. In other words, instances are safe to delete at will.
133 class WEBKIT_STORAGE_EXPORT AppCacheResponseReader : public AppCacheResponseIO {
134 public:
135 virtual ~AppCacheResponseReader();
137 // Reads http info from storage. Always returns the result of the read
138 // asynchronously through the 'callback'. Returns the number of bytes read
139 // or a net:: error code. Guaranteed to not perform partial reads of
140 // the info data. The reader acquires a reference to the 'info_buf' until
141 // completion at which time the callback is invoked with either a negative
142 // error code or the number of bytes read. The 'info_buf' argument should
143 // contain a NULL http_info when ReadInfo is called. The 'callback' is a
144 // required parameter.
145 // Should only be called where there is no Read operation in progress.
146 // (virtual for testing)
147 virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
148 const net::CompletionCallback& callback);
150 // Reads data from storage. Always returns the result of the read
151 // asynchronously through the 'callback'. Returns the number of bytes read
152 // or a net:: error code. EOF is indicated with a return value of zero.
153 // The reader acquires a reference to the provided 'buf' until completion
154 // at which time the callback is invoked with either a negative error code
155 // or the number of bytes read. The 'callback' is a required parameter.
156 // Should only be called where there is no Read operation in progress.
157 // (virtual for testing)
158 virtual void ReadData(net::IOBuffer* buf, int buf_len,
159 const net::CompletionCallback& callback);
161 // Returns true if there is a read operation, for data or info, pending.
162 bool IsReadPending() { return IsIOPending(); }
164 // Used to support range requests. If not called, the reader will
165 // read the entire response body. If called, this must be called prior
166 // to the first call to the ReadData method.
167 void SetReadRange(int offset, int length);
169 protected:
170 friend class AppCacheStorageImpl;
171 friend class MockAppCacheStorage;
173 // Should only be constructed by the storage class.
174 AppCacheResponseReader(int64 response_id,
175 int64 group_id,
176 AppCacheDiskCacheInterface* disk_cache);
178 virtual void OnIOComplete(int result) OVERRIDE;
179 void ContinueReadInfo();
180 void ContinueReadData();
181 void OpenEntryIfNeededAndContinue();
182 void OnOpenEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
184 int range_offset_;
185 int range_length_;
186 int read_position_;
187 net::CompletionCallback open_callback_;
188 base::WeakPtrFactory<AppCacheResponseReader> weak_factory_;
191 // Writes new response data to storage. If the object is deleted
192 // and there is a write in progress, the implementation will return
193 // immediately but will take care of any side effect of cancelling the
194 // operation. In other words, instances are safe to delete at will.
195 class WEBKIT_STORAGE_EXPORT AppCacheResponseWriter : public AppCacheResponseIO {
196 public:
197 virtual ~AppCacheResponseWriter();
199 // Writes the http info to storage. Always returns the result of the write
200 // asynchronously through the 'callback'. Returns the number of bytes written
201 // or a net:: error code. The writer acquires a reference to the 'info_buf'
202 // until completion at which time the callback is invoked with either a
203 // negative error code or the number of bytes written. The 'callback' is a
204 // required parameter. The contents of 'info_buf' are not modified.
205 // Should only be called where there is no Write operation in progress.
206 void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
207 const net::CompletionCallback& callback);
209 // Writes data to storage. Always returns the result of the write
210 // asynchronously through the 'callback'. Returns the number of bytes written
211 // or a net:: error code. Guaranteed to not perform partial writes.
212 // The writer acquires a reference to the provided 'buf' until completion at
213 // which time the callback is invoked with either a negative error code or
214 // the number of bytes written. The 'callback' is a required parameter.
215 // The contents of 'buf' are not modified.
216 // Should only be called where there is no Write operation in progress.
217 void WriteData(net::IOBuffer* buf, int buf_len,
218 const net::CompletionCallback& callback);
220 // Returns true if there is a write pending.
221 bool IsWritePending() { return IsIOPending(); }
223 // Returns the amount written, info and data.
224 int64 amount_written() { return info_size_ + write_position_; }
226 private:
227 friend class AppCacheStorageImpl;
228 friend class MockAppCacheStorage;
230 enum CreationPhase {
231 NO_ATTEMPT,
232 INITIAL_ATTEMPT,
233 DOOM_EXISTING,
234 SECOND_ATTEMPT
237 // Should only be constructed by the storage class.
238 AppCacheResponseWriter(int64 response_id,
239 int64 group_id,
240 AppCacheDiskCacheInterface* disk_cache);
242 virtual void OnIOComplete(int result) OVERRIDE;
243 void ContinueWriteInfo();
244 void ContinueWriteData();
245 void CreateEntryIfNeededAndContinue();
246 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
248 int info_size_;
249 int write_position_;
250 int write_amount_;
251 CreationPhase creation_phase_;
252 net::CompletionCallback create_callback_;
253 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
256 } // namespace appcache
258 #endif // WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_