- add sources.
[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/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"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }
22
23 namespace drive {
24
25 typedef std::vector<ResourceEntry> ResourceEntryVector;
26
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;
32
33 typedef base::Callback<void(FileError error,
34                             scoped_ptr<ResourceEntryVector> entries)>
35     ReadDirectoryCallback;
36
37 typedef base::Callback<void(const ResourceEntry& entry)> IterateCallback;
38
39 namespace internal {
40
41 // Storage for Drive Metadata.
42 // All methods must be run with |blocking_task_runner| unless otherwise noted.
43 class ResourceMetadata {
44  public:
45   typedef ResourceMetadataStorage::Iterator Iterator;
46
47   ResourceMetadata(
48       ResourceMetadataStorage* storage,
49       scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
50
51   // Initializes this object.
52   // This method should be called before any other methods.
53   FileError Initialize() WARN_UNUSED_RESULT;
54
55   // Destroys this object.  This method posts a task to |blocking_task_runner_|
56   // to safely delete this object.
57   // Must be called on the UI thread.
58   void Destroy();
59
60   // Resets this object.
61   FileError Reset();
62
63   // Returns the largest changestamp.
64   int64 GetLargestChangestamp();
65
66   // Sets the largest changestamp.
67   FileError SetLargestChangestamp(int64 value);
68
69   // Adds |entry| to the metadata tree based on its parent_local_id.
70   FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
71
72   // Removes entry with |id| from its parent.
73   FileError RemoveEntry(const std::string& id);
74
75   // Finds an entry (a file or a directory) by |id|.
76   FileError GetResourceEntryById(const std::string& id,
77                                  ResourceEntry* out_entry);
78
79   // Finds an entry (a file or a directory) by |file_path|.
80   // |callback| must not be null.
81   // Must be called on the UI thread.
82   void GetResourceEntryByPathOnUIThread(
83       const base::FilePath& file_path,
84       const GetResourceEntryCallback& callback);
85
86   // Synchronous version of GetResourceEntryByPathOnUIThread().
87   FileError GetResourceEntryByPath(const base::FilePath& file_path,
88                                    ResourceEntry* out_entry);
89
90   // Finds and reads a directory by |file_path|.
91   // |callback| must not be null.
92   // Must be called on the UI thread.
93   void ReadDirectoryByPathOnUIThread(const base::FilePath& file_path,
94                                      const ReadDirectoryCallback& callback);
95
96   // Synchronous version of ReadDirectoryByPathOnUIThread().
97   FileError ReadDirectoryByPath(const base::FilePath& file_path,
98                                 ResourceEntryVector* out_entries);
99
100   // Replaces an existing entry with the same local ID as |entry|.
101   FileError RefreshEntry(const ResourceEntry& entry);
102
103   // Recursively gets directories under the entry pointed to by |id|.
104   void GetSubDirectoriesRecursively(const std::string& id,
105                                     std::set<base::FilePath>* sub_directories);
106
107   // Returns the id of the resource named |base_name| directly under
108   // the directory with |parent_local_id|.
109   // If not found, empty string will be returned.
110   std::string GetChildId(const std::string& parent_local_id,
111                          const std::string& base_name);
112
113   // Returns an object to iterate over entries.
114   scoped_ptr<Iterator> GetIterator();
115
116   // Returns virtual file path of the entry.
117   base::FilePath GetFilePath(const std::string& id);
118
119   // Returns ID of the entry at the given path.
120   FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
121
122   // Returns the local ID associated with the given resource ID.
123   FileError GetIdByResourceId(const std::string& resource_id,
124                               std::string* out_local_id);
125
126  private:
127   // Note: Use Destroy() to delete this object.
128   ~ResourceMetadata();
129
130   // Sets up entries which should be present by default.
131   bool SetUpDefaultEntries();
132
133   // Used to implement Destroy().
134   void DestroyOnBlockingPool();
135
136   // Puts an entry under its parent directory. Removes the child from the old
137   // parent if there is. This method will also do name de-duplication to ensure
138   // that the exposed presentation path does not have naming conflicts. Two
139   // files with the same name "Foo" will be renamed to "Foo (1)" and "Foo (2)".
140   bool PutEntryUnderDirectory(const ResourceEntry& entry);
141
142   // Removes the entry and its descendants.
143   bool RemoveEntryRecursively(const std::string& id);
144
145   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
146
147   ResourceMetadataStorage* storage_;
148
149   // This should remain the last member so it'll be destroyed first and
150   // invalidate its weak pointers before other members are destroyed.
151   base::WeakPtrFactory<ResourceMetadata> weak_ptr_factory_;
152
153   DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
154 };
155
156 }  // namespace internal
157 }  // namespace drive
158
159 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_