OLD | NEW |
| (Empty) |
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 WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_ | |
6 #define WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/observer_list.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 #include "webkit/browser/fileapi/file_system_url.h" | |
16 | |
17 namespace fileapi { | |
18 class FileSystemURL; | |
19 } | |
20 | |
21 namespace sync_file_system { | |
22 | |
23 // Represents local file sync status. | |
24 // This class is supposed to run only on IO thread. | |
25 // | |
26 // This class manages two important synchronization flags: writing (counter) | |
27 // and syncing (flag). Writing counter keeps track of which URL is in | |
28 // writing and syncing flag indicates which URL is in syncing. | |
29 // | |
30 // An entry can have multiple writers but sync is exclusive and cannot overwrap | |
31 // with any writes or syncs. | |
32 class WEBKIT_STORAGE_EXPORT LocalFileSyncStatus : public base::NonThreadSafe { | |
33 public: | |
34 class WEBKIT_STORAGE_EXPORT Observer { | |
35 public: | |
36 Observer() {} | |
37 virtual ~Observer() {} | |
38 virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) = 0; | |
39 virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) = 0; | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(Observer); | |
42 }; | |
43 | |
44 LocalFileSyncStatus(); | |
45 ~LocalFileSyncStatus(); | |
46 | |
47 // Increment writing counter for |url|. | |
48 // This should not be called if the |url| is not writable. | |
49 void StartWriting(const fileapi::FileSystemURL& url); | |
50 | |
51 // Decrement writing counter for |url|. | |
52 void EndWriting(const fileapi::FileSystemURL& url); | |
53 | |
54 // Start syncing for |url| and disable writing. | |
55 // This should not be called if |url| is in syncing or in writing. | |
56 void StartSyncing(const fileapi::FileSystemURL& url); | |
57 | |
58 // Clears the syncing flag for |url| and enable writing. | |
59 void EndSyncing(const fileapi::FileSystemURL& url); | |
60 | |
61 // Returns true if the |url| or its parent or child is in writing. | |
62 bool IsWriting(const fileapi::FileSystemURL& url) const; | |
63 | |
64 // Returns true if the |url| is enabled for writing (i.e. not in syncing). | |
65 bool IsWritable(const fileapi::FileSystemURL& url) const; | |
66 | |
67 // Returns true if the |url| is enabled for syncing (i.e. neither in | |
68 // syncing nor writing). | |
69 bool IsSyncable(const fileapi::FileSystemURL& url) const; | |
70 | |
71 void AddObserver(Observer* observer); | |
72 void RemoveObserver(Observer* observer); | |
73 | |
74 private: | |
75 typedef std::map<fileapi::FileSystemURL,int64, | |
76 fileapi::FileSystemURL::Comparator> URLCountMap; | |
77 | |
78 bool IsChildOrParentWriting(const fileapi::FileSystemURL& url) const; | |
79 bool IsChildOrParentSyncing(const fileapi::FileSystemURL& url) const; | |
80 | |
81 // If this count is non-zero positive there're ongoing write operations. | |
82 URLCountMap writing_; | |
83 | |
84 // If this flag is set sync process is running on the file. | |
85 fileapi::FileSystemURLSet syncing_; | |
86 | |
87 ObserverList<Observer> observer_list_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncStatus); | |
90 }; | |
91 | |
92 } // namespace sync_file_system | |
93 | |
94 #endif // WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_STATUS_H_ | |
OLD | NEW |