Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / sync_client.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }
22
23 namespace drive {
24
25 class FileCacheEntry;
26 class JobScheduler;
27 class ResourceEntry;
28 struct ClientContext;
29
30 namespace file_system {
31 class DownloadOperation;
32 class OperationObserver;
33 }
34
35 namespace internal {
36
37 class ChangeListLoader;
38 class EntryUpdatePerformer;
39 class FileCache;
40 class LoaderController;
41 class ResourceMetadata;
42
43 // The SyncClient is used to synchronize pinned files on Drive and the
44 // cache on the local drive.
45 //
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.
49 class SyncClient {
50  public:
51   SyncClient(base::SequencedTaskRunner* blocking_task_runner,
52              file_system::OperationObserver* observer,
53              JobScheduler* scheduler,
54              ResourceMetadata* metadata,
55              FileCache* cache,
56              LoaderController* loader_controller,
57              const base::FilePath& temporary_file_directory);
58   virtual ~SyncClient();
59
60   // Adds a fetch task.
61   void AddFetchTask(const std::string& local_id);
62
63   // Removes a fetch task.
64   void RemoveFetchTask(const std::string& local_id);
65
66   // Adds a update task.
67   void AddUpdateTask(const ClientContext& context, const std::string& local_id);
68
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();
73
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();
78
79   // Sets a delay for testing.
80   void set_delay_for_testing(const base::TimeDelta& delay) {
81     delay_ = delay;
82   }
83
84   // Starts the sync loop if it's not running.
85   void StartSyncLoop();
86
87  private:
88   // Types of sync tasks.
89   enum SyncType {
90     FETCH,  // Fetch a file from the Drive server.
91     UPDATE,  // Updates an entry's metadata or content on the Drive server.
92   };
93
94   // States of sync tasks.
95   enum SyncState {
96     PENDING,
97     RUNNING,
98   };
99
100   struct SyncTask {
101     SyncTask();
102     ~SyncTask();
103     SyncState state;
104     base::Closure task;
105     bool should_run_again;
106     base::Closure cancel_closure;
107   };
108
109   typedef std::map<std::pair<SyncType, std::string>, SyncTask> SyncTasks;
110
111   // Adds a FETCH task.
112   void AddFetchTaskInternal(const std::string& local_id,
113                             const base::TimeDelta& delay);
114
115   // Adds a UPDATE task.
116   void AddUpdateTaskInternal(const ClientContext& context,
117                              const std::string& local_id,
118                              const base::TimeDelta& delay);
119
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);
124
125   // Called when a task is ready to start.
126   void StartTask(const SyncTasks::key_type& key);
127
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);
131
132   // Adds fetch tasks.
133   void AddFetchTasks(const std::vector<std::string>* local_ids);
134
135   // Used as GetFileContentInitializedCallback.
136   void OnGetFileContentInitialized(
137       FileError error,
138       scoped_ptr<ResourceEntry> entry,
139       const base::FilePath& local_cache_file_path,
140       const base::Closure& cancel_download_closure);
141
142   // Erases the task and returns true if task is completed.
143   bool OnTaskComplete(SyncType type, const std::string& local_id);
144
145   // Called when the file for |local_id| is fetched.
146   void OnFetchFileComplete(const std::string& local_id,
147                            FileError error,
148                            const base::FilePath& local_path,
149                            scoped_ptr<ResourceEntry> entry);
150
151   // Called when the entry is updated.
152   void OnUpdateComplete(const std::string& local_id, FileError error);
153
154   // Adds update tasks for |entries|.
155   void AddChildUpdateTasks(const ResourceEntryVector* entries,
156                            FileError error);
157
158   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
159   file_system::OperationObserver* operation_observer_;
160   ResourceMetadata* metadata_;
161   FileCache* cache_;
162
163   // Used to fetch pinned files.
164   scoped_ptr<file_system::DownloadOperation> download_operation_;
165
166   // Used to update entry metadata.
167   scoped_ptr<EntryUpdatePerformer> entry_update_performer_;
168
169   // Sync tasks to be processed.
170   SyncTasks tasks_;
171
172   // The delay is used for delaying processing tasks in AddTask().
173   base::TimeDelta delay_;
174
175   // The delay is used for delaying retry of tasks on server errors.
176   base::TimeDelta long_delay_;
177
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_;
181
182   DISALLOW_COPY_AND_ASSIGN(SyncClient);
183 };
184
185 }  // namespace internal
186 }  // namespace drive
187
188 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_