[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 | |
| 33 | class DriveServiceInterface; |
| 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, |
| 54 | DriveServiceInterface* drive_service, |
| 55 | AboutResourceLoader* about_resource_loader, |
| 56 | LoaderController* apply_task_controller); |
| 57 | ~DirectoryLoader(); |
| 58 | |
| 59 | // Adds and removes the observer. |
| 60 | void AddObserver(ChangeListLoaderObserver* observer); |
| 61 | void RemoveObserver(ChangeListLoaderObserver* observer); |
| 62 | |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame^] | 63 | // Reads the directory contents. |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 64 | // |callback| must not be null. |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame^] | 65 | void ReadDirectory(const base::FilePath& directory_path, |
| 66 | const ReadDirectoryCallback& callback); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | class FeedFetcher; |
| 70 | |
[email protected] | b71475f | 2014-03-03 08:09:11 | [diff] [blame^] | 71 | // Part of ReadDirectory(). |
| 72 | void ReadDirectoryAfterLoad(const base::FilePath& directory_path, |
| 73 | const ReadDirectoryCallback& callback, |
| 74 | FileError error); |
| 75 | void ReadDirectoryAfterRead(const ReadDirectoryCallback& callback, |
| 76 | scoped_ptr<ResourceEntryVector> entries, |
| 77 | FileError error); |
| 78 | |
| 79 | // Starts loading the directory contents if needed. |
| 80 | // |callback| must not be null. |
| 81 | void LoadDirectoryIfNeeded(const base::FilePath& directory_path, |
| 82 | const FileOperationCallback& callback); |
[email protected] | 54cb8e0 | 2014-02-24 09:29:19 | [diff] [blame] | 83 | void LoadDirectoryIfNeededAfterGetEntry(const base::FilePath& directory_path, |
| 84 | const FileOperationCallback& callback, |
| 85 | bool should_try_loading_parent, |
| 86 | const ResourceEntry* entry, |
| 87 | FileError error); |
| 88 | void LoadDirectoryIfNeededAfterLoadParent( |
| 89 | const base::FilePath& directory_path, |
| 90 | const FileOperationCallback& callback, |
| 91 | FileError error); |
| 92 | |
| 93 | // Starts loading directory contents and calls |callback| when it's done. |
| 94 | void Load(const DirectoryFetchInfo& directory_fetch_info, |
| 95 | const FileOperationCallback& callback); |
| 96 | void LoadAfterGetLargestChangestamp( |
| 97 | const DirectoryFetchInfo& directory_fetch_info, |
| 98 | int64 local_changestamp); |
| 99 | void LoadAfterGetAboutResource( |
| 100 | const DirectoryFetchInfo& directory_fetch_info, |
| 101 | int64 local_changestamp, |
| 102 | google_apis::GDataErrorCode status, |
| 103 | scoped_ptr<google_apis::AboutResource> about_resource); |
| 104 | |
| 105 | // Part of Load(). |
| 106 | // This function should be called when the directory load is complete. |
| 107 | // Flushes the callbacks waiting for the directory to be loaded. |
| 108 | void OnDirectoryLoadComplete(const DirectoryFetchInfo& directory_fetch_info, |
| 109 | FileError error); |
| 110 | |
| 111 | // ================= Implementation for directory loading ================= |
| 112 | // Loads the directory contents from server, and updates the local metadata. |
| 113 | // Runs |callback| when it is finished. |
| 114 | void LoadDirectoryFromServer(const DirectoryFetchInfo& directory_fetch_info); |
| 115 | |
| 116 | // Part of LoadDirectoryFromServer() for a normal directory. |
| 117 | void LoadDirectoryFromServerAfterLoad( |
| 118 | const DirectoryFetchInfo& directory_fetch_info, |
| 119 | FeedFetcher* fetcher, |
| 120 | FileError error, |
| 121 | ScopedVector<ChangeList> change_lists); |
| 122 | |
| 123 | // Part of LoadDirectoryFromServer(). |
| 124 | void LoadDirectoryFromServerAfterRefresh( |
| 125 | const DirectoryFetchInfo& directory_fetch_info, |
| 126 | const base::FilePath* directory_path, |
| 127 | FileError error); |
| 128 | |
| 129 | EventLogger* logger_; // Not owned. |
| 130 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 131 | ResourceMetadata* resource_metadata_; // Not owned. |
| 132 | JobScheduler* scheduler_; // Not owned. |
| 133 | DriveServiceInterface* drive_service_; // Not owned. |
| 134 | AboutResourceLoader* about_resource_loader_; // Not owned. |
| 135 | LoaderController* loader_controller_; // Not owned. |
| 136 | ObserverList<ChangeListLoaderObserver> observers_; |
| 137 | typedef std::map<std::string, std::vector<FileOperationCallback> > |
| 138 | LoadCallbackMap; |
| 139 | LoadCallbackMap pending_load_callback_; |
| 140 | |
| 141 | // Set of the running feed fetcher for the fast fetch. |
| 142 | std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_; |
| 143 | |
| 144 | // Note: This should remain the last member so it'll be destroyed and |
| 145 | // invalidate its weak pointers before any other members are destroyed. |
| 146 | base::WeakPtrFactory<DirectoryLoader> weak_ptr_factory_; |
| 147 | DISALLOW_COPY_AND_ASSIGN(DirectoryLoader); |
| 148 | }; |
| 149 | |
| 150 | } // namespace internal |
| 151 | } // namespace drive |
| 152 | |
| 153 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DIRECTORY_LOADER_H_ |