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 3 // found in the LICENSE file. 4 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_ 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 LocalFileSyncStatus 33 : public base::NonThreadSafe { 34 public: 35 class Observer { 36 public: 37 Observer() {} 38 virtual ~Observer() {} 39 virtual void OnSyncEnabled(const fileapi::FileSystemURL& url) = 0; 40 virtual void OnWriteEnabled(const fileapi::FileSystemURL& url) = 0; 41 private: 42 DISALLOW_COPY_AND_ASSIGN(Observer); 43 }; 44 45 LocalFileSyncStatus(); 46 ~LocalFileSyncStatus(); 47 48 // Increment writing counter for |url|. 49 // This should not be called if the |url| is not writable. 50 void StartWriting(const fileapi::FileSystemURL& url); 51 52 // Decrement writing counter for |url|. 53 void EndWriting(const fileapi::FileSystemURL& url); 54 55 // Start syncing for |url| and disable writing. 56 // This should not be called if |url| is in syncing or in writing. 57 void StartSyncing(const fileapi::FileSystemURL& url); 58 59 // Clears the syncing flag for |url| and enable writing. 60 void EndSyncing(const fileapi::FileSystemURL& url); 61 62 // Returns true if the |url| or its parent or child is in writing. 63 bool IsWriting(const fileapi::FileSystemURL& url) const; 64 65 // Returns true if the |url| is enabled for writing (i.e. not in syncing). 66 bool IsWritable(const fileapi::FileSystemURL& url) const; 67 68 // Returns true if the |url| is enabled for syncing (i.e. neither in 69 // syncing nor writing). 70 bool IsSyncable(const fileapi::FileSystemURL& url) const; 71 72 void AddObserver(Observer* observer); 73 void RemoveObserver(Observer* observer); 74 75 private: 76 typedef std::map<fileapi::FileSystemURL,int64, 77 fileapi::FileSystemURL::Comparator> URLCountMap; 78 79 bool IsChildOrParentWriting(const fileapi::FileSystemURL& url) const; 80 bool IsChildOrParentSyncing(const fileapi::FileSystemURL& url) const; 81 82 // If this count is non-zero positive there're ongoing write operations. 83 URLCountMap writing_; 84 85 // If this flag is set sync process is running on the file. 86 fileapi::FileSystemURLSet syncing_; 87 88 ObserverList<Observer> observer_list_; 89 90 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncStatus); 91 }; 92 93 } // namespace sync_file_system 94 95 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_STATUS_H_ 96