1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_RESOURCE_METADATA_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_RESOURCE_METADATA_H_
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/callback_forward.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time.h"
#include "chrome/browser/chromeos/drive/drive_file_error.h"
namespace base {
class SequencedTaskRunner;
}
namespace google_apis {
class ResourceEntry;
}
namespace drive {
struct CreateDBParams;
class DriveDirectory;
class DriveEntry;
class DriveEntryProto;
class DriveFile;
class ResourceMetadataDB;
typedef std::vector<DriveEntryProto> DriveEntryProtoVector;
typedef std::map<std::string /* resource_id */, DriveEntryProto>
DriveEntryProtoMap;
// File type on the drive file system can be either a regular file or
// a hosted document.
enum DriveFileType {
REGULAR_FILE,
HOSTED_DOCUMENT,
};
// The root directory name used for the Google Drive file system tree. The
// name is used in URLs for the file manager, hence user-visible.
const base::FilePath::CharType kDriveRootDirectory[] =
FILE_PATH_LITERAL("drive");
// This should be incremented when incompatibility change is made in
// drive.proto.
const int32 kProtoVersion = 2;
// Callback similar to FileOperationCallback but with a given |file_path|.
// Used for operations that change a file path like moving files.
typedef base::Callback<void(DriveFileError error,
const base::FilePath& file_path)>
FileMoveCallback;
// Used to get entry info from the file system.
// If |error| is not DRIVE_FILE_OK, |entry_info| is set to NULL.
typedef base::Callback<void(DriveFileError error,
scoped_ptr<DriveEntryProto> entry_proto)>
GetEntryInfoCallback;
typedef base::Callback<void(DriveFileError error,
scoped_ptr<DriveEntryProtoVector> entries)>
ReadDirectoryCallback;
// Used to get entry info from the file system, with the Drive file path.
// If |error| is not DRIVE_FILE_OK, |entry_proto| is set to NULL.
//
// |drive_file_path| parameter is provided as DriveEntryProto does not contain
// the Drive file path (i.e. only contains the base name without parent
// directory names).
typedef base::Callback<void(DriveFileError error,
const base::FilePath& drive_file_path,
scoped_ptr<DriveEntryProto> entry_proto)>
GetEntryInfoWithFilePathCallback;
// Used to get a set of changed directories for feed processing.
typedef base::Callback<void(const std::set<base::FilePath>&)>
GetChildDirectoriesCallback;
typedef base::Callback<void(int64)> GetChangestampCallback;
// This is a part of EntryInfoPairResult.
struct EntryInfoResult {
EntryInfoResult();
~EntryInfoResult();
base::FilePath path;
DriveFileError error;
scoped_ptr<DriveEntryProto> proto;
};
// The result of GetEntryInfoPairCallback(). Used to get a pair of entries
// in one function call.
struct EntryInfoPairResult {
EntryInfoPairResult();
~EntryInfoPairResult();
EntryInfoResult first;
EntryInfoResult second; // Only filled if the first entry is found.
};
// Used to receive the result from GetEntryInfoPairCallback().
typedef base::Callback<void(scoped_ptr<EntryInfoPairResult> pair_result)>
GetEntryInfoPairCallback;
// Interface class for resource metadata storage.
class DriveResourceMetadataInterface {
public:
virtual ~DriveResourceMetadataInterface() {}
// Largest change timestamp that was the source of content for the current
// state of the root directory.
virtual void GetLargestChangestamp(
const GetChangestampCallback& callback) = 0;
virtual void SetLargestChangestamp(int64 value,
const FileOperationCallback& callback) = 0;
// Adds |entry| to directory with path |directory_path| and invoke the
// callback asynchronously. |callback| must not be null.
virtual void AddEntryToDirectory(const base::FilePath& directory_path,
scoped_ptr<google_apis::ResourceEntry> entry,
const FileMoveCallback& callback) = 0;
// Adds |entry_proto| to the metadata tree, based on its parent_resource_id.
// |callback| must not be null.
virtual void AddEntryToParent(const DriveEntryProto& entry_proto,
const FileMoveCallback& callback) = 0;
// Moves entry specified by |file_path| to the directory specified by
// |directory_path| and calls the callback asynchronously. Removes the entry
// from the previous parent. |callback| must not be null.
virtual void MoveEntryToDirectory(const base::FilePath& file_path,
const base::FilePath& directory_path,
const FileMoveCallback& callback) = 0;
// Renames entry specified by |file_path| with the new name |new_name| and
// calls |callback| asynchronously. |callback| must not be null.
virtual void RenameEntry(const base::FilePath& file_path,
const base::FilePath::StringType& new_name,
const FileMoveCallback& callback) = 0;
// Removes entry with |resource_id| from its parent. Calls |callback| with the
// path of the parent directory. |callback| must not be null.
virtual void RemoveEntryFromParent(const std::string& resource_id,
const FileMoveCallback& callback) = 0;
// Finds an entry (a file or a directory) by |resource_id|.
// |callback| must not be null.
virtual void GetEntryInfoByResourceId(
const std::string& resource_id,
const GetEntryInfoWithFilePathCallback& callback) = 0;
// Finds an entry (a file or a directory) by |file_path|.
// |callback| must not be null.
virtual void GetEntryInfoByPath(const base::FilePath& file_path,
const GetEntryInfoCallback& callback) = 0;
// Finds and reads a directory by |file_path|.
// |callback| must not be null.
virtual void ReadDirectoryByPath(const base::FilePath& file_path,
const ReadDirectoryCallback& callback) = 0;
// Similar to GetEntryInfoByPath() but this function finds a pair of
// entries by |first_path| and |second_path|. If the entry for
// |first_path| is not found, this function does not try to get the
// entry of |second_path|. |callback| must not be null.
virtual void GetEntryInfoPairByPaths(
const base::FilePath& first_path,
const base::FilePath& second_path,
const GetEntryInfoPairCallback& callback) = 0;
// Refreshes a drive entry with the same resource id as |entry_proto|.
// |callback| is run with the error, file path and proto of the entry.
// |callback| must not be null.
virtual void RefreshEntry(
const DriveEntryProto& entry_proto,
const GetEntryInfoWithFilePathCallback& callback) = 0;
// Removes all child files of |directory| and replaces them with
// |entry_proto_map|. |callback| is called with the directory path.
// |callback| must not be null.
virtual void RefreshDirectory(const std::string& directory_resource_id,
const DriveEntryProtoMap& entry_proto_map,
const FileMoveCallback& callback) = 0;
// Recursively get child directories of entry pointed to by |resource_id|.
virtual void GetChildDirectories(
const std::string& resource_id,
const GetChildDirectoriesCallback& changed_dirs_callback) = 0;
// Removes all files/directories under root (not including root).
virtual void RemoveAll(const base::Closure& callback) = 0;
protected:
DriveResourceMetadataInterface() {}
};
// Storage for Drive Metadata.
class DriveResourceMetadata : public DriveResourceMetadataInterface {
public:
// Map of resource id and serialized DriveEntry.
typedef std::map<std::string, std::string> SerializedMap;
// Map of resource id strings to DriveEntry*.
typedef std::map<std::string, DriveEntry*> ResourceMap;
// |root_resource_id| is the resource id for the root directory.
explicit DriveResourceMetadata(const std::string& root_resource_id);
virtual ~DriveResourceMetadata();
// Last time when we dumped serialized file system to disk.
const base::Time& last_serialized() const { return last_serialized_; }
void set_last_serialized(const base::Time& time) { last_serialized_ = time; }
// Size of serialized file system on disk in bytes.
size_t serialized_size() const { return serialized_size_; }
void set_serialized_size(size_t size) { serialized_size_ = size; }
// True if the file system feed is loaded from the cache or from the server.
bool loaded() const { return loaded_; }
void set_loaded(bool loaded) { loaded_ = loaded; }
// DriveResourceMetadataInterface implementation.
virtual void GetLargestChangestamp(
const GetChangestampCallback& callback) OVERRIDE;
virtual void SetLargestChangestamp(
int64 value,
const FileOperationCallback& callback) OVERRIDE;
virtual void AddEntryToDirectory(
const base::FilePath& directory_path,
scoped_ptr<google_apis::ResourceEntry> entry,
const FileMoveCallback& callback) OVERRIDE;
virtual void AddEntryToParent(const DriveEntryProto& entry_proto,
const FileMoveCallback& callback) OVERRIDE;
virtual void MoveEntryToDirectory(const base::FilePath& file_path,
const base::FilePath& directory_path,
const FileMoveCallback& callback) OVERRIDE;
virtual void RenameEntry(const base::FilePath& file_path,
const base::FilePath::StringType& new_name,
const FileMoveCallback& callback) OVERRIDE;
virtual void RemoveEntryFromParent(const std::string& resource_id,
const FileMoveCallback& callback) OVERRIDE;
virtual void GetEntryInfoByResourceId(
const std::string& resource_id,
const GetEntryInfoWithFilePathCallback& callback) OVERRIDE;
virtual void GetEntryInfoByPath(
const base::FilePath& file_path,
const GetEntryInfoCallback& callback) OVERRIDE;
virtual void ReadDirectoryByPath(
const base::FilePath& file_path,
const ReadDirectoryCallback& callback) OVERRIDE;
virtual void GetEntryInfoPairByPaths(
const base::FilePath& first_path,
const base::FilePath& second_path,
const GetEntryInfoPairCallback& callback) OVERRIDE;
virtual void RefreshEntry(
const DriveEntryProto& entry_proto,
const GetEntryInfoWithFilePathCallback& callback) OVERRIDE;
virtual void RefreshDirectory(const std::string& directory_resource_id,
const DriveEntryProtoMap& entry_proto_map,
const FileMoveCallback& callback) OVERRIDE;
virtual void GetChildDirectories(
const std::string& resource_id,
const GetChildDirectoriesCallback& changed_dirs_callback) OVERRIDE;
virtual void RemoveAll(const base::Closure& callback) OVERRIDE;
// Serializes/Parses to/from string via proto classes.
void SerializeToString(std::string* serialized_proto) const;
bool ParseFromString(const std::string& serialized_proto);
// Restores from and saves to database, calling |callback| asynchronously.
// |callback| must not be null.
void InitFromDB(const base::FilePath& db_path,
base::SequencedTaskRunner* blocking_task_runner,
const FileOperationCallback& callback);
void SaveToDB();
// TODO(achuith): Remove all DriveEntry based methods. crbug.com/127856.
// Creates DriveEntry from proto.
scoped_ptr<DriveEntry> CreateDriveEntryFromProto(
const DriveEntryProto& entry_proto);
// Creates a DriveFile instance.
scoped_ptr<DriveFile> CreateDriveFile();
// Creates a DriveDirectory instance.
scoped_ptr<DriveDirectory> CreateDriveDirectory();
// Adds the entry to resource map. Returns false if an entry with the same
// resource_id exists.
bool AddEntryToResourceMap(DriveEntry* entry);
// Removes the entry from resource map.
void RemoveEntryFromResourceMap(const std::string& resource_id);
// Returns the DriveEntry* with the corresponding |resource_id|.
// TODO(satorux): Remove this in favor of GetEntryInfoByResourceId()
// but can be difficult. See crbug.com/137374
DriveEntry* GetEntryByResourceId(const std::string& resource_id);
private:
// Initializes the resource map using serialized_resources fetched from the
// database.
// |callback| must not be null.
void InitResourceMap(CreateDBParams* create_params,
const FileOperationCallback& callback);
// Clears root_ and the resource map.
void ClearRoot();
// Creates DriveEntry from serialized string.
scoped_ptr<DriveEntry> CreateDriveEntryFromProtoString(
const std::string& serialized_proto);
// Continues with GetEntryInfoPairByPaths after the first DriveEntry has been
// asynchronously fetched. This fetches the second DriveEntry only if the
// first was found.
void GetEntryInfoPairByPathsAfterGetFirst(
const base::FilePath& first_path,
const base::FilePath& second_path,
const GetEntryInfoPairCallback& callback,
DriveFileError error,
scoped_ptr<DriveEntryProto> entry_proto);
// Continues with GetIntroInfoPairByPaths after the second DriveEntry has been
// asynchronously fetched.
void GetEntryInfoPairByPathsAfterGetSecond(
const base::FilePath& second_path,
const GetEntryInfoPairCallback& callback,
scoped_ptr<EntryInfoPairResult> result,
DriveFileError error,
scoped_ptr<DriveEntryProto> entry_proto);
// Searches for |file_path| synchronously.
DriveEntry* FindEntryByPathSync(const base::FilePath& file_path);
// Helper function to add |entry_proto| as a child to |directory|.
// |callback| must not be null.
void AddEntryToDirectoryInternal(DriveDirectory* directory,
const DriveEntryProto& entry_proto,
const FileMoveCallback& callback);
// Helper function to get a parent directory given |parent_resource_id|.
// Returns root if |parent_resource_id| is empty. Returns NULL if
// |parent_resource_id| is not empty and the corresponding entry is not a
// directory.
DriveDirectory* GetParent(const std::string& parent_resource_id);
// Private data members.
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
scoped_ptr<ResourceMetadataDB> resource_metadata_db_;
ResourceMap resource_map_;
scoped_ptr<DriveDirectory> root_; // Stored in the serialized proto.
base::Time last_serialized_;
size_t serialized_size_;
int64 largest_changestamp_; // Stored in the serialized proto.
bool loaded_;
// This should remain the last member so it'll be destroyed first and
// invalidate its weak pointers before other members are destroyed.
base::WeakPtrFactory<DriveResourceMetadata> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DriveResourceMetadata);
};
} // namespace drive
#endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_RESOURCE_METADATA_H_
|