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 #ifndef NET_DISK_CACHE_FLASH_ENTRY_IMPL_H_
      6 #define NET_DISK_CACHE_FLASH_ENTRY_IMPL_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "net/base/net_export.h"
     13 #include "net/disk_cache/disk_cache.h"
     14 #include "net/disk_cache/flash/internal_entry.h"
     15 
     16 namespace base {
     17 
     18 class MessageLoopProxy;
     19 
     20 }  // namespace base
     21 
     22 namespace disk_cache {
     23 
     24 class InternalEntry;
     25 class IOBuffer;
     26 class LogStore;
     27 
     28 // We use split objects to minimize the context switches between the main thread
     29 // and the cache thread in the most common case of creating a new entry.
     30 //
     31 // All calls on a new entry are served synchronously.  When an object is
     32 // destructed (via final Close() call), a message is posted to the cache thread
     33 // to save the object to storage.
     34 //
     35 // When an entry is not new, every asynchronous call is posted to the cache
     36 // thread, just as before; synchronous calls like GetKey() and GetDataSize() are
     37 // served from the main thread.
     38 class NET_EXPORT_PRIVATE FlashEntryImpl
     39     : public Entry,
     40       public base::RefCountedThreadSafe<FlashEntryImpl> {
     41   friend class base::RefCountedThreadSafe<FlashEntryImpl>;
     42  public:
     43   FlashEntryImpl(const std::string& key,
     44                  LogStore* store,
     45                  base::MessageLoopProxy* cache_thread);
     46   FlashEntryImpl(int32 id,
     47                  LogStore* store,
     48                  base::MessageLoopProxy* cache_thread);
     49 
     50   int Init(const CompletionCallback& callback);
     51 
     52   // disk_cache::Entry interface.
     53   virtual void Doom() OVERRIDE;
     54   virtual void Close() OVERRIDE;
     55   virtual std::string GetKey() const OVERRIDE;
     56   virtual base::Time GetLastUsed() const OVERRIDE;
     57   virtual base::Time GetLastModified() const OVERRIDE;
     58   virtual int32 GetDataSize(int index) const OVERRIDE;
     59   virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len,
     60                        const CompletionCallback& callback) OVERRIDE;
     61   virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len,
     62                         const CompletionCallback& callback,
     63                         bool truncate) OVERRIDE;
     64   virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
     65                              const CompletionCallback& callback) OVERRIDE;
     66   virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
     67                               const CompletionCallback& callback) OVERRIDE;
     68   virtual int GetAvailableRange(int64 offset, int len, int64* start,
     69                                 const CompletionCallback& callback) OVERRIDE;
     70   virtual bool CouldBeSparse() const OVERRIDE;
     71   virtual void CancelSparseIO() OVERRIDE;
     72   virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE;
     73 
     74  private:
     75   void OnInitComplete(scoped_ptr<KeyAndStreamSizes> key_and_stream_sizes);
     76   virtual ~FlashEntryImpl();
     77 
     78   bool init_;
     79   std::string key_;
     80   int stream_sizes_[kFlashLogStoreEntryNumStreams];
     81 
     82   // Used if |this| is an newly created entry.
     83   scoped_refptr<InternalEntry> new_internal_entry_;
     84 
     85   // Used if |this| is an existing entry.
     86   scoped_refptr<InternalEntry> old_internal_entry_;
     87 
     88   // Copy of the callback for asynchronous calls on |old_internal_entry_|.
     89   CompletionCallback callback_;
     90 
     91   scoped_refptr<base::MessageLoopProxy> cache_thread_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(FlashEntryImpl);
     94 };
     95 
     96 }  // namespace disk_cache
     97 
     98 #endif  // NET_DISK_CACHE_FLASH_ENTRY_IMPL_H_
     99