blob: 47f33522475238740ab41df08b4eda977df1fd7a [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
avid0181f32015-12-10 19:41:478#include <stdint.h>
9
dcheng59716272016-04-09 05:19:0810#include <memory>
11
[email protected]073aabe2009-10-21 18:09:3312#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]e29f9332011-10-28 22:44:5714#include "base/memory/weak_ptr.h"
[email protected]98d6d4562014-06-25 20:57:5515#include "content/common/appcache_interfaces.h"
16#include "content/common/content_export.h"
[email protected]e7dff7ba2009-10-09 03:25:5117#include "net/base/completion_callback.h"
18#include "net/http/http_response_info.h"
[email protected]ad677772013-06-29 14:18:3819#include "url/gurl.h"
[email protected]e7dff7ba2009-10-09 03:25:5120
21namespace net {
22class IOBuffer;
23}
[email protected]e7dff7ba2009-10-09 03:25:5124
[email protected]71fe3d12014-04-22 00:32:3825namespace content {
[email protected]9f67dd02013-10-22 23:08:2026class AppCacheStorage;
[email protected]0234b1c2014-06-28 06:12:2827class MockAppCacheStorage;
[email protected]073aabe2009-10-21 18:09:3328
[email protected]fd2885a2010-03-17 22:02:2829static const int kUnkownResponseDataSize = -1;
30
[email protected]e7dff7ba2009-10-09 03:25:5131// Response info for a particular response id. Instances are tracked in
32// the working set.
[email protected]98d6d4562014-06-25 20:57:5533class CONTENT_EXPORT AppCacheResponseInfo
[email protected]e7dff7ba2009-10-09 03:25:5134 : public base::RefCounted<AppCacheResponseInfo> {
[email protected]d144b052009-10-13 01:45:0935 public:
[email protected]e7dff7ba2009-10-09 03:25:5136 // AppCacheResponseInfo takes ownership of the http_info.
avid0181f32015-12-10 19:41:4737 AppCacheResponseInfo(AppCacheStorage* storage,
38 const GURL& manifest_url,
39 int64_t response_id,
40 net::HttpResponseInfo* http_info,
41 int64_t response_data_size);
[email protected]e7dff7ba2009-10-09 03:25:5142
[email protected]3367fc1d2009-11-09 00:09:2143 const GURL& manifest_url() const { return manifest_url_; }
avid0181f32015-12-10 19:41:4744 int64_t response_id() const { return response_id_; }
[email protected]e7dff7ba2009-10-09 03:25:5145 const net::HttpResponseInfo* http_response_info() const {
46 return http_response_info_.get();
47 }
avid0181f32015-12-10 19:41:4748 int64_t response_data_size() const { return response_data_size_; }
[email protected]e7dff7ba2009-10-09 03:25:5149
50 private:
[email protected]122331d52009-11-05 23:40:4551 friend class base::RefCounted<AppCacheResponseInfo>;
[email protected]20f0487a2010-09-30 20:06:3052 virtual ~AppCacheResponseInfo();
[email protected]122331d52009-11-05 23:40:4553
[email protected]3367fc1d2009-11-09 00:09:2154 const GURL manifest_url_;
avid0181f32015-12-10 19:41:4755 const int64_t response_id_;
dcheng59716272016-04-09 05:19:0856 const std::unique_ptr<net::HttpResponseInfo> http_response_info_;
avid0181f32015-12-10 19:41:4757 const int64_t response_data_size_;
[email protected]9f67dd02013-10-22 23:08:2058 AppCacheStorage* storage_;
[email protected]e7dff7ba2009-10-09 03:25:5159};
60
[email protected]e7dff7ba2009-10-09 03:25:5161// A refcounted wrapper for HttpResponseInfo so we can apply the
62// refcounting semantics used with IOBuffer with these structures too.
[email protected]98d6d4562014-06-25 20:57:5563struct CONTENT_EXPORT HttpResponseInfoIOBuffer
[email protected]e7dff7ba2009-10-09 03:25:5164 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> {
dcheng59716272016-04-09 05:19:0865 std::unique_ptr<net::HttpResponseInfo> http_info;
[email protected]fd2885a2010-03-17 22:02:2866 int response_data_size;
[email protected]073aabe2009-10-21 18:09:3367
[email protected]20f0487a2010-09-30 20:06:3068 HttpResponseInfoIOBuffer();
69 explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info);
[email protected]122331d52009-11-05 23:40:4570
[email protected]8022ed52014-05-06 03:13:4071 protected:
[email protected]122331d52009-11-05 23:40:4572 friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>;
[email protected]20f0487a2010-09-30 20:06:3073 virtual ~HttpResponseInfoIOBuffer();
[email protected]073aabe2009-10-21 18:09:3374};
75
[email protected]50304162011-12-21 17:51:3076// Low level storage API used by the response reader and writer.
[email protected]98d6d4562014-06-25 20:57:5577class CONTENT_EXPORT AppCacheDiskCacheInterface {
[email protected]f5a73e072011-09-19 19:34:1778 public:
79 class Entry {
80 public:
avid0181f32015-12-10 19:41:4781 virtual int Read(int index,
82 int64_t offset,
83 net::IOBuffer* buf,
84 int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2785 const net::CompletionCallback& callback) = 0;
avid0181f32015-12-10 19:41:4786 virtual int Write(int index,
87 int64_t offset,
88 net::IOBuffer* buf,
89 int buf_len,
[email protected]2a65aceb82011-12-19 20:59:2790 const net::CompletionCallback& callback) = 0;
avid0181f32015-12-10 19:41:4791 virtual int64_t GetSize(int index) = 0;
[email protected]f5a73e072011-09-19 19:34:1792 virtual void Close() = 0;
93 protected:
94 virtual ~Entry() {}
95 };
96
nhiroki5361d342016-04-07 03:00:4197 AppCacheDiskCacheInterface();
98
avid0181f32015-12-10 19:41:4799 virtual int CreateEntry(int64_t key,
100 Entry** entry,
[email protected]50304162011-12-21 17:51:30101 const net::CompletionCallback& callback) = 0;
avid0181f32015-12-10 19:41:47102 virtual int OpenEntry(int64_t key,
103 Entry** entry,
[email protected]50304162011-12-21 17:51:30104 const net::CompletionCallback& callback) = 0;
avid0181f32015-12-10 19:41:47105 virtual int DoomEntry(int64_t key,
106 const net::CompletionCallback& callback) = 0;
[email protected]f5a73e072011-09-19 19:34:17107
nhiroki5361d342016-04-07 03:00:41108 base::WeakPtr<AppCacheDiskCacheInterface> GetWeakPtr();
109
[email protected]f5a73e072011-09-19 19:34:17110 protected:
nhiroki5361d342016-04-07 03:00:41111 virtual ~AppCacheDiskCacheInterface();
112
113 base::WeakPtrFactory<AppCacheDiskCacheInterface> weak_factory_;
[email protected]f5a73e072011-09-19 19:34:17114};
115
[email protected]073aabe2009-10-21 18:09:33116// Common base class for response reader and writer.
[email protected]98d6d4562014-06-25 20:57:55117class CONTENT_EXPORT AppCacheResponseIO {
[email protected]073aabe2009-10-21 18:09:33118 public:
119 virtual ~AppCacheResponseIO();
avid0181f32015-12-10 19:41:47120 int64_t response_id() const { return response_id_; }
[email protected]073aabe2009-10-21 18:09:33121
122 protected:
nhiroki5361d342016-04-07 03:00:41123 AppCacheResponseIO(
124 int64_t response_id,
nhiroki5361d342016-04-07 03:00:41125 const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache);
[email protected]073aabe2009-10-21 18:09:33126
127 virtual void OnIOComplete(int result) = 0;
horoa9500072015-02-18 05:08:15128 virtual void OnOpenEntryComplete() {}
[email protected]073aabe2009-10-21 18:09:33129
[email protected]50304162011-12-21 17:51:30130 bool IsIOPending() { return !callback_.is_null(); }
[email protected]72a3ce52011-12-22 05:10:24131 void ScheduleIOCompletionCallback(int result);
132 void InvokeUserCompletionCallback(int result);
[email protected]073aabe2009-10-21 18:09:33133 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
134 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len);
horoa9500072015-02-18 05:08:15135 void OpenEntryIfNeeded();
[email protected]073aabe2009-10-21 18:09:33136
avid0181f32015-12-10 19:41:47137 const int64_t response_id_;
nhiroki5361d342016-04-07 03:00:41138 base::WeakPtr<AppCacheDiskCacheInterface> disk_cache_;
[email protected]f5a73e072011-09-19 19:34:17139 AppCacheDiskCacheInterface::Entry* entry_;
[email protected]073aabe2009-10-21 18:09:33140 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
141 scoped_refptr<net::IOBuffer> buffer_;
[email protected]fd2885a2010-03-17 22:02:28142 int buffer_len_;
[email protected]50304162011-12-21 17:51:30143 net::CompletionCallback callback_;
horoa9500072015-02-18 05:08:15144 net::CompletionCallback open_callback_;
[email protected]e29f9332011-10-28 22:44:57145 base::WeakPtrFactory<AppCacheResponseIO> weak_factory_;
[email protected]073aabe2009-10-21 18:09:33146
147 private:
148 void OnRawIOComplete(int result);
horoa9500072015-02-18 05:08:15149 void OpenEntryCallback(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]e7dff7ba2009-10-09 03:25:51150};
151
152// Reads existing response data from storage. If the object is deleted
153// and there is a read in progress, the implementation will return
154// immediately but will take care of any side effect of cancelling the
155// operation. In other words, instances are safe to delete at will.
[email protected]98d6d4562014-06-25 20:57:55156class CONTENT_EXPORT AppCacheResponseReader
[email protected]e5bf2fd2013-06-13 02:59:18157 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51158 public:
dchengc2282aa2014-10-21 12:07:58159 ~AppCacheResponseReader() override;
[email protected]fd2885a2010-03-17 22:02:28160
[email protected]073aabe2009-10-21 18:09:33161 // Reads http info from storage. Always returns the result of the read
162 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51163 // or a net:: error code. Guaranteed to not perform partial reads of
[email protected]073aabe2009-10-21 18:09:33164 // the info data. The reader acquires a reference to the 'info_buf' until
165 // completion at which time the callback is invoked with either a negative
166 // error code or the number of bytes read. The 'info_buf' argument should
167 // contain a NULL http_info when ReadInfo is called. The 'callback' is a
168 // required parameter.
[email protected]e7dff7ba2009-10-09 03:25:51169 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35170 // (virtual for testing)
171 virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf,
[email protected]50304162011-12-21 17:51:30172 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51173
[email protected]073aabe2009-10-21 18:09:33174 // Reads data from storage. Always returns the result of the read
175 // asynchronously through the 'callback'. Returns the number of bytes read
[email protected]e7dff7ba2009-10-09 03:25:51176 // or a net:: error code. EOF is indicated with a return value of zero.
[email protected]073aabe2009-10-21 18:09:33177 // The reader acquires a reference to the provided 'buf' until completion
178 // at which time the callback is invoked with either a negative error code
179 // or the number of bytes read. The 'callback' is a required parameter.
[email protected]e7dff7ba2009-10-09 03:25:51180 // Should only be called where there is no Read operation in progress.
[email protected]20d06f62011-07-22 23:57:35181 // (virtual for testing)
182 virtual void ReadData(net::IOBuffer* buf, int buf_len,
[email protected]50304162011-12-21 17:51:30183 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51184
185 // Returns true if there is a read operation, for data or info, pending.
[email protected]073aabe2009-10-21 18:09:33186 bool IsReadPending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51187
188 // Used to support range requests. If not called, the reader will
189 // read the entire response body. If called, this must be called prior
190 // to the first call to the ReadData method.
[email protected]073aabe2009-10-21 18:09:33191 void SetReadRange(int offset, int length);
[email protected]e7dff7ba2009-10-09 03:25:51192
[email protected]20d06f62011-07-22 23:57:35193 protected:
[email protected]e7dff7ba2009-10-09 03:25:51194 friend class AppCacheStorageImpl;
[email protected]71fe3d12014-04-22 00:32:38195 friend class content::MockAppCacheStorage;
[email protected]e7dff7ba2009-10-09 03:25:51196
[email protected]8022ed52014-05-06 03:13:40197 // Should only be constructed by the storage class and derivatives.
nhiroki5361d342016-04-07 03:00:41198 AppCacheResponseReader(
199 int64_t response_id,
nhiroki5361d342016-04-07 03:00:41200 const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache);
[email protected]e7dff7ba2009-10-09 03:25:51201
dchengc2282aa2014-10-21 12:07:58202 void OnIOComplete(int result) override;
horoa9500072015-02-18 05:08:15203 void OnOpenEntryComplete() override;
[email protected]fd2885a2010-03-17 22:02:28204 void ContinueReadInfo();
205 void ContinueReadData();
[email protected]073aabe2009-10-21 18:09:33206
207 int range_offset_;
208 int range_length_;
209 int read_position_;
horoa9500072015-02-18 05:08:15210 int reading_metadata_size_;
[email protected]43fe9f52012-01-03 18:52:10211 base::WeakPtrFactory<AppCacheResponseReader> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51212};
213
214// Writes new response data to storage. If the object is deleted
215// and there is a write in progress, the implementation will return
216// immediately but will take care of any side effect of cancelling the
217// operation. In other words, instances are safe to delete at will.
[email protected]98d6d4562014-06-25 20:57:55218class CONTENT_EXPORT AppCacheResponseWriter
[email protected]e5bf2fd2013-06-13 02:59:18219 : public AppCacheResponseIO {
[email protected]e7dff7ba2009-10-09 03:25:51220 public:
dchengc2282aa2014-10-21 12:07:58221 ~AppCacheResponseWriter() override;
[email protected]fd2885a2010-03-17 22:02:28222
[email protected]073aabe2009-10-21 18:09:33223 // Writes the http info to storage. Always returns the result of the write
224 // asynchronously through the 'callback'. Returns the number of bytes written
225 // or a net:: error code. The writer acquires a reference to the 'info_buf'
226 // until completion at which time the callback is invoked with either a
227 // negative error code or the number of bytes written. The 'callback' is a
228 // required parameter. The contents of 'info_buf' are not modified.
[email protected]e7dff7ba2009-10-09 03:25:51229 // Should only be called where there is no Write operation in progress.
ellyjones4af0ccc2015-10-08 20:38:49230 // (virtual for testing)
231 virtual void WriteInfo(HttpResponseInfoIOBuffer* info_buf,
232 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51233
[email protected]073aabe2009-10-21 18:09:33234 // Writes data to storage. Always returns the result of the write
235 // asynchronously through the 'callback'. Returns the number of bytes written
[email protected]e7dff7ba2009-10-09 03:25:51236 // or a net:: error code. Guaranteed to not perform partial writes.
[email protected]073aabe2009-10-21 18:09:33237 // The writer acquires a reference to the provided 'buf' until completion at
238 // which time the callback is invoked with either a negative error code or
239 // the number of bytes written. The 'callback' is a required parameter.
240 // The contents of 'buf' are not modified.
[email protected]e7dff7ba2009-10-09 03:25:51241 // Should only be called where there is no Write operation in progress.
ellyjones4af0ccc2015-10-08 20:38:49242 // (virtual for testing)
243 virtual void WriteData(net::IOBuffer* buf,
244 int buf_len,
245 const net::CompletionCallback& callback);
[email protected]e7dff7ba2009-10-09 03:25:51246
247 // Returns true if there is a write pending.
[email protected]073aabe2009-10-21 18:09:33248 bool IsWritePending() { return IsIOPending(); }
[email protected]e7dff7ba2009-10-09 03:25:51249
[email protected]0dfeec32010-01-06 22:25:33250 // Returns the amount written, info and data.
avid0181f32015-12-10 19:41:47251 int64_t amount_written() { return info_size_ + write_position_; }
[email protected]0dfeec32010-01-06 22:25:33252
[email protected]8022ed52014-05-06 03:13:40253 protected:
254 // Should only be constructed by the storage class and derivatives.
nhiroki5361d342016-04-07 03:00:41255 AppCacheResponseWriter(
256 int64_t response_id,
nhiroki5361d342016-04-07 03:00:41257 const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache);
[email protected]8022ed52014-05-06 03:13:40258
[email protected]e7dff7ba2009-10-09 03:25:51259 private:
260 friend class AppCacheStorageImpl;
[email protected]71fe3d12014-04-22 00:32:38261 friend class content::MockAppCacheStorage;
[email protected]e7dff7ba2009-10-09 03:25:51262
[email protected]fd2885a2010-03-17 22:02:28263 enum CreationPhase {
264 NO_ATTEMPT,
265 INITIAL_ATTEMPT,
266 DOOM_EXISTING,
267 SECOND_ATTEMPT
268 };
269
dchengc2282aa2014-10-21 12:07:58270 void OnIOComplete(int result) override;
[email protected]fd2885a2010-03-17 22:02:28271 void ContinueWriteInfo();
272 void ContinueWriteData();
273 void CreateEntryIfNeededAndContinue();
[email protected]43fe9f52012-01-03 18:52:10274 void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv);
[email protected]073aabe2009-10-21 18:09:33275
[email protected]0dfeec32010-01-06 22:25:33276 int info_size_;
[email protected]073aabe2009-10-21 18:09:33277 int write_position_;
278 int write_amount_;
[email protected]fd2885a2010-03-17 22:02:28279 CreationPhase creation_phase_;
[email protected]43fe9f52012-01-03 18:52:10280 net::CompletionCallback create_callback_;
281 base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_;
[email protected]e7dff7ba2009-10-09 03:25:51282};
283
horoa9500072015-02-18 05:08:15284// Writes metadata of the existing response to storage. If the object is deleted
285// and there is a write in progress, the implementation will return
286// immediately but will take care of any side effect of cancelling the
287// operation. In other words, instances are safe to delete at will.
288class CONTENT_EXPORT AppCacheResponseMetadataWriter
289 : public AppCacheResponseIO {
290 public:
291 ~AppCacheResponseMetadataWriter() override;
292
293 // Writes metadata to storage. Always returns the result of the write
294 // asynchronously through the 'callback'. Returns the number of bytes written
295 // or a net:: error code. Guaranteed to not perform partial writes.
296 // The writer acquires a reference to the provided 'buf' until completion at
297 // which time the callback is invoked with either a negative error code or
298 // the number of bytes written. The 'callback' is a required parameter.
299 // The contents of 'buf' are not modified.
300 // Should only be called where there is no WriteMetadata operation in
301 // progress.
302 void WriteMetadata(net::IOBuffer* buf,
303 int buf_len,
304 const net::CompletionCallback& callback);
305
306 // Returns true if there is a write pending.
307 bool IsWritePending() { return IsIOPending(); }
308
309 protected:
310 friend class AppCacheStorageImpl;
311 friend class content::MockAppCacheStorage;
nhiroki5361d342016-04-07 03:00:41312
horoa9500072015-02-18 05:08:15313 // Should only be constructed by the storage class and derivatives.
nhiroki5361d342016-04-07 03:00:41314 AppCacheResponseMetadataWriter(
315 int64_t response_id,
nhiroki5361d342016-04-07 03:00:41316 const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache);
horoa9500072015-02-18 05:08:15317
318 private:
319 void OnIOComplete(int result) override;
320 void OnOpenEntryComplete() override;
321
322 int write_amount_;
323 base::WeakPtrFactory<AppCacheResponseMetadataWriter> weak_factory_;
324};
325
[email protected]98d6d4562014-06-25 20:57:55326} // namespace content
[email protected]e7dff7ba2009-10-09 03:25:51327
[email protected]98d6d4562014-06-25 20:57:55328#endif // CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_