blob: 474b4d0c6cd7585ebc215d3275c90ed8071065e2 [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]98d6d4562014-06-25 20:57:555#ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_
6#define CONTENT_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]98d6d4562014-06-25 20:57:5512#include "content/common/appcache_interfaces.h"
13#include "content/common/content_export.h"
[email protected]e7dff7ba2009-10-09 03:25:5114#include "net/base/completion_callback.h"
15#include "net/http/http_response_info.h"
[email protected]ad677772013-06-29 14:18:3816#include "url/gurl.h"
[email protected]e7dff7ba2009-10-09 03:25:5117
18namespace net {
19class IOBuffer;
20}
[email protected]e7dff7ba2009-10-09 03:25:5121
[email protected]71fe3d12014-04-22 00:32:3822namespace content {
23class MockAppCacheStorage;
24}
25
[email protected]98d6d4562014-06-25 20:57:5526namespace content {
[email protected]e7dff7ba2009-10-09 03:25:5127
[email protected]9f67dd02013-10-22 23:08:2028class AppCacheStorage;
[email protected]073aabe2009-10-21 18:09:3329
[email protected]fd2885a2010-03-17 22:02:2830static const int kUnkownResponseDataSize = -1;
31
[email protected]e7dff7ba2009-10-09 03:25:5132// Response info for a particular response id. Instances are tracked in
33// the working set.
[email protected]98d6d4562014-06-25 20:57:5534class CONTENT_EXPORT AppCacheResponseInfo
[email protected]e7dff7ba2009-10-09 03:25:5135 : public base::RefCounted<AppCacheResponseInfo> {
[email protected]d144b052009-10-13 01:45:0936 public:
[email protected]e7dff7ba2009-10-09 03:25:5137 // AppCacheResponseInfo takes ownership of the http_info.
[email protected]9f67dd02013-10-22 23:08:2038 AppCacheResponseInfo(AppCacheStorage* storage, const GURL& manifest_url,
[email protected]fd2885a2010-03-17 22:02:2839 int64 response_id, net::HttpResponseInfo* http_info,
40 int64 response_data_size);
[email protected]e7dff7ba2009-10-09 03:25:5141
[email protected]3367fc1d2009-11-09 00:09:2142 const GURL& manifest_url() const { return manifest_url_; }
[email protected]e7dff7ba2009-10-09 03:25:5143 int64 response_id() const { return response_id_; }
[email protected]e7dff7ba2009-10-09 03:25:5144 const net::HttpResponseInfo* http_response_info() const {
45 return http_response_info_.get();
46 }
[email protected]fd2885a2010-03-17 22:02:2847 int64 response_data_size() const { return response_data_size_; }
[email protected]e7dff7ba2009-10-09 03:25:5148
49 private:
[email protected]122331d52009-11-05 23:40:4550 friend class base::RefCounted<AppCacheResponseInfo>;
[email protected]20f0487a2010-09-30 20:06:3051 virtual ~AppCacheResponseInfo();
[email protected]122331d52009-11-05 23:40:4552
[email protected]3367fc1d2009-11-09 00:09:2153 const GURL manifest_url_;
[email protected]e7dff7ba2009-10-09 03:25:5154 const int64 response_id_;
55 const scoped_ptr<net::HttpResponseInfo> http_response_info_;
[email protected]fd2885a2010-03-17 22:02:2856 const int64 response_data_size_;
[email protected]9f67dd02013-10-22 23:08:2057 AppCacheStorage* storage_;
[email protected]e7dff7ba2009-10-09 03:25:5158};
59
[email protected]e7dff7ba2009-10-09 03:25:5160// A refcounted wrapper for HttpResponseInfo so we can apply the
61// refcounting semantics used with IOBuffer with these structures too.
[email protected]98d6d4562014-06-25 20:57:5562struct CONTENT_EXPORT HttpResponseInfoIOBuffer
[email protected]e7dff7ba2009-10-09 03:25:5163 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> {
64 scoped_ptr<net::HttpResponseInfo> http_info;
[email protected]fd2885a2010-03-17 22:02:2865 int response_data_size;
[email protected]073aabe2009-10-21 18:09:3366
[email protected]20f0487a2010-09-30 20:06:3067 HttpResponseInfoIOBuffer();
68 explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info);
[email protected]122331d52009-11-05 23:40:4569
[email protected]8022ed52014-05-06 03:13:4070 protected:
[email protected]122331d52009-11-05 23:40:4571 friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>;
[email protected]20f0487a2010-09-30 20:06:3072 virtual ~HttpResponseInfoIOBuffer();
[email protected]073aabe2009-10-21 18:09:3373};
74
[email protected]50304162011-12-21 17:51:3075// Low level storage API used by the response reader and writer.
[email protected]98d6d4562014-06-25 20:57:5576class CONTENT_EXPORT AppCacheDiskCacheInterface {
[email protected]f5a73e072011-09-19 19:34:1777 public:
78 class Entry {
79 public:
80 virtual int Read(int index, int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2781 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1782 virtual int Write(int index, int64 offset, net::IOBuffer* buf, int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2783 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1784 virtual int64 GetSize(int index) = 0;
85 virtual void Close() = 0;
86 protected:
87 virtual ~Entry() {}
88 };
89
90 virtual int CreateEntry(int64 key, Entry** entry,
[email protected]50304162011-12-21 17:51:3091 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1792 virtual int OpenEntry(int64 key, Entry** entry,
[email protected]50304162011-12-21 17:51:3093 const net::CompletionCallback& callback) = 0;
94 virtual int DoomEntry(int64 key, const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:1795
96 protected:
97 friend class base::RefCounted<AppCacheDiskCacheInterface>;
98 virtual ~AppCacheDiskCacheInterface() {}
99};
100
[email protected]073aabe2009-10-21 18:09:33101// Common base class for response reader and writer.
[email protected]98d6d4562014-06-25 20:57:55102class CONTENT_EXPORT AppCacheResponseIO {
[email protected]073aabe2009-10-21 18:09:33103 public:
104 virtual ~AppCacheResponseIO();
105 int64 response_id() const { return response_id_; }
106
107 protected:
[email protected]f5a73e072011-09-19 19:34:17108 AppCacheResponseIO(int64 response_id,
[email protected]4252f602011-10-21 19:18:36109 int64 group_id,
[email protected]f5a73e072011-09-19 19:34:17110 AppCacheDiskCacheInterface* disk_cache);
[email protected]073aabe2009-10-21 18:09:33111
112 virtual void OnIOComplete(int result) = 0;
113
[email protected]50304162011-12-21 17:51:30114 bool IsIOPending() { return !callback_.is_null(); }
[email protected]72a3ce52011-12-22 05:10:24115 void ScheduleIOCompletionCallback(int result);
116 void InvokeUserCompletionCallback(int result);
[email protected]073aabe2009-10-21 18:09:33117 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
118 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
119
120 const int64 response_id_;
[email protected]4252f602011-10-21 19:18:36121 const int64 group_id_;
[email protected]f5a73e072011-09-19 19:34:17122 AppCacheDiskCacheInterface* disk_cache_;
123 AppCacheDiskCacheInterface::Entry* entry_;
[email protected]073aabe2009-10-21 18:09:33124 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
125 scoped_refptr<net::IOBuffer> buffer_;
[email protected]fd2885a2010-03-17 22:02:28126 int buffer_len_;
[email protected]50304162011-12-21 17:51:30127 net::CompletionCallback callback_;
[email protected]e29f9332011-10-28 22:44:57128 base::WeakPtrFactory<AppCacheResponseIO> weak_factory_;
[email protected]073aabe2009-10-21 18:09:33129
130 private:
131 void OnRawIOComplete(int result);
[email protected]e7dff7ba2009-10-09 03:25:51132};
133
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.
[email protected]98d6d4562014-06-25 20:57:55138class CONTENT_EXPORT AppCacheResponseReader
[email protected]e5bf2fd2013-06-13 02:59:18139 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51140 public:
[email protected]fd2885a2010-03-17 22:02:28141 virtual ~AppCacheResponseReader();
142
[email protected]073aabe2009-10-21 18:09:33143 // Reads http info from storage. Always returns the result of the read
144 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51145 // or a net:: error code. Guaranteed to not perform partial reads of
[email protected]073aabe2009-10-21 18:09:33146 // 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.
[email protected]e7dff7ba2009-10-09 03:25:51151 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35152 // (virtual for testing)
153 virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
[email protected]50304162011-12-21 17:51:30154 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51155
[email protected]073aabe2009-10-21 18:09:33156 // Reads data from storage. Always returns the result of the read
157 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51158 // or a net:: error code. EOF is indicated with a return value of zero.
[email protected]073aabe2009-10-21 18:09:33159 // 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.
[email protected]e7dff7ba2009-10-09 03:25:51162 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35163 // (virtual for testing)
164 virtual void ReadData(net::IOBuffer* buf, int buf_len,
[email protected]50304162011-12-21 17:51:30165 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51166
167 // Returns true if there is a read operation, for data or info, pending.
[email protected]073aabe2009-10-21 18:09:33168 bool IsReadPending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51169
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.
[email protected]073aabe2009-10-21 18:09:33173 void SetReadRange(int offset, int length);
[email protected]e7dff7ba2009-10-09 03:25:51174
[email protected]20d06f62011-07-22 23:57:35175 protected:
[email protected]e7dff7ba2009-10-09 03:25:51176 friend class AppCacheStorageImpl;
[email protected]71fe3d12014-04-22 00:32:38177 friend class content::MockAppCacheStorage;
[email protected]e7dff7ba2009-10-09 03:25:51178
[email protected]8022ed52014-05-06 03:13:40179 // Should only be constructed by the storage class and derivatives.
[email protected]f5a73e072011-09-19 19:34:17180 AppCacheResponseReader(int64 response_id,
[email protected]4252f602011-10-21 19:18:36181 int64 group_id,
[email protected]f5a73e072011-09-19 19:34:17182 AppCacheDiskCacheInterface* disk_cache);
[email protected]e7dff7ba2009-10-09 03:25:51183
[email protected]0da6b4452011-11-16 21:36:16184 virtual void OnIOComplete(int result) OVERRIDE;
[email protected]fd2885a2010-03-17 22:02:28185 void ContinueReadInfo();
186 void ContinueReadData();
187 void OpenEntryIfNeededAndContinue();
[email protected]43fe9f52012-01-03 18:52:10188 void OnOpenEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]073aabe2009-10-21 18:09:33189
190 int range_offset_;
191 int range_length_;
192 int read_position_;
[email protected]43fe9f52012-01-03 18:52:10193 net::CompletionCallback open_callback_;
194 base::WeakPtrFactory<AppCacheResponseReader> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51195};
196
197// Writes new response data to storage. If the object is deleted
198// and there is a write in progress, the implementation will return
199// immediately but will take care of any side effect of cancelling the
200// operation. In other words, instances are safe to delete at will.
[email protected]98d6d4562014-06-25 20:57:55201class CONTENT_EXPORT AppCacheResponseWriter
[email protected]e5bf2fd2013-06-13 02:59:18202 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51203 public:
[email protected]fd2885a2010-03-17 22:02:28204 virtual ~AppCacheResponseWriter();
205
[email protected]073aabe2009-10-21 18:09:33206 // Writes the http info to storage. Always returns the result of the write
207 // asynchronously through the 'callback'. Returns the number of bytes written
208 // or a net:: error code. The writer acquires a reference to the 'info_buf'
209 // until completion at which time the callback is invoked with either a
210 // negative error code or the number of bytes written. The 'callback' is a
211 // required parameter. The contents of 'info_buf' are not modified.
[email protected]e7dff7ba2009-10-09 03:25:51212 // Should only be called where there is no Write operation in progress.
[email protected]073aabe2009-10-21 18:09:33213 void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
[email protected]50304162011-12-21 17:51:30214 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51215
[email protected]073aabe2009-10-21 18:09:33216 // Writes data to storage. Always returns the result of the write
217 // asynchronously through the 'callback'. Returns the number of bytes written
[email protected]e7dff7ba2009-10-09 03:25:51218 // or a net:: error code. Guaranteed to not perform partial writes.
[email protected]073aabe2009-10-21 18:09:33219 // 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.
[email protected]e7dff7ba2009-10-09 03:25:51223 // Should only be called where there is no Write operation in progress.
[email protected]073aabe2009-10-21 18:09:33224 void WriteData(net::IOBuffer* buf, int buf_len,
[email protected]50304162011-12-21 17:51:30225 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51226
227 // Returns true if there is a write pending.
[email protected]073aabe2009-10-21 18:09:33228 bool IsWritePending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51229
[email protected]0dfeec32010-01-06 22:25:33230 // Returns the amount written, info and data.
231 int64 amount_written() { return info_size_ + write_position_; }
232
[email protected]8022ed52014-05-06 03:13:40233 protected:
234 // Should only be constructed by the storage class and derivatives.
235 AppCacheResponseWriter(int64 response_id,
236 int64 group_id,
237 AppCacheDiskCacheInterface* disk_cache);
238
[email protected]e7dff7ba2009-10-09 03:25:51239 private:
240 friend class AppCacheStorageImpl;
[email protected]71fe3d12014-04-22 00:32:38241 friend class content::MockAppCacheStorage;
[email protected]e7dff7ba2009-10-09 03:25:51242
[email protected]fd2885a2010-03-17 22:02:28243 enum CreationPhase {
244 NO_ATTEMPT,
245 INITIAL_ATTEMPT,
246 DOOM_EXISTING,
247 SECOND_ATTEMPT
248 };
249
[email protected]0da6b4452011-11-16 21:36:16250 virtual void OnIOComplete(int result) OVERRIDE;
[email protected]fd2885a2010-03-17 22:02:28251 void ContinueWriteInfo();
252 void ContinueWriteData();
253 void CreateEntryIfNeededAndContinue();
[email protected]43fe9f52012-01-03 18:52:10254 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]073aabe2009-10-21 18:09:33255
[email protected]0dfeec32010-01-06 22:25:33256 int info_size_;
[email protected]073aabe2009-10-21 18:09:33257 int write_position_;
258 int write_amount_;
[email protected]fd2885a2010-03-17 22:02:28259 CreationPhase creation_phase_;
[email protected]43fe9f52012-01-03 18:52:10260 net::CompletionCallback create_callback_;
261 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51262};
263
[email protected]98d6d4562014-06-25 20:57:55264} // namespace content
[email protected]e7dff7ba2009-10-09 03:25:51265
[email protected]98d6d4562014-06-25 20:57:55266#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_