OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 20 matching lines...) Expand all Loading... |
31 class GDataFile; | 31 class GDataFile; |
32 class GDataDirectory; | 32 class GDataDirectory; |
33 class GDataDirectoryService; | 33 class GDataDirectoryService; |
34 class ResourceMetadataDB; | 34 class ResourceMetadataDB; |
35 | 35 |
36 class GDataEntryProto; | 36 class GDataEntryProto; |
37 class GDataDirectoryProto; | 37 class GDataDirectoryProto; |
38 class GDataRootDirectoryProto; | 38 class GDataRootDirectoryProto; |
39 class PlatformFileInfoProto; | 39 class PlatformFileInfoProto; |
40 | 40 |
41 // Callback type used to get result of file search. | |
42 // If |error| is not PLATFORM_FILE_OK, |entry| is set to NULL. | |
43 typedef base::Callback<void(GDataFileError error, GDataEntry* entry)> | |
44 FindEntryCallback; | |
45 | |
46 // The root directory content origin. | |
47 enum ContentOrigin { | |
48 UNINITIALIZED, | |
49 // Content is currently loading from somewhere. Needs to wait. | |
50 INITIALIZING, | |
51 // Content is initialized, but during refreshing. | |
52 REFRESHING, | |
53 // Content is initialized from disk cache. | |
54 FROM_CACHE, | |
55 // Content is initialized from the direct server response. | |
56 FROM_SERVER, | |
57 }; | |
58 | |
59 // File type on the gdata file system can be either a regular file or | 41 // File type on the gdata file system can be either a regular file or |
60 // a hosted document. | 42 // a hosted document. |
61 enum GDataFileType { | 43 enum GDataFileType { |
62 REGULAR_FILE, | 44 REGULAR_FILE, |
63 HOSTED_DOCUMENT, | 45 HOSTED_DOCUMENT, |
64 }; | 46 }; |
65 | 47 |
66 // The root directory name used for the Google Drive file system tree. The | |
67 // name is used in URLs for the file manager, hence user-visible. | |
68 const FilePath::CharType kGDataRootDirectory[] = FILE_PATH_LITERAL("drive"); | |
69 | |
70 // The resource ID for the root directory is defined in the spec: | |
71 // https://developers.google.com/google-apps/documents-list/ | |
72 const char kGDataRootDirectoryResourceId[] = "folder:root"; | |
73 | |
74 // This should be incremented when incompatibility change is made in | |
75 // gdata.proto. | |
76 const int32 kProtoVersion = 2; | |
77 | |
78 // Used for file operations like removing files. | |
79 typedef base::Callback<void(GDataFileError error)> | |
80 FileOperationCallback; | |
81 | |
82 // Callback similar to FileOperationCallback but with a given |file_path|. | |
83 // Used for operations that change a file path like moving files. | |
84 typedef base::Callback<void(GDataFileError error, | |
85 const FilePath& file_path)> | |
86 FileMoveCallback; | |
87 | |
88 // Used to get entry info from the file system. | |
89 // If |error| is not GDATA_FILE_OK, |entry_info| is set to NULL. | |
90 typedef base::Callback<void(GDataFileError error, | |
91 scoped_ptr<GDataEntryProto> entry_proto)> | |
92 GetEntryInfoCallback; | |
93 | |
94 // Used to read a directory from the file system. | 48 // Used to read a directory from the file system. |
95 // If |error| is not GDATA_FILE_OK, |entries| is set to NULL. | 49 // If |error| is not GDATA_FILE_OK, |entries| is set to NULL. |
96 // |entries| are contents, both files and directories, of the directory. | 50 // |entries| are contents, both files and directories, of the directory. |
97 typedef std::vector<GDataEntryProto> GDataEntryProtoVector; | 51 typedef std::vector<GDataEntryProto> GDataEntryProtoVector; |
98 typedef base::Callback<void(GDataFileError error, | |
99 scoped_ptr<GDataEntryProtoVector> entries)> | |
100 ReadDirectoryCallback; | |
101 | |
102 // This is a part of EntryInfoPairResult. | |
103 struct EntryInfoResult { | |
104 EntryInfoResult(); | |
105 ~EntryInfoResult(); | |
106 | |
107 FilePath path; | |
108 GDataFileError error; | |
109 scoped_ptr<GDataEntryProto> proto; | |
110 }; | |
111 | |
112 // The result of GetEntryInfoPairCallback(). Used to get a pair of entries | |
113 // in one function call. | |
114 struct EntryInfoPairResult { | |
115 EntryInfoPairResult(); | |
116 ~EntryInfoPairResult(); | |
117 | |
118 EntryInfoResult first; | |
119 EntryInfoResult second; // Only filled if the first entry is found. | |
120 }; | |
121 | |
122 // Used to receive the result from GetEntryInfoPairCallback(). | |
123 typedef base::Callback<void(scoped_ptr<EntryInfoPairResult> pair_result)> | |
124 GetEntryInfoPairCallback; | |
125 | 52 |
126 // Base class for representing files and directories in gdata virtual file | 53 // Base class for representing files and directories in gdata virtual file |
127 // system. | 54 // system. |
128 class GDataEntry { | 55 class GDataEntry { |
129 public: | 56 public: |
130 virtual ~GDataEntry(); | 57 virtual ~GDataEntry(); |
131 | 58 |
132 virtual GDataFile* AsGDataFile(); | 59 virtual GDataFile* AsGDataFile(); |
133 virtual GDataDirectory* AsGDataDirectory(); | 60 virtual GDataDirectory* AsGDataDirectory(); |
134 | 61 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 void RemoveChildFiles(); | 297 void RemoveChildFiles(); |
371 void RemoveChildDirectories(); | 298 void RemoveChildDirectories(); |
372 | 299 |
373 // Collection of children GDataEntry items. | 300 // Collection of children GDataEntry items. |
374 GDataFileCollection child_files_; | 301 GDataFileCollection child_files_; |
375 GDataDirectoryCollection child_directories_; | 302 GDataDirectoryCollection child_directories_; |
376 | 303 |
377 DISALLOW_COPY_AND_ASSIGN(GDataDirectory); | 304 DISALLOW_COPY_AND_ASSIGN(GDataDirectory); |
378 }; | 305 }; |
379 | 306 |
380 // TODO(achuith,hashimoto,satorux): Move this to a separate file. | |
381 // crbug.com/140317. | |
382 // Class to handle GDataEntry* lookups, add/remove GDataEntry*. | |
383 class GDataDirectoryService { | |
384 public: | |
385 // Callback for GetEntryByResourceIdAsync. | |
386 typedef base::Callback<void(GDataEntry* entry)> GetEntryByResourceIdCallback; | |
387 | |
388 // Map of resource id and serialized GDataEntry. | |
389 typedef std::map<std::string, std::string> SerializedMap; | |
390 // Map of resource id strings to GDataEntry*. | |
391 typedef std::map<std::string, GDataEntry*> ResourceMap; | |
392 | |
393 GDataDirectoryService(); | |
394 ~GDataDirectoryService(); | |
395 | |
396 GDataDirectory* root() { return root_.get(); } | |
397 | |
398 // Last time when we dumped serialized file system to disk. | |
399 const base::Time& last_serialized() const { return last_serialized_; } | |
400 void set_last_serialized(const base::Time& time) { last_serialized_ = time; } | |
401 // Size of serialized file system on disk in bytes. | |
402 const size_t serialized_size() const { return serialized_size_; } | |
403 void set_serialized_size(size_t size) { serialized_size_ = size; } | |
404 | |
405 // Largest change timestamp that was the source of content for the current | |
406 // state of the root directory. | |
407 const int64 largest_changestamp() const { return largest_changestamp_; } | |
408 void set_largest_changestamp(int64 value) { largest_changestamp_ = value; } | |
409 | |
410 // The root directory content origin. | |
411 const ContentOrigin origin() const { return origin_; } | |
412 void set_origin(ContentOrigin value) { origin_ = value; } | |
413 | |
414 // Creates a GDataEntry from a DocumentEntry. | |
415 GDataEntry* FromDocumentEntry(DocumentEntry* doc); | |
416 | |
417 // Creates a GDataFile instance. | |
418 GDataFile* CreateGDataFile(); | |
419 | |
420 // Creates a GDataDirectory instance. | |
421 GDataDirectory* CreateGDataDirectory(); | |
422 | |
423 // Sets root directory resource id and initialize the root entry. | |
424 void InitializeRootEntry(const std::string& root_id); | |
425 | |
426 // Add |new entry| to |directory| and invoke the callback asynchronously. | |
427 // |callback| may not be null. | |
428 // TODO(achuith,satorux): Use GDataEntryProto instead for new_entry. | |
429 // crbug.com/142048 | |
430 void AddEntryToDirectory(GDataDirectory* directory, | |
431 GDataEntry* new_entry, | |
432 const FileMoveCallback& callback); | |
433 | |
434 // Moves |entry| to |directory_path| asynchronously. Removes entry from | |
435 // previous parent. Must be called on UI thread. |callback| is called on the | |
436 // UI thread. |callback| may not be null. | |
437 void MoveEntryToDirectory(const FilePath& directory_path, | |
438 GDataEntry* entry, | |
439 const FileMoveCallback& callback); | |
440 | |
441 // Removes |entry| from its parent. Calls |callback| with the path of the | |
442 // parent directory. |callback| may not be null. | |
443 void RemoveEntryFromParent(GDataEntry* entry, | |
444 const FileMoveCallback& callback); | |
445 | |
446 // Adds the entry to resource map. | |
447 void AddEntryToResourceMap(GDataEntry* entry); | |
448 | |
449 // Removes the entry from resource map. | |
450 void RemoveEntryFromResourceMap(GDataEntry* entry); | |
451 | |
452 // Searches for |file_path| synchronously. | |
453 // TODO(satorux): Replace this with an async version crbug.com/137160 | |
454 GDataEntry* FindEntryByPathSync(const FilePath& file_path); | |
455 | |
456 // Searches for |file_path| synchronously, and runs |callback|. | |
457 // TODO(satorux): Replace this with an async version crbug.com/137160 | |
458 void FindEntryByPathAndRunSync(const FilePath& file_path, | |
459 const FindEntryCallback& callback); | |
460 | |
461 // Returns the GDataEntry* with the corresponding |resource_id|. | |
462 // TODO(achuith): Get rid of this in favor of async version crbug.com/13957. | |
463 GDataEntry* GetEntryByResourceId(const std::string& resource_id); | |
464 | |
465 // Returns the GDataEntry* in the callback with the corresponding | |
466 // |resource_id|. TODO(achuith): Rename this to GetEntryByResourceId. | |
467 void GetEntryByResourceIdAsync(const std::string& resource_id, | |
468 const GetEntryByResourceIdCallback& callback); | |
469 | |
470 // Finds an entry (a file or a directory) by |file_path|. | |
471 // | |
472 // Must be called from UI thread. |callback| is run on UI thread. | |
473 void GetEntryInfoByPath(const FilePath& file_path, | |
474 const GetEntryInfoCallback& callback); | |
475 | |
476 // Finds and reads a directory by |file_path|. | |
477 // | |
478 // Must be called from UI thread. |callback| is run on UI thread. | |
479 void ReadDirectoryByPath(const FilePath& file_path, | |
480 const ReadDirectoryCallback& callback); | |
481 | |
482 // Similar to GetEntryInfoByPath() but this function finds a pair of | |
483 // entries by |first_path| and |second_path|. If the entry for | |
484 // |first_path| is not found, this function does not try to get the | |
485 // entry of |second_path|. | |
486 // | |
487 // Must be called from UI thread. |callback| is run on UI thread. | |
488 void GetEntryInfoPairByPaths( | |
489 const FilePath& first_path, | |
490 const FilePath& second_path, | |
491 const GetEntryInfoPairCallback& callback); | |
492 | |
493 // Replaces file entry with the same resource id as |fresh_file| with its | |
494 // fresh value |fresh_file|. | |
495 void RefreshFile(scoped_ptr<GDataFile> fresh_file); | |
496 | |
497 // Removes all child files of |directory| and replace with file_map. | |
498 // |callback| is called with the directory path. |callback| may not be null. | |
499 void RefreshDirectory(const std::string& directory_resource_id, | |
500 const ResourceMap& file_map, | |
501 const FileMoveCallback& callback); | |
502 | |
503 // Serializes/Parses to/from string via proto classes. | |
504 void SerializeToString(std::string* serialized_proto) const; | |
505 bool ParseFromString(const std::string& serialized_proto); | |
506 | |
507 // Restores from and saves to database. | |
508 void InitFromDB(const FilePath& db_path, | |
509 base::SequencedTaskRunner* blocking_task_runner, | |
510 const FileOperationCallback& callback); | |
511 void SaveToDB(); | |
512 | |
513 private: | |
514 // Initializes the resource map using serialized_resources fetched from the | |
515 // database. | |
516 void InitResourceMap(CreateDBParams* create_params, | |
517 const FileOperationCallback& callback); | |
518 | |
519 // Clears root_ and the resource map. | |
520 void ClearRoot(); | |
521 | |
522 // Creates GDataEntry from serialized string. | |
523 scoped_ptr<GDataEntry> FromProtoString( | |
524 const std::string& serialized_proto); | |
525 | |
526 // Continues with GetEntryInfoPairByPaths after the first GDataEntry has been | |
527 // asynchronously fetched. This fetches the second GDataEntry only if the | |
528 // first was found. | |
529 void GetEntryInfoPairByPathsAfterGetFirst( | |
530 const FilePath& first_path, | |
531 const FilePath& second_path, | |
532 const GetEntryInfoPairCallback& callback, | |
533 GDataFileError error, | |
534 scoped_ptr<GDataEntryProto> entry_proto); | |
535 | |
536 // Continues with GetIntroInfoPairByPaths after the second GDataEntry has been | |
537 // asynchronously fetched. | |
538 void GetEntryInfoPairByPathsAfterGetSecond( | |
539 const FilePath& second_path, | |
540 const GetEntryInfoPairCallback& callback, | |
541 scoped_ptr<EntryInfoPairResult> result, | |
542 GDataFileError error, | |
543 scoped_ptr<GDataEntryProto> entry_proto); | |
544 | |
545 // These internal functions need friend access to private GDataDirectory | |
546 // methods. | |
547 // Replaces file entry |old_entry| with its fresh value |fresh_file|. | |
548 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, | |
549 GDataEntry* old_entry); | |
550 | |
551 // Removes all child files of |directory| and replace with file_map. | |
552 // |callback| may not be null. | |
553 static void RefreshDirectoryInternal(const ResourceMap& file_map, | |
554 const FileMoveCallback& callback, | |
555 GDataEntry* directory_entry); | |
556 | |
557 | |
558 // Private data members. | |
559 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
560 scoped_ptr<ResourceMetadataDB> directory_service_db_; | |
561 | |
562 ResourceMap resource_map_; | |
563 | |
564 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. | |
565 | |
566 base::Time last_serialized_; | |
567 size_t serialized_size_; | |
568 int64 largest_changestamp_; // Stored in the serialized proto. | |
569 ContentOrigin origin_; | |
570 | |
571 // This should remain the last member so it'll be destroyed first and | |
572 // invalidate its weak pointers before other members are destroyed. | |
573 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; | |
574 | |
575 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); | |
576 }; | |
577 | |
578 } // namespace gdata | 307 } // namespace gdata |
579 | 308 |
580 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 309 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
OLD | NEW |