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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
18 class SequencedTaskRunner;
27 namespace file_system {
28 class DownloadOperation;
29 class OperationObserver;
30 class UpdateOperation;
36 class ResourceMetadata;
38 // The SyncClient is used to synchronize pinned files on Drive and the
39 // cache on the local drive.
41 // If the user logs out before fetching of the pinned files is complete, this
42 // client resumes fetching operations next time the user logs in, based on
43 // the states left in the cache.
46 // Types of sync tasks.
48 FETCH, // Fetch a file from the Drive server.
49 UPLOAD, // Upload a file to the Drive server.
50 UPLOAD_NO_CONTENT_CHECK, // Upload a file without checking if the file is
51 // really modified. This type is used for upload
52 // retry tasks that should have already run the
53 // check at least once.
56 SyncClient(base::SequencedTaskRunner* blocking_task_runner,
57 file_system::OperationObserver* observer,
58 JobScheduler* scheduler,
59 ResourceMetadata* metadata,
61 const base::FilePath& temporary_file_directory);
62 virtual ~SyncClient();
64 // Adds a fetch task to the queue.
65 void AddFetchTask(const std::string& local_id);
67 // Removes a fetch task from the queue.
68 void RemoveFetchTask(const std::string& local_id);
70 // Adds an upload task to the queue.
71 void AddUploadTask(const std::string& local_id);
73 // Starts processing the backlog (i.e. pinned-but-not-filed files and
74 // dirty-but-not-uploaded files). Kicks off retrieval of the local
75 // IDs of these files, and then starts the sync loop.
76 void StartProcessingBacklog();
78 // Starts checking the existing pinned files to see if these are
79 // up-to-date. If stale files are detected, the local IDs of these files
80 // are added to the queue and the sync loop is started.
81 void StartCheckingExistingPinnedFiles();
83 // Sets a delay for testing.
84 void set_delay_for_testing(const base::TimeDelta& delay) {
88 // Starts the sync loop if it's not running.
92 // Adds the given task to the queue. If the same task is queued, remove the
93 // existing one, and adds a new one to the end of the queue.
94 void AddTaskToQueue(SyncType type,
95 const std::string& local_id,
96 const base::TimeDelta& delay);
98 // Called when a task is ready to be added to the queue.
99 void StartTask(SyncType type, const std::string& local_id);
101 // Called when the local IDs of files in the backlog are obtained.
102 void OnGetLocalIdsOfBacklog(const std::vector<std::string>* to_fetch,
103 const std::vector<std::string>* to_upload);
106 void AddFetchTasks(const std::vector<std::string>* local_ids);
108 // Called when the file for |local_id| is fetched.
109 // Calls DoSyncLoop() to go back to the sync loop.
110 void OnFetchFileComplete(const std::string& local_id,
112 const base::FilePath& local_path,
113 scoped_ptr<ResourceEntry> entry);
115 // Called when the file for |local_id| is uploaded.
116 // Calls DoSyncLoop() to go back to the sync loop.
117 void OnUploadFileComplete(const std::string& local_id, FileError error);
119 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
120 ResourceMetadata* metadata_;
123 // Used to fetch pinned files.
124 scoped_ptr<file_system::DownloadOperation> download_operation_;
126 // Used to upload committed files.
127 scoped_ptr<file_system::UpdateOperation> update_operation_;
129 // List of the local ids of resources which have a fetch task created.
130 std::set<std::string> fetch_list_;
132 // List of the local ids of resources which have a upload task created.
133 std::set<std::string> upload_list_;
135 // Fetch tasks which have been created, but not started yet. If they are
136 // removed before starting, they will be cancelled.
137 std::set<std::string> pending_fetch_list_;
139 // The delay is used for delaying processing tasks in AddTaskToQueue().
140 base::TimeDelta delay_;
142 // The delay is used for delaying retry of tasks on server errors.
143 base::TimeDelta long_delay_;
145 // Note: This should remain the last member so it'll be destroyed and
146 // invalidate its weak pointers before any other members are destroyed.
147 base::WeakPtrFactory<SyncClient> weak_ptr_factory_;
149 DISALLOW_COPY_AND_ASSIGN(SyncClient);
152 } // namespace internal
155 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_