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/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"
20 class SequencedTaskRunner
;
30 namespace file_system
{
31 class DownloadOperation
;
32 class OperationObserver
;
33 class UpdateOperation
;
38 class EntryUpdatePerformer
;
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.
50 SyncClient(base::SequencedTaskRunner
* blocking_task_runner
,
51 file_system::OperationObserver
* observer
,
52 JobScheduler
* scheduler
,
53 ResourceMetadata
* metadata
,
55 const base::FilePath
& temporary_file_directory
);
56 virtual ~SyncClient();
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
) {
85 // Starts the sync loop if it's not running.
89 // Types of sync tasks.
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.
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
);
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
,
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_
;
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.
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
193 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_