• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RESOURCE_METADATA_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_
7 
8 #include <set>
9 #include <string>
10 #include <vector>
11 
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/chromeos/drive/file_errors.h"
15 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
16 
17 namespace base {
18 class SequencedTaskRunner;
19 }
20 
21 namespace drive {
22 
23 typedef std::vector<ResourceEntry> ResourceEntryVector;
24 
25 namespace internal {
26 
27 class FileCache;
28 
29 // Storage for Drive Metadata.
30 // All methods except the constructor and Destroy() function must be run with
31 // |blocking_task_runner| unless otherwise noted.
32 class ResourceMetadata {
33  public:
34   typedef ResourceMetadataStorage::Iterator Iterator;
35 
36   ResourceMetadata(
37       ResourceMetadataStorage* storage,
38       FileCache* cache,
39       scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
40 
41   // Initializes this object.
42   // This method should be called before any other methods.
43   FileError Initialize() WARN_UNUSED_RESULT;
44 
45   // Destroys this object.  This method posts a task to |blocking_task_runner_|
46   // to safely delete this object.
47   // Must be called on the UI thread.
48   void Destroy();
49 
50   // Resets this object.
51   FileError Reset();
52 
53   // Returns the largest changestamp.
54   FileError GetLargestChangestamp(int64* out_value);
55 
56   // Sets the largest changestamp.
57   FileError SetLargestChangestamp(int64 value);
58 
59   // Adds |entry| to the metadata tree based on its parent_local_id.
60   FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
61 
62   // Removes entry with |id| from its parent.
63   FileError RemoveEntry(const std::string& id);
64 
65   // Finds an entry (a file or a directory) by |id|.
66   FileError GetResourceEntryById(const std::string& id,
67                                  ResourceEntry* out_entry);
68 
69   // Synchronous version of GetResourceEntryByPathOnUIThread().
70   FileError GetResourceEntryByPath(const base::FilePath& file_path,
71                                    ResourceEntry* out_entry);
72 
73   // Finds and reads a directory by |file_path|.
74   FileError ReadDirectoryByPath(const base::FilePath& file_path,
75                                 ResourceEntryVector* out_entries);
76 
77   // Finds and reads a directory by |id|.
78   FileError ReadDirectoryById(const std::string& id,
79                               ResourceEntryVector* out_entries);
80 
81   // Replaces an existing entry with the same local ID as |entry|.
82   FileError RefreshEntry(const ResourceEntry& entry);
83 
84   // Recursively gets directories under the entry pointed to by |id|.
85   FileError GetSubDirectoriesRecursively(
86       const std::string& id,
87       std::set<base::FilePath>* sub_directories);
88 
89   // Returns the id of the resource named |base_name| directly under
90   // the directory with |parent_local_id|.
91   // If not found, empty string will be returned.
92   FileError GetChildId(const std::string& parent_local_id,
93                        const std::string& base_name,
94                        std::string* out_child_id);
95 
96   // Returns an object to iterate over entries.
97   scoped_ptr<Iterator> GetIterator();
98 
99   // Returns virtual file path of the entry.
100   FileError GetFilePath(const std::string& id, base::FilePath* out_file_path);
101 
102   // Returns ID of the entry at the given path.
103   FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
104 
105   // Returns the local ID associated with the given resource ID.
106   FileError GetIdByResourceId(const std::string& resource_id,
107                               std::string* out_local_id);
108 
109  private:
110   // Note: Use Destroy() to delete this object.
111   ~ResourceMetadata();
112 
113   // Sets up entries which should be present by default.
114   FileError SetUpDefaultEntries();
115 
116   // Used to implement Destroy().
117   void DestroyOnBlockingPool();
118 
119   // Puts an entry under its parent directory. Removes the child from the old
120   // parent if there is. This method will also do name de-duplication to ensure
121   // that the exposed presentation path does not have naming conflicts. Two
122   // files with the same name "Foo" will be renamed to "Foo (1)" and "Foo (2)".
123   FileError PutEntryUnderDirectory(const ResourceEntry& entry);
124 
125   // Returns an unused base name for |entry|.
126   FileError GetDeduplicatedBaseName(const ResourceEntry& entry,
127                                     std::string* base_name);
128 
129   // Removes the entry and its descendants.
130   FileError RemoveEntryRecursively(const std::string& id);
131 
132   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
133 
134   ResourceMetadataStorage* storage_;
135   FileCache* cache_;
136 
137   DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
138 };
139 
140 }  // namespace internal
141 }  // namespace drive
142 
143 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_
144