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 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "chrome/browser/chromeos/gdata/gdata_params.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_params.h" |
19 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" |
20 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" | 20 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" |
21 #include "chrome/browser/profiles/profile_keyed_service.h" | 21 #include "chrome/browser/profiles/profile_keyed_service.h" |
22 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 22 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
23 | 23 |
24 namespace base { | |
25 class SequencedTaskRunner; | |
26 } | |
27 | |
24 namespace gdata { | 28 namespace gdata { |
25 | 29 |
30 struct CreateDBParams; | |
26 class GDataFile; | 31 class GDataFile; |
27 class GDataDirectory; | 32 class GDataDirectory; |
28 class GDataDirectoryService; | 33 class GDataDirectoryService; |
34 class GDataDirectoryServiceDB; | |
29 | 35 |
30 class GDataEntryProto; | 36 class GDataEntryProto; |
31 class GDataDirectoryProto; | 37 class GDataDirectoryProto; |
32 class GDataRootDirectoryProto; | 38 class GDataRootDirectoryProto; |
33 class PlatformFileInfoProto; | 39 class PlatformFileInfoProto; |
34 | 40 |
35 // The root directory content origin. | 41 // The root directory content origin. |
36 enum ContentOrigin { | 42 enum ContentOrigin { |
37 UNINITIALIZED, | 43 UNINITIALIZED, |
38 // Content is currently loading from somewhere. Needs to wait. | 44 // Content is currently loading from somewhere. Needs to wait. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 const GDataFile* AsGDataFileConst() const; | 88 const GDataFile* AsGDataFileConst() const; |
83 const GDataDirectory* AsGDataDirectoryConst() const; | 89 const GDataDirectory* AsGDataDirectoryConst() const; |
84 | 90 |
85 // Converts DocumentEntry into GDataEntry. | 91 // Converts DocumentEntry into GDataEntry. |
86 static GDataEntry* FromDocumentEntry( | 92 static GDataEntry* FromDocumentEntry( |
87 GDataDirectory* parent, | 93 GDataDirectory* parent, |
88 DocumentEntry* doc, | 94 DocumentEntry* doc, |
89 GDataDirectoryService* directory_service); | 95 GDataDirectoryService* directory_service); |
90 | 96 |
91 // Serialize/Parse to/from string via proto classes. | 97 // Serialize/Parse to/from string via proto classes. |
92 // TODO(achuith): Correctly set up parent_ and root_ links in | |
93 // FromProtoString. | |
94 void SerializeToString(std::string* serialized_proto) const; | 98 void SerializeToString(std::string* serialized_proto) const; |
99 // TODO(achuith): Get rid of this. crbug.com/140314. | |
95 static scoped_ptr<GDataEntry> FromProtoString( | 100 static scoped_ptr<GDataEntry> FromProtoString( |
96 const std::string& serialized_proto); | 101 const std::string& serialized_proto); |
97 | 102 |
98 // Converts the proto representation to the platform file. | 103 // Converts the proto representation to the platform file. |
99 static void ConvertProtoToPlatformFileInfo( | 104 static void ConvertProtoToPlatformFileInfo( |
100 const PlatformFileInfoProto& proto, | 105 const PlatformFileInfoProto& proto, |
101 base::PlatformFileInfo* file_info); | 106 base::PlatformFileInfo* file_info); |
102 | 107 |
103 // Converts the platform file info to the proto representation. | 108 // Converts the platform file info to the proto representation. |
104 static void ConvertPlatformFileInfoToProto( | 109 static void ConvertPlatformFileInfoToProto( |
105 const base::PlatformFileInfo& file_info, | 110 const base::PlatformFileInfo& file_info, |
106 PlatformFileInfoProto* proto); | 111 PlatformFileInfoProto* proto); |
107 | 112 |
108 // Converts to/from proto. Only handles the common part (i.e. does not | 113 // Converts to/from proto. Only handles the common part (i.e. does not |
109 // touch |file_specific_info| and |directory_specific_info|. | 114 // touch |file_specific_info|). |
110 bool FromProto(const GDataEntryProto& proto) WARN_UNUSED_RESULT; | 115 bool FromProto(const GDataEntryProto& proto) WARN_UNUSED_RESULT; |
111 void ToProto(GDataEntryProto* proto) const; | 116 void ToProto(GDataEntryProto* proto) const; |
112 | 117 |
113 // Similar to ToProto() but this fills in |file_specific_info| and | 118 // Similar to ToProto() but this fills in |file_specific_info| and |
114 // |directory_specific_info| based on the actual type of the entry. | 119 // |directory_specific_info| based on the actual type of the entry. |
115 // Used to obtain full metadata of a file or a directory as | 120 // Used to obtain full metadata of a file or a directory as |
116 // GDataEntryProto. | 121 // GDataEntryProto. |
117 void ToProtoFull(GDataEntryProto* proto) const; | 122 void ToProtoFull(GDataEntryProto* proto) const; |
118 | 123 |
119 // Escapes forward slashes from file names with magic unicode character | 124 // Escapes forward slashes from file names with magic unicode character |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 // entry instance. | 334 // entry instance. |
330 bool RemoveChild(GDataEntry* entry); | 335 bool RemoveChild(GDataEntry* entry); |
331 | 336 |
332 // Collection of children GDataEntry items. | 337 // Collection of children GDataEntry items. |
333 GDataFileCollection child_files_; | 338 GDataFileCollection child_files_; |
334 GDataDirectoryCollection child_directories_; | 339 GDataDirectoryCollection child_directories_; |
335 | 340 |
336 DISALLOW_COPY_AND_ASSIGN(GDataDirectory); | 341 DISALLOW_COPY_AND_ASSIGN(GDataDirectory); |
337 }; | 342 }; |
338 | 343 |
344 // TODO(achuith,hashimoto,satorux): Move this to a separate file. | |
345 // crbug.com/140317. | |
339 // Class to handle GDataEntry* lookups, add/remove GDataEntry*. | 346 // Class to handle GDataEntry* lookups, add/remove GDataEntry*. |
340 class GDataDirectoryService { | 347 class GDataDirectoryService { |
341 public: | 348 public: |
342 // Callback for GetEntryByResourceIdAsync. | 349 // Callback for GetEntryByResourceIdAsync. |
343 typedef base::Callback<void(GDataEntry* entry)> GetEntryByResourceIdCallback; | 350 typedef base::Callback<void(GDataEntry* entry)> GetEntryByResourceIdCallback; |
344 | 351 |
352 // Map of resource id and serialized GDataEntry. | |
353 typedef std::map<std::string, std::string> SerializedMap; | |
354 | |
345 GDataDirectoryService(); | 355 GDataDirectoryService(); |
346 ~GDataDirectoryService(); | 356 ~GDataDirectoryService(); |
347 | 357 |
348 GDataDirectory* root() { return root_.get(); } | 358 GDataDirectory* root() { return root_.get(); } |
349 | 359 |
350 // Last time when we dumped serialized file system to disk. | 360 // Last time when we dumped serialized file system to disk. |
351 const base::Time& last_serialized() const { return last_serialized_; } | 361 const base::Time& last_serialized() const { return last_serialized_; } |
352 void set_last_serialized(const base::Time& time) { last_serialized_ = time; } | 362 void set_last_serialized(const base::Time& time) { last_serialized_ = time; } |
353 // Size of serialized file system on disk in bytes. | 363 // Size of serialized file system on disk in bytes. |
354 const size_t serialized_size() const { return serialized_size_; } | 364 const size_t serialized_size() const { return serialized_size_; } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 void RefreshFile(scoped_ptr<GDataFile> fresh_file); | 408 void RefreshFile(scoped_ptr<GDataFile> fresh_file); |
399 | 409 |
400 // Replaces file entry |old_entry| with its fresh value |fresh_file|. | 410 // Replaces file entry |old_entry| with its fresh value |fresh_file|. |
401 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, | 411 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, |
402 GDataEntry* old_entry); | 412 GDataEntry* old_entry); |
403 | 413 |
404 // Serializes/Parses to/from string via proto classes. | 414 // Serializes/Parses to/from string via proto classes. |
405 void SerializeToString(std::string* serialized_proto) const; | 415 void SerializeToString(std::string* serialized_proto) const; |
406 bool ParseFromString(const std::string& serialized_proto); | 416 bool ParseFromString(const std::string& serialized_proto); |
407 | 417 |
418 // Restores from and saves to database. | |
419 void InitFromDB(const FilePath& db_path, | |
420 base::SequencedTaskRunner* blocking_task_runner, | |
421 LoadRootFeedParams* load_params, | |
422 const base::Closure& callback); | |
423 void SaveToDB(); | |
424 | |
408 private: | 425 private: |
409 // A map table of file's resource string to its GDataFile* entry. | 426 // A map table of file's resource string to its GDataFile* entry. |
410 typedef std::map<std::string, GDataEntry*> ResourceMap; | 427 typedef std::map<std::string, GDataEntry*> ResourceMap; |
411 | 428 |
429 // Initializes the resource map using serialized_resources fetched from the | |
430 // database. | |
431 void InitResourceMap(CreateDBParams* create_params, | |
432 LoadRootFeedParams* load_params, | |
433 const base::Closure& callback); | |
434 | |
435 // Clears root_ and the resource map. | |
436 void ClearRoot(); | |
437 | |
438 // Creates GDataEntry from serialized string. | |
439 scoped_ptr<GDataEntry> FromProtoString( | |
440 const std::string& serialized_proto); | |
441 | |
442 // Private data members. | |
443 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
444 scoped_ptr<GDataDirectoryServiceDB> directory_service_db_; | |
445 | |
446 ResourceMap resource_map_; | |
447 | |
412 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. | 448 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. |
413 ResourceMap resource_map_; | |
414 | 449 |
415 base::Time last_serialized_; | 450 base::Time last_serialized_; |
416 size_t serialized_size_; | 451 size_t serialized_size_; |
417 int largest_changestamp_; // Stored in the serialized proto. | 452 int largest_changestamp_; // Stored in the serialized proto. |
418 ContentOrigin origin_; | 453 ContentOrigin origin_; |
419 | 454 |
455 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; | |
satorux1
2012/08/03 06:32:50
Please add
// Note: This should remain the las
achuithb
2012/08/03 19:15:59
Done.
| |
456 | |
420 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); | 457 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); |
421 }; | 458 }; |
422 | 459 |
423 } // namespace gdata | 460 } // namespace gdata |
424 | 461 |
425 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 462 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
OLD | NEW |