Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / resource_metadata.h
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 // 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 {
31  public:
32   typedef ResourceMetadataStorage::Iterator Iterator;
33
34   ResourceMetadata(
35       ResourceMetadataStorage* storage,
36       scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
37
38   // Initializes this object.
39   // This method should be called before any other methods.
40   FileError Initialize() WARN_UNUSED_RESULT;
41
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.
45   void Destroy();
46
47   // Resets this object.
48   FileError Reset();
49
50   // Returns the largest changestamp.
51   int64 GetLargestChangestamp();
52
53   // Sets the largest changestamp.
54   FileError SetLargestChangestamp(int64 value);
55
56   // Adds |entry| to the metadata tree based on its parent_local_id.
57   FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
58
59   // Removes entry with |id| from its parent.
60   FileError RemoveEntry(const std::string& id);
61
62   // Finds an entry (a file or a directory) by |id|.
63   FileError GetResourceEntryById(const std::string& id,
64                                  ResourceEntry* out_entry);
65
66   // Synchronous version of GetResourceEntryByPathOnUIThread().
67   FileError GetResourceEntryByPath(const base::FilePath& file_path,
68                                    ResourceEntry* out_entry);
69
70   // Finds and reads a directory by |file_path|.
71   FileError ReadDirectoryByPath(const base::FilePath& file_path,
72                                 ResourceEntryVector* out_entries);
73
74   // Finds and reads a directory by |id|.
75   FileError ReadDirectoryById(const std::string& id,
76                               ResourceEntryVector* out_entries);
77
78   // Replaces an existing entry with the same local ID as |entry|.
79   FileError RefreshEntry(const ResourceEntry& entry);
80
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);
84
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);
90
91   // Returns an object to iterate over entries.
92   scoped_ptr<Iterator> GetIterator();
93
94   // Returns virtual file path of the entry.
95   base::FilePath GetFilePath(const std::string& id);
96
97   // Returns ID of the entry at the given path.
98   FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
99
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);
103
104  private:
105   // Note: Use Destroy() to delete this object.
106   ~ResourceMetadata();
107
108   // Sets up entries which should be present by default.
109   bool SetUpDefaultEntries();
110
111   // Used to implement Destroy().
112   void DestroyOnBlockingPool();
113
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);
119
120   // Returns an unused base name for |entry|.
121   std::string GetDeduplicatedBaseName(const ResourceEntry& entry);
122
123   // Removes the entry and its descendants.
124   bool RemoveEntryRecursively(const std::string& id);
125
126   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
127
128   ResourceMetadataStorage* storage_;
129
130   DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
131 };
132
133 }  // namespace internal
134 }  // namespace drive
135
136 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_