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