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/callback_forward.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/chromeos/drive/file_errors.h"
17 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
20 class SequencedTaskRunner;
25 typedef std::vector<ResourceEntry> ResourceEntryVector;
27 // Used to get a resource entry from the file system.
28 // If |error| is not FILE_ERROR_OK, |entry_info| is set to NULL.
29 typedef base::Callback<void(FileError error,
30 scoped_ptr<ResourceEntry> entry)>
31 GetResourceEntryCallback;
33 typedef base::Callback<void(const ResourceEntry& entry)> IterateCallback;
37 // Storage for Drive Metadata.
38 // All methods must be run with |blocking_task_runner| unless otherwise noted.
39 class ResourceMetadata {
41 typedef ResourceMetadataStorage::Iterator Iterator;
44 ResourceMetadataStorage* storage,
45 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
47 // Initializes this object.
48 // This method should be called before any other methods.
49 FileError Initialize() WARN_UNUSED_RESULT;
51 // Destroys this object. This method posts a task to |blocking_task_runner_|
52 // to safely delete this object.
53 // Must be called on the UI thread.
56 // Resets this object.
59 // Returns the largest changestamp.
60 int64 GetLargestChangestamp();
62 // Sets the largest changestamp.
63 FileError SetLargestChangestamp(int64 value);
65 // Adds |entry| to the metadata tree based on its parent_local_id.
66 FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
68 // Removes entry with |id| from its parent.
69 FileError RemoveEntry(const std::string& id);
71 // Finds an entry (a file or a directory) by |id|.
72 FileError GetResourceEntryById(const std::string& id,
73 ResourceEntry* out_entry);
75 // Finds an entry (a file or a directory) by |file_path|.
76 // |callback| must not be null.
77 // Must be called on the UI thread.
78 void GetResourceEntryByPathOnUIThread(
79 const base::FilePath& file_path,
80 const GetResourceEntryCallback& callback);
82 // Synchronous version of GetResourceEntryByPathOnUIThread().
83 FileError GetResourceEntryByPath(const base::FilePath& file_path,
84 ResourceEntry* out_entry);
86 // Finds and reads a directory by |file_path|.
87 FileError ReadDirectoryByPath(const base::FilePath& file_path,
88 ResourceEntryVector* out_entries);
90 // Finds and reads a directory by |id|.
91 FileError ReadDirectoryById(const std::string& id,
92 ResourceEntryVector* out_entries);
94 // Replaces an existing entry with the same local ID as |entry|.
95 FileError RefreshEntry(const ResourceEntry& entry);
97 // Recursively gets directories under the entry pointed to by |id|.
98 void GetSubDirectoriesRecursively(const std::string& id,
99 std::set<base::FilePath>* sub_directories);
101 // Returns the id of the resource named |base_name| directly under
102 // the directory with |parent_local_id|.
103 // If not found, empty string will be returned.
104 std::string GetChildId(const std::string& parent_local_id,
105 const std::string& base_name);
107 // Returns an object to iterate over entries.
108 scoped_ptr<Iterator> GetIterator();
110 // Returns virtual file path of the entry.
111 base::FilePath GetFilePath(const std::string& id);
113 // Returns ID of the entry at the given path.
114 FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
116 // Returns the local ID associated with the given resource ID.
117 FileError GetIdByResourceId(const std::string& resource_id,
118 std::string* out_local_id);
121 // Note: Use Destroy() to delete this object.
124 // Sets up entries which should be present by default.
125 bool SetUpDefaultEntries();
127 // Used to implement Destroy().
128 void DestroyOnBlockingPool();
130 // Puts an entry under its parent directory. Removes the child from the old
131 // parent if there is. This method will also do name de-duplication to ensure
132 // that the exposed presentation path does not have naming conflicts. Two
133 // files with the same name "Foo" will be renamed to "Foo (1)" and "Foo (2)".
134 bool PutEntryUnderDirectory(const ResourceEntry& entry);
136 // Removes the entry and its descendants.
137 bool RemoveEntryRecursively(const std::string& id);
139 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
141 ResourceMetadataStorage* storage_;
143 // This should remain the last member so it'll be destroyed first and
144 // invalidate its weak pointers before other members are destroyed.
145 base::WeakPtrFactory<ResourceMetadata> weak_ptr_factory_;
147 DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
150 } // namespace internal
153 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_