Home | History | Annotate | Download | only in drive
      1 // Copyright 2013 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_RESOURCE_METADATA_STORAGE_H_
      6 #define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "chrome/browser/chromeos/drive/drive.pb.h"
     16 #include "chrome/browser/chromeos/drive/file_errors.h"
     17 
     18 namespace base {
     19 class SequencedTaskRunner;
     20 }
     21 
     22 namespace leveldb {
     23 class DB;
     24 class Iterator;
     25 }
     26 
     27 namespace drive {
     28 
     29 class ResourceEntry;
     30 class ResourceMetadataHeader;
     31 
     32 namespace internal {
     33 
     34 // Storage for ResourceMetadata which is responsible to manage resource
     35 // entries and child-parent relationships between entries.
     36 class ResourceMetadataStorage {
     37  public:
     38   // This should be incremented when incompatibility change is made to DB
     39   // format.
     40   static const int kDBVersion = 13;
     41 
     42   // Object to iterate over entries stored in this storage.
     43   class Iterator {
     44    public:
     45     explicit Iterator(scoped_ptr<leveldb::Iterator> it);
     46     ~Iterator();
     47 
     48     // Returns true if this iterator cannot advance any more and does not point
     49     // to a valid entry. Get() and Advance() should not be called in such cases.
     50     bool IsAtEnd() const;
     51 
     52     // Returns the ID of the entry currently pointed by this object.
     53     std::string GetID() const;
     54 
     55     // Returns the entry currently pointed by this object.
     56     const ResourceEntry& GetValue() const;
     57 
     58     // Advances to the next entry.
     59     void Advance();
     60 
     61     // Returns true if this object has encountered any error.
     62     bool HasError() const;
     63 
     64    private:
     65     ResourceEntry entry_;
     66     scoped_ptr<leveldb::Iterator> it_;
     67 
     68     DISALLOW_COPY_AND_ASSIGN(Iterator);
     69   };
     70 
     71   // Cache information recovered from trashed DB.
     72   struct RecoveredCacheInfo {
     73     RecoveredCacheInfo();
     74     ~RecoveredCacheInfo();
     75 
     76     bool is_dirty;
     77     std::string md5;
     78     std::string title;
     79   };
     80   typedef std::map<std::string, RecoveredCacheInfo> RecoveredCacheInfoMap;
     81 
     82   // Returns true if the DB was successfully upgraded to the newest version.
     83   static bool UpgradeOldDB(const base::FilePath& directory_path);
     84 
     85   ResourceMetadataStorage(const base::FilePath& directory_path,
     86                           base::SequencedTaskRunner* blocking_task_runner);
     87 
     88   const base::FilePath& directory_path() const { return directory_path_; }
     89 
     90   // Returns true when cache entries were not loaded to the DB during
     91   // initialization.
     92   bool cache_file_scan_is_needed() const { return cache_file_scan_is_needed_; }
     93 
     94   // Destroys this object.
     95   void Destroy();
     96 
     97   // Initializes this object.
     98   bool Initialize();
     99 
    100   // Collects cache info from trashed resource map DB.
    101   void RecoverCacheInfoFromTrashedResourceMap(RecoveredCacheInfoMap* out_info);
    102 
    103   // Sets the largest changestamp.
    104   FileError SetLargestChangestamp(int64 largest_changestamp);
    105 
    106   // Gets the largest changestamp.
    107   FileError GetLargestChangestamp(int64* largest_changestamp);
    108 
    109   // Puts the entry to this storage.
    110   FileError PutEntry(const ResourceEntry& entry);
    111 
    112   // Gets an entry stored in this storage.
    113   FileError GetEntry(const std::string& id, ResourceEntry* out_entry);
    114 
    115   // Removes an entry from this storage.
    116   FileError RemoveEntry(const std::string& id);
    117 
    118   // Returns an object to iterate over entries stored in this storage.
    119   scoped_ptr<Iterator> GetIterator();
    120 
    121   // Returns the ID of the parent's child.
    122   FileError GetChild(const std::string& parent_id,
    123                      const std::string& child_name,
    124                      std::string* child_id);
    125 
    126   // Returns the IDs of the parent's children.
    127   FileError GetChildren(const std::string& parent_id,
    128                         std::vector<std::string>* children);
    129 
    130   // Returns the local ID associated with the given resource ID.
    131   FileError GetIdByResourceId(const std::string& resource_id,
    132                               std::string* out_id);
    133 
    134  private:
    135   friend class ResourceMetadataStorageTest;
    136 
    137   // To destruct this object, use Destroy().
    138   ~ResourceMetadataStorage();
    139 
    140   // Used to implement Destroy().
    141   void DestroyOnBlockingPool();
    142 
    143   // Returns a string to be used as a key for child entry.
    144   static std::string GetChildEntryKey(const std::string& parent_id,
    145                                       const std::string& child_name);
    146 
    147   // Puts header.
    148   FileError PutHeader(const ResourceMetadataHeader& header);
    149 
    150   // Gets header.
    151   FileError GetHeader(ResourceMetadataHeader* out_header);
    152 
    153   // Checks validity of the data.
    154   bool CheckValidity();
    155 
    156   // Path to the directory where the data is stored.
    157   base::FilePath directory_path_;
    158 
    159   bool cache_file_scan_is_needed_;
    160 
    161   // Entries stored in this storage.
    162   scoped_ptr<leveldb::DB> resource_map_;
    163 
    164   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    165 
    166   DISALLOW_COPY_AND_ASSIGN(ResourceMetadataStorage);
    167 };
    168 
    169 }  // namespace internal
    170 }  // namespace drive
    171 
    172 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_STORAGE_H_
    173