summaryrefslogtreecommitdiff
path: root/chromium/net/disk_cache/flash/internal_entry.cc
blob: c6bae8926aa0b39ada7c4dbc186a69ad1c62de95 (plain)
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
// Copyright (c) 2012 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 "net/disk_cache/flash/internal_entry.h"

#include "base/memory/ref_counted.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/flash/log_store.h"
#include "net/disk_cache/flash/log_store_entry.h"

using net::IOBuffer;
using net::StringIOBuffer;
using net::CompletionCallback;

namespace disk_cache {

KeyAndStreamSizes::KeyAndStreamSizes() {
}

InternalEntry::InternalEntry(const std::string& key, LogStore* store)
    : store_(store),
      entry_(new LogStoreEntry(store_)) {
  entry_->Init();
  WriteKey(entry_.get(), key);
}

InternalEntry::InternalEntry(int32 id, LogStore* store)
    : store_(store),
      entry_(new LogStoreEntry(store_, id)) {
}

InternalEntry::~InternalEntry() {
}

scoped_ptr<KeyAndStreamSizes> InternalEntry::Init() {
  scoped_ptr<KeyAndStreamSizes> null;
  if (entry_->IsNew())
    return null.Pass();
  if (!entry_->Init())
    return null.Pass();

  scoped_ptr<KeyAndStreamSizes> rv(new KeyAndStreamSizes);
  if (!ReadKey(entry_.get(), &rv->key))
    return null.Pass();
  for (int i = 0; i < kFlashLogStoreEntryNumStreams; ++i)
    rv->stream_sizes[i] = entry_->GetDataSize(i+1);
  return rv.Pass();
}

int32 InternalEntry::GetDataSize(int index) const {
  return entry_->GetDataSize(++index);
}

int InternalEntry::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
                            const CompletionCallback& callback) {
  return entry_->ReadData(++index, offset, buf, buf_len);
}

int InternalEntry::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
                             const CompletionCallback& callback) {
  return entry_->WriteData(++index, offset, buf, buf_len);
}

void InternalEntry::Close() {
  entry_->Close();
}

bool InternalEntry::WriteKey(LogStoreEntry* entry, const std::string& key) {
  int key_size = static_cast<int>(key.size());
  scoped_refptr<IOBuffer> key_buf(new StringIOBuffer(key));
  return entry->WriteData(0, 0, key_buf.get(), key_size) == key_size;
}

bool InternalEntry::ReadKey(LogStoreEntry* entry, std::string* key) {
  int key_size = entry->GetDataSize(0);
  scoped_refptr<net::IOBuffer> key_buf(new net::IOBuffer(key_size));
  if (entry->ReadData(0, 0, key_buf.get(), key_size) != key_size)
    return false;
  key->assign(key_buf->data(), key_size);
  return true;
}

}  // namespace disk_cache