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