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/threading/thread_checker.h"
16 #include "base/time/time.h"
17 #include "components/drive/file_errors.h"
18 #include "components/drive/job_scheduler.h"
19 #include "components/drive/resource_metadata.h"
22 class SequencedTaskRunner
;
32 namespace file_system
{
33 class DownloadOperation
;
34 class OperationDelegate
;
39 class ChangeListLoader
;
40 class EntryUpdatePerformer
;
42 class LoaderController
;
43 class ResourceMetadata
;
45 // The SyncClient is used to synchronize pinned files on Drive and the
46 // cache on the local drive.
48 // If the user logs out before fetching of the pinned files is complete, this
49 // client resumes fetching operations next time the user logs in, based on
50 // the states left in the cache.
53 SyncClient(base::SequencedTaskRunner
* blocking_task_runner
,
54 file_system::OperationDelegate
* delegate
,
55 JobScheduler
* scheduler
,
56 ResourceMetadata
* metadata
,
58 LoaderController
* loader_controller
,
59 const base::FilePath
& temporary_file_directory
);
60 virtual ~SyncClient();
63 void AddFetchTask(const std::string
& local_id
);
65 // Removes a fetch task.
66 void RemoveFetchTask(const std::string
& local_id
);
68 // Adds a update task.
69 void AddUpdateTask(const ClientContext
& context
, const std::string
& local_id
);
71 // Waits for the update task to complete and runs the callback.
72 // Returns false if no task is found for the spcecified ID.
73 bool WaitForUpdateTaskToComplete(const std::string
& local_id
,
74 const FileOperationCallback
& callback
);
76 // Starts processing the backlog (i.e. pinned-but-not-filed files and
77 // dirty-but-not-uploaded files). Kicks off retrieval of the local
78 // IDs of these files, and then starts the sync loop.
79 void StartProcessingBacklog();
81 // Starts checking the existing pinned files to see if these are
82 // up-to-date. If stale files are detected, the local IDs of these files
83 // are added and the sync loop is started.
84 void StartCheckingExistingPinnedFiles();
86 // Sets a delay for testing.
87 void set_delay_for_testing(const base::TimeDelta
& delay
) {
92 // Types of sync tasks.
94 FETCH
, // Fetch a file from the Drive server.
95 UPDATE
, // Updates an entry's metadata or content on the Drive server.
98 // States of sync tasks.
100 SUSPENDED
, // Task is currently inactive.
101 PENDING
, // Task is going to run.
102 RUNNING
, // Task is running.
105 typedef std::pair
<SyncType
, std::string
> SyncTaskKey
;
111 ClientContext context
;
112 base::Callback
<base::Closure(const ClientContext
& context
)> task
;
113 bool should_run_again
;
114 base::Closure cancel_closure
;
115 std::vector
<SyncTaskKey
> dependent_tasks
;
116 std::vector
<FileOperationCallback
> waiting_callbacks
;
119 typedef std::map
<SyncTaskKey
, SyncTask
> SyncTasks
;
121 // Performs a FETCH task.
122 base::Closure
PerformFetchTask(const std::string
& local_id
,
123 const ClientContext
& context
);
125 // Adds a FETCH task.
126 void AddFetchTaskInternal(const std::string
& local_id
,
127 const base::TimeDelta
& delay
);
129 // Performs a UPDATE task.
130 base::Closure
PerformUpdateTask(const std::string
& local_id
,
131 const ClientContext
& context
);
133 // Adds a UPDATE task.
134 void AddUpdateTaskInternal(const ClientContext
& context
,
135 const std::string
& local_id
,
136 const base::TimeDelta
& delay
);
138 // Adds the given task. If the same task is found, does nothing.
139 void AddTask(const SyncTasks::key_type
& key
,
140 const SyncTask
& task
,
141 const base::TimeDelta
& delay
);
143 // Called when a task is ready to start.
144 void StartTask(const SyncTasks::key_type
& key
);
145 void StartTaskAfterGetParentResourceEntry(const SyncTasks::key_type
& key
,
146 const ResourceEntry
* parent
,
149 // Called when the local IDs of files in the backlog are obtained.
150 void OnGetLocalIdsOfBacklog(const std::vector
<std::string
>* to_fetch
,
151 const std::vector
<std::string
>* to_update
);
154 void AddFetchTasks(const std::vector
<std::string
>* local_ids
);
156 // Called when a task is completed.
157 void OnTaskComplete(SyncType type
,
158 const std::string
& local_id
,
161 // Called when the file for |local_id| is fetched.
162 void OnFetchFileComplete(const std::string
& local_id
,
164 const base::FilePath
& local_path
,
165 scoped_ptr
<ResourceEntry
> entry
);
167 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
168 file_system::OperationDelegate
* operation_delegate_
;
169 ResourceMetadata
* metadata_
;
172 // Used to fetch pinned files.
173 scoped_ptr
<file_system::DownloadOperation
> download_operation_
;
175 // Used to update entry metadata.
176 scoped_ptr
<EntryUpdatePerformer
> entry_update_performer_
;
178 // Sync tasks to be processed.
181 // The delay is used for delaying processing tasks in AddTask().
182 base::TimeDelta delay_
;
184 // The delay is used for delaying retry of tasks on server errors.
185 base::TimeDelta long_delay_
;
187 base::ThreadChecker thread_checker_
;
189 // Note: This should remain the last member so it'll be destroyed and
190 // invalidate its weak pointers before any other members are destroyed.
191 base::WeakPtrFactory
<SyncClient
> weak_ptr_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(SyncClient
);
196 } // namespace internal
199 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_