| 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 |
| 420 // Move |entry| to |directory_path| asynchronously. Removes entry from | 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 |
| 430 // Moves |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 not 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 // Removes |entry| from its parent. Calls |callback| with the path of the |
| 438 // parent directory. |callback| may not 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 // Removes all child files of |directory| and replace with file_map. |
| 479 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, | 494 // |callback| is called with the directory path. |callback| may not be null. |
| 480 GDataEntry* old_entry); | 495 void RefreshDirectory(const std::string& directory_resource_id, |
| 496 const ResourceMap& file_map, |
| 497 const FileMoveCallback& callback); |
| 481 | 498 |
| 482 // Serializes/Parses to/from string via proto classes. | 499 // Serializes/Parses to/from string via proto classes. |
| 483 void SerializeToString(std::string* serialized_proto) const; | 500 void SerializeToString(std::string* serialized_proto) const; |
| 484 bool ParseFromString(const std::string& serialized_proto); | 501 bool ParseFromString(const std::string& serialized_proto); |
| 485 | 502 |
| 486 // Restores from and saves to database. | 503 // Restores from and saves to database. |
| 487 void InitFromDB(const FilePath& db_path, | 504 void InitFromDB(const FilePath& db_path, |
| 488 base::SequencedTaskRunner* blocking_task_runner, | 505 base::SequencedTaskRunner* blocking_task_runner, |
| 489 const FileOperationCallback& callback); | 506 const FileOperationCallback& callback); |
| 490 void SaveToDB(); | 507 void SaveToDB(); |
| 491 | 508 |
| 492 private: | 509 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 | 510 // Initializes the resource map using serialized_resources fetched from the |
| 497 // database. | 511 // database. |
| 498 void InitResourceMap(CreateDBParams* create_params, | 512 void InitResourceMap(CreateDBParams* create_params, |
| 499 const FileOperationCallback& callback); | 513 const FileOperationCallback& callback); |
| 500 | 514 |
| 501 // Clears root_ and the resource map. | 515 // Clears root_ and the resource map. |
| 502 void ClearRoot(); | 516 void ClearRoot(); |
| 503 | 517 |
| 504 // Creates GDataEntry from serialized string. | 518 // Creates GDataEntry from serialized string. |
| 505 scoped_ptr<GDataEntry> FromProtoString( | 519 scoped_ptr<GDataEntry> FromProtoString( |
| 506 const std::string& serialized_proto); | 520 const std::string& serialized_proto); |
| 507 | 521 |
| 508 // ... | 522 // Continues with GetEntryInfoPairByPaths after the first GDataEntry has been |
| 523 // asynchronously fetched. This fetches the second GDataEntry only if the |
| 524 // first was found. |
| 509 void GetEntryInfoPairByPathsAfterGetFirst( | 525 void GetEntryInfoPairByPathsAfterGetFirst( |
| 510 const FilePath& first_path, | 526 const FilePath& first_path, |
| 511 const FilePath& second_path, | 527 const FilePath& second_path, |
| 512 const GetEntryInfoPairCallback& callback, | 528 const GetEntryInfoPairCallback& callback, |
| 513 GDataFileError error, | 529 GDataFileError error, |
| 514 scoped_ptr<GDataEntryProto> entry_proto); | 530 scoped_ptr<GDataEntryProto> entry_proto); |
| 515 | 531 |
| 516 // ... | 532 // Continues with GetIntroInfoPairByPaths after the second GDataEntry has been |
| 533 // asynchronously fetched. |
| 517 void GetEntryInfoPairByPathsAfterGetSecond( | 534 void GetEntryInfoPairByPathsAfterGetSecond( |
| 518 const FilePath& second_path, | 535 const FilePath& second_path, |
| 519 const GetEntryInfoPairCallback& callback, | 536 const GetEntryInfoPairCallback& callback, |
| 520 scoped_ptr<EntryInfoPairResult> result, | 537 scoped_ptr<EntryInfoPairResult> result, |
| 521 GDataFileError error, | 538 GDataFileError error, |
| 522 scoped_ptr<GDataEntryProto> entry_proto); | 539 scoped_ptr<GDataEntryProto> entry_proto); |
| 523 | 540 |
| 541 // These internal functions need friend access to private GDataDirectory |
| 542 // methods. |
| 543 // Replaces file entry |old_entry| with its fresh value |fresh_file|. |
| 544 static void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file, |
| 545 GDataEntry* old_entry); |
| 546 |
| 547 // Removes all child files of |directory| and replace with file_map. |
| 548 // |callback| may not be null. |
| 549 static void RefreshDirectoryInternal(const ResourceMap& file_map, |
| 550 const FileMoveCallback& callback, |
| 551 GDataEntry* directory_entry); |
| 552 |
| 553 |
| 524 // Private data members. | 554 // Private data members. |
| 525 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 555 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 526 scoped_ptr<ResourceMetadataDB> directory_service_db_; | 556 scoped_ptr<ResourceMetadataDB> directory_service_db_; |
| 527 | 557 |
| 528 ResourceMap resource_map_; | 558 ResourceMap resource_map_; |
| 529 | 559 |
| 530 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. | 560 scoped_ptr<GDataDirectory> root_; // Stored in the serialized proto. |
| 531 | 561 |
| 532 base::Time last_serialized_; | 562 base::Time last_serialized_; |
| 533 size_t serialized_size_; | 563 size_t serialized_size_; |
| 534 int64 largest_changestamp_; // Stored in the serialized proto. | 564 int64 largest_changestamp_; // Stored in the serialized proto. |
| 535 ContentOrigin origin_; | 565 ContentOrigin origin_; |
| 536 | 566 |
| 537 // This should remain the last member so it'll be destroyed first and | 567 // This should remain the last member so it'll be destroyed first and |
| 538 // invalidate its weak pointers before other members are destroyed. | 568 // invalidate its weak pointers before other members are destroyed. |
| 539 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; | 569 base::WeakPtrFactory<GDataDirectoryService> weak_ptr_factory_; |
| 540 | 570 |
| 541 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); | 571 DISALLOW_COPY_AND_ASSIGN(GDataDirectoryService); |
| 542 }; | 572 }; |
| 543 | 573 |
| 544 } // namespace gdata | 574 } // namespace gdata |
| 545 | 575 |
| 546 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ | 576 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_FILES_H_ |
| OLD | NEW |