OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ |
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ | 6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "chrome/browser/sync_file_system/local/local_origin_change_observer.h" | 17 #include "chrome/browser/sync_file_system/local/local_origin_change_observer.h" |
18 #include "chrome/browser/sync_file_system/remote_change_processor.h" | 18 #include "chrome/browser/sync_file_system/remote_change_processor.h" |
19 #include "chrome/browser/sync_file_system/sync_callbacks.h" | 19 #include "chrome/browser/sync_file_system/sync_callbacks.h" |
20 #include "chrome/browser/sync_file_system/sync_status_code.h" | 20 #include "chrome/browser/sync_file_system/sync_status_code.h" |
21 | 21 |
22 class GURL; | 22 class GURL; |
23 class Profile; | 23 class Profile; |
24 | 24 |
25 namespace fileapi { | 25 namespace storage { |
26 class FileSystemContext; | 26 class FileSystemContext; |
27 } | 27 } |
28 | 28 |
29 namespace leveldb { | 29 namespace leveldb { |
30 class Env; | 30 class Env; |
31 } | 31 } |
32 | 32 |
33 namespace webkit_blob { | 33 namespace storage { |
34 class ScopedFile; | 34 class ScopedFile; |
35 } | 35 } |
36 | 36 |
37 namespace sync_file_system { | 37 namespace sync_file_system { |
38 | 38 |
39 class FileChange; | 39 class FileChange; |
40 class LocalChangeProcessor; | 40 class LocalChangeProcessor; |
41 class LocalFileSyncContext; | 41 class LocalFileSyncContext; |
42 struct LocalFileSyncInfo; | 42 struct LocalFileSyncInfo; |
43 | 43 |
(...skipping 29 matching lines...) Expand all Loading... |
73 static scoped_ptr<LocalFileSyncService> Create(Profile* profile); | 73 static scoped_ptr<LocalFileSyncService> Create(Profile* profile); |
74 static scoped_ptr<LocalFileSyncService> CreateForTesting( | 74 static scoped_ptr<LocalFileSyncService> CreateForTesting( |
75 Profile* profile, | 75 Profile* profile, |
76 leveldb::Env* env_override); | 76 leveldb::Env* env_override); |
77 virtual ~LocalFileSyncService(); | 77 virtual ~LocalFileSyncService(); |
78 | 78 |
79 void Shutdown(); | 79 void Shutdown(); |
80 | 80 |
81 void MaybeInitializeFileSystemContext( | 81 void MaybeInitializeFileSystemContext( |
82 const GURL& app_origin, | 82 const GURL& app_origin, |
83 fileapi::FileSystemContext* file_system_context, | 83 storage::FileSystemContext* file_system_context, |
84 const SyncStatusCallback& callback); | 84 const SyncStatusCallback& callback); |
85 | 85 |
86 void AddChangeObserver(Observer* observer); | 86 void AddChangeObserver(Observer* observer); |
87 | 87 |
88 // Registers |url| to wait until sync is enabled for |url|. | 88 // Registers |url| to wait until sync is enabled for |url|. |
89 // |on_syncable_callback| is to be called when |url| becomes syncable | 89 // |on_syncable_callback| is to be called when |url| becomes syncable |
90 // (i.e. when we have no pending writes and the file is successfully locked | 90 // (i.e. when we have no pending writes and the file is successfully locked |
91 // for sync). | 91 // for sync). |
92 // Calling this method again while this already has another URL waiting | 92 // Calling this method again while this already has another URL waiting |
93 // for sync will overwrite the previously registered URL. | 93 // for sync will overwrite the previously registered URL. |
94 void RegisterURLForWaitingSync(const fileapi::FileSystemURL& url, | 94 void RegisterURLForWaitingSync(const storage::FileSystemURL& url, |
95 const base::Closure& on_syncable_callback); | 95 const base::Closure& on_syncable_callback); |
96 | 96 |
97 // Synchronize one (or a set of) local change(s) to the remote server | 97 // Synchronize one (or a set of) local change(s) to the remote server |
98 // using local_change_processor given by SetLocalChangeProcessor(). | 98 // using local_change_processor given by SetLocalChangeProcessor(). |
99 // |processor| must have same or longer lifetime than this service. | 99 // |processor| must have same or longer lifetime than this service. |
100 // It is invalid to call this method before calling SetLocalChangeProcessor(). | 100 // It is invalid to call this method before calling SetLocalChangeProcessor(). |
101 void ProcessLocalChange(const SyncFileCallback& callback); | 101 void ProcessLocalChange(const SyncFileCallback& callback); |
102 | 102 |
103 // Sets a local change processor. The value is ignored if | 103 // Sets a local change processor. The value is ignored if |
104 // SetLocalChangeProcessorCallback() is called separately. | 104 // SetLocalChangeProcessorCallback() is called separately. |
105 // Either this or SetLocalChangeProcessorCallback() must be called before | 105 // Either this or SetLocalChangeProcessorCallback() must be called before |
106 // any ProcessLocalChange(). | 106 // any ProcessLocalChange(). |
107 void SetLocalChangeProcessor(LocalChangeProcessor* local_change_processor); | 107 void SetLocalChangeProcessor(LocalChangeProcessor* local_change_processor); |
108 | 108 |
109 // Sets a closure which gets a local change processor for the given origin. | 109 // Sets a closure which gets a local change processor for the given origin. |
110 // Note that once this is called it overrides the direct processor setting | 110 // Note that once this is called it overrides the direct processor setting |
111 // done by SetLocalChangeProcessor(). | 111 // done by SetLocalChangeProcessor(). |
112 // Either this or SetLocalChangeProcessor() must be called before any | 112 // Either this or SetLocalChangeProcessor() must be called before any |
113 // ProcessLocalChange(). | 113 // ProcessLocalChange(). |
114 // | 114 // |
115 // TODO(kinuko): Remove this method once we stop using multiple backends | 115 // TODO(kinuko): Remove this method once we stop using multiple backends |
116 // (crbug.com/324215), or deprecate the other if we keep doing so. | 116 // (crbug.com/324215), or deprecate the other if we keep doing so. |
117 void SetLocalChangeProcessorCallback( | 117 void SetLocalChangeProcessorCallback( |
118 const GetLocalChangeProcessorCallback& get_local_change_processor); | 118 const GetLocalChangeProcessorCallback& get_local_change_processor); |
119 | 119 |
120 // Returns true via |callback| if the given file |url| has local pending | 120 // Returns true via |callback| if the given file |url| has local pending |
121 // changes. | 121 // changes. |
122 void HasPendingLocalChanges( | 122 void HasPendingLocalChanges(const storage::FileSystemURL& url, |
123 const fileapi::FileSystemURL& url, | 123 const HasPendingLocalChangeCallback& callback); |
124 const HasPendingLocalChangeCallback& callback); | |
125 | 124 |
126 void PromoteDemotedChanges(const base::Closure& callback); | 125 void PromoteDemotedChanges(const base::Closure& callback); |
127 | 126 |
128 // Returns the metadata of a remote file pointed by |url|. | 127 // Returns the metadata of a remote file pointed by |url|. |
129 virtual void GetLocalFileMetadata( | 128 virtual void GetLocalFileMetadata(const storage::FileSystemURL& url, |
130 const fileapi::FileSystemURL& url, | 129 const SyncFileMetadataCallback& callback); |
131 const SyncFileMetadataCallback& callback); | |
132 | 130 |
133 // RemoteChangeProcessor overrides. | 131 // RemoteChangeProcessor overrides. |
134 virtual void PrepareForProcessRemoteChange( | 132 virtual void PrepareForProcessRemoteChange( |
135 const fileapi::FileSystemURL& url, | 133 const storage::FileSystemURL& url, |
136 const PrepareChangeCallback& callback) OVERRIDE; | 134 const PrepareChangeCallback& callback) OVERRIDE; |
137 virtual void ApplyRemoteChange( | 135 virtual void ApplyRemoteChange(const FileChange& change, |
138 const FileChange& change, | 136 const base::FilePath& local_path, |
139 const base::FilePath& local_path, | 137 const storage::FileSystemURL& url, |
140 const fileapi::FileSystemURL& url, | 138 const SyncStatusCallback& callback) OVERRIDE; |
141 const SyncStatusCallback& callback) OVERRIDE; | |
142 virtual void FinalizeRemoteSync( | 139 virtual void FinalizeRemoteSync( |
143 const fileapi::FileSystemURL& url, | 140 const storage::FileSystemURL& url, |
144 bool clear_local_changes, | 141 bool clear_local_changes, |
145 const base::Closure& completion_callback) OVERRIDE; | 142 const base::Closure& completion_callback) OVERRIDE; |
146 virtual void RecordFakeLocalChange( | 143 virtual void RecordFakeLocalChange( |
147 const fileapi::FileSystemURL& url, | 144 const storage::FileSystemURL& url, |
148 const FileChange& change, | 145 const FileChange& change, |
149 const SyncStatusCallback& callback) OVERRIDE; | 146 const SyncStatusCallback& callback) OVERRIDE; |
150 | 147 |
151 // LocalOriginChangeObserver override. | 148 // LocalOriginChangeObserver override. |
152 virtual void OnChangesAvailableInOrigins( | 149 virtual void OnChangesAvailableInOrigins( |
153 const std::set<GURL>& origins) OVERRIDE; | 150 const std::set<GURL>& origins) OVERRIDE; |
154 | 151 |
155 // Called when a particular origin (app) is disabled/enabled while | 152 // Called when a particular origin (app) is disabled/enabled while |
156 // the service is running. This may be called for origins/apps that | 153 // the service is running. This may be called for origins/apps that |
157 // are not initialized for the service. | 154 // are not initialized for the service. |
158 void SetOriginEnabled(const GURL& origin, bool enabled); | 155 void SetOriginEnabled(const GURL& origin, bool enabled); |
159 | 156 |
160 private: | 157 private: |
161 typedef std::map<GURL, fileapi::FileSystemContext*> OriginToContext; | 158 typedef std::map<GURL, storage::FileSystemContext*> OriginToContext; |
162 friend class OriginChangeMapTest; | 159 friend class OriginChangeMapTest; |
163 | 160 |
164 class OriginChangeMap { | 161 class OriginChangeMap { |
165 public: | 162 public: |
166 typedef std::map<GURL, int64> Map; | 163 typedef std::map<GURL, int64> Map; |
167 | 164 |
168 OriginChangeMap(); | 165 OriginChangeMap(); |
169 ~OriginChangeMap(); | 166 ~OriginChangeMap(); |
170 | 167 |
171 // Sets |origin| to the next origin to process. (For now we simply apply | 168 // Sets |origin| to the next origin to process. (For now we simply apply |
(...skipping 14 matching lines...) Expand all Loading... |
186 Map::iterator next_; | 183 Map::iterator next_; |
187 | 184 |
188 // Holds a set of disabled (but initialized) origins. | 185 // Holds a set of disabled (but initialized) origins. |
189 std::set<GURL> disabled_origins_; | 186 std::set<GURL> disabled_origins_; |
190 }; | 187 }; |
191 | 188 |
192 LocalFileSyncService(Profile* profile, leveldb::Env* env_override); | 189 LocalFileSyncService(Profile* profile, leveldb::Env* env_override); |
193 | 190 |
194 void DidInitializeFileSystemContext( | 191 void DidInitializeFileSystemContext( |
195 const GURL& app_origin, | 192 const GURL& app_origin, |
196 fileapi::FileSystemContext* file_system_context, | 193 storage::FileSystemContext* file_system_context, |
197 const SyncStatusCallback& callback, | 194 const SyncStatusCallback& callback, |
198 SyncStatusCode status); | 195 SyncStatusCode status); |
199 void DidInitializeForRemoteSync( | 196 void DidInitializeForRemoteSync( |
200 const fileapi::FileSystemURL& url, | 197 const storage::FileSystemURL& url, |
201 fileapi::FileSystemContext* file_system_context, | 198 storage::FileSystemContext* file_system_context, |
202 const PrepareChangeCallback& callback, | 199 const PrepareChangeCallback& callback, |
203 SyncStatusCode status); | 200 SyncStatusCode status); |
204 | 201 |
205 // Runs local_sync_callback_ and resets it. | 202 // Runs local_sync_callback_ and resets it. |
206 void RunLocalSyncCallback( | 203 void RunLocalSyncCallback(SyncStatusCode status, |
207 SyncStatusCode status, | 204 const storage::FileSystemURL& url); |
208 const fileapi::FileSystemURL& url); | |
209 | 205 |
210 // Callback for ApplyRemoteChange. | 206 // Callback for ApplyRemoteChange. |
211 void DidApplyRemoteChange( | 207 void DidApplyRemoteChange( |
212 const SyncStatusCallback& callback, | 208 const SyncStatusCallback& callback, |
213 SyncStatusCode status); | 209 SyncStatusCode status); |
214 | 210 |
215 // Callbacks for ProcessLocalChange. | 211 // Callbacks for ProcessLocalChange. |
216 void DidGetFileForLocalSync( | 212 void DidGetFileForLocalSync(SyncStatusCode status, |
217 SyncStatusCode status, | 213 const LocalFileSyncInfo& sync_file_info, |
218 const LocalFileSyncInfo& sync_file_info, | 214 storage::ScopedFile snapshot); |
219 webkit_blob::ScopedFile snapshot); | 215 void ProcessNextChangeForURL(storage::ScopedFile snapshot, |
220 void ProcessNextChangeForURL( | 216 const LocalFileSyncInfo& sync_file_info, |
221 webkit_blob::ScopedFile snapshot, | 217 const FileChange& last_change, |
222 const LocalFileSyncInfo& sync_file_info, | 218 const FileChangeList& changes, |
223 const FileChange& last_change, | 219 SyncStatusCode status); |
224 const FileChangeList& changes, | |
225 SyncStatusCode status); | |
226 | 220 |
227 // A thin wrapper of get_local_change_processor_. | 221 // A thin wrapper of get_local_change_processor_. |
228 LocalChangeProcessor* GetLocalChangeProcessor( | 222 LocalChangeProcessor* GetLocalChangeProcessor( |
229 const fileapi::FileSystemURL& url); | 223 const storage::FileSystemURL& url); |
230 | 224 |
231 Profile* profile_; | 225 Profile* profile_; |
232 | 226 |
233 scoped_refptr<LocalFileSyncContext> sync_context_; | 227 scoped_refptr<LocalFileSyncContext> sync_context_; |
234 | 228 |
235 // Origin to context map. (Assuming that as far as we're in the same | 229 // Origin to context map. (Assuming that as far as we're in the same |
236 // profile single origin wouldn't belong to multiple FileSystemContexts.) | 230 // profile single origin wouldn't belong to multiple FileSystemContexts.) |
237 OriginToContext origin_to_contexts_; | 231 OriginToContext origin_to_contexts_; |
238 | 232 |
239 // Origins which have pending changes but have not been initialized yet. | 233 // Origins which have pending changes but have not been initialized yet. |
(...skipping 11 matching lines...) Expand all Loading... |
251 GetLocalChangeProcessorCallback get_local_change_processor_; | 245 GetLocalChangeProcessorCallback get_local_change_processor_; |
252 | 246 |
253 ObserverList<Observer> change_observers_; | 247 ObserverList<Observer> change_observers_; |
254 | 248 |
255 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncService); | 249 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncService); |
256 }; | 250 }; |
257 | 251 |
258 } // namespace sync_file_system | 252 } // namespace sync_file_system |
259 | 253 |
260 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ | 254 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_SERVICE_H_ |
OLD | NEW |