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