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.
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_
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"
21 namespace sync_file_system
{
23 // Represents local file sync status.
24 // This class is supposed to run only on IO thread.
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.
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
{
38 virtual ~Observer() {}
39 virtual void OnSyncEnabled(const fileapi::FileSystemURL
& url
) = 0;
40 virtual void OnWriteEnabled(const fileapi::FileSystemURL
& url
) = 0;
42 DISALLOW_COPY_AND_ASSIGN(Observer
);
45 LocalFileSyncStatus();
46 ~LocalFileSyncStatus();
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
);
52 // Decrement writing counter for |url|.
53 void EndWriting(const fileapi::FileSystemURL
& url
);
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
);
59 // Clears the syncing flag for |url| and enable writing.
60 void EndSyncing(const fileapi::FileSystemURL
& url
);
62 // Returns true if the |url| or its parent or child is in writing.
63 bool IsWriting(const fileapi::FileSystemURL
& url
) const;
65 // Returns true if the |url| is enabled for writing (i.e. not in syncing).
66 bool IsWritable(const fileapi::FileSystemURL
& url
) const;
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;
72 void AddObserver(Observer
* observer
);
73 void RemoveObserver(Observer
* observer
);
76 typedef std::map
<fileapi::FileSystemURL
, int64
,
77 fileapi::FileSystemURL::Comparator
> URLCountMap
;
79 bool IsChildOrParentWriting(const fileapi::FileSystemURL
& url
) const;
80 bool IsChildOrParentSyncing(const fileapi::FileSystemURL
& url
) const;
82 // If this count is non-zero positive there're ongoing write operations.
85 // If this flag is set sync process is running on the file.
86 fileapi::FileSystemURLSet syncing_
;
88 ObserverList
<Observer
> observer_list_
;
90 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncStatus
);
93 } // namespace sync_file_system
95 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_STATUS_H_