[email protected] | 43fe9f5 | 2012-01-03 18:52:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_ |
| 6 | #define CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_ |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 7 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | e29f933 | 2011-10-28 22:44:57 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 15 | #include "content/common/appcache_interfaces.h" |
| 16 | #include "content/common/content_export.h" |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 17 | #include "net/base/completion_callback.h" |
| 18 | #include "net/http/http_response_info.h" |
[email protected] | ad67777 | 2013-06-29 14:18:38 | [diff] [blame] | 19 | #include "url/gurl.h" |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | class IOBuffer; |
| 23 | } |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 24 | |
[email protected] | 71fe3d1 | 2014-04-22 00:32:38 | [diff] [blame] | 25 | namespace content { |
[email protected] | 9f67dd0 | 2013-10-22 23:08:20 | [diff] [blame] | 26 | class AppCacheStorage; |
[email protected] | 0234b1c | 2014-06-28 06:12:28 | [diff] [blame] | 27 | class MockAppCacheStorage; |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 28 | |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 29 | static const int kUnkownResponseDataSize = -1; |
| 30 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 31 | // Response info for a particular response id. Instances are tracked in |
| 32 | // the working set. |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 33 | class CONTENT_EXPORT AppCacheResponseInfo |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 34 | : public base::RefCounted<AppCacheResponseInfo> { |
[email protected] | d144b05 | 2009-10-13 01:45:09 | [diff] [blame] | 35 | public: |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 36 | // AppCacheResponseInfo takes ownership of the http_info. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 37 | 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 42 | |
[email protected] | 3367fc1d | 2009-11-09 00:09:21 | [diff] [blame] | 43 | const GURL& manifest_url() const { return manifest_url_; } |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 44 | int64_t response_id() const { return response_id_; } |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 45 | const net::HttpResponseInfo* http_response_info() const { |
| 46 | return http_response_info_.get(); |
| 47 | } |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 48 | int64_t response_data_size() const { return response_data_size_; } |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 49 | |
| 50 | private: |
[email protected] | 122331d5 | 2009-11-05 23:40:45 | [diff] [blame] | 51 | friend class base::RefCounted<AppCacheResponseInfo>; |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 52 | virtual ~AppCacheResponseInfo(); |
[email protected] | 122331d5 | 2009-11-05 23:40:45 | [diff] [blame] | 53 | |
[email protected] | 3367fc1d | 2009-11-09 00:09:21 | [diff] [blame] | 54 | const GURL manifest_url_; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 55 | const int64_t response_id_; |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 56 | const std::unique_ptr<net::HttpResponseInfo> http_response_info_; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 57 | const int64_t response_data_size_; |
[email protected] | 9f67dd0 | 2013-10-22 23:08:20 | [diff] [blame] | 58 | AppCacheStorage* storage_; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 59 | }; |
| 60 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 61 | // A refcounted wrapper for HttpResponseInfo so we can apply the |
| 62 | // refcounting semantics used with IOBuffer with these structures too. |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 63 | struct CONTENT_EXPORT HttpResponseInfoIOBuffer |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 64 | : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 65 | std::unique_ptr<net::HttpResponseInfo> http_info; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 66 | int response_data_size; |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 67 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 68 | HttpResponseInfoIOBuffer(); |
| 69 | explicit HttpResponseInfoIOBuffer(net::HttpResponseInfo* info); |
[email protected] | 122331d5 | 2009-11-05 23:40:45 | [diff] [blame] | 70 | |
[email protected] | 8022ed5 | 2014-05-06 03:13:40 | [diff] [blame] | 71 | protected: |
[email protected] | 122331d5 | 2009-11-05 23:40:45 | [diff] [blame] | 72 | friend class base::RefCountedThreadSafe<HttpResponseInfoIOBuffer>; |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 73 | virtual ~HttpResponseInfoIOBuffer(); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 74 | }; |
| 75 | |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 76 | // Low level storage API used by the response reader and writer. |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 77 | class CONTENT_EXPORT AppCacheDiskCacheInterface { |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 78 | public: |
| 79 | class Entry { |
| 80 | public: |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 81 | virtual int Read(int index, |
| 82 | int64_t offset, |
| 83 | net::IOBuffer* buf, |
| 84 | int buf_len, |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 85 | const net::CompletionCallback& callback) = 0; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 86 | virtual int Write(int index, |
| 87 | int64_t offset, |
| 88 | net::IOBuffer* buf, |
| 89 | int buf_len, |
[email protected] | 2a65aceb8 | 2011-12-19 20:59:27 | [diff] [blame] | 90 | const net::CompletionCallback& callback) = 0; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 91 | virtual int64_t GetSize(int index) = 0; |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 92 | virtual void Close() = 0; |
| 93 | protected: |
| 94 | virtual ~Entry() {} |
| 95 | }; |
| 96 | |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 97 | AppCacheDiskCacheInterface(); |
| 98 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 99 | virtual int CreateEntry(int64_t key, |
| 100 | Entry** entry, |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 101 | const net::CompletionCallback& callback) = 0; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 102 | virtual int OpenEntry(int64_t key, |
| 103 | Entry** entry, |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 104 | const net::CompletionCallback& callback) = 0; |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 105 | virtual int DoomEntry(int64_t key, |
| 106 | const net::CompletionCallback& callback) = 0; |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 107 | |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 108 | base::WeakPtr<AppCacheDiskCacheInterface> GetWeakPtr(); |
| 109 | |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 110 | protected: |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 111 | virtual ~AppCacheDiskCacheInterface(); |
| 112 | |
| 113 | base::WeakPtrFactory<AppCacheDiskCacheInterface> weak_factory_; |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 114 | }; |
| 115 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 116 | // Common base class for response reader and writer. |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 117 | class CONTENT_EXPORT AppCacheResponseIO { |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 118 | public: |
| 119 | virtual ~AppCacheResponseIO(); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 120 | int64_t response_id() const { return response_id_; } |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 121 | |
| 122 | protected: |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 123 | AppCacheResponseIO( |
| 124 | int64_t response_id, |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 125 | const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 126 | |
| 127 | virtual void OnIOComplete(int result) = 0; |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 128 | virtual void OnOpenEntryComplete() {} |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 129 | |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 130 | bool IsIOPending() { return !callback_.is_null(); } |
[email protected] | 72a3ce5 | 2011-12-22 05:10:24 | [diff] [blame] | 131 | void ScheduleIOCompletionCallback(int result); |
| 132 | void InvokeUserCompletionCallback(int result); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 133 | 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); |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 135 | void OpenEntryIfNeeded(); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 136 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 137 | const int64_t response_id_; |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 138 | base::WeakPtr<AppCacheDiskCacheInterface> disk_cache_; |
[email protected] | f5a73e07 | 2011-09-19 19:34:17 | [diff] [blame] | 139 | AppCacheDiskCacheInterface::Entry* entry_; |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 140 | scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_; |
| 141 | scoped_refptr<net::IOBuffer> buffer_; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 142 | int buffer_len_; |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 143 | net::CompletionCallback callback_; |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 144 | net::CompletionCallback open_callback_; |
[email protected] | e29f933 | 2011-10-28 22:44:57 | [diff] [blame] | 145 | base::WeakPtrFactory<AppCacheResponseIO> weak_factory_; |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | void OnRawIOComplete(int result); |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 149 | void OpenEntryCallback(AppCacheDiskCacheInterface::Entry** entry, int rv); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 150 | }; |
| 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] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 156 | class CONTENT_EXPORT AppCacheResponseReader |
[email protected] | e5bf2fd | 2013-06-13 02:59:18 | [diff] [blame] | 157 | : public AppCacheResponseIO { |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 158 | public: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 159 | ~AppCacheResponseReader() override; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 160 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 161 | // 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 163 | // or a net:: error code. Guaranteed to not perform partial reads of |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 164 | // 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 169 | // Should only be called where there is no Read operation in progress. |
[email protected] | 20d06f6 | 2011-07-22 23:57:35 | [diff] [blame] | 170 | // (virtual for testing) |
| 171 | virtual void ReadInfo(HttpResponseInfoIOBuffer* info_buf, |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 172 | const net::CompletionCallback& callback); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 173 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 174 | // Reads data from storage. Always returns the result of the read |
| 175 | // asynchronously through the 'callback'. Returns the number of bytes read |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 176 | // or a net:: error code. EOF is indicated with a return value of zero. |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 177 | // 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 180 | // Should only be called where there is no Read operation in progress. |
[email protected] | 20d06f6 | 2011-07-22 23:57:35 | [diff] [blame] | 181 | // (virtual for testing) |
| 182 | virtual void ReadData(net::IOBuffer* buf, int buf_len, |
[email protected] | 5030416 | 2011-12-21 17:51:30 | [diff] [blame] | 183 | const net::CompletionCallback& callback); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 184 | |
| 185 | // Returns true if there is a read operation, for data or info, pending. |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 186 | bool IsReadPending() { return IsIOPending(); } |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 187 | |
| 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] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 191 | void SetReadRange(int offset, int length); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 192 | |
[email protected] | 20d06f6 | 2011-07-22 23:57:35 | [diff] [blame] | 193 | protected: |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 194 | friend class AppCacheStorageImpl; |
[email protected] | 71fe3d1 | 2014-04-22 00:32:38 | [diff] [blame] | 195 | friend class content::MockAppCacheStorage; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 196 | |
[email protected] | 8022ed5 | 2014-05-06 03:13:40 | [diff] [blame] | 197 | // Should only be constructed by the storage class and derivatives. |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 198 | AppCacheResponseReader( |
| 199 | int64_t response_id, |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 200 | const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 201 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 202 | void OnIOComplete(int result) override; |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 203 | void OnOpenEntryComplete() override; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 204 | void ContinueReadInfo(); |
| 205 | void ContinueReadData(); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 206 | |
| 207 | int range_offset_; |
| 208 | int range_length_; |
| 209 | int read_position_; |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 210 | int reading_metadata_size_; |
[email protected] | 43fe9f5 | 2012-01-03 18:52:10 | [diff] [blame] | 211 | base::WeakPtrFactory<AppCacheResponseReader> weak_factory_; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 212 | }; |
| 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] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 218 | class CONTENT_EXPORT AppCacheResponseWriter |
[email protected] | e5bf2fd | 2013-06-13 02:59:18 | [diff] [blame] | 219 | : public AppCacheResponseIO { |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 220 | public: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 221 | ~AppCacheResponseWriter() override; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 222 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 223 | // 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 229 | // Should only be called where there is no Write operation in progress. |
ellyjones | 4af0ccc | 2015-10-08 20:38:49 | [diff] [blame] | 230 | // (virtual for testing) |
| 231 | virtual void WriteInfo(HttpResponseInfoIOBuffer* info_buf, |
| 232 | const net::CompletionCallback& callback); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 233 | |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 234 | // Writes data to storage. Always returns the result of the write |
| 235 | // asynchronously through the 'callback'. Returns the number of bytes written |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 236 | // or a net:: error code. Guaranteed to not perform partial writes. |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 237 | // 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] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 241 | // Should only be called where there is no Write operation in progress. |
ellyjones | 4af0ccc | 2015-10-08 20:38:49 | [diff] [blame] | 242 | // (virtual for testing) |
| 243 | virtual void WriteData(net::IOBuffer* buf, |
| 244 | int buf_len, |
| 245 | const net::CompletionCallback& callback); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 246 | |
| 247 | // Returns true if there is a write pending. |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 248 | bool IsWritePending() { return IsIOPending(); } |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 249 | |
[email protected] | 0dfeec3 | 2010-01-06 22:25:33 | [diff] [blame] | 250 | // Returns the amount written, info and data. |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 251 | int64_t amount_written() { return info_size_ + write_position_; } |
[email protected] | 0dfeec3 | 2010-01-06 22:25:33 | [diff] [blame] | 252 | |
[email protected] | 8022ed5 | 2014-05-06 03:13:40 | [diff] [blame] | 253 | protected: |
| 254 | // Should only be constructed by the storage class and derivatives. |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 255 | AppCacheResponseWriter( |
| 256 | int64_t response_id, |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 257 | const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache); |
[email protected] | 8022ed5 | 2014-05-06 03:13:40 | [diff] [blame] | 258 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 259 | private: |
| 260 | friend class AppCacheStorageImpl; |
[email protected] | 71fe3d1 | 2014-04-22 00:32:38 | [diff] [blame] | 261 | friend class content::MockAppCacheStorage; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 262 | |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 263 | enum CreationPhase { |
| 264 | NO_ATTEMPT, |
| 265 | INITIAL_ATTEMPT, |
| 266 | DOOM_EXISTING, |
| 267 | SECOND_ATTEMPT |
| 268 | }; |
| 269 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 270 | void OnIOComplete(int result) override; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 271 | void ContinueWriteInfo(); |
| 272 | void ContinueWriteData(); |
| 273 | void CreateEntryIfNeededAndContinue(); |
[email protected] | 43fe9f5 | 2012-01-03 18:52:10 | [diff] [blame] | 274 | void OnCreateEntryComplete(AppCacheDiskCacheInterface::Entry** entry, int rv); |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 275 | |
[email protected] | 0dfeec3 | 2010-01-06 22:25:33 | [diff] [blame] | 276 | int info_size_; |
[email protected] | 073aabe | 2009-10-21 18:09:33 | [diff] [blame] | 277 | int write_position_; |
| 278 | int write_amount_; |
[email protected] | fd2885a | 2010-03-17 22:02:28 | [diff] [blame] | 279 | CreationPhase creation_phase_; |
[email protected] | 43fe9f5 | 2012-01-03 18:52:10 | [diff] [blame] | 280 | net::CompletionCallback create_callback_; |
| 281 | base::WeakPtrFactory<AppCacheResponseWriter> weak_factory_; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 282 | }; |
| 283 | |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 284 | // 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. |
| 288 | class 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; |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 312 | |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 313 | // Should only be constructed by the storage class and derivatives. |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 314 | AppCacheResponseMetadataWriter( |
| 315 | int64_t response_id, |
nhiroki | 5361d34 | 2016-04-07 03:00:41 | [diff] [blame] | 316 | const base::WeakPtr<AppCacheDiskCacheInterface>& disk_cache); |
horo | a950007 | 2015-02-18 05:08:15 | [diff] [blame] | 317 | |
| 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] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 326 | } // namespace content |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 327 | |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 328 | #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_RESPONSE_H_ |