[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/callback.h" |
| 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/memory/scoped_vector.h" |
| 17 | #include "base/observer_list.h" |
| 18 | #include "chrome/browser/chromeos/drive/file_errors.h" |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame] | 19 | #include "chrome/browser/chromeos/drive/file_system_interface.h" |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 20 | #include "google_apis/drive/drive_common_callbacks.h" |
| 21 | #include "google_apis/drive/gdata_errorcode.h" |
| 22 | |
| 23 | namespace base { |
| 24 | class SequencedTaskRunner; |
| 25 | } // namespace base |
| 26 | |
| 27 | namespace google_apis { |
| 28 | class AboutResource; |
| 29 | } // namespace google_apis |
| 30 | |
| 31 | namespace drive { |
| 32 | |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 33 | class EventLogger; |
| 34 | class JobScheduler; |
| 35 | class ResourceEntry; |
| 36 | |
| 37 | namespace internal { |
| 38 | |
| 39 | class AboutResourceLoader; |
| 40 | class ChangeList; |
| 41 | class ChangeListLoaderObserver; |
| 42 | class DirectoryFetchInfo; |
| 43 | class LoaderController; |
| 44 | class ResourceMetadata; |
| 45 | |
| 46 | // DirectoryLoader is used to load directory contents. |
| 47 | class DirectoryLoader { |
| 48 | public: |
| 49 | DirectoryLoader(EventLogger* logger, |
| 50 | base::SequencedTaskRunner* blocking_task_runner, |
| 51 | ResourceMetadata* resource_metadata, |
| 52 | JobScheduler* scheduler, |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 53 | AboutResourceLoader* about_resource_loader, |
| 54 | LoaderController* apply_task_controller); |
| 55 | ~DirectoryLoader(); |
| 56 | |
| 57 | // Adds and removes the observer. |
| 58 | void AddObserver(ChangeListLoaderObserver* observer); |
| 59 | void RemoveObserver(ChangeListLoaderObserver* observer); |
| 60 | |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame] | 61 | // Reads the directory contents. |
[email protected] | f173c6d | 2014-04-14 10:49:50 | [diff] [blame^] | 62 | // |entries_callback| can be null. |
| 63 | // |completion_callback| must not be null. |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame] | 64 | void ReadDirectory(const base::FilePath& directory_path, |
[email protected] | f173c6d | 2014-04-14 10:49:50 | [diff] [blame^] | 65 | const ReadDirectoryEntriesCallback& entries_callback, |
| 66 | const FileOperationCallback& completion_callback); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | class FeedFetcher; |
[email protected] | bbf59f2 | 2014-03-24 13:55:26 | [diff] [blame] | 70 | struct ReadDirectoryCallbackState; |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 71 | |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame] | 72 | // Part of ReadDirectory(). |
[email protected] | f173c6d | 2014-04-14 10:49:50 | [diff] [blame^] | 73 | void ReadDirectoryAfterGetEntry( |
| 74 | const base::FilePath& directory_path, |
| 75 | const ReadDirectoryEntriesCallback& entries_callback, |
| 76 | const FileOperationCallback& completion_callback, |
| 77 | bool should_try_loading_parent, |
| 78 | const ResourceEntry* entry, |
| 79 | FileError error); |
| 80 | void ReadDirectoryAfterLoadParent( |
| 81 | const base::FilePath& directory_path, |
| 82 | const ReadDirectoryEntriesCallback& entries_callback, |
| 83 | const FileOperationCallback& completion_callback, |
| 84 | FileError error); |
[email protected] | c8f46ab | 2014-03-12 17:54:30 | [diff] [blame] | 85 | void ReadDirectoryAfterGetAboutResource( |
[email protected] | 8542d3c | 2014-03-19 05:57:22 | [diff] [blame] | 86 | const std::string& local_id, |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 87 | google_apis::GDataErrorCode status, |
| 88 | scoped_ptr<google_apis::AboutResource> about_resource); |
[email protected] | 8542d3c | 2014-03-19 05:57:22 | [diff] [blame] | 89 | void ReadDirectoryAfterCheckLocalState( |
| 90 | scoped_ptr<google_apis::AboutResource> about_resource, |
| 91 | const std::string& local_id, |
| 92 | const ResourceEntry* entry, |
[email protected] | 8542d3c | 2014-03-19 05:57:22 | [diff] [blame] | 93 | const int64* local_changestamp, |
| 94 | FileError error); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 95 | |
[email protected] | c8f46ab | 2014-03-12 17:54:30 | [diff] [blame] | 96 | // Part of ReadDirectory(). |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 97 | // This function should be called when the directory load is complete. |
| 98 | // Flushes the callbacks waiting for the directory to be loaded. |
[email protected] | 8542d3c | 2014-03-19 05:57:22 | [diff] [blame] | 99 | void OnDirectoryLoadComplete(const std::string& local_id, FileError error); |
[email protected] | bbf59f2 | 2014-03-24 13:55:26 | [diff] [blame] | 100 | void OnDirectoryLoadCompleteAfterRead(const std::string& local_id, |
| 101 | const ResourceEntryVector* entries, |
| 102 | FileError error); |
| 103 | |
| 104 | // Sends |entries| to the callbacks. |
| 105 | void SendEntries(const std::string& local_id, |
[email protected] | f173c6d | 2014-04-14 10:49:50 | [diff] [blame^] | 106 | const ResourceEntryVector& entries); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 107 | |
| 108 | // ================= Implementation for directory loading ================= |
| 109 | // Loads the directory contents from server, and updates the local metadata. |
| 110 | // Runs |callback| when it is finished. |
| 111 | void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info); |
| 112 | |
| 113 | // Part of LoadDirectoryFromServer() for a normal directory. |
| 114 | void LoadDirectoryFromServerAfterLoad( |
| 115 | const DirectoryFetchInfo& directory_fetch_info, |
| 116 | FeedFetcher* fetcher, |
[email protected] | bbf59f2 | 2014-03-24 13:55:26 | [diff] [blame] | 117 | FileError error); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 118 | |
| 119 | // Part of LoadDirectoryFromServer(). |
[email protected] | bbf59f2 | 2014-03-24 13:55:26 | [diff] [blame] | 120 | void LoadDirectoryFromServerAfterUpdateChangestamp( |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 121 | const DirectoryFetchInfo& directory_fetch_info, |
| 122 | const base::FilePath* directory_path, |
| 123 | FileError error); |
| 124 | |
| 125 | EventLogger* logger_; // Not owned. |
| 126 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 127 | ResourceMetadata* resource_metadata_; // Not owned. |
| 128 | JobScheduler* scheduler_; // Not owned. |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 129 | AboutResourceLoader* about_resource_loader_; // Not owned. |
| 130 | LoaderController* loader_controller_; // Not owned. |
| 131 | ObserverList<ChangeListLoaderObserver> observers_; |
[email protected] | bbf59f2 | 2014-03-24 13:55:26 | [diff] [blame] | 132 | typedef std::map<std::string, std::vector<ReadDirectoryCallbackState> > |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 133 | LoadCallbackMap; |
| 134 | LoadCallbackMap pending_load_callback_; |
| 135 | |
| 136 | // Set of the running feed fetcher for the fast fetch. |
| 137 | std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_; |
| 138 | |
| 139 | // Note: This should remain the last member so it'll be destroyed and |
| 140 | // invalidate its weak pointers before any other members are destroyed. |
| 141 | base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_; |
| 142 | DISALLOW_COPY_AND_ASSIGN(DirectoryLoader); |
| 143 | }; |
| 144 | |
| 145 | } // namespace internal |
| 146 | } // namespace drive |
| 147 | |
| 148 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_ |