1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/flash/internal_entry.h"
7 #include "base/memory/ref_counted.h"
8 #include "net/base/completion_callback.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
11 #include "net/disk_cache/flash/log_store.h"
12 #include "net/disk_cache/flash/log_store_entry.h"
15 using net::StringIOBuffer;
16 using net::CompletionCallback;
18 namespace disk_cache {
20 KeyAndStreamSizes::KeyAndStreamSizes() {
23 InternalEntry::InternalEntry(const std::string& key, LogStore* store)
25 entry_(new LogStoreEntry(store_)) {
27 WriteKey(entry_.get(), key);
30 InternalEntry::InternalEntry(int32 id, LogStore* store)
32 entry_(new LogStoreEntry(store_, id)) {
35 InternalEntry::~InternalEntry() {
38 scoped_ptr<KeyAndStreamSizes> InternalEntry::Init() {
39 scoped_ptr<KeyAndStreamSizes> null;
45 scoped_ptr<KeyAndStreamSizes> rv(new KeyAndStreamSizes);
46 if (!ReadKey(entry_.get(), &rv->key))
48 for (int i = 0; i < kFlashLogStoreEntryNumStreams; ++i)
49 rv->stream_sizes[i] = entry_->GetDataSize(i+1);
53 int32 InternalEntry::GetDataSize(int index) const {
54 return entry_->GetDataSize(++index);
57 int InternalEntry::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
58 const CompletionCallback& callback) {
59 return entry_->ReadData(++index, offset, buf, buf_len);
62 int InternalEntry::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
63 const CompletionCallback& callback) {
64 return entry_->WriteData(++index, offset, buf, buf_len);
67 void InternalEntry::Close() {
71 bool InternalEntry::WriteKey(LogStoreEntry* entry, const std::string& key) {
72 int key_size = static_cast<int>(key.size());
73 scoped_refptr<IOBuffer> key_buf(new StringIOBuffer(key));
74 return entry->WriteData(0, 0, key_buf.get(), key_size) == key_size;
77 bool InternalEntry::ReadKey(LogStoreEntry* entry, std::string* key) {
78 int key_size = entry->GetDataSize(0);
79 scoped_refptr<net::IOBuffer> key_buf(new net::IOBuffer(key_size));
80 if (entry->ReadData(0, 0, key_buf.get(), key_size) != key_size)
82 key->assign(key_buf->data(), key_size);
86 } // namespace disk_cache