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 "base/location.h"
      6 #include "base/message_loop/message_loop_proxy.h"
      7 #include "base/task_runner_util.h"
      8 #include "net/base/io_buffer.h"
      9 #include "net/base/net_errors.h"
     10 #include "net/disk_cache/flash/flash_entry_impl.h"
     11 #include "net/disk_cache/flash/internal_entry.h"
     12 
     13 namespace disk_cache {
     14 
     15 FlashEntryImpl::FlashEntryImpl(const std::string& key,
     16                                LogStore* store,
     17                                base::MessageLoopProxy* cache_thread)
     18     : init_(false),
     19       key_(key),
     20       new_internal_entry_(new InternalEntry(key, store)),
     21       cache_thread_(cache_thread) {
     22   memset(stream_sizes_, 0, sizeof(stream_sizes_));
     23 }
     24 
     25 FlashEntryImpl::FlashEntryImpl(int32 id,
     26                                LogStore* store,
     27                                base::MessageLoopProxy* cache_thread)
     28     : init_(false),
     29       old_internal_entry_(new InternalEntry(id, store)),
     30       cache_thread_(cache_thread) {
     31 }
     32 
     33 int FlashEntryImpl::Init(const CompletionCallback& callback) {
     34   if (new_internal_entry_.get()) {
     35     DCHECK(callback.is_null());
     36     init_ = true;
     37     return net::OK;
     38   }
     39   DCHECK(!callback.is_null() && old_internal_entry_.get());
     40   callback_ = callback;
     41   PostTaskAndReplyWithResult(cache_thread_.get(),
     42                              FROM_HERE,
     43                              Bind(&InternalEntry::Init, old_internal_entry_),
     44                              Bind(&FlashEntryImpl::OnInitComplete, this));
     45   return net::ERR_IO_PENDING;
     46 }
     47 
     48 void FlashEntryImpl::Doom() {
     49   DCHECK(init_);
     50   NOTREACHED();
     51 }
     52 
     53 void FlashEntryImpl::Close() {
     54   DCHECK(init_);
     55   Release();
     56 }
     57 
     58 std::string FlashEntryImpl::GetKey() const {
     59   DCHECK(init_);
     60   return key_;
     61 }
     62 
     63 base::Time FlashEntryImpl::GetLastUsed() const {
     64   DCHECK(init_);
     65   NOTREACHED();
     66   return base::Time::Now();
     67 }
     68 
     69 base::Time FlashEntryImpl::GetLastModified() const {
     70   DCHECK(init_);
     71   NOTREACHED();
     72   return base::Time::Now();
     73 }
     74 
     75 int32 FlashEntryImpl::GetDataSize(int index) const {
     76   DCHECK(init_);
     77   return new_internal_entry_->GetDataSize(index);
     78 }
     79 
     80 int FlashEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
     81                              const CompletionCallback& callback) {
     82   DCHECK(init_);
     83   return new_internal_entry_->ReadData(index, offset, buf, buf_len, callback);
     84 }
     85 
     86 int FlashEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
     87                               const CompletionCallback& callback,
     88                               bool truncate) {
     89   DCHECK(init_);
     90   return new_internal_entry_->WriteData(index, offset, buf, buf_len, callback);
     91 }
     92 
     93 int FlashEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
     94                                    const CompletionCallback& callback) {
     95   DCHECK(init_);
     96   NOTREACHED();
     97   return net::ERR_FAILED;
     98 }
     99 
    100 int FlashEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
    101                                     const CompletionCallback& callback) {
    102   DCHECK(init_);
    103   NOTREACHED();
    104   return net::ERR_FAILED;
    105 }
    106 
    107 int FlashEntryImpl::GetAvailableRange(int64 offset, int len, int64* start,
    108                                       const CompletionCallback& callback) {
    109   DCHECK(init_);
    110   NOTREACHED();
    111   return net::ERR_FAILED;
    112 }
    113 
    114 bool FlashEntryImpl::CouldBeSparse() const {
    115   DCHECK(init_);
    116   NOTREACHED();
    117   return false;
    118 }
    119 
    120 void FlashEntryImpl::CancelSparseIO() {
    121   DCHECK(init_);
    122   NOTREACHED();
    123 }
    124 
    125 int FlashEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) {
    126   DCHECK(init_);
    127   NOTREACHED();
    128   return net::ERR_FAILED;
    129 }
    130 
    131 void FlashEntryImpl::OnInitComplete(
    132     scoped_ptr<KeyAndStreamSizes> key_and_stream_sizes) {
    133   DCHECK(!callback_.is_null());
    134   if (!key_and_stream_sizes) {
    135     callback_.Run(net::ERR_FAILED);
    136   } else {
    137     key_ = key_and_stream_sizes->key;
    138     memcpy(stream_sizes_, key_and_stream_sizes->stream_sizes,
    139            sizeof(stream_sizes_));
    140     init_ = true;
    141     callback_.Run(net::OK);
    142   }
    143 }
    144 
    145 FlashEntryImpl::~FlashEntryImpl() {
    146   cache_thread_->PostTask(FROM_HERE,
    147                           Bind(&InternalEntry::Close, new_internal_entry_));
    148 }
    149 
    150 }  // namespace disk_cache
    151