| 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_SYNC_CLIENT_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "chrome/browser/chromeos/cros/network_library.h" | 17 #include "chrome/browser/chromeos/cros/network_library.h" |
| 18 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 18 #include "chrome/browser/chromeos/gdata/drive_cache.h" |
| 19 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" |
| 20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| 23 | 23 |
| 24 class Profile; | 24 class Profile; |
| 25 class PrefChangeRegistrar; | 25 class PrefChangeRegistrar; |
| 26 | 26 |
| 27 namespace gdata { | 27 namespace gdata { |
| 28 | 28 |
| 29 // The GDataSyncClient is used to synchronize pinned files on gdata and the | 29 // The GDataSyncClient is used to synchronize pinned files on gdata and the |
| 30 // cache on the local drive. The sync client works as follows. | 30 // cache on the local drive. The sync client works as follows. |
| 31 // | 31 // |
| 32 // When the user pins files on gdata, this client is notified about the files | 32 // When the user pins files on gdata, this client is notified about the files |
| 33 // that get pinned, and queues tasks and starts fetching these files in the | 33 // that get pinned, and queues tasks and starts fetching these files in the |
| 34 // background. | 34 // background. |
| 35 // | 35 // |
| 36 // When the user unpins files on gdata, this client is notified about the | 36 // When the user unpins files on gdata, this client is notified about the |
| 37 // files that get unpinned, cancels tasks if these are still in the queue. | 37 // files that get unpinned, cancels tasks if these are still in the queue. |
| 38 // | 38 // |
| 39 // If the user logs out before fetching of the pinned files is complete, this | 39 // If the user logs out before fetching of the pinned files is complete, this |
| 40 // client resumes fetching operations next time the user logs in, based on | 40 // client resumes fetching operations next time the user logs in, based on |
| 41 // the states left in the cache. | 41 // the states left in the cache. |
| 42 // | 42 // |
| 43 // TODO(satorux): This client should also upload pinned but dirty (locally | 43 // TODO(satorux): This client should also upload pinned but dirty (locally |
| 44 // edited) files to gdata. Will work on this once downloading is done. | 44 // edited) files to gdata. Will work on this once downloading is done. |
| 45 // crosbug.com/27836. | 45 // crosbug.com/27836. |
| 46 class GDataSyncClient : public GDataFileSystemInterface::Observer, | 46 class GDataSyncClient : public GDataFileSystemInterface::Observer, |
| 47 public GDataCache::Observer, | 47 public DriveCache::Observer, |
| 48 public chromeos::NetworkLibrary::NetworkManagerObserver, | 48 public chromeos::NetworkLibrary::NetworkManagerObserver, |
| 49 public content::NotificationObserver { | 49 public content::NotificationObserver { |
| 50 public: | 50 public: |
| 51 // Types of sync tasks. | 51 // Types of sync tasks. |
| 52 enum SyncType { | 52 enum SyncType { |
| 53 FETCH, // Fetch a file from the gdata server. | 53 FETCH, // Fetch a file from the gdata server. |
| 54 UPLOAD, // Upload a file to the gdata server. | 54 UPLOAD, // Upload a file to the gdata server. |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // The struct is used to queue tasks for fetching and uploading. | 57 // The struct is used to queue tasks for fetching and uploading. |
| 58 struct SyncTask { | 58 struct SyncTask { |
| 59 SyncTask(SyncType in_sync_type, | 59 SyncTask(SyncType in_sync_type, |
| 60 const std::string& in_resource_id, | 60 const std::string& in_resource_id, |
| 61 const base::Time& in_timestamp); | 61 const base::Time& in_timestamp); |
| 62 | 62 |
| 63 SyncType sync_type; | 63 SyncType sync_type; |
| 64 std::string resource_id; | 64 std::string resource_id; |
| 65 base::Time timestamp; | 65 base::Time timestamp; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // |profile| is used to access user preferences. | 68 // |profile| is used to access user preferences. |
| 69 // |file_system| is used access the | 69 // |file_system| is used access the |
| 70 // cache (ex. store a file to the cache when the file is downloaded). | 70 // cache (ex. store a file to the cache when the file is downloaded). |
| 71 GDataSyncClient(Profile* profile, | 71 GDataSyncClient(Profile* profile, |
| 72 GDataFileSystemInterface* file_system, | 72 GDataFileSystemInterface* file_system, |
| 73 GDataCache* cache); | 73 DriveCache* cache); |
| 74 virtual ~GDataSyncClient(); | 74 virtual ~GDataSyncClient(); |
| 75 | 75 |
| 76 // Initializes the GDataSyncClient. | 76 // Initializes the GDataSyncClient. |
| 77 void Initialize(); | 77 void Initialize(); |
| 78 | 78 |
| 79 // GDataFileSystemInterface overrides. | 79 // GDataFileSystemInterface overrides. |
| 80 virtual void OnInitialLoadFinished() OVERRIDE; | 80 virtual void OnInitialLoadFinished() OVERRIDE; |
| 81 virtual void OnFeedFromServerLoaded() OVERRIDE; | 81 virtual void OnFeedFromServerLoaded() OVERRIDE; |
| 82 | 82 |
| 83 // GDataCache::Observer overrides. | 83 // DriveCache::Observer overrides. |
| 84 virtual void OnCachePinned(const std::string& resource_id, | 84 virtual void OnCachePinned(const std::string& resource_id, |
| 85 const std::string& md5) OVERRIDE; | 85 const std::string& md5) OVERRIDE; |
| 86 virtual void OnCacheUnpinned(const std::string& resource_id, | 86 virtual void OnCacheUnpinned(const std::string& resource_id, |
| 87 const std::string& md5) OVERRIDE; | 87 const std::string& md5) OVERRIDE; |
| 88 virtual void OnCacheCommitted(const std::string& resource_id) OVERRIDE; | 88 virtual void OnCacheCommitted(const std::string& resource_id) OVERRIDE; |
| 89 | 89 |
| 90 // Starts processing the backlog (i.e. pinned-but-not-filed files and | 90 // Starts processing the backlog (i.e. pinned-but-not-filed files and |
| 91 // dirty-but-not-uploaded files). Kicks off retrieval of the resource | 91 // dirty-but-not-uploaded files). Kicks off retrieval of the resource |
| 92 // IDs of these files, and then starts the sync loop. | 92 // IDs of these files, and then starts the sync loop. |
| 93 void StartProcessingBacklog(); | 93 void StartProcessingBacklog(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 // chromeos::NetworkLibrary::NetworkManagerObserver override. | 177 // chromeos::NetworkLibrary::NetworkManagerObserver override. |
| 178 virtual void OnNetworkManagerChanged( | 178 virtual void OnNetworkManagerChanged( |
| 179 chromeos::NetworkLibrary* network_library) OVERRIDE; | 179 chromeos::NetworkLibrary* network_library) OVERRIDE; |
| 180 | 180 |
| 181 // content::NotificationObserver override. | 181 // content::NotificationObserver override. |
| 182 virtual void Observe(int type, | 182 virtual void Observe(int type, |
| 183 const content::NotificationSource& source, | 183 const content::NotificationSource& source, |
| 184 const content::NotificationDetails& details) OVERRIDE; | 184 const content::NotificationDetails& details) OVERRIDE; |
| 185 Profile* profile_; | 185 Profile* profile_; |
| 186 GDataFileSystemInterface* file_system_; // Owned by GDataSystemService. | 186 GDataFileSystemInterface* file_system_; // Owned by GDataSystemService. |
| 187 GDataCache* cache_; // Owned by GDataSystemService. | 187 DriveCache* cache_; // Owned by GDataSystemService. |
| 188 scoped_ptr<PrefChangeRegistrar> registrar_; | 188 scoped_ptr<PrefChangeRegistrar> registrar_; |
| 189 | 189 |
| 190 // The queue of tasks used to fetch/upload files in the background | 190 // The queue of tasks used to fetch/upload files in the background |
| 191 // thread. Note that this class does not use a lock to protect |queue_| as | 191 // thread. Note that this class does not use a lock to protect |queue_| as |
| 192 // all methods touching |queue_| run on the UI thread. | 192 // all methods touching |queue_| run on the UI thread. |
| 193 std::deque<SyncTask> queue_; | 193 std::deque<SyncTask> queue_; |
| 194 | 194 |
| 195 // The delay is used for delaying processing SyncTasks in DoSyncLoop(). | 195 // The delay is used for delaying processing SyncTasks in DoSyncLoop(). |
| 196 base::TimeDelta delay_; | 196 base::TimeDelta delay_; |
| 197 | 197 |
| 198 // True if the sync loop is running. | 198 // True if the sync loop is running. |
| 199 bool sync_loop_is_running_; | 199 bool sync_loop_is_running_; |
| 200 | 200 |
| 201 // Note: This should remain the last member so it'll be destroyed and | 201 // Note: This should remain the last member so it'll be destroyed and |
| 202 // invalidate its weak pointers before any other members are destroyed. | 202 // invalidate its weak pointers before any other members are destroyed. |
| 203 base::WeakPtrFactory<GDataSyncClient> weak_ptr_factory_; | 203 base::WeakPtrFactory<GDataSyncClient> weak_ptr_factory_; |
| 204 | 204 |
| 205 DISALLOW_COPY_AND_ASSIGN(GDataSyncClient); | 205 DISALLOW_COPY_AND_ASSIGN(GDataSyncClient); |
| 206 }; | 206 }; |
| 207 | 207 |
| 208 } // namespace gdata | 208 } // namespace gdata |
| 209 | 209 |
| 210 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_ | 210 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_SYNC_CLIENT_H_ |
| OLD | NEW |