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 | 11 |
11 #include "base/callback.h" | 12 #include "base/callback.h" |
12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "base/platform_file.h" | 17 #include "base/platform_file.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "chrome/browser/chromeos/gdata/gdata_params.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_params.h" |
19 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | 20 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 | 319 |
319 // Collection of children files/directories. | 320 // Collection of children files/directories. |
320 const GDataFileCollection& child_files() const { return child_files_; } | 321 const GDataFileCollection& child_files() const { return child_files_; } |
321 const GDataDirectoryCollection& child_directories() const { | 322 const GDataDirectoryCollection& child_directories() const { |
322 return child_directories_; | 323 return child_directories_; |
323 } | 324 } |
324 | 325 |
325 private: | 326 private: |
326 // TODO(satorux): Remove the friend statements. crbug.com/139649 | 327 // TODO(satorux): Remove the friend statements. crbug.com/139649 |
327 friend class GDataDirectoryService; | 328 friend class GDataDirectoryService; |
328 friend class GDataFileSystem; | |
329 friend class GDataWapiFeedProcessor; | 329 friend class GDataWapiFeedProcessor; |
330 | 330 |
331 explicit GDataDirectory(GDataDirectoryService* directory_service); | 331 explicit GDataDirectory(GDataDirectoryService* directory_service); |
332 | 332 |
333 // Initializes from DocumentEntry. | 333 // Initializes from DocumentEntry. |
334 virtual void InitFromDocumentEntry(DocumentEntry* doc) OVERRIDE; | 334 virtual void InitFromDocumentEntry(DocumentEntry* doc) OVERRIDE; |
335 | 335 |
336 virtual GDataDirectory* AsGDataDirectory() OVERRIDE; | 336 virtual GDataDirectory* AsGDataDirectory() OVERRIDE; |
337 | 337 |
338 // Adds child file to the directory and takes over the ownership of |file| | 338 // Adds child file to the directory and takes over the ownership of |file| |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 // TODO(achuith,hashimoto,satorux): Move this to a separate file. | 376 // TODO(achuith,hashimoto,satorux): Move this to a separate file. |
377 // crbug.com/140317. | 377 // crbug.com/140317. |
378 // Class to handle GDataEntry* lookups, add/remove GDataEntry*. | 378 // Class to handle GDataEntry* lookups, add/remove GDataEntry*. |
379 class GDataDirectoryService { | 379 class GDataDirectoryService { |
380 public: | 380 public: |
381 // Callback for GetEntryByResourceIdAsync. | 381 // Callback for GetEntryByResourceIdAsync. |
382 typedef base::Callback<void(GDataEntry* entry)> GetEntryByResourceIdCallback; | 382 typedef base::Callback<void(GDataEntry* entry)> GetEntryByResourceIdCallback; |
383 | 383 |
384 // Map of resource id and serialized GDataEntry. | 384 // Map of resource id and serialized GDataEntry. |
385 typedef std::map<std::string, std::string> SerializedMap; | 385 typedef std::map<std::string, std::string> SerializedMap; |
386 // Map of resource id strings to GDataEntry*. | |
387 typedef std::map<std::string, GDataEntry*> ResourceMap; | |
386 | 388 |
387 GDataDirectoryService(); | 389 GDataDirectoryService(); |
388 ~GDataDirectoryService(); | 390 ~GDataDirectoryService(); |
389 | 391 |
390 GDataDirectory* root() { return root_.get(); } | 392 GDataDirectory* root() { return root_.get(); } |
391 | 393 |
392 // Last time when we dumped serialized file system to disk. | 394 // Last time when we dumped serialized file system to disk. |
393 const base::Time& last_serialized() const { return last_serialized_; } | 395 const base::Time& last_serialized() const { return last_serialized_; } |
394 void set_last_serialized(const base::Time& time) { last_serialized_ = time; } | 396 void set_last_serialized(const base::Time& time) { last_serialized_ = time; } |
395 // Size of serialized file system on disk in bytes. | 397 // Size of serialized file system on disk in bytes. |
(...skipping 14 matching lines...) Expand all Loading... | |
410 | 412 |
411 // Creates a GDataFile instance. | 413 // Creates a GDataFile instance. |
412 GDataFile* CreateGDataFile(); | 414 GDataFile* CreateGDataFile(); |
413 | 415 |
414 // Creates a GDataDirectory instance. | 416 // Creates a GDataDirectory instance. |
415 GDataDirectory* CreateGDataDirectory(); | 417 GDataDirectory* CreateGDataDirectory(); |
416 | 418 |
417 // Sets root directory resource id and initialize the root entry. | 419 // Sets root directory resource id and initialize the root entry. |
418 void InitializeRootEntry(const std::string& root_id); | 420 void InitializeRootEntry(const std::string& root_id); |
419 | 421 |
422 // Add |new entry| to |directory| and invoke the callback asynchronously. | |
423 // |callback| may not be null. | |
424 // TODO(achuith,satorux): Use GDataEntryProto instead for new_entry. | |
425 // crbug.com/142048 | |
426 void AddEntryToDirectory(GDataDirectory* directory, | |
427 GDataEntry* new_entry, | |
428 const FileMoveCallback& callback); | |
429 | |
420 // Move |entry| to |directory_path| asynchronously. Removes entry from | 430 // Move |entry| to |directory_path| asynchronously. Removes entry from |
421 // previous parent. Must be called on UI thread. |callback| is called on the | 431 // previous parent. Must be called on UI thread. |callback| is called on the |
422 // UI thread. | 432 // UI thread. |callback| may be null. |
423 void MoveEntryToDirectory(const FilePath& directory_path, | 433 void MoveEntryToDirectory(const FilePath& directory_path, |
424 GDataEntry* entry, | 434 GDataEntry* entry, |
425 const FileMoveCallback& callback); | 435 const FileMoveCallback& callback); |
426 | 436 |
437 // Remove |entry| from its parent. Calls |callback| with the path of the | |
438 // parent directory. |callback| can be null. | |
439 void RemoveEntryFromParent(GDataEntry* entry, | |
440 const FileMoveCallback& callback); | |
441 | |
427 // Adds the entry to resource map. | 442 // Adds the entry to resource map. |
428 void AddEntryToResourceMap(GDataEntry* entry); | 443 void AddEntryToResourceMap(GDataEntry* entry); |
429 | 444 |
430 // Removes the entry from resource map. | 445 // Removes the entry from resource map. |
431 void RemoveEntryFromResourceMap(GDataEntry* entry); | 446 void RemoveEntryFromResourceMap(GDataEntry* entry); |
432 | 447 |
433 // Searches for |file_path| synchronously. | 448 // Searches for |file_path| synchronously. |
434 // TODO(satorux): Replace this with an async version crbug.com/137160 | 449 // TODO(satorux): Replace this with an async version crbug.com/137160 |
435 GDataEntry* FindEntryByPathSync(const FilePath& file_path); | 450 GDataEntry* FindEntryByPathSync(const FilePath& file_path); |
436 | 451 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 // Must be called from UI thread. |callback| is run on UI thread. | 483 // Must be called from UI thread. |callback| is run on UI thread. |
469 void GetEntryInfoPairByPaths( | 484 void GetEntryInfoPairByPaths( |
470 const FilePath& first_path, | 485 const FilePath& first_path, |
471 const FilePath& second_path, | 486 const FilePath& second_path, |
472 const GetEntryInfoPairCallback& callback); | 487 const GetEntryInfoPairCallback& callback); |
473 | 488 |
474 // Replaces file entry with the same resource id as |fresh_file| with its | 489 // Replaces file entry with the same resource id as |fresh_file| with its |
475 // fresh value |fresh_file|. | 490 // fresh value |fresh_file|. |
476 void RefreshFile(scoped_ptr<GDataFile> fresh_file); | 491 void RefreshFile(scoped_ptr<GDataFile> fresh_file); |
477 | 492 |
478 // Replaces file entry |old_entry| with its fresh value |fresh_file|. | 493 // Remove all child files of |directory| and replace with file_map. |
479 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, | 494 // Note that |callback| is only called if the directory is successfully |
satorux1
2012/08/11 11:48:17
this behavior looks unusual. In other places, we c
achuithb
2012/08/13 22:18:10
Good point. Done.
| |
480 GDataEntry* old_entry); | 495 // refreshed. |callback| is called with the directory path. |
496 // |callback| may not be null. | |
497 void RefreshDirectory(const std::string& directory_resource_id, | |
498 const ResourceMap& file_map, | |
499 const FileMoveCallback& callback); | |
481 | 500 |
482 // Serializes/Parses to/from string via proto classes. | 501 // Serializes/Parses to/from string via proto classes. |
483 void SerializeToString(std::string* serialized_proto) const; | 502 void SerializeToString(std::string* serialized_proto) const; |
484 bool ParseFromString(const std::string& serialized_proto); | 503 bool ParseFromString(const std::string& serialized_proto); |
485 | 504 |
486 // Restores from and saves to database. | 505 // Restores from and saves to database. |
487 void InitFromDB(const FilePath& db_path, | 506 void InitFromDB(const FilePath& db_path, |
488 base::SequencedTaskRunner* blocking_task_runner, | 507 base::SequencedTaskRunner* blocking_task_runner, |
489 const FileOperationCallback& callback); | 508 const FileOperationCallback& callback); |
490 void SaveToDB(); | 509 void SaveToDB(); |
491 | 510 |
492 private: | 511 private: |
493 // A map table of file's resource string to its GDataFile* entry. | |
494 typedef std::map<std::string, GDataEntry*> ResourceMap; | |
495 | |
496 // Initializes the resource map using serialized_resources fetched from the | 512 // Initializes the resource map using serialized_resources fetched from the |
497 // database. | 513 // database. |
498 void InitResourceMap(CreateDBParams* create_params, | 514 void InitResourceMap(CreateDBParams* create_params, |
499 const FileOperationCallback& callback); | 515 const FileOperationCallback& callback); |
500 | 516 |
501 // Clears root_ and the resource map. | 517 // Clears root_ and the resource map. |
502 void ClearRoot(); | 518 void ClearRoot(); |
503 | 519 |
504 // Creates GDataEntry from serialized string. | 520 // Creates GDataEntry from serialized string. |
505 scoped_ptr<GDataEntry> FromProtoString( | 521 scoped_ptr<GDataEntry> FromProtoString( |
506 const std::string& serialized_proto); | 522 const std::string& serialized_proto); |
507 | 523 |
508 // ... | 524 // Continue with GetEntryInfoPairByPaths after the first GDataEntry has been |
525 // asynchronously fetched. This fetches the second GDataEntry only if the | |
526 // first was found. | |
satorux1
2012/08/11 11:48:17
Thanks!
achuithb
2012/08/13 22:18:10
Np.
| |
509 void GetEntryInfoPairByPathsAfterGetFirst( | 527 void GetEntryInfoPairByPathsAfterGetFirst( |
510 const FilePath& first_path, | 528 const FilePath& first_path, |
511 const FilePath& second_path, | 529 const FilePath& second_path, |
512 const GetEntryInfoPairCallback& callback, | 530 const GetEntryInfoPairCallback& callback, |
513 GDataFileError error, | 531 GDataFileError error, |
514 scoped_ptr<GDataEntryProto> entry_proto); | 532 scoped_ptr<GDataEntryProto> entry_proto); |
515 | 533 |
516 // ... | 534 // Continue with GetIntroInfoPairByPaths after the second GDataEntry has been |
535 // asynchronously fetched. | |
517 void GetEntryInfoPairByPathsAfterGetSecond( | 536 void GetEntryInfoPairByPathsAfterGetSecond( |
518 const FilePath& second_path, | 537 const FilePath& second_path, |
519 const GetEntryInfoPairCallback& callback, | 538 const GetEntryInfoPairCallback& callback, |
520 scoped_ptr<EntryInfoPairResult> result, | 539 scoped_ptr<EntryInfoPairResult> result, |
521 GDataFileError error, | 540 GDataFileError error, |
522 scoped_ptr<GDataEntryProto> entry_proto); | 541 scoped_ptr<GDataEntryProto> entry_proto); |
523 | 542 |
543 // These internal functions need friend access to private GDataDirectory | |
544 // methods. | |
545 // Replaces file entry |old_entry| with its fresh value |fresh_file|. | |
546 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, | |
547 GDataEntry* old_entry); | |
548 | |
549 // Remove all child files of |directory| and replace with file_map. | |
satorux1
2012/08/11 11:48:17
Removes
| |
550 // Note that |callback| is only called if the directory is successfully | |
551 // refreshed. |callback| may not be null. | |
satorux1
2012/08/11 11:48:17
Ditto. This behavior is worrisome.
achuithb
2012/08/13 22:18:10
Done.
| |
552 static void RefreshDirectoryInternal(const ResourceMap& file_map, | |
553 const FileMoveCallback& callback, | |
554 GDataEntry* directory_entry); | |
555 | |
556 | |
524 // Private data members. | 557 // Private data members. |
525 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 558 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
526 scoped_ptr<ResourceMetadataDB> directory_service_db_; | 559 scoped_ptr<ResourceMetadataDB> directory_service_db_; |
527 | 560 |
528 ResourceMap resource_map_; | 561 ResourceMap resource_map_; |
529 | 562 |
530 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. | 563 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. |
531 | 564 |
532 base::Time last_serialized_; | 565 base::Time last_serialized_; |
533 size_t serialized_size_; | 566 size_t serialized_size_; |
534 int64 largest_changestamp_; // Stored in the serialized proto. | 567 int64 largest_changestamp_; // Stored in the serialized proto. |
535 ContentOrigin origin_; | 568 ContentOrigin origin_; |
536 | 569 |
537 // This should remain the last member so it'll be destroyed first and | 570 // This should remain the last member so it'll be destroyed first and |
538 // invalidate its weak pointers before other members are destroyed. | 571 // invalidate its weak pointers before other members are destroyed. |
539 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; | 572 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; |
540 | 573 |
541 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); | 574 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); |
542 }; | 575 }; |
543 | 576 |
544 } // namespace gdata | 577 } // namespace gdata |
545 | 578 |
546 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 579 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
OLD | NEW |