OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SYNCABLE_SYNCABLE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <bitset> | 10 #include <bitset> |
11 #include <cstddef> | 11 #include <cstddef> |
12 #include <iosfwd> | 12 #include <iosfwd> |
13 #include <limits> | 13 #include <limits> |
14 #include <map> | 14 #include <map> |
15 #include <set> | 15 #include <set> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/atomicops.h" | 19 #include "base/atomicops.h" |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
21 #include "base/compiler_specific.h" | 21 #include "base/compiler_specific.h" |
22 #include "base/file_path.h" | 22 #include "base/file_path.h" |
23 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
24 #include "base/location.h" | 24 #include "base/location.h" |
25 #include "base/logging.h" | 25 #include "base/logging.h" |
26 #include "base/memory/ref_counted.h" | 26 #include "base/memory/ref_counted.h" |
27 #include "base/observer_list_threadsafe.h" | |
28 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
29 #include "base/time.h" | 28 #include "base/time.h" |
30 #include "chrome/browser/sync/protocol/sync.pb.h" | 29 #include "chrome/browser/sync/protocol/sync.pb.h" |
31 #include "chrome/browser/sync/syncable/blob.h" | 30 #include "chrome/browser/sync/syncable/blob.h" |
32 #include "chrome/browser/sync/syncable/dir_open_result.h" | 31 #include "chrome/browser/sync/syncable/dir_open_result.h" |
33 #include "chrome/browser/sync/syncable/directory_event.h" | 32 #include "chrome/browser/sync/syncable/directory_event.h" |
34 #include "chrome/browser/sync/syncable/syncable_id.h" | 33 #include "chrome/browser/sync/syncable/syncable_id.h" |
35 #include "chrome/browser/sync/syncable/model_type.h" | 34 #include "chrome/browser/sync/syncable/model_type.h" |
36 #include "chrome/browser/sync/util/immutable.h" | 35 #include "chrome/browser/sync/util/immutable.h" |
37 #include "chrome/browser/sync/util/time.h" | 36 #include "chrome/browser/sync/util/time.h" |
| 37 #include "chrome/browser/sync/util/weak_handle.h" |
38 | 38 |
39 namespace base { | 39 namespace base { |
40 class DictionaryValue; | 40 class DictionaryValue; |
41 class ListValue; | 41 class ListValue; |
42 } | 42 } |
43 | 43 |
44 namespace sync_api { | 44 namespace sync_api { |
45 class ReadTransaction; | 45 class ReadTransaction; |
46 class WriteNode; | 46 class WriteNode; |
47 class ReadNode; | 47 class ReadNode; |
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 EntryKernelSet dirty_metas; | 814 EntryKernelSet dirty_metas; |
815 MetahandleSet metahandles_to_purge; | 815 MetahandleSet metahandles_to_purge; |
816 }; | 816 }; |
817 | 817 |
818 Directory(); | 818 Directory(); |
819 virtual ~Directory(); | 819 virtual ~Directory(); |
820 | 820 |
821 // Does not take ownership of |delegate|, which must not be NULL. | 821 // Does not take ownership of |delegate|, which must not be NULL. |
822 // Starts sending events to |delegate| if the returned result is | 822 // Starts sending events to |delegate| if the returned result is |
823 // OPENED. Note that events to |delegate| may be sent from *any* | 823 // OPENED. Note that events to |delegate| may be sent from *any* |
824 // thread. | 824 // thread. |transaction_observer| must be initialized. |
825 DirOpenResult Open(const FilePath& file_path, const std::string& name, | 825 DirOpenResult Open(const FilePath& file_path, const std::string& name, |
826 DirectoryChangeDelegate* delegate); | 826 DirectoryChangeDelegate* delegate, |
| 827 const browser_sync::WeakHandle<TransactionObserver>& |
| 828 transaction_observer); |
827 | 829 |
828 // Stops sending events to the delegate. | 830 // Stops sending events to the delegate and the transaction |
| 831 // observer. |
829 void Close(); | 832 void Close(); |
830 | 833 |
831 int64 NextMetahandle(); | 834 int64 NextMetahandle(); |
832 // Always returns a negative id. Positive client ids are generated | 835 // Always returns a negative id. Positive client ids are generated |
833 // by the server only. | 836 // by the server only. |
834 Id NextId(); | 837 Id NextId(); |
835 | 838 |
836 const FilePath& file_path() const { return kernel_->db_path; } | 839 const FilePath& file_path() const { return kernel_->db_path; } |
837 bool good() const { return NULL != store_; } | 840 bool good() const { return NULL != store_; } |
838 | 841 |
(...skipping 20 matching lines...) Expand all Loading... |
859 // later. | 862 // later. |
860 std::string store_birthday() const; | 863 std::string store_birthday() const; |
861 void set_store_birthday(const std::string& store_birthday); | 864 void set_store_birthday(const std::string& store_birthday); |
862 | 865 |
863 std::string GetNotificationState() const; | 866 std::string GetNotificationState() const; |
864 void SetNotificationState(const std::string& notification_state); | 867 void SetNotificationState(const std::string& notification_state); |
865 | 868 |
866 // Unique to each account / client pair. | 869 // Unique to each account / client pair. |
867 std::string cache_guid() const; | 870 std::string cache_guid() const; |
868 | 871 |
869 // These are backed by a thread-safe observer list, and so can be | |
870 // called on any thread, and events will be sent to the observer on | |
871 // the same thread that it was added on. | |
872 void AddTransactionObserver(TransactionObserver* observer); | |
873 void RemoveTransactionObserver(TransactionObserver* observer); | |
874 | |
875 protected: // for friends, mainly used by Entry constructors | 872 protected: // for friends, mainly used by Entry constructors |
876 virtual EntryKernel* GetEntryByHandle(int64 handle); | 873 virtual EntryKernel* GetEntryByHandle(int64 handle); |
877 virtual EntryKernel* GetEntryByHandle(int64 metahandle, | 874 virtual EntryKernel* GetEntryByHandle(int64 metahandle, |
878 ScopedKernelLock* lock); | 875 ScopedKernelLock* lock); |
879 virtual EntryKernel* GetEntryById(const Id& id); | 876 virtual EntryKernel* GetEntryById(const Id& id); |
880 EntryKernel* GetEntryByServerTag(const std::string& tag); | 877 EntryKernel* GetEntryByServerTag(const std::string& tag); |
881 virtual EntryKernel* GetEntryByClientTag(const std::string& tag); | 878 virtual EntryKernel* GetEntryByClientTag(const std::string& tag); |
882 EntryKernel* GetRootEntry(); | 879 EntryKernel* GetRootEntry(); |
883 bool ReindexId(EntryKernel* const entry, const Id& new_id); | 880 bool ReindexId(EntryKernel* const entry, const Id& new_id); |
884 void ReindexParentId(EntryKernel* const entry, const Id& new_parent_id); | 881 void ReindexParentId(EntryKernel* const entry, const Id& new_parent_id); |
885 void ClearDirtyMetahandles(); | 882 void ClearDirtyMetahandles(); |
886 | 883 |
887 // These don't do semantic checking. | 884 // These don't do semantic checking. |
888 // The semantic checking is implemented higher up. | 885 // The semantic checking is implemented higher up. |
889 void UnlinkEntryFromOrder(EntryKernel* entry, | 886 void UnlinkEntryFromOrder(EntryKernel* entry, |
890 WriteTransaction* trans, | 887 WriteTransaction* trans, |
891 ScopedKernelLock* lock); | 888 ScopedKernelLock* lock); |
892 | 889 |
893 // Overridden by tests. | 890 // Overridden by tests. |
894 virtual DirectoryBackingStore* CreateBackingStore( | 891 virtual DirectoryBackingStore* CreateBackingStore( |
895 const std::string& dir_name, | 892 const std::string& dir_name, |
896 const FilePath& backing_filepath); | 893 const FilePath& backing_filepath); |
897 | 894 |
898 private: | 895 private: |
899 // These private versions expect the kernel lock to already be held | 896 // These private versions expect the kernel lock to already be held |
900 // before calling. | 897 // before calling. |
901 EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock); | 898 EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock); |
902 | 899 |
903 DirOpenResult OpenImpl(const FilePath& file_path, const std::string& name, | 900 DirOpenResult OpenImpl( |
904 DirectoryChangeDelegate* delegate); | 901 const FilePath& file_path, const std::string& name, |
| 902 DirectoryChangeDelegate* delegate, |
| 903 const browser_sync::WeakHandle<TransactionObserver>& |
| 904 transaction_observer); |
905 | 905 |
906 template <class T> void TestAndSet(T* kernel_data, const T* data_to_set); | 906 template <class T> void TestAndSet(T* kernel_data, const T* data_to_set); |
907 | 907 |
908 public: | 908 public: |
909 typedef std::vector<int64> ChildHandles; | 909 typedef std::vector<int64> ChildHandles; |
910 | 910 |
911 // Returns the child meta handles (even those for deleted/unlinked | 911 // Returns the child meta handles (even those for deleted/unlinked |
912 // nodes) for given parent id. Clears |result| if there are no | 912 // nodes) for given parent id. Clears |result| if there are no |
913 // children. | 913 // children. |
914 void GetChildHandlesById(BaseTransaction*, const Id& parent_id, | 914 void GetChildHandlesById(BaseTransaction*, const Id& parent_id, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 // ParentIdChildIndex. | 1042 // ParentIdChildIndex. |
1043 typedef Index<ParentIdAndHandleIndexer>::Set ParentIdChildIndex; | 1043 typedef Index<ParentIdAndHandleIndexer>::Set ParentIdChildIndex; |
1044 | 1044 |
1045 // Contains both deleted and existing entries with tags. | 1045 // Contains both deleted and existing entries with tags. |
1046 // We can't store only existing tags because the client would create | 1046 // We can't store only existing tags because the client would create |
1047 // items that had a duplicated ID in the end, resulting in a DB key | 1047 // items that had a duplicated ID in the end, resulting in a DB key |
1048 // violation. ID reassociation would fail after an attempted commit. | 1048 // violation. ID reassociation would fail after an attempted commit. |
1049 typedef Index<ClientTagIndexer>::Set ClientTagIndex; | 1049 typedef Index<ClientTagIndexer>::Set ClientTagIndex; |
1050 | 1050 |
1051 protected: | 1051 protected: |
1052 // Used by tests. | 1052 // Used by tests. |delegate| must not be NULL. |
1053 void InitKernel(const std::string& name, DirectoryChangeDelegate* delegate); | 1053 // |transaction_observer| must be initialized. |
| 1054 void InitKernelForTest( |
| 1055 const std::string& name, |
| 1056 DirectoryChangeDelegate* delegate, |
| 1057 const browser_sync::WeakHandle<TransactionObserver>& |
| 1058 transaction_observer); |
1054 | 1059 |
1055 private: | 1060 private: |
1056 struct Kernel { | 1061 struct Kernel { |
1057 // |delegate| can be NULL. | 1062 // |delegate| must not be NULL. |transaction_observer| must be |
| 1063 // initialized. |
1058 Kernel(const FilePath& db_path, const std::string& name, | 1064 Kernel(const FilePath& db_path, const std::string& name, |
1059 const KernelLoadInfo& info, DirectoryChangeDelegate* delegate); | 1065 const KernelLoadInfo& info, DirectoryChangeDelegate* delegate, |
| 1066 const browser_sync::WeakHandle<TransactionObserver>& |
| 1067 transaction_observer); |
1060 | 1068 |
1061 ~Kernel(); | 1069 ~Kernel(); |
1062 | 1070 |
1063 void AddRef(); // For convenience. | 1071 void AddRef(); // For convenience. |
1064 void Release(); | 1072 void Release(); |
1065 | 1073 |
1066 FilePath const db_path; | 1074 FilePath const db_path; |
1067 // TODO(timsteele): audit use of the member and remove if possible | 1075 // TODO(timsteele): audit use of the member and remove if possible |
1068 volatile base::subtle::AtomicWord refcount; | 1076 volatile base::subtle::AtomicWord refcount; |
1069 | 1077 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1118 // unique server IDs. No need to lock, only written at init time. | 1126 // unique server IDs. No need to lock, only written at init time. |
1119 const std::string cache_guid; | 1127 const std::string cache_guid; |
1120 | 1128 |
1121 // It doesn't make sense for two threads to run SaveChanges at the same | 1129 // It doesn't make sense for two threads to run SaveChanges at the same |
1122 // time; this mutex protects that activity. | 1130 // time; this mutex protects that activity. |
1123 base::Lock save_changes_mutex; | 1131 base::Lock save_changes_mutex; |
1124 | 1132 |
1125 // The next metahandle is protected by kernel mutex. | 1133 // The next metahandle is protected by kernel mutex. |
1126 int64 next_metahandle; | 1134 int64 next_metahandle; |
1127 | 1135 |
1128 // The delegate for directory change events. Can be NULL. | 1136 // The delegate for directory change events. Must not be NULL. |
1129 DirectoryChangeDelegate* const delegate; | 1137 DirectoryChangeDelegate* const delegate; |
1130 | 1138 |
1131 // The transaction observers. | 1139 // The transaction observer. |
1132 scoped_refptr<ObserverListThreadSafe<TransactionObserver> > observers; | 1140 const browser_sync::WeakHandle<TransactionObserver> transaction_observer; |
1133 }; | 1141 }; |
1134 | 1142 |
1135 // Helper method used to do searches on |parent_id_child_index|. | 1143 // Helper method used to do searches on |parent_id_child_index|. |
1136 ParentIdChildIndex::iterator LocateInParentChildIndex( | 1144 ParentIdChildIndex::iterator LocateInParentChildIndex( |
1137 const ScopedKernelLock& lock, | 1145 const ScopedKernelLock& lock, |
1138 const Id& parent_id, | 1146 const Id& parent_id, |
1139 int64 position_in_parent, | 1147 int64 position_in_parent, |
1140 const Id& item_id_for_tiebreaking); | 1148 const Id& item_id_for_tiebreaking); |
1141 | 1149 |
1142 // Return an iterator to the beginning of the range of the children of | 1150 // Return an iterator to the beginning of the range of the children of |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1266 bool IsLegalNewParent(BaseTransaction* trans, const Id& id, const Id& parentid); | 1274 bool IsLegalNewParent(BaseTransaction* trans, const Id& id, const Id& parentid); |
1267 | 1275 |
1268 // This function sets only the flags needed to get this entry to sync. | 1276 // This function sets only the flags needed to get this entry to sync. |
1269 void MarkForSyncing(syncable::MutableEntry* e); | 1277 void MarkForSyncing(syncable::MutableEntry* e); |
1270 | 1278 |
1271 } // namespace syncable | 1279 } // namespace syncable |
1272 | 1280 |
1273 std::ostream& operator <<(std::ostream&, const syncable::Blob&); | 1281 std::ostream& operator <<(std::ostream&, const syncable::Blob&); |
1274 | 1282 |
1275 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 1283 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
OLD | NEW |