Only allow leveldb to use the minimum amount of file descriptors.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local_file_sync_service.h
blob63d12c13fd85adb4fa157f7ebc686c88a5212297
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_SYNC_FILE_SYSTEM_LOCAL_FILE_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_FILE_SYNC_SERVICE_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/sync_file_system/remote_change_processor.h"
18 #include "webkit/browser/fileapi/syncable/local_origin_change_observer.h"
19 #include "webkit/browser/fileapi/syncable/sync_callbacks.h"
20 #include "webkit/browser/fileapi/syncable/sync_status_code.h"
22 class GURL;
23 class Profile;
25 namespace fileapi {
26 class FileSystemContext;
29 namespace sync_file_system {
31 class FileChange;
32 class LocalChangeProcessor;
33 class LocalFileSyncContext;
34 struct LocalFileSyncInfo;
36 // Maintains local file change tracker and sync status.
37 // Owned by SyncFileSystemService (which is a per-profile object).
38 class LocalFileSyncService
39 : public RemoteChangeProcessor,
40 public LocalOriginChangeObserver,
41 public base::SupportsWeakPtr<LocalFileSyncService> {
42 public:
43 class Observer {
44 public:
45 Observer() {}
46 virtual ~Observer() {}
48 // This is called when there're one or more local changes available.
49 // |pending_changes_hint| indicates the pending queue length to help sync
50 // scheduling but the value may not be accurately reflect the real-time
51 // value.
52 virtual void OnLocalChangeAvailable(int64 pending_changes_hint) = 0;
54 private:
55 DISALLOW_COPY_AND_ASSIGN(Observer);
58 typedef base::Callback<void(SyncStatusCode status,
59 bool has_pending_changes)>
60 HasPendingLocalChangeCallback;
62 explicit LocalFileSyncService(Profile* profile);
63 virtual ~LocalFileSyncService();
65 void Shutdown();
67 void MaybeInitializeFileSystemContext(
68 const GURL& app_origin,
69 fileapi::FileSystemContext* file_system_context,
70 const SyncStatusCallback& callback);
72 void AddChangeObserver(Observer* observer);
74 // Registers |url| to wait until sync is enabled for |url|.
75 // |on_syncable_callback| is to be called when |url| becomes syncable
76 // (i.e. when we have no pending writes and the file is successfully locked
77 // for sync).
78 // Calling this method again while this already has another URL waiting
79 // for sync will overwrite the previously registered URL.
80 void RegisterURLForWaitingSync(const fileapi::FileSystemURL& url,
81 const base::Closure& on_syncable_callback);
83 // Synchronize one (or a set of) local change(s) to the remote server
84 // using local_change_processor given by SetLocalChangeProcessor().
85 // |processor| must have same or longer lifetime than this service.
86 // It is invalid to call this method before calling SetLocalChangeProcessor().
87 void ProcessLocalChange(const SyncFileCallback& callback);
89 // Sets a local change processor. This must be called before any
90 // ProcessLocalChange().
91 void SetLocalChangeProcessor(LocalChangeProcessor* processor);
93 // Returns true via |callback| if the given file |url| has local pending
94 // changes.
95 void HasPendingLocalChanges(
96 const fileapi::FileSystemURL& url,
97 const HasPendingLocalChangeCallback& callback);
99 // A local or remote sync has been finished (either successfully or
100 // with an error). Clears the internal sync flag and enable writing for |url|.
101 void ClearSyncFlagForURL(const fileapi::FileSystemURL& url);
103 // Returns the metadata of a remote file pointed by |url|.
104 virtual void GetLocalFileMetadata(
105 const fileapi::FileSystemURL& url,
106 const SyncFileMetadataCallback& callback);
108 // RemoteChangeProcessor overrides.
109 virtual void PrepareForProcessRemoteChange(
110 const fileapi::FileSystemURL& url,
111 const PrepareChangeCallback& callback) OVERRIDE;
112 virtual void ApplyRemoteChange(
113 const FileChange& change,
114 const base::FilePath& local_path,
115 const fileapi::FileSystemURL& url,
116 const SyncStatusCallback& callback) OVERRIDE;
117 virtual void ClearLocalChanges(
118 const fileapi::FileSystemURL& url,
119 const base::Closure& completion_callback) OVERRIDE;
120 virtual void RecordFakeLocalChange(
121 const fileapi::FileSystemURL& url,
122 const FileChange& change,
123 const SyncStatusCallback& callback) OVERRIDE;
125 // LocalOriginChangeObserver override.
126 virtual void OnChangesAvailableInOrigins(
127 const std::set<GURL>& origins) OVERRIDE;
129 // Called when a particular origin (app) is disabled/enabled while
130 // the service is running. This may be called for origins/apps that
131 // are not initialized for the service.
132 void SetOriginEnabled(const GURL& origin, bool enabled);
134 private:
135 friend class OriginChangeMapTest;
137 class OriginChangeMap {
138 public:
139 typedef std::map<GURL, int64> Map;
141 OriginChangeMap();
142 ~OriginChangeMap();
144 // Sets |origin| to the next origin to process. (For now we simply apply
145 // round-robin to pick the next origin to avoid starvation.)
146 // Returns false if no origins to process.
147 bool NextOriginToProcess(GURL* origin);
149 int64 GetTotalChangeCount() const;
151 // Update change_count_map_ for |origin|.
152 void SetOriginChangeCount(const GURL& origin, int64 changes);
154 void SetOriginEnabled(const GURL& origin, bool enabled);
156 private:
157 // Per-origin changes (cached info, could be stale).
158 Map change_count_map_;
159 Map::iterator next_;
161 // Holds a set of disabled (but initialized) origins.
162 std::set<GURL> disabled_origins_;
165 void DidInitializeFileSystemContext(
166 const GURL& app_origin,
167 fileapi::FileSystemContext* file_system_context,
168 const SyncStatusCallback& callback,
169 SyncStatusCode status);
170 void DidInitializeForRemoteSync(
171 const fileapi::FileSystemURL& url,
172 fileapi::FileSystemContext* file_system_context,
173 const PrepareChangeCallback& callback,
174 SyncStatusCode status);
176 // Runs local_sync_callback_ and resets it.
177 void RunLocalSyncCallback(
178 SyncStatusCode status,
179 const fileapi::FileSystemURL& url);
181 // Callbacks for ProcessLocalChange.
182 void DidGetFileForLocalSync(
183 SyncStatusCode status,
184 const LocalFileSyncInfo& sync_file_info);
185 void ProcessNextChangeForURL(
186 const LocalFileSyncInfo& sync_file_info,
187 const FileChange& last_change,
188 const FileChangeList& changes,
189 SyncStatusCode status);
191 Profile* profile_;
193 scoped_refptr<LocalFileSyncContext> sync_context_;
195 // Origin to context map. (Assuming that as far as we're in the same
196 // profile single origin wouldn't belong to multiple FileSystemContexts.)
197 std::map<GURL, fileapi::FileSystemContext*> origin_to_contexts_;
199 // Origins which have pending changes but have not been initialized yet.
200 // (Used only for handling dirty files left in the local tracker database
201 // after a restart.)
202 std::set<GURL> pending_origins_with_changes_;
204 OriginChangeMap origin_change_map_;
206 // This callback is non-null while a local sync is running (i.e.
207 // ProcessLocalChange has been called and has not been returned yet).
208 SyncFileCallback local_sync_callback_;
210 LocalChangeProcessor* local_change_processor_;
212 ObserverList<Observer> change_observers_;
214 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncService);
217 } // namespace sync_file_system
219 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_FILE_SYNC_SERVICE_H_