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