1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/appcache/appcache_response.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/pickle.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/base/io_buffer.h"
#include "net/disk_cache/disk_cache.h"
#include "webkit/appcache/appcache_service.h"
using disk_cache::Entry;
namespace appcache {
namespace {
// Disk cache entry data indices.
enum {
kResponseInfoIndex,
kResponseContentIndex
};
// Disk cache entry keys.
std::string response_key(int64 response_id) {
return Int64ToString(response_id);
}
// An IOBuffer that wraps a pickle's data. Ownership of the
// pickle is transfered to the WrappedPickleIOBuffer object.
class WrappedPickleIOBuffer : public net::WrappedIOBuffer {
public:
explicit WrappedPickleIOBuffer(const Pickle* pickle) :
net::WrappedIOBuffer(reinterpret_cast<const char*>(pickle->data())),
pickle_(pickle) {
DCHECK(pickle->data());
}
private:
~WrappedPickleIOBuffer() {}
scoped_ptr<const Pickle> pickle_;
};
} // anon namespace
// AppCacheResponseInfo ----------------------------------------------
AppCacheResponseInfo::AppCacheResponseInfo(
AppCacheService* service, const GURL& manifest_url,
int64 response_id, net::HttpResponseInfo* http_info)
: manifest_url_(manifest_url), response_id_(response_id),
http_response_info_(http_info), service_(service) {
DCHECK(http_info);
DCHECK(response_id != kNoResponseId);
service_->storage()->working_set()->AddResponseInfo(this);
}
AppCacheResponseInfo::~AppCacheResponseInfo() {
service_->storage()->working_set()->RemoveResponseInfo(this);
}
// AppCacheResponseIO ----------------------------------------------
AppCacheResponseIO::AppCacheResponseIO(
int64 response_id, disk_cache::Backend* disk_cache)
: response_id_(response_id), disk_cache_(disk_cache),
entry_(NULL), user_callback_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(raw_callback_(
new net::CancelableCompletionCallback<AppCacheResponseIO>(
this, &AppCacheResponseIO::OnRawIOComplete))) {
}
AppCacheResponseIO::~AppCacheResponseIO() {
raw_callback_->Cancel();
if (entry_)
entry_->Close();
}
void AppCacheResponseIO::ScheduleIOCompletionCallback(int result) {
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&AppCacheResponseIO::OnIOComplete, result));
}
void AppCacheResponseIO::InvokeUserCompletionCallback(int result) {
// Clear the user callback and buffers prior to invoking the callback
// so the caller can schedule additional operations in the callback.
buffer_ = NULL;
info_buffer_ = NULL;
net::CompletionCallback* temp_user_callback = user_callback_;
user_callback_ = NULL;
temp_user_callback->Run(result);
}
void AppCacheResponseIO::ReadRaw(int index, int offset,
net::IOBuffer* buf, int buf_len) {
DCHECK(entry_);
raw_callback_->AddRef(); // Balanced in OnRawIOComplete.
int rv = entry_->ReadData(index, offset, buf, buf_len, raw_callback_);
if (rv != net::ERR_IO_PENDING) {
raw_callback_->Release();
ScheduleIOCompletionCallback(rv);
}
}
void AppCacheResponseIO::WriteRaw(int index, int offset,
net::IOBuffer* buf, int buf_len) {
DCHECK(entry_);
const bool kTruncate = true;
raw_callback_->AddRef(); // Balanced in OnRawIOComplete.
int rv = entry_->WriteData(index, offset, buf, buf_len, raw_callback_,
kTruncate);
if (rv != net::ERR_IO_PENDING) {
raw_callback_->Release();
ScheduleIOCompletionCallback(rv);
}
}
void AppCacheResponseIO::OnRawIOComplete(int result) {
raw_callback_->Release(); // Balance the AddRefs
OnIOComplete(result);
}
// AppCacheResponseReader ----------------------------------------------
void AppCacheResponseReader::ReadInfo(HttpResponseInfoIOBuffer* info_buf,
net::CompletionCallback* callback) {
DCHECK(callback && !IsReadPending());
DCHECK(info_buf && !info_buf->http_info.get());
DCHECK(!buffer_.get() && !info_buffer_.get());
user_callback_ = callback; // cleared on completion
if (!OpenEntryIfNeeded()) {
ScheduleIOCompletionCallback(net::ERR_CACHE_MISS);
return;
}
int size = entry_->GetDataSize(kResponseInfoIndex);
if (size <= 0) {
ScheduleIOCompletionCallback(net::ERR_CACHE_MISS);
return;
}
info_buffer_ = info_buf;
buffer_ = new net::IOBuffer(size);
ReadRaw(kResponseInfoIndex, 0, buffer_.get(), size);
}
void AppCacheResponseReader::ReadData(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(callback && !IsReadPending());
DCHECK(buf && (buf_len >= 0));
DCHECK(!buffer_.get() && !info_buffer_.get());
user_callback_ = callback; // cleared on completion
if (!OpenEntryIfNeeded()) {
ScheduleIOCompletionCallback(net::ERR_CACHE_MISS);
return;
}
buffer_ = buf;
if (read_position_ + buf_len > range_length_) {
// TODO(michaeln): What about integer overflows?
DCHECK(range_length_ >= read_position_);
buf_len = range_length_ - read_position_;
}
ReadRaw(kResponseContentIndex, range_offset_ + read_position_,
buf, buf_len);
}
int AppCacheResponseReader::GetResourceSize() {
if (!OpenEntryIfNeeded())
return -1;
return entry_->GetDataSize(kResponseContentIndex);
}
void AppCacheResponseReader::SetReadRange(int offset, int length) {
DCHECK(!IsReadPending() && !read_position_);
range_offset_ = offset;
range_length_ = length;
}
void AppCacheResponseReader::OnIOComplete(int result) {
if (result >= 0) {
if (info_buffer_.get()) {
// Allocate and deserialize the http info structure.
Pickle pickle(buffer_->data(), result);
bool response_truncated = false;
info_buffer_->http_info.reset(new net::HttpResponseInfo);
info_buffer_->http_info->InitFromPickle(pickle, &response_truncated);
DCHECK(!response_truncated);
} else {
read_position_ += result;
}
}
InvokeUserCompletionCallback(result);
}
bool AppCacheResponseReader::OpenEntryIfNeeded() {
if (!entry_ && disk_cache_)
disk_cache_->OpenEntry(response_key(response_id_), &entry_);
return entry_ ? true : false;
}
// AppCacheResponseWriter ----------------------------------------------
void AppCacheResponseWriter::WriteInfo(HttpResponseInfoIOBuffer* info_buf,
net::CompletionCallback* callback) {
DCHECK(callback && !IsWritePending());
DCHECK(info_buf && info_buf->http_info.get());
DCHECK(!buffer_.get() && !info_buffer_.get());
user_callback_ = callback; // cleared on completion
if (!CreateEntryIfNeeded()) {
ScheduleIOCompletionCallback(net::ERR_FAILED);
return;
}
const bool kSkipTransientHeaders = true;
const bool kTruncated = false;
Pickle* pickle = new Pickle;
info_buf->http_info->Persist(pickle, kSkipTransientHeaders, kTruncated);
info_buffer_ = info_buf;
write_amount_ = static_cast<int>(pickle->size());
buffer_ = new WrappedPickleIOBuffer(pickle); // takes ownership of pickle
WriteRaw(kResponseInfoIndex, 0, buffer_, write_amount_);
}
void AppCacheResponseWriter::WriteData(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(callback && !IsWritePending());
DCHECK(buf && (buf_len >= 0));
DCHECK(!buffer_.get() && !info_buffer_.get());
user_callback_ = callback; // cleared on completion
if (!CreateEntryIfNeeded()) {
ScheduleIOCompletionCallback(net::ERR_FAILED);
return;
}
buffer_ = buf;
write_amount_ = buf_len;
WriteRaw(kResponseContentIndex, write_position_, buf, buf_len);
}
void AppCacheResponseWriter::OnIOComplete(int result) {
if (result >= 0) {
DCHECK(write_amount_ == result);
if (!info_buffer_.get())
write_position_ += result;
else
info_size_ = result;
}
InvokeUserCompletionCallback(result);
}
bool AppCacheResponseWriter::CreateEntryIfNeeded() {
if (entry_)
return true;
if (!disk_cache_)
return false;
std::string key(response_key(response_id_));
if (!disk_cache_->CreateEntry(key, &entry_)) {
// We may try to overrite existing entries.
DCHECK(!entry_);
disk_cache_->DoomEntry(key);
disk_cache_->CreateEntry(key, &entry_);
}
return entry_ ? true : false;
}
} // namespace appcache
|