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