Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / local_file_sync_status.h
blob1556d97eab13766c880924771838cc2033d433b6
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_
8 #include <map>
9 #include <set>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/observer_list.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "storage/browser/fileapi/file_system_url.h"
18 namespace storage {
19 class FileSystemURL;
22 namespace sync_file_system {
24 // Represents local file sync status.
25 // This class is supposed to run only on IO thread.
27 // This class manages two important synchronization flags: writing (counter)
28 // and syncing (flag). Writing counter keeps track of which URL is in
29 // writing and syncing flag indicates which URL is in syncing.
31 // An entry can have multiple writers but sync is exclusive and cannot overwrap
32 // with any writes or syncs.
33 class LocalFileSyncStatus
34 : public base::NonThreadSafe {
35 public:
36 typedef std::pair<GURL, storage::FileSystemType> OriginAndType;
38 class Observer {
39 public:
40 Observer() {}
41 virtual ~Observer() {}
42 virtual void OnSyncEnabled(const storage::FileSystemURL& url) = 0;
43 virtual void OnWriteEnabled(const storage::FileSystemURL& url) = 0;
45 private:
46 DISALLOW_COPY_AND_ASSIGN(Observer);
49 LocalFileSyncStatus();
50 ~LocalFileSyncStatus();
52 // Increment writing counter for |url|.
53 // This should not be called if the |url| is not writable.
54 void StartWriting(const storage::FileSystemURL& url);
56 // Decrement writing counter for |url|.
57 void EndWriting(const storage::FileSystemURL& url);
59 // Start syncing for |url| and disable writing.
60 // This should not be called if |url| is in syncing or in writing.
61 void StartSyncing(const storage::FileSystemURL& url);
63 // Clears the syncing flag for |url| and enable writing.
64 void EndSyncing(const storage::FileSystemURL& url);
66 // Returns true if the |url| or its parent or child is in writing.
67 bool IsWriting(const storage::FileSystemURL& url) const;
69 // Returns true if the |url| is enabled for writing (i.e. not in syncing).
70 bool IsWritable(const storage::FileSystemURL& url) const;
72 // Returns true if the |url| is enabled for syncing (i.e. neither in
73 // syncing nor writing).
74 bool IsSyncable(const storage::FileSystemURL& url) const;
76 void AddObserver(Observer* observer);
77 void RemoveObserver(Observer* observer);
79 private:
80 FRIEND_TEST_ALL_PREFIXES(LocalFileSyncStatusTest, WritingOnPathsWithPeriod);
81 FRIEND_TEST_ALL_PREFIXES(LocalFileSyncStatusTest, SyncingOnPathsWithPeriod);
83 typedef std::set<base::FilePath> PathSet;
84 typedef std::map<OriginAndType, PathSet> URLSet;
86 typedef std::map<base::FilePath, int64> PathBucket;
87 typedef std::map<OriginAndType, PathBucket> URLBucket;
89 bool IsChildOrParentWriting(const storage::FileSystemURL& url) const;
90 bool IsChildOrParentSyncing(const storage::FileSystemURL& url) const;
92 // If this count is non-zero positive there're ongoing write operations.
93 URLBucket writing_;
95 // If this flag is set sync process is running on the file.
96 URLSet syncing_;
98 base::ObserverList<Observer> observer_list_;
100 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncStatus);
103 } // namespace sync_file_system
105 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_LOCAL_FILE_SYNC_STATUS_H_