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/resource_metadata.h"
20 class SequencedTaskRunner
;
30 namespace file_system
{
31 class DownloadOperation
;
32 class OperationObserver
;
37 class ChangeListLoader
;
38 class EntryUpdatePerformer
;
40 class LoaderController
;
41 class ResourceMetadata
;
43 // The SyncClient is used to synchronize pinned files on Drive and the
44 // cache on the local drive.
46 // If the user logs out before fetching of the pinned files is complete, this
47 // client resumes fetching operations next time the user logs in, based on
48 // the states left in the cache.
51 SyncClient(base::SequencedTaskRunner
* blocking_task_runner
,
52 file_system::OperationObserver
* observer
,
53 JobScheduler
* scheduler
,
54 ResourceMetadata
* metadata
,
56 LoaderController
* loader_controller
,
57 const base::FilePath
& temporary_file_directory
);
58 virtual ~SyncClient();
61 void AddFetchTask(const std::string
& local_id
);
63 // Removes a fetch task.
64 void RemoveFetchTask(const std::string
& local_id
);
66 // Adds a update task.
67 void AddUpdateTask(const ClientContext
& context
, const std::string
& local_id
);
69 // Starts processing the backlog (i.e. pinned-but-not-filed files and
70 // dirty-but-not-uploaded files). Kicks off retrieval of the local
71 // IDs of these files, and then starts the sync loop.
72 void StartProcessingBacklog();
74 // Starts checking the existing pinned files to see if these are
75 // up-to-date. If stale files are detected, the local IDs of these files
76 // are added and the sync loop is started.
77 void StartCheckingExistingPinnedFiles();
79 // Sets a delay for testing.
80 void set_delay_for_testing(const base::TimeDelta
& delay
) {
84 // Starts the sync loop if it's not running.
88 // Types of sync tasks.
90 FETCH
, // Fetch a file from the Drive server.
91 UPDATE
, // Updates an entry's metadata or content on the Drive server.
94 // States of sync tasks.
105 bool should_run_again
;
106 base::Closure cancel_closure
;
109 typedef std::map
<std::pair
<SyncType
, std::string
>, SyncTask
> SyncTasks
;
111 // Adds a FETCH task.
112 void AddFetchTaskInternal(const std::string
& local_id
,
113 const base::TimeDelta
& delay
);
115 // Adds a UPDATE task.
116 void AddUpdateTaskInternal(const ClientContext
& context
,
117 const std::string
& local_id
,
118 const base::TimeDelta
& delay
);
120 // Adds the given task. If the same task is found, does nothing.
121 void AddTask(const SyncTasks::key_type
& key
,
122 const SyncTask
& task
,
123 const base::TimeDelta
& delay
);
125 // Called when a task is ready to start.
126 void StartTask(const SyncTasks::key_type
& key
);
128 // Called when the local IDs of files in the backlog are obtained.
129 void OnGetLocalIdsOfBacklog(const std::vector
<std::string
>* to_fetch
,
130 const std::vector
<std::string
>* to_update
);
133 void AddFetchTasks(const std::vector
<std::string
>* local_ids
);
135 // Used as GetFileContentInitializedCallback.
136 void OnGetFileContentInitialized(
138 scoped_ptr
<ResourceEntry
> entry
,
139 const base::FilePath
& local_cache_file_path
,
140 const base::Closure
& cancel_download_closure
);
142 // Erases the task and returns true if task is completed.
143 bool OnTaskComplete(SyncType type
, const std::string
& local_id
);
145 // Called when the file for |local_id| is fetched.
146 void OnFetchFileComplete(const std::string
& local_id
,
148 const base::FilePath
& local_path
,
149 scoped_ptr
<ResourceEntry
> entry
);
151 // Called when the entry is updated.
152 void OnUpdateComplete(const std::string
& local_id
, FileError error
);
154 // Adds update tasks for |entries|.
155 void AddChildUpdateTasks(const ResourceEntryVector
* entries
,
158 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
159 file_system::OperationObserver
* operation_observer_
;
160 ResourceMetadata
* metadata_
;
163 // Used to fetch pinned files.
164 scoped_ptr
<file_system::DownloadOperation
> download_operation_
;
166 // Used to update entry metadata.
167 scoped_ptr
<EntryUpdatePerformer
> entry_update_performer_
;
169 // Sync tasks to be processed.
172 // The delay is used for delaying processing tasks in AddTask().
173 base::TimeDelta delay_
;
175 // The delay is used for delaying retry of tasks on server errors.
176 base::TimeDelta long_delay_
;
178 // Note: This should remain the last member so it'll be destroyed and
179 // invalidate its weak pointers before any other members are destroyed.
180 base::WeakPtrFactory
<SyncClient
> weak_ptr_factory_
;
182 DISALLOW_COPY_AND_ASSIGN(SyncClient
);
185 } // namespace internal
188 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_