Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / sync_client.h
blobe402ddb592bc1625734c6972e369c8faf82901b3
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/chromeos/drive/file_errors.h"
17 #include "chrome/browser/chromeos/drive/file_system/update_operation.h"
19 namespace base {
20 class SequencedTaskRunner;
23 namespace drive {
25 class FileCacheEntry;
26 class JobScheduler;
27 class ResourceEntry;
28 struct ClientContext;
30 namespace file_system {
31 class DownloadOperation;
32 class OperationObserver;
33 class UpdateOperation;
36 namespace internal {
38 class EntryUpdatePerformer;
39 class FileCache;
40 class ResourceMetadata;
42 // The SyncClient is used to synchronize pinned files on Drive and the
43 // cache on the local drive.
45 // If the user logs out before fetching of the pinned files is complete, this
46 // client resumes fetching operations next time the user logs in, based on
47 // the states left in the cache.
48 class SyncClient {
49 public:
50 SyncClient(base::SequencedTaskRunner* blocking_task_runner,
51 file_system::OperationObserver* observer,
52 JobScheduler* scheduler,
53 ResourceMetadata* metadata,
54 FileCache* cache,
55 const base::FilePath& temporary_file_directory);
56 virtual ~SyncClient();
58 // Adds a fetch task.
59 void AddFetchTask(const std::string& local_id);
61 // Removes a fetch task.
62 void RemoveFetchTask(const std::string& local_id);
64 // Adds an upload task.
65 void AddUploadTask(const ClientContext& context, const std::string& local_id);
67 // Adds a update task.
68 void AddUpdateTask(const std::string& local_id);
70 // Starts processing the backlog (i.e. pinned-but-not-filed files and
71 // dirty-but-not-uploaded files). Kicks off retrieval of the local
72 // IDs of these files, and then starts the sync loop.
73 void StartProcessingBacklog();
75 // Starts checking the existing pinned files to see if these are
76 // up-to-date. If stale files are detected, the local IDs of these files
77 // are added and the sync loop is started.
78 void StartCheckingExistingPinnedFiles();
80 // Sets a delay for testing.
81 void set_delay_for_testing(const base::TimeDelta& delay) {
82 delay_ = delay;
85 // Starts the sync loop if it's not running.
86 void StartSyncLoop();
88 private:
89 // Types of sync tasks.
90 enum SyncType {
91 FETCH, // Fetch a file from the Drive server.
92 UPLOAD, // Upload a file to the Drive server.
93 UPDATE, // Updates an entry's metadata on the Drive server.
96 // States of sync tasks.
97 enum SyncState {
98 PENDING,
99 RUNNING,
102 struct SyncTask {
103 SyncTask();
104 ~SyncTask();
105 SyncState state;
106 base::Closure task;
107 bool should_run_again;
110 typedef std::map<std::pair<SyncType, std::string>, SyncTask> SyncTasks;
112 // Adds a FETCH task.
113 void AddFetchTaskInternal(const std::string& local_id,
114 const base::TimeDelta& delay);
116 // Adds a UPLOAD task.
117 void AddUploadTaskInternal(
118 const ClientContext& context,
119 const std::string& local_id,
120 file_system::UpdateOperation::ContentCheckMode content_check_mode,
121 const base::TimeDelta& delay);
123 // Adds a UPDATE task.
124 void AddUpdateTaskInternal(const std::string& local_id,
125 const base::TimeDelta& delay);
127 // Adds the given task. If the same task is found, does nothing.
128 void AddTask(const SyncTasks::key_type& key,
129 const SyncTask& task,
130 const base::TimeDelta& delay);
132 // Called when a task is ready to start.
133 void StartTask(const SyncTasks::key_type& key);
135 // Called when the local IDs of files in the backlog are obtained.
136 void OnGetLocalIdsOfBacklog(const std::vector<std::string>* to_fetch,
137 const std::vector<std::string>* to_upload,
138 const std::vector<std::string>* to_update);
140 // Adds fetch tasks.
141 void AddFetchTasks(const std::vector<std::string>* local_ids);
143 // Erases the task and returns true if task is completed.
144 bool OnTaskComplete(SyncType type, const std::string& local_id);
146 // Called when the file for |local_id| is fetched.
147 // Calls DoSyncLoop() to go back to the sync loop.
148 void OnFetchFileComplete(const std::string& local_id,
149 FileError error,
150 const base::FilePath& local_path,
151 scoped_ptr<ResourceEntry> entry);
153 // Called when the file for |local_id| is uploaded.
154 // Calls DoSyncLoop() to go back to the sync loop.
155 void OnUploadFileComplete(const std::string& local_id, FileError error);
157 // Called when the entry is updated.
158 void OnUpdateComplete(const std::string& local_id, FileError error);
160 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
161 file_system::OperationObserver* operation_observer_;
162 ResourceMetadata* metadata_;
163 FileCache* cache_;
165 // Used to fetch pinned files.
166 scoped_ptr<file_system::DownloadOperation> download_operation_;
168 // Used to upload committed files.
169 scoped_ptr<file_system::UpdateOperation> update_operation_;
171 // Used to update entry metadata.
172 scoped_ptr<EntryUpdatePerformer> entry_update_performer_;
174 // Sync tasks to be processed.
175 SyncTasks tasks_;
177 // The delay is used for delaying processing tasks in AddTask().
178 base::TimeDelta delay_;
180 // The delay is used for delaying retry of tasks on server errors.
181 base::TimeDelta long_delay_;
183 // Note: This should remain the last member so it'll be destroyed and
184 // invalidate its weak pointers before any other members are destroyed.
185 base::WeakPtrFactory<SyncClient> weak_ptr_factory_;
187 DISALLOW_COPY_AND_ASSIGN(SyncClient);
190 } // namespace internal
191 } // namespace drive
193 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_