| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ | 5 #ifndef WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ |
| 6 #define WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ | 6 #define WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "base/scoped_ptr.h" |
| 10 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
| 11 #include "net/http/http_response_info.h" | 12 #include "net/http/http_response_info.h" |
| 12 #include "webkit/appcache/appcache_interfaces.h" | 13 #include "webkit/appcache/appcache_interfaces.h" |
| 13 #include "webkit/appcache/appcache_service.h" | |
| 14 #include "webkit/appcache/appcache_storage.h" | |
| 15 | 14 |
| 16 namespace net { | 15 namespace net { |
| 17 class IOBuffer; | 16 class IOBuffer; |
| 18 } | 17 } |
| 19 namespace disk_cache { | 18 namespace disk_cache { |
| 19 class Entry; |
| 20 class Backend; | 20 class Backend; |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 namespace appcache { | 23 namespace appcache { |
| 24 | 24 |
| 25 class AppCacheService; |
| 26 |
| 25 // Response info for a particular response id. Instances are tracked in | 27 // Response info for a particular response id. Instances are tracked in |
| 26 // the working set. | 28 // the working set. |
| 27 class AppCacheResponseInfo | 29 class AppCacheResponseInfo |
| 28 : public base::RefCounted<AppCacheResponseInfo> { | 30 : public base::RefCounted<AppCacheResponseInfo> { |
| 29 public: | 31 public: |
| 30 // AppCacheResponseInfo takes ownership of the http_info. | 32 // AppCacheResponseInfo takes ownership of the http_info. |
| 31 AppCacheResponseInfo(AppCacheService* service, int64 response_id, | 33 AppCacheResponseInfo(AppCacheService* service, int64 response_id, |
| 32 net::HttpResponseInfo* http_info) | 34 net::HttpResponseInfo* http_info); |
| 33 : response_id_(response_id), http_response_info_(http_info), | 35 ~AppCacheResponseInfo(); |
| 34 service_(service) { | 36 // TODO(michaeln): should the ctor/dtor be hidden from public view? |
| 35 DCHECK(http_info); | |
| 36 DCHECK(response_id != kNoResponseId); | |
| 37 service_->storage()->working_set()->AddResponseInfo(this); | |
| 38 } | |
| 39 | |
| 40 ~AppCacheResponseInfo() { | |
| 41 service_->storage()->working_set()->RemoveResponseInfo(this); | |
| 42 } | |
| 43 | 37 |
| 44 int64 response_id() const { return response_id_; } | 38 int64 response_id() const { return response_id_; } |
| 45 | 39 |
| 46 const net::HttpResponseInfo* http_response_info() const { | 40 const net::HttpResponseInfo* http_response_info() const { |
| 47 return http_response_info_.get(); | 41 return http_response_info_.get(); |
| 48 } | 42 } |
| 49 | 43 |
| 50 private: | 44 private: |
| 51 const int64 response_id_; | 45 const int64 response_id_; |
| 52 const scoped_ptr<net::HttpResponseInfo> http_response_info_; | 46 const scoped_ptr<net::HttpResponseInfo> http_response_info_; |
| 53 const AppCacheService* service_; | 47 const AppCacheService* service_; |
| 54 }; | 48 }; |
| 55 | 49 |
| 56 // Common base class for response reader and writer. | |
| 57 class AppCacheResponseIO { | |
| 58 public: | |
| 59 virtual ~AppCacheResponseIO() {} | |
| 60 int64 response_id() const { return response_id_; } | |
| 61 protected: | |
| 62 explicit AppCacheResponseIO( | |
| 63 int64 response_id, disk_cache::Backend* disk_cache) | |
| 64 : response_id_(response_id), disk_cache_(disk_cache) {} | |
| 65 const int64 response_id_; | |
| 66 disk_cache::Backend* disk_cache_; | |
| 67 }; | |
| 68 | |
| 69 // A refcounted wrapper for HttpResponseInfo so we can apply the | 50 // A refcounted wrapper for HttpResponseInfo so we can apply the |
| 70 // refcounting semantics used with IOBuffer with these structures too. | 51 // refcounting semantics used with IOBuffer with these structures too. |
| 71 struct HttpResponseInfoIOBuffer | 52 struct HttpResponseInfoIOBuffer |
| 72 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> { | 53 : public base::RefCountedThreadSafe<HttpResponseInfoIOBuffer> { |
| 73 scoped_ptr<net::HttpResponseInfo> http_info; | 54 scoped_ptr<net::HttpResponseInfo> http_info; |
| 55 |
| 56 HttpResponseInfoIOBuffer() {} |
| 57 HttpResponseInfoIOBuffer(net::HttpResponseInfo* info) : http_info(info) {} |
| 58 }; |
| 59 |
| 60 // Common base class for response reader and writer. |
| 61 class AppCacheResponseIO { |
| 62 public: |
| 63 virtual ~AppCacheResponseIO(); |
| 64 int64 response_id() const { return response_id_; } |
| 65 |
| 66 protected: |
| 67 friend class ScopedRunnableMethodFactory<AppCacheResponseIO>; |
| 68 |
| 69 AppCacheResponseIO(int64 response_id, disk_cache::Backend* disk_cache); |
| 70 |
| 71 virtual void OnIOComplete(int result) = 0; |
| 72 |
| 73 bool IsIOPending() { return user_callback_ ? true : false; } |
| 74 void ScheduleIOCompletionCallback(int result); |
| 75 void InvokeUserCompletionCallback(int result); |
| 76 void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len); |
| 77 void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len); |
| 78 |
| 79 const int64 response_id_; |
| 80 disk_cache::Backend* disk_cache_; |
| 81 disk_cache::Entry* entry_; |
| 82 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_; |
| 83 scoped_refptr<net::IOBuffer> buffer_; |
| 84 net::CompletionCallback* user_callback_; |
| 85 |
| 86 private: |
| 87 void OnRawIOComplete(int result); |
| 88 |
| 89 ScopedRunnableMethodFactory<AppCacheResponseIO> method_factory_; |
| 90 scoped_refptr<net::CancelableCompletionCallback<AppCacheResponseIO> > |
| 91 raw_callback_; |
| 74 }; | 92 }; |
| 75 | 93 |
| 76 // Reads existing response data from storage. If the object is deleted | 94 // Reads existing response data from storage. If the object is deleted |
| 77 // and there is a read in progress, the implementation will return | 95 // and there is a read in progress, the implementation will return |
| 78 // immediately but will take care of any side effect of cancelling the | 96 // immediately but will take care of any side effect of cancelling the |
| 79 // operation. In other words, instances are safe to delete at will. | 97 // operation. In other words, instances are safe to delete at will. |
| 80 class AppCacheResponseReader : public AppCacheResponseIO { | 98 class AppCacheResponseReader : public AppCacheResponseIO { |
| 81 public: | 99 public: |
| 82 // Reads http info from storage. Returns the number of bytes read | 100 // Reads http info from storage. Always returns the result of the read |
| 101 // asynchronously through the 'callback'. Returns the number of bytes read |
| 83 // or a net:: error code. Guaranteed to not perform partial reads of | 102 // or a net:: error code. Guaranteed to not perform partial reads of |
| 84 // the info data. ERR_IO_PENDING is returned if the | 103 // the info data. The reader acquires a reference to the 'info_buf' until |
| 85 // operation could not be completed synchronously, in which case the reader | 104 // completion at which time the callback is invoked with either a negative |
| 86 // acquires a reference to the provided 'info_buf' until completion at which | 105 // error code or the number of bytes read. The 'info_buf' argument should |
| 87 // time the callback is invoked with either a negative error code or the | 106 // contain a NULL http_info when ReadInfo is called. The 'callback' is a |
| 88 // number of bytes written. The 'info_buf' argument should contain a NULL | 107 // required parameter. |
| 89 // http_info when ReadInfo is called. The 'callback' is a required parameter. | |
| 90 // Should only be called where there is no Read operation in progress. | 108 // Should only be called where there is no Read operation in progress. |
| 91 int ReadInfo(HttpResponseInfoIOBuffer* info_buf, | 109 void ReadInfo(HttpResponseInfoIOBuffer* info_buf, |
| 92 net::CompletionCallback* callback) { | 110 net::CompletionCallback* callback); |
| 93 DCHECK(info_buf && !info_buf->http_info.get()); | |
| 94 return -2; | |
| 95 } | |
| 96 | 111 |
| 97 // Reads data from storage. Returns the number of bytes read | 112 // Reads data from storage. Always returns the result of the read |
| 113 // asynchronously through the 'callback'. Returns the number of bytes read |
| 98 // or a net:: error code. EOF is indicated with a return value of zero. | 114 // or a net:: error code. EOF is indicated with a return value of zero. |
| 99 // ERR_IO_PENDING is returned if the operation could not be completed | 115 // The reader acquires a reference to the provided 'buf' until completion |
| 100 // synchronously, in which case the reader acquires a reference to the | 116 // at which time the callback is invoked with either a negative error code |
| 101 // provided 'buf' until completion at which time the callback is invoked | 117 // or the number of bytes read. The 'callback' is a required parameter. |
| 102 // with either a negative error code or the number of bytes read. The | |
| 103 // 'callback' is a required parameter. | |
| 104 // Should only be called where there is no Read operation in progress. | 118 // Should only be called where there is no Read operation in progress. |
| 105 int ReadData(net::IOBuffer* buf, int buf_len, | 119 void ReadData(net::IOBuffer* buf, int buf_len, |
| 106 net::CompletionCallback* callback) { return -2; } | 120 net::CompletionCallback* callback); |
| 107 | 121 |
| 108 // Returns true if there is a read operation, for data or info, pending. | 122 // Returns true if there is a read operation, for data or info, pending. |
| 109 bool IsReadPending() { return false; } | 123 bool IsReadPending() { return IsIOPending(); } |
| 110 | 124 |
| 111 // Used to support range requests. If not called, the reader will | 125 // Used to support range requests. If not called, the reader will |
| 112 // read the entire response body. If called, this must be called prior | 126 // read the entire response body. If called, this must be called prior |
| 113 // to the first call to the ReadData method. | 127 // to the first call to the ReadData method. |
| 114 void SetReadRange(int64 offset, int64 length) { | 128 void SetReadRange(int offset, int length); |
| 115 range_offset_ = offset; | |
| 116 range_length_ = length; | |
| 117 } | |
| 118 | 129 |
| 119 private: | 130 private: |
| 120 friend class AppCacheStorageImpl; | 131 friend class AppCacheStorageImpl; |
| 121 friend class MockAppCacheStorage; | 132 friend class MockAppCacheStorage; |
| 122 | 133 |
| 123 // Should only be constructed by the storage class. | 134 // Should only be constructed by the storage class. |
| 124 explicit AppCacheResponseReader( | 135 AppCacheResponseReader(int64 response_id, disk_cache::Backend* disk_cache) |
| 125 int64 response_id, disk_cache::Backend* disk_cache) | |
| 126 : AppCacheResponseIO(response_id, disk_cache), | 136 : AppCacheResponseIO(response_id, disk_cache), |
| 127 range_offset_(0), range_length_(kint64max) {} | 137 range_offset_(0), range_length_(kint32max), |
| 138 read_position_(0) {} |
| 128 | 139 |
| 129 int64 range_offset_; | 140 virtual void OnIOComplete(int result); |
| 130 int64 range_length_; | 141 bool OpenEntryIfNeeded(); |
| 142 |
| 143 int range_offset_; |
| 144 int range_length_; |
| 145 int read_position_; |
| 131 }; | 146 }; |
| 132 | 147 |
| 133 // Writes new response data to storage. If the object is deleted | 148 // Writes new response data to storage. If the object is deleted |
| 134 // and there is a write in progress, the implementation will return | 149 // and there is a write in progress, the implementation will return |
| 135 // immediately but will take care of any side effect of cancelling the | 150 // immediately but will take care of any side effect of cancelling the |
| 136 // operation. In other words, instances are safe to delete at will. | 151 // operation. In other words, instances are safe to delete at will. |
| 137 class AppCacheResponseWriter : public AppCacheResponseIO { | 152 class AppCacheResponseWriter : public AppCacheResponseIO { |
| 138 public: | 153 public: |
| 139 // Writes the http info to storage. Returns the number of bytes written | 154 // Writes the http info to storage. Always returns the result of the write |
| 140 // or a net:: error code. ERR_IO_PENDING is returned if the | 155 // asynchronously through the 'callback'. Returns the number of bytes written |
| 141 // operation could not be completed synchronously, in which case the writer | 156 // or a net:: error code. The writer acquires a reference to the 'info_buf' |
| 142 // acquires a reference to the provided 'info_buf' until completion at which | 157 // until completion at which time the callback is invoked with either a |
| 143 // time the callback is invoked with either a negative error code or the | 158 // negative error code or the number of bytes written. The 'callback' is a |
| 144 // number of bytes written. The 'callback' is a required parameter. The | 159 // required parameter. The contents of 'info_buf' are not modified. |
| 145 // contents of 'info_buf' are not modified. | |
| 146 // Should only be called where there is no Write operation in progress. | 160 // Should only be called where there is no Write operation in progress. |
| 147 int WriteInfo(HttpResponseInfoIOBuffer* info_buf, | 161 void WriteInfo(HttpResponseInfoIOBuffer* info_buf, |
| 148 net::CompletionCallback* callback) { | 162 net::CompletionCallback* callback); |
| 149 DCHECK(info_buf && info_buf->http_info.get()); | |
| 150 return -2; | |
| 151 } | |
| 152 | 163 |
| 153 // Writes data to storage. Returns the number of bytes written | 164 // Writes data to storage. Always returns the result of the write |
| 165 // asynchronously through the 'callback'. Returns the number of bytes written |
| 154 // or a net:: error code. Guaranteed to not perform partial writes. | 166 // or a net:: error code. Guaranteed to not perform partial writes. |
| 155 // ERR_IO_PENDING is returned if the operation could not be completed | 167 // The writer acquires a reference to the provided 'buf' until completion at |
| 156 // synchronously, in which case the writer acquires a reference to the | 168 // which time the callback is invoked with either a negative error code or |
| 157 // provided 'buf' until completion at which time the callback is invoked | 169 // the number of bytes written. The 'callback' is a required parameter. |
| 158 // with either a negative error code or the number of bytes written. The | 170 // The contentsof 'buf' are not modified. |
| 159 // 'callback' is a required parameter. The contents of 'buf' are not | |
| 160 // modified. | |
| 161 // Should only be called where there is no Write operation in progress. | 171 // Should only be called where there is no Write operation in progress. |
| 162 int WriteData(net::IOBuffer* buf, int buf_len, | 172 void WriteData(net::IOBuffer* buf, int buf_len, |
| 163 net::CompletionCallback* callback) { return -2; } | 173 net::CompletionCallback* callback); |
| 164 | 174 |
| 165 // Returns true if there is a write pending. | 175 // Returns true if there is a write pending. |
| 166 bool IsWritePending() { return false; } | 176 bool IsWritePending() { return IsIOPending(); } |
| 167 | 177 |
| 168 private: | 178 private: |
| 169 friend class AppCacheStorageImpl; | 179 friend class AppCacheStorageImpl; |
| 170 friend class MockAppCacheStorage; | 180 friend class MockAppCacheStorage; |
| 171 | 181 |
| 172 // Should only be constructed by the storage class. | 182 // Should only be constructed by the storage class. |
| 173 explicit AppCacheResponseWriter( | 183 AppCacheResponseWriter(int64 response_id, disk_cache::Backend* disk_cache) |
| 174 int64 response_id, disk_cache::Backend* disk_cache) | 184 : AppCacheResponseIO(response_id, disk_cache), |
| 175 : AppCacheResponseIO(response_id, disk_cache) {} | 185 write_position_(0), write_amount_(0) {} |
| 186 |
| 187 virtual void OnIOComplete(int result); |
| 188 bool CreateEntryIfNeeded(); |
| 189 |
| 190 int write_position_; |
| 191 int write_amount_; |
| 176 }; | 192 }; |
| 177 | 193 |
| 178 } // namespace appcache | 194 } // namespace appcache |
| 179 | 195 |
| 180 #endif // WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ | 196 #endif // WEBKIT_APPCACHE_APPCACHE_RESPONSE_H_ |
| 181 | 197 |
| OLD | NEW |