- add sources.
[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 <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
16
17 namespace base {
18 class SequencedTaskRunner;
19 }
20
21 namespace drive {
22
23 class FileCacheEntry;
24 class JobScheduler;
25 class ResourceEntry;
26
27 namespace file_system {
28 class DownloadOperation;
29 class OperationObserver;
30 class UpdateOperation;
31 }
32
33 namespace internal {
34
35 class FileCache;
36 class ResourceMetadata;
37
38 // The SyncClient is used to synchronize pinned files on Drive and the
39 // cache on the local drive.
40 //
41 // If the user logs out before fetching of the pinned files is complete, this
42 // client resumes fetching operations next time the user logs in, based on
43 // the states left in the cache.
44 class SyncClient {
45  public:
46   // Types of sync tasks.
47   enum SyncType {
48     FETCH,  // Fetch a file from the Drive server.
49     UPLOAD,  // Upload a file to the Drive server.
50     UPLOAD_NO_CONTENT_CHECK,  // Upload a file without checking if the file is
51                               // really modified. This type is used for upload
52                               // retry tasks that should have already run the
53                               // check at least once.
54   };
55
56   SyncClient(base::SequencedTaskRunner* blocking_task_runner,
57              file_system::OperationObserver* observer,
58              JobScheduler* scheduler,
59              ResourceMetadata* metadata,
60              FileCache* cache,
61              const base::FilePath& temporary_file_directory);
62   virtual ~SyncClient();
63
64   // Adds a fetch task to the queue.
65   void AddFetchTask(const std::string& local_id);
66
67   // Removes a fetch task from the queue.
68   void RemoveFetchTask(const std::string& local_id);
69
70   // Adds an upload task to the queue.
71   void AddUploadTask(const std::string& local_id);
72
73   // Starts processing the backlog (i.e. pinned-but-not-filed files and
74   // dirty-but-not-uploaded files). Kicks off retrieval of the local
75   // IDs of these files, and then starts the sync loop.
76   void StartProcessingBacklog();
77
78   // Starts checking the existing pinned files to see if these are
79   // up-to-date. If stale files are detected, the local IDs of these files
80   // are added to the queue and the sync loop is started.
81   void StartCheckingExistingPinnedFiles();
82
83   // Sets a delay for testing.
84   void set_delay_for_testing(const base::TimeDelta& delay) {
85     delay_ = delay;
86   }
87
88   // Starts the sync loop if it's not running.
89   void StartSyncLoop();
90
91  private:
92   // Adds the given task to the queue. If the same task is queued, remove the
93   // existing one, and adds a new one to the end of the queue.
94   void AddTaskToQueue(SyncType type,
95                       const std::string& local_id,
96                       const base::TimeDelta& delay);
97
98   // Called when a task is ready to be added to the queue.
99   void StartTask(SyncType type, const std::string& local_id);
100
101   // Called when the local IDs of files in the backlog are obtained.
102   void OnGetLocalIdsOfBacklog(const std::vector<std::string>* to_fetch,
103                               const std::vector<std::string>* to_upload);
104
105   // Adds fetch tasks.
106   void AddFetchTasks(const std::vector<std::string>* local_ids);
107
108   // Called when the file for |local_id| is fetched.
109   // Calls DoSyncLoop() to go back to the sync loop.
110   void OnFetchFileComplete(const std::string& local_id,
111                            FileError error,
112                            const base::FilePath& local_path,
113                            scoped_ptr<ResourceEntry> entry);
114
115   // Called when the file for |local_id| is uploaded.
116   // Calls DoSyncLoop() to go back to the sync loop.
117   void OnUploadFileComplete(const std::string& local_id, FileError error);
118
119   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
120   ResourceMetadata* metadata_;
121   FileCache* cache_;
122
123   // Used to fetch pinned files.
124   scoped_ptr<file_system::DownloadOperation> download_operation_;
125
126   // Used to upload committed files.
127   scoped_ptr<file_system::UpdateOperation> update_operation_;
128
129   // List of the local ids of resources which have a fetch task created.
130   std::set<std::string> fetch_list_;
131
132   // List of the local ids of resources which have a upload task created.
133   std::set<std::string> upload_list_;
134
135   // Fetch tasks which have been created, but not started yet.  If they are
136   // removed before starting, they will be cancelled.
137   std::set<std::string> pending_fetch_list_;
138
139   // The delay is used for delaying processing tasks in AddTaskToQueue().
140   base::TimeDelta delay_;
141
142   // The delay is used for delaying retry of tasks on server errors.
143   base::TimeDelta long_delay_;
144
145   // Note: This should remain the last member so it'll be destroyed and
146   // invalidate its weak pointers before any other members are destroyed.
147   base::WeakPtrFactory<SyncClient> weak_ptr_factory_;
148
149   DISALLOW_COPY_AND_ASSIGN(SyncClient);
150 };
151
152 }  // namespace internal
153 }  // namespace drive
154
155 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_SYNC_CLIENT_H_