blob: 610bdf4bc8bf93adaa905e50fae606e319aa7744 [file] [log] [blame]
[email protected]43fe9f52012-01-03 18:52:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e7dff7ba2009-10-09 03:25:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d5b2fdf2013-06-05 09:36:555#ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_
6#define WEBKIT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_
[email protected]e7dff7ba2009-10-09 03:25:517
[email protected]073aabe2009-10-21 18:09:338#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
[email protected]e29f9332011-10-28 22:44:5711#include "base/memory/weak_ptr.h"
[email protected]e7dff7ba2009-10-09 03:25:5112#include "net/base/completion_callback.h"
13#include "net/http/http_response_info.h"
[email protected]ad677772013-06-29 14:18:3814#include "url/gurl.h"
[email protected]e5bf2fd2013-06-13 02:59:1815#include "webkit/browser/webkit_storage_browser_export.h"
[email protected]d5b2fdf2013-06-05 09:36:5516#include "webkit/common/appcache/appcache_interfaces.h"
[email protected]e7dff7ba2009-10-09 03:25:5117
18namespace net {
19class IOBuffer;
20}
[email protected]e7dff7ba2009-10-09 03:25:5121
[email protected]e7dff7ba2009-10-09 03:25:5122namespace appcache {
23
[email protected]9f67dd02013-10-22 23:08:2024class AppCacheStorage;
[email protected]073aabe2009-10-21 18:09:3325
[email protected]fd2885a2010-03-17 22:02:2826static const int kUnkownResponseDataSize = -1;
27
[email protected]e7dff7ba2009-10-09 03:25:5128// Response info for a particular response id. Instances are tracked in
29// the working set.
[email protected]e5bf2fd2013-06-13 02:59:1830class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheResponseInfo
[email protected]e7dff7ba2009-10-09 03:25:5131 : public base::RefCounted<AppCacheResponseInfo> {
[email protected]d144b052009-10-13 01:45:0932 public:
[email protected]e7dff7ba2009-10-09 03:25:5133 // AppCacheResponseInfo takes ownership of the http_info.
[email protected]9f67dd02013-10-22 23:08:2034 AppCacheResponseInfo(AppCacheStorage* storage, const GURL& manifest_url,
[email protected]fd2885a2010-03-17 22:02:2835 int64 response_id, net::HttpResponseInfo* http_info,
36 int64 response_data_size);
[email protected]e7dff7ba2009-10-09 03:25:5137
[email protected]3367fc1d2009-11-09 00:09:2138 const GURL& manifest_url() const { return manifest_url_; }
[email protected]e7dff7ba2009-10-09 03:25:5139 int64 response_id() const { return response_id_; }
[email protected]e7dff7ba2009-10-09 03:25:5140 const net::HttpResponseInfo* http_response_info() const {
41 return http_response_info_.get();
42 }
[email protected]fd2885a2010-03-17 22:02:2843 int64 response_data_size() const { return response_data_size_; }
[email protected]e7dff7ba2009-10-09 03:25:5144
45 private:
[email protected]122331d52009-11-05 23:40:4546 friend class base::RefCounted<AppCacheResponseInfo>;
[email protected]20f0487a2010-09-30 20:06:3047 virtual ~AppCacheResponseInfo();
[email protected]122331d52009-11-05 23:40:4548
[email protected]3367fc1d2009-11-09 00:09:2149 const GURL manifest_url_;
[email protected]e7dff7ba2009-10-09 03:25:5150 const int64 response_id_;
51 const scoped_ptr<net::HttpResponseInfo> http_response_info_;
[email protected]fd2885a2010-03-17 22:02:2852 const int64 response_data_size_;
[email protected]9f67dd02013-10-22 23:08:2053 AppCacheStorage* storage_;
[email protected]e7dff7ba2009-10-09 03:25:5154};
55
[email protected]e7dff7ba2009-10-09 03:25:5156// A refcounted wrapper for HttpResponseInfo so we can apply the
57// refcounting semantics used with IOBuffer with these structures too.
[email protected]e5bf2fd2013-06-13 02:59:1858struct WEBKIT_STORAGE_BROWSER_EXPORT HttpResponseInfoIOBuffer
[email protected]e7dff7ba2009-10-09 03:25:5159 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> {
60 scoped_ptr<net::HttpResponseInfo> http_info;
[email protected]fd2885a2010-03-17 22:02:2861 int response_data_size;
[email protected]073aabe2009-10-21 18:09:3362
[email protected]20f0487a2010-09-30 20:06:3063 HttpResponseInfoIOBuffer();
64 explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info);
[email protected]122331d52009-11-05 23:40:4565
66 private:
67 friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>;
[email protected]20f0487a2010-09-30 20:06:3068 virtual ~HttpResponseInfoIOBuffer();
[email protected]073aabe2009-10-21 18:09:3369};
70
[email protected]50304162011-12-21 17:51:3071// Low level storage API used by the response reader and writer.
[email protected]e5bf2fd2013-06-13 02:59:1872class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDiskCacheInterface {
[email protected]f5a73e072011-09-19 19:34:1773 public:
74 class Entry {
75 public:
76 virtual int Read(int index, int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2777 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1778 virtual int Write(int index, int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2779 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1780 virtual int64 GetSize(int index) = 0;
81 virtual void Close() = 0;
82 protected:
83 virtual ~Entry() {}
84 };
85
86 virtual int CreateEntry(int64 key, Entry** entry,
[email protected]50304162011-12-21 17:51:3087 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1788 virtual int OpenEntry(int64 key, Entry** entry,
[email protected]50304162011-12-21 17:51:3089 const net::CompletionCallback& callback) = 0;
90 virtual int DoomEntry(int64 key, const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1791
92 protected:
93 friend class base::RefCounted<AppCacheDiskCacheInterface>;
94 virtual ~AppCacheDiskCacheInterface() {}
95};
96
[email protected]073aabe2009-10-21 18:09:3397// Common base class for response reader and writer.
[email protected]e5bf2fd2013-06-13 02:59:1898class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheResponseIO {
[email protected]073aabe2009-10-21 18:09:3399 public:
100 virtual ~AppCacheResponseIO();
101 int64 response_id() const { return response_id_; }
102
103 protected:
[email protected]f5a73e072011-09-19 19:34:17104 AppCacheResponseIO(int64 response_id,
[email protected]4252f602011-10-21 19:18:36105 int64 group_id,
[email protected]f5a73e072011-09-19 19:34:17106 AppCacheDiskCacheInterface* disk_cache);
[email protected]073aabe2009-10-21 18:09:33107
108 virtual void OnIOComplete(int result) = 0;
109
[email protected]50304162011-12-21 17:51:30110 bool IsIOPending() { return !callback_.is_null(); }
[email protected]72a3ce52011-12-22 05:10:24111 void ScheduleIOCompletionCallback(int result);
112 void InvokeUserCompletionCallback(int result);
[email protected]073aabe2009-10-21 18:09:33113 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
114 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
115
116 const int64 response_id_;
[email protected]4252f602011-10-21 19:18:36117 const int64 group_id_;
[email protected]f5a73e072011-09-19 19:34:17118 AppCacheDiskCacheInterface* disk_cache_;
119 AppCacheDiskCacheInterface::Entry* entry_;
[email protected]073aabe2009-10-21 18:09:33120 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
121 scoped_refptr<net::IOBuffer> buffer_;
[email protected]fd2885a2010-03-17 22:02:28122 int buffer_len_;
[email protected]50304162011-12-21 17:51:30123 net::CompletionCallback callback_;
[email protected]e29f9332011-10-28 22:44:57124 base::WeakPtrFactory<AppCacheResponseIO> weak_factory_;
[email protected]073aabe2009-10-21 18:09:33125
126 private:
127 void OnRawIOComplete(int result);
[email protected]e7dff7ba2009-10-09 03:25:51128};
129
130// Reads existing response data from storage. If the object is deleted
131// and there is a read in progress, the implementation will return
132// immediately but will take care of any side effect of cancelling the
133// operation. In other words, instances are safe to delete at will.
[email protected]e5bf2fd2013-06-13 02:59:18134class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheResponseReader
135 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51136 public:
[email protected]fd2885a2010-03-17 22:02:28137 virtual ~AppCacheResponseReader();
138
[email protected]073aabe2009-10-21 18:09:33139 // Reads http info from storage. Always returns the result of the read
140 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51141 // or a net:: error code. Guaranteed to not perform partial reads of
[email protected]073aabe2009-10-21 18:09:33142 // the info data. The reader acquires a reference to the 'info_buf' until
143 // completion at which time the callback is invoked with either a negative
144 // error code or the number of bytes read. The 'info_buf' argument should
145 // contain a NULL http_info when ReadInfo is called. The 'callback' is a
146 // required parameter.
[email protected]e7dff7ba2009-10-09 03:25:51147 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35148 // (virtual for testing)
149 virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
[email protected]50304162011-12-21 17:51:30150 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51151
[email protected]073aabe2009-10-21 18:09:33152 // Reads data from storage. Always returns the result of the read
153 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51154 // or a net:: error code. EOF is indicated with a return value of zero.
[email protected]073aabe2009-10-21 18:09:33155 // The reader acquires a reference to the provided 'buf' until completion
156 // at which time the callback is invoked with either a negative error code
157 // or the number of bytes read. The 'callback' is a required parameter.
[email protected]e7dff7ba2009-10-09 03:25:51158 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35159 // (virtual for testing)
160 virtual void ReadData(net::IOBuffer* buf, int buf_len,
[email protected]50304162011-12-21 17:51:30161 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51162
163 // Returns true if there is a read operation, for data or info, pending.
[email protected]073aabe2009-10-21 18:09:33164 bool IsReadPending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51165
166 // Used to support range requests. If not called, the reader will
167 // read the entire response body. If called, this must be called prior
168 // to the first call to the ReadData method.
[email protected]073aabe2009-10-21 18:09:33169 void SetReadRange(int offset, int length);
[email protected]e7dff7ba2009-10-09 03:25:51170
[email protected]20d06f62011-07-22 23:57:35171 protected:
[email protected]e7dff7ba2009-10-09 03:25:51172 friend class AppCacheStorageImpl;
173 friend class MockAppCacheStorage;
174
175 // Should only be constructed by the storage class.
[email protected]f5a73e072011-09-19 19:34:17176 AppCacheResponseReader(int64 response_id,
[email protected]4252f602011-10-21 19:18:36177 int64 group_id,
[email protected]f5a73e072011-09-19 19:34:17178 AppCacheDiskCacheInterface* disk_cache);
[email protected]e7dff7ba2009-10-09 03:25:51179
[email protected]0da6b4452011-11-16 21:36:16180 virtual void OnIOComplete(int result) OVERRIDE;
[email protected]fd2885a2010-03-17 22:02:28181 void ContinueReadInfo();
182 void ContinueReadData();
183 void OpenEntryIfNeededAndContinue();
[email protected]43fe9f52012-01-03 18:52:10184 void OnOpenEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]073aabe2009-10-21 18:09:33185
186 int range_offset_;
187 int range_length_;
188 int read_position_;
[email protected]43fe9f52012-01-03 18:52:10189 net::CompletionCallback open_callback_;
190 base::WeakPtrFactory<AppCacheResponseReader> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51191};
192
193// Writes new response data to storage. If the object is deleted
194// and there is a write in progress, the implementation will return
195// immediately but will take care of any side effect of cancelling the
196// operation. In other words, instances are safe to delete at will.
[email protected]e5bf2fd2013-06-13 02:59:18197class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheResponseWriter
198 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51199 public:
[email protected]fd2885a2010-03-17 22:02:28200 virtual ~AppCacheResponseWriter();
201
[email protected]073aabe2009-10-21 18:09:33202 // Writes the http info to storage. Always returns the result of the write
203 // asynchronously through the 'callback'. Returns the number of bytes written
204 // or a net:: error code. The writer acquires a reference to the 'info_buf'
205 // until completion at which time the callback is invoked with either a
206 // negative error code or the number of bytes written. The 'callback' is a
207 // required parameter. The contents of 'info_buf' are not modified.
[email protected]e7dff7ba2009-10-09 03:25:51208 // Should only be called where there is no Write operation in progress.
[email protected]073aabe2009-10-21 18:09:33209 void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
[email protected]50304162011-12-21 17:51:30210 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51211
[email protected]073aabe2009-10-21 18:09:33212 // Writes data to storage. Always returns the result of the write
213 // asynchronously through the 'callback'. Returns the number of bytes written
[email protected]e7dff7ba2009-10-09 03:25:51214 // or a net:: error code. Guaranteed to not perform partial writes.
[email protected]073aabe2009-10-21 18:09:33215 // The writer acquires a reference to the provided 'buf' until completion at
216 // which time the callback is invoked with either a negative error code or
217 // the number of bytes written. The 'callback' is a required parameter.
218 // The contents of 'buf' are not modified.
[email protected]e7dff7ba2009-10-09 03:25:51219 // Should only be called where there is no Write operation in progress.
[email protected]073aabe2009-10-21 18:09:33220 void WriteData(net::IOBuffer* buf, int buf_len,
[email protected]50304162011-12-21 17:51:30221 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51222
223 // Returns true if there is a write pending.
[email protected]073aabe2009-10-21 18:09:33224 bool IsWritePending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51225
[email protected]0dfeec32010-01-06 22:25:33226 // Returns the amount written, info and data.
227 int64 amount_written() { return info_size_ + write_position_; }
228
[email protected]e7dff7ba2009-10-09 03:25:51229 private:
230 friend class AppCacheStorageImpl;
231 friend class MockAppCacheStorage;
232
[email protected]fd2885a2010-03-17 22:02:28233 enum CreationPhase {
234 NO_ATTEMPT,
235 INITIAL_ATTEMPT,
236 DOOM_EXISTING,
237 SECOND_ATTEMPT
238 };
239
[email protected]e7dff7ba2009-10-09 03:25:51240 // Should only be constructed by the storage class.
[email protected]f5a73e072011-09-19 19:34:17241 AppCacheResponseWriter(int64 response_id,
[email protected]4252f602011-10-21 19:18:36242 int64 group_id,
[email protected]f5a73e072011-09-19 19:34:17243 AppCacheDiskCacheInterface* disk_cache);
[email protected]073aabe2009-10-21 18:09:33244
[email protected]0da6b4452011-11-16 21:36:16245 virtual void OnIOComplete(int result) OVERRIDE;
[email protected]fd2885a2010-03-17 22:02:28246 void ContinueWriteInfo();
247 void ContinueWriteData();
248 void CreateEntryIfNeededAndContinue();
[email protected]43fe9f52012-01-03 18:52:10249 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]073aabe2009-10-21 18:09:33250
[email protected]0dfeec32010-01-06 22:25:33251 int info_size_;
[email protected]073aabe2009-10-21 18:09:33252 int write_position_;
253 int write_amount_;
[email protected]fd2885a2010-03-17 22:02:28254 CreationPhase creation_phase_;
[email protected]43fe9f52012-01-03 18:52:10255 net::CompletionCallback create_callback_;
256 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51257};
258
259} // namespace appcache
260
[email protected]d5b2fdf2013-06-05 09:36:55261#endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_