Home | History | Annotate | Download | only in flash
      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.
      4 
      5 #include "net/disk_cache/flash/internal_entry.h"
      6 
      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"
     13 
     14 using net::IOBuffer;
     15 using net::StringIOBuffer;
     16 using net::CompletionCallback;
     17 
     18 namespace disk_cache {
     19 
     20 KeyAndStreamSizes::KeyAndStreamSizes() {
     21 }
     22 
     23 InternalEntry::InternalEntry(const std::string& key, LogStore* store)
     24     : store_(store),
     25       entry_(new LogStoreEntry(store_)) {
     26   entry_->Init();
     27   WriteKey(entry_.get(), key);
     28 }
     29 
     30 InternalEntry::InternalEntry(int32 id, LogStore* store)
     31     : store_(store),
     32       entry_(new LogStoreEntry(store_, id)) {
     33 }
     34 
     35 InternalEntry::~InternalEntry() {
     36 }
     37 
     38 scoped_ptr<KeyAndStreamSizes> InternalEntry::Init() {
     39   scoped_ptr<KeyAndStreamSizes> null;
     40   if (entry_->IsNew())
     41     return null.Pass();
     42   if (!entry_->Init())
     43     return null.Pass();
     44 
     45   scoped_ptr<KeyAndStreamSizes> rv(new KeyAndStreamSizes);
     46   if (!ReadKey(entry_.get(), &rv->key))
     47     return null.Pass();
     48   for (int i = 0; i < kFlashLogStoreEntryNumStreams; ++i)
     49     rv->stream_sizes[i] = entry_->GetDataSize(i+1);
     50   return rv.Pass();
     51 }
     52 
     53 int32 InternalEntry::GetDataSize(int index) const {
     54   return entry_->GetDataSize(++index);
     55 }
     56 
     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);
     60 }
     61 
     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);
     65 }
     66 
     67 void InternalEntry::Close() {
     68   entry_->Close();
     69 }
     70 
     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;
     75 }
     76 
     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)
     81     return false;
     82   key->assign(key_buf->data(), key_size);
     83   return true;
     84 }
     85 
     86 }  // namespace disk_cache
     87