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