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