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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_
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"
18 class SequencedTaskRunner;
23 typedef std::vector<ResourceEntry> ResourceEntryVector;
27 // Storage for Drive Metadata.
28 // All methods except the constructor and Destroy() function must be run with
29 // |blocking_task_runner| unless otherwise noted.
30 class ResourceMetadata {
32 typedef ResourceMetadataStorage::Iterator Iterator;
35 ResourceMetadataStorage* storage,
36 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
38 // Initializes this object.
39 // This method should be called before any other methods.
40 FileError Initialize() WARN_UNUSED_RESULT;
42 // Destroys this object. This method posts a task to |blocking_task_runner_|
43 // to safely delete this object.
44 // Must be called on the UI thread.
47 // Resets this object.
50 // Returns the largest changestamp.
51 int64 GetLargestChangestamp();
53 // Sets the largest changestamp.
54 FileError SetLargestChangestamp(int64 value);
56 // Adds |entry| to the metadata tree based on its parent_local_id.
57 FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
59 // Removes entry with |id| from its parent.
60 FileError RemoveEntry(const std::string& id);
62 // Finds an entry (a file or a directory) by |id|.
63 FileError GetResourceEntryById(const std::string& id,
64 ResourceEntry* out_entry);
66 // Synchronous version of GetResourceEntryByPathOnUIThread().
67 FileError GetResourceEntryByPath(const base::FilePath& file_path,
68 ResourceEntry* out_entry);
70 // Finds and reads a directory by |file_path|.
71 FileError ReadDirectoryByPath(const base::FilePath& file_path,
72 ResourceEntryVector* out_entries);
74 // Finds and reads a directory by |id|.
75 FileError ReadDirectoryById(const std::string& id,
76 ResourceEntryVector* out_entries);
78 // Replaces an existing entry with the same local ID as |entry|.
79 FileError RefreshEntry(const ResourceEntry& entry);
81 // Recursively gets directories under the entry pointed to by |id|.
82 void GetSubDirectoriesRecursively(const std::string& id,
83 std::set<base::FilePath>* sub_directories);
85 // Returns the id of the resource named |base_name| directly under
86 // the directory with |parent_local_id|.
87 // If not found, empty string will be returned.
88 std::string GetChildId(const std::string& parent_local_id,
89 const std::string& base_name);
91 // Returns an object to iterate over entries.
92 scoped_ptr<Iterator> GetIterator();
94 // Returns virtual file path of the entry.
95 base::FilePath GetFilePath(const std::string& id);
97 // Returns ID of the entry at the given path.
98 FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
100 // Returns the local ID associated with the given resource ID.
101 FileError GetIdByResourceId(const std::string& resource_id,
102 std::string* out_local_id);
105 // Note: Use Destroy() to delete this object.
108 // Sets up entries which should be present by default.
109 bool SetUpDefaultEntries();
111 // Used to implement Destroy().
112 void DestroyOnBlockingPool();
114 // Puts an entry under its parent directory. Removes the child from the old
115 // parent if there is. This method will also do name de-duplication to ensure
116 // that the exposed presentation path does not have naming conflicts. Two
117 // files with the same name "Foo" will be renamed to "Foo (1)" and "Foo (2)".
118 bool PutEntryUnderDirectory(const ResourceEntry& entry);
120 // Returns an unused base name for |entry|.
121 std::string GetDeduplicatedBaseName(const ResourceEntry& entry);
123 // Removes the entry and its descendants.
124 bool RemoveEntryRecursively(const std::string& id);
126 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
128 ResourceMetadataStorage* storage_;
130 DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
133 } // namespace internal
136 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_