| 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_DRIVE_CHANGE_LIST_LOADER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 class ResourceEntry; | 39 class ResourceEntry; |
| 40 | 40 |
| 41 namespace internal { | 41 namespace internal { |
| 42 | 42 |
| 43 class ChangeList; | 43 class ChangeList; |
| 44 class ChangeListLoaderObserver; | 44 class ChangeListLoaderObserver; |
| 45 class ChangeListProcessor; | 45 class ChangeListProcessor; |
| 46 class DirectoryFetchInfo; | 46 class DirectoryFetchInfo; |
| 47 class ResourceMetadata; | 47 class ResourceMetadata; |
| 48 | 48 |
| 49 // Delays execution of tasks as long as more than one lock is alive. |
| 50 // Used to ensure that ChangeListLoader does not cause race condition by adding |
| 51 // new entries created by sync tasks before they do. |
| 52 // All code which may add entries found on the server to the local metadata |
| 53 // should use this class. |
| 54 class LoaderController { |
| 55 public: |
| 56 LoaderController(); |
| 57 ~LoaderController(); |
| 58 |
| 59 // Increments the lock count and returns an object which decrements the count |
| 60 // on its destruction. |
| 61 // While the lock count is positive, tasks will be pending. |
| 62 scoped_ptr<base::ScopedClosureRunner> GetLock(); |
| 63 |
| 64 // Runs the task if the lock count is 0, otherwise it will be pending. |
| 65 void ScheduleRun(const base::Closure& task); |
| 66 |
| 67 private: |
| 68 // Decrements the lock count. |
| 69 void Unlock(); |
| 70 |
| 71 int lock_count_; |
| 72 std::vector<base::Closure> pending_tasks_; |
| 73 |
| 74 base::WeakPtrFactory<LoaderController> weak_ptr_factory_; |
| 75 DISALLOW_COPY_AND_ASSIGN(LoaderController); |
| 76 }; |
| 77 |
| 49 // ChangeListLoader is used to load the change list, the full resource list, | 78 // ChangeListLoader is used to load the change list, the full resource list, |
| 50 // and directory contents, from WAPI (codename for Documents List API) | 79 // and directory contents, from WAPI (codename for Documents List API) |
| 51 // or Google Drive API. The class also updates the resource metadata with | 80 // or Google Drive API. The class also updates the resource metadata with |
| 52 // the change list loaded from the server. | 81 // the change list loaded from the server. |
| 53 // | 82 // |
| 54 // Note that the difference between "resource list" and "change list" is | 83 // Note that the difference between "resource list" and "change list" is |
| 55 // subtle hence the two words are often used interchangeably. To be precise, | 84 // subtle hence the two words are often used interchangeably. To be precise, |
| 56 // "resource list" refers to metadata from the server when fetching the full | 85 // "resource list" refers to metadata from the server when fetching the full |
| 57 // resource metadata, or fetching directory contents, whereas "change list" | 86 // resource metadata, or fetching directory contents, whereas "change list" |
| 58 // refers to metadata from the server when fetching changes (delta). | 87 // refers to metadata from the server when fetching changes (delta). |
| 59 class ChangeListLoader { | 88 class ChangeListLoader { |
| 60 public: | 89 public: |
| 61 // Resource feed fetcher from the server. | 90 // Resource feed fetcher from the server. |
| 62 class FeedFetcher; | 91 class FeedFetcher; |
| 63 | 92 |
| 64 ChangeListLoader(base::SequencedTaskRunner* blocking_task_runner, | 93 ChangeListLoader(base::SequencedTaskRunner* blocking_task_runner, |
| 65 ResourceMetadata* resource_metadata, | 94 ResourceMetadata* resource_metadata, |
| 66 JobScheduler* scheduler, | 95 JobScheduler* scheduler, |
| 67 DriveServiceInterface* drive_service); | 96 DriveServiceInterface* drive_service, |
| 97 LoaderController* apply_task_controller); |
| 68 ~ChangeListLoader(); | 98 ~ChangeListLoader(); |
| 69 | 99 |
| 70 // Indicates whether there is a request for full resource list or change | 100 // Indicates whether there is a request for full resource list or change |
| 71 // list fetching is in flight (i.e. directory contents fetching does not | 101 // list fetching is in flight (i.e. directory contents fetching does not |
| 72 // count). | 102 // count). |
| 73 bool IsRefreshing() const; | 103 bool IsRefreshing() const; |
| 74 | 104 |
| 75 // Adds and removes the observer. | 105 // Adds and removes the observer. |
| 76 void AddObserver(ChangeListLoaderObserver* observer); | 106 void AddObserver(ChangeListLoaderObserver* observer); |
| 77 void RemoveObserver(ChangeListLoaderObserver* observer); | 107 void RemoveObserver(ChangeListLoaderObserver* observer); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 97 const FileOperationCallback& callback); | 127 const FileOperationCallback& callback); |
| 98 | 128 |
| 99 // Calls Load() with an empty DirectoryFetchInfo(). Only for testing purposes. | 129 // Calls Load() with an empty DirectoryFetchInfo(). Only for testing purposes. |
| 100 void LoadForTesting(const FileOperationCallback& callback); | 130 void LoadForTesting(const FileOperationCallback& callback); |
| 101 | 131 |
| 102 // Gets the about resource from the cache or the server. If the cache is | 132 // Gets the about resource from the cache or the server. If the cache is |
| 103 // availlavle, just runs |callback| with the cached about resource. If not, | 133 // availlavle, just runs |callback| with the cached about resource. If not, |
| 104 // calls |UpdateAboutResource| passing |callback|. | 134 // calls |UpdateAboutResource| passing |callback|. |
| 105 void GetAboutResource(const google_apis::AboutResourceCallback& callback); | 135 void GetAboutResource(const google_apis::AboutResourceCallback& callback); |
| 106 | 136 |
| 107 // Increments the lock count and returns an object which decrements the count | |
| 108 // on its destruction. | |
| 109 // While the lock count is positive, ChangeListLoader does not add any entries | |
| 110 // to the local metadata. | |
| 111 scoped_ptr<base::ScopedClosureRunner> GetLock(); | |
| 112 | |
| 113 private: | 137 private: |
| 114 // Part of LoadDirectoryIfNeeded(). | 138 // Part of LoadDirectoryIfNeeded(). |
| 115 void LoadDirectoryIfNeededAfterGetEntry(const base::FilePath& directory_path, | 139 void LoadDirectoryIfNeededAfterGetEntry(const base::FilePath& directory_path, |
| 116 const FileOperationCallback& callback, | 140 const FileOperationCallback& callback, |
| 117 bool should_try_loading_parent, | 141 bool should_try_loading_parent, |
| 118 const ResourceEntry* entry, | 142 const ResourceEntry* entry, |
| 119 FileError error); | 143 FileError error); |
| 120 void LoadDirectoryIfNeededAfterLoadParent( | 144 void LoadDirectoryIfNeededAfterLoadParent( |
| 121 const base::FilePath& directory_path, | 145 const base::FilePath& directory_path, |
| 122 const FileOperationCallback& callback, | 146 const FileOperationCallback& callback, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 FileError error, | 215 FileError error, |
| 192 ScopedVector<ChangeList> change_lists); | 216 ScopedVector<ChangeList> change_lists); |
| 193 | 217 |
| 194 // Part of LoadDirectoryFromServer(). | 218 // Part of LoadDirectoryFromServer(). |
| 195 void LoadDirectoryFromServerAfterRefresh( | 219 void LoadDirectoryFromServerAfterRefresh( |
| 196 const DirectoryFetchInfo& directory_fetch_info, | 220 const DirectoryFetchInfo& directory_fetch_info, |
| 197 const FileOperationCallback& callback, | 221 const FileOperationCallback& callback, |
| 198 const base::FilePath* directory_path, | 222 const base::FilePath* directory_path, |
| 199 FileError error); | 223 FileError error); |
| 200 | 224 |
| 201 // ================= Implementation for lock mechanism ==================== | |
| 202 // Runs the closure if the lock count is 0, otherwise it will be pending. | |
| 203 // All tasks which may add entries should be run via this method. | |
| 204 void ApplyChange(const base::Closure& closure); | |
| 205 | |
| 206 // Decrements the lock count. | |
| 207 void Unlock(); | |
| 208 | |
| 209 // ================= Implementation for other stuff ================= | 225 // ================= Implementation for other stuff ================= |
| 210 | 226 |
| 211 // Gets the about resource from the server, and caches it if successful. This | 227 // Gets the about resource from the server, and caches it if successful. This |
| 212 // function calls JobScheduler::GetAboutResource internally. The cache will be | 228 // function calls JobScheduler::GetAboutResource internally. The cache will be |
| 213 // used in |GetAboutResource|. | 229 // used in |GetAboutResource|. |
| 214 void UpdateAboutResource( | 230 void UpdateAboutResource( |
| 215 const google_apis::AboutResourceCallback& callback); | 231 const google_apis::AboutResourceCallback& callback); |
| 216 // Part of UpdateAboutResource(). | 232 // Part of UpdateAboutResource(). |
| 217 // This function should be called when the latest about resource is being | 233 // This function should be called when the latest about resource is being |
| 218 // fetched from the server. The retrieved about resoure is cloned, and one is | 234 // fetched from the server. The retrieved about resoure is cloned, and one is |
| 219 // cached and the other is passed to |callback|. | 235 // cached and the other is passed to |callback|. |
| 220 void UpdateAboutResourceAfterGetAbout( | 236 void UpdateAboutResourceAfterGetAbout( |
| 221 const google_apis::AboutResourceCallback& callback, | 237 const google_apis::AboutResourceCallback& callback, |
| 222 google_apis::GDataErrorCode status, | 238 google_apis::GDataErrorCode status, |
| 223 scoped_ptr<google_apis::AboutResource> about_resource); | 239 scoped_ptr<google_apis::AboutResource> about_resource); |
| 224 | 240 |
| 225 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 241 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 226 ResourceMetadata* resource_metadata_; // Not owned. | 242 ResourceMetadata* resource_metadata_; // Not owned. |
| 227 JobScheduler* scheduler_; // Not owned. | 243 JobScheduler* scheduler_; // Not owned. |
| 228 DriveServiceInterface* drive_service_; // Not owned. | 244 DriveServiceInterface* drive_service_; // Not owned. |
| 245 LoaderController* loader_controller_; // Not owned. |
| 229 ObserverList<ChangeListLoaderObserver> observers_; | 246 ObserverList<ChangeListLoaderObserver> observers_; |
| 230 typedef std::map<std::string, std::vector<FileOperationCallback> > | 247 typedef std::map<std::string, std::vector<FileOperationCallback> > |
| 231 LoadCallbackMap; | 248 LoadCallbackMap; |
| 232 LoadCallbackMap pending_load_callback_; | 249 LoadCallbackMap pending_load_callback_; |
| 233 FileOperationCallback pending_update_check_callback_; | 250 FileOperationCallback pending_update_check_callback_; |
| 234 | 251 |
| 235 int lock_count_; | |
| 236 std::vector<base::Closure> pending_apply_closures_; | |
| 237 | |
| 238 // Running feed fetcher. | 252 // Running feed fetcher. |
| 239 scoped_ptr<FeedFetcher> change_feed_fetcher_; | 253 scoped_ptr<FeedFetcher> change_feed_fetcher_; |
| 240 | 254 |
| 241 // Set of the running feed fetcher for the fast fetch. | 255 // Set of the running feed fetcher for the fast fetch. |
| 242 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_; | 256 std::set<FeedFetcher*> fast_fetch_feed_fetcher_set_; |
| 243 | 257 |
| 244 // The cache of the about resource. | 258 // The cache of the about resource. |
| 245 scoped_ptr<google_apis::AboutResource> cached_about_resource_; | 259 scoped_ptr<google_apis::AboutResource> cached_about_resource_; |
| 246 | 260 |
| 247 // True if the full resource list is loaded (i.e. the resource metadata is | 261 // True if the full resource list is loaded (i.e. the resource metadata is |
| 248 // stored locally). | 262 // stored locally). |
| 249 bool loaded_; | 263 bool loaded_; |
| 250 | 264 |
| 251 // Note: This should remain the last member so it'll be destroyed and | 265 // Note: This should remain the last member so it'll be destroyed and |
| 252 // invalidate its weak pointers before any other members are destroyed. | 266 // invalidate its weak pointers before any other members are destroyed. |
| 253 base::WeakPtrFactory<ChangeListLoader> weak_ptr_factory_; | 267 base::WeakPtrFactory<ChangeListLoader> weak_ptr_factory_; |
| 254 DISALLOW_COPY_AND_ASSIGN(ChangeListLoader); | 268 DISALLOW_COPY_AND_ASSIGN(ChangeListLoader); |
| 255 }; | 269 }; |
| 256 | 270 |
| 257 } // namespace internal | 271 } // namespace internal |
| 258 } // namespace drive | 272 } // namespace drive |
| 259 | 273 |
| 260 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_ | 274 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_CHANGE_LIST_LOADER_H_ |
| OLD | NEW |