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 CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/files/file_path.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "chrome/browser/chromeos/drive/drive.pb.h" 15 16 namespace base { 17 class SequencedTaskRunner; 18 } // namespace base 19 20 namespace leveldb { 21 class DB; 22 class Iterator; 23 } // namespace leveldb 24 25 namespace drive { 26 namespace internal { 27 28 // FileCacheMetadata maintains metadata of FileCache's cached files. 29 // This class only manages metadata. File operations are done by FileCache. 30 // All member access including ctor and dtor must be made on the blocking pool. 31 // 32 // OBSOLETE: This class is maintained only for importing old data. 33 // TODO(hashimoto): Remove this class at some point. 34 class FileCacheMetadata { 35 public: 36 // Result of Initialize(). 37 enum InitializeResult { 38 INITIALIZE_FAILED, // Could not open nor create DB. 39 INITIALIZE_OPENED, // Opened an existing DB. 40 INITIALIZE_CREATED, // Created a new DB. 41 }; 42 43 // Object to iterate over entries stored in the cache metadata. 44 class Iterator { 45 public: 46 explicit Iterator(scoped_ptr<leveldb::Iterator> it); 47 ~Iterator(); 48 49 // Returns true if this iterator cannot advance any more and does not point 50 // to a valid entry. GetKey(), GetValue() and Advance() should not be called 51 // in such cases. 52 bool IsAtEnd() const; 53 54 // Returns the key of the entry currently pointed by this object. 55 std::string GetKey() const; 56 57 // Returns the value of the entry currently pointed by this object. 58 const FileCacheEntry& GetValue() const; 59 60 // Advances to the next entry. 61 void Advance(); 62 63 // Returns true if this object has encountered any error. 64 bool HasError() const; 65 66 private: 67 // Used to implement Advance(). 68 void AdvanceInternal(); 69 70 scoped_ptr<leveldb::Iterator> it_; 71 FileCacheEntry entry_; 72 73 DISALLOW_COPY_AND_ASSIGN(Iterator); 74 }; 75 76 // Tests are allowed to pass NULL as |blocking_task_runner|. 77 explicit FileCacheMetadata(base::SequencedTaskRunner* blocking_task_runner); 78 79 ~FileCacheMetadata(); 80 81 // Initialize the cache metadata store. Returns true on success. 82 InitializeResult Initialize(const base::FilePath& db_path); 83 // Adds a new cache entry corresponding to |resource_id| if it doesn't 84 // exist, otherwise update the existing entry. 85 void AddOrUpdateCacheEntry(const std::string& resource_id, 86 const FileCacheEntry& cache_entry); 87 88 // Removes entry corresponding to |resource_id| from cache map. 89 void RemoveCacheEntry(const std::string& resource_id); 90 91 // Gets the cache entry for file corresponding to |resource_id| and returns 92 // true if entry exists in cache map. Otherwise, returns false. 93 bool GetCacheEntry(const std::string& resource_id, FileCacheEntry* entry); 94 95 // Returns an object to iterate over entries. 96 scoped_ptr<Iterator> GetIterator(); 97 98 private: 99 // Checks whether the current thread is on the right sequenced worker pool 100 // with the right sequence ID. If not, DCHECK will fail. 101 void AssertOnSequencedWorkerPool(); 102 103 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 104 scoped_ptr<leveldb::DB> level_db_; 105 106 DISALLOW_COPY_AND_ASSIGN(FileCacheMetadata); 107 }; 108 109 } // namespace internal 110 } // namespace drive 111 112 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_ 113