Upstream version 5.34.104.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/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(const ResourceEntry& entry)> IterateCallback;
34
35 namespace internal {
36
37 // Storage for Drive Metadata.
38 // All methods must be run with |blocking_task_runner| unless otherwise noted.
39 class ResourceMetadata {
40  public:
41   typedef ResourceMetadataStorage::Iterator Iterator;
42
43   ResourceMetadata(
44       ResourceMetadataStorage* storage,
45       scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
46
47   // Initializes this object.
48   // This method should be called before any other methods.
49   FileError Initialize() WARN_UNUSED_RESULT;
50
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.
54   void Destroy();
55
56   // Resets this object.
57   FileError Reset();
58
59   // Returns the largest changestamp.
60   int64 GetLargestChangestamp();
61
62   // Sets the largest changestamp.
63   FileError SetLargestChangestamp(int64 value);
64
65   // Adds |entry| to the metadata tree based on its parent_local_id.
66   FileError AddEntry(const ResourceEntry& entry, std::string* out_id);
67
68   // Removes entry with |id| from its parent.
69   FileError RemoveEntry(const std::string& id);
70
71   // Finds an entry (a file or a directory) by |id|.
72   FileError GetResourceEntryById(const std::string& id,
73                                  ResourceEntry* out_entry);
74
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);
81
82   // Synchronous version of GetResourceEntryByPathOnUIThread().
83   FileError GetResourceEntryByPath(const base::FilePath& file_path,
84                                    ResourceEntry* out_entry);
85
86   // Finds and reads a directory by |file_path|.
87   FileError ReadDirectoryByPath(const base::FilePath& file_path,
88                                 ResourceEntryVector* out_entries);
89
90   // Finds and reads a directory by |id|.
91   FileError ReadDirectoryById(const std::string& id,
92                               ResourceEntryVector* out_entries);
93
94   // Replaces an existing entry with the same local ID as |entry|.
95   FileError RefreshEntry(const ResourceEntry& entry);
96
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);
100
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);
106
107   // Returns an object to iterate over entries.
108   scoped_ptr<Iterator> GetIterator();
109
110   // Returns virtual file path of the entry.
111   base::FilePath GetFilePath(const std::string& id);
112
113   // Returns ID of the entry at the given path.
114   FileError GetIdByPath(const base::FilePath& file_path, std::string* out_id);
115
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);
119
120  private:
121   // Note: Use Destroy() to delete this object.
122   ~ResourceMetadata();
123
124   // Sets up entries which should be present by default.
125   bool SetUpDefaultEntries();
126
127   // Used to implement Destroy().
128   void DestroyOnBlockingPool();
129
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);
135
136   // Removes the entry and its descendants.
137   bool RemoveEntryRecursively(const std::string& id);
138
139   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
140
141   ResourceMetadataStorage* storage_;
142
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_;
146
147   DISALLOW_COPY_AND_ASSIGN(ResourceMetadata);
148 };
149
150 }  // namespace internal
151 }  // namespace drive
152
153 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_RESOURCE_METADATA_H_