| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 entry. | 3 // found in the LICENSE entry. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ |
| 6 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ | 6 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
| 14 #include "base/time.h" | |
| 15 #include "chrome/browser/sync/engine/client_command_channel.h" | |
| 16 #include "chrome/browser/sync/engine/conflict_resolver.h" | 14 #include "chrome/browser/sync/engine/conflict_resolver.h" |
| 17 #include "chrome/browser/sync/engine/syncer_types.h" | 15 #include "chrome/browser/sync/engine/syncer_types.h" |
| 18 #include "chrome/browser/sync/engine/syncproto.h" | 16 #include "chrome/browser/sync/engine/syncproto.h" |
| 17 #include "chrome/browser/sync/sessions/sync_session.h" |
| 19 #include "chrome/browser/sync/syncable/directory_event.h" | 18 #include "chrome/browser/sync/syncable/directory_event.h" |
| 20 #include "chrome/browser/sync/util/closure.h" | 19 #include "chrome/browser/sync/util/closure.h" |
| 21 #include "chrome/browser/sync/util/event_sys-inl.h" | 20 #include "chrome/browser/sync/util/event_sys-inl.h" |
| 22 #include "chrome/browser/sync/util/event_sys.h" | 21 #include "chrome/browser/sync/util/event_sys.h" |
| 23 #include "chrome/browser/sync/util/extensions_activity_monitor.h" | 22 #include "chrome/browser/sync/util/extensions_activity_monitor.h" |
| 24 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 23 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 25 | 24 |
| 26 namespace syncable { | 25 namespace syncable { |
| 27 class Directory; | 26 class Directory; |
| 28 class DirectoryManager; | 27 class DirectoryManager; |
| 29 class Entry; | 28 class Entry; |
| 30 class Id; | 29 class Id; |
| 31 class MutableEntry; | 30 class MutableEntry; |
| 32 class WriteTransaction; | 31 class WriteTransaction; |
| 33 } // namespace syncable | 32 } // namespace syncable |
| 34 | 33 |
| 35 namespace browser_sync { | 34 namespace browser_sync { |
| 36 | 35 |
| 37 class ModelSafeWorker; | 36 class ModelSafeWorker; |
| 38 class ServerConnectionManager; | 37 class ServerConnectionManager; |
| 39 class SyncProcessState; | 38 class SyncProcessState; |
| 40 class SyncerSession; | |
| 41 class URLFactory; | 39 class URLFactory; |
| 42 struct HttpResponse; | 40 struct HttpResponse; |
| 43 | 41 |
| 44 static const int kDefaultMaxCommitBatchSize = 25; | 42 static const int kDefaultMaxCommitBatchSize = 25; |
| 45 | 43 |
| 46 enum SyncerStep { | 44 enum SyncerStep { |
| 47 SYNCER_BEGIN, | 45 SYNCER_BEGIN, |
| 48 DOWNLOAD_UPDATES, | 46 DOWNLOAD_UPDATES, |
| 49 PROCESS_CLIENT_COMMAND, | 47 PROCESS_CLIENT_COMMAND, |
| 50 VERIFY_UPDATES, | 48 VERIFY_UPDATES, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 68 // A Syncer instance expects to run on a dedicated thread. Calls | 66 // A Syncer instance expects to run on a dedicated thread. Calls |
| 69 // to SyncShare() may take an unbounded amount of time, as SyncerCommands | 67 // to SyncShare() may take an unbounded amount of time, as SyncerCommands |
| 70 // may block on network i/o, on lock contention, or on tasks posted to | 68 // may block on network i/o, on lock contention, or on tasks posted to |
| 71 // other threads. | 69 // other threads. |
| 72 class Syncer { | 70 class Syncer { |
| 73 public: | 71 public: |
| 74 typedef std::vector<int64> UnsyncedMetaHandles; | 72 typedef std::vector<int64> UnsyncedMetaHandles; |
| 75 | 73 |
| 76 // The constructor may be called from a thread that is not the Syncer's | 74 // The constructor may be called from a thread that is not the Syncer's |
| 77 // dedicated thread, to allow some flexibility in the setup. | 75 // dedicated thread, to allow some flexibility in the setup. |
| 78 Syncer(syncable::DirectoryManager* dirman, const PathString& account_name, | 76 explicit Syncer(sessions::SyncSessionContext* context); |
| 79 ServerConnectionManager* connection_manager, | 77 ~Syncer() {} |
| 80 ModelSafeWorker* model_safe_worker); | |
| 81 ~Syncer(); | |
| 82 | 78 |
| 83 // Called by other threads to tell the syncer to stop what it's doing | 79 // Called by other threads to tell the syncer to stop what it's doing |
| 84 // and return early from SyncShare, if possible. | 80 // and return early from SyncShare, if possible. |
| 85 bool ExitRequested() { return early_exit_requested_; } | 81 bool ExitRequested() { return early_exit_requested_; } |
| 86 void RequestEarlyExit() { early_exit_requested_ = true; } | 82 void RequestEarlyExit() { early_exit_requested_ = true; } |
| 87 | 83 |
| 88 // SyncShare(...) variants cause one sync cycle to occur. The return value | 84 // SyncShare(...) variants cause one sync cycle to occur. The return value |
| 89 // indicates whether we should sync again. If we should not sync again, | 85 // indicates whether we should sync again. If we should not sync again, |
| 90 // it doesn't necessarily mean everything is OK; we could be throttled for | 86 // it doesn't necessarily mean everything is OK; we could be throttled for |
| 91 // example. Like a good parent, it is the caller's responsibility to clean up | 87 // example. Like a good parent, it is the caller's responsibility to clean up |
| 92 // after the syncer when it finishes a sync share operation and honor | 88 // after the syncer when it finishes a sync share operation and honor |
| 93 // server mandated throttles. | 89 // server mandated throttles. |
| 94 // The zero-argument version of SyncShare is provided for unit tests. | 90 // The zero-argument version of SyncShare is provided for unit tests. |
| 95 // When |sync_process_state| is provided, it is used as the syncer state | 91 // When |sync_process_state| is provided, it is used as the syncer state |
| 96 // for the sync cycle. It is treated as an input/output parameter. | 92 // for the sync cycle. It is treated as an input/output parameter. |
| 97 // When |first_step| and |last_step| are provided, this means to perform | 93 // When |first_step| and |last_step| are provided, this means to perform |
| 98 // a partial sync cycle, stopping after |last_step| is performed. | 94 // a partial sync cycle, stopping after |last_step| is performed. |
| 99 bool SyncShare(); | 95 bool SyncShare(sessions::SyncSession::Delegate* delegate); |
| 100 bool SyncShare(SyncProcessState* sync_process_state); | 96 bool SyncShare(SyncerStep first_step, SyncerStep last_step, |
| 101 bool SyncShare(SyncerStep first_step, SyncerStep last_step); | 97 sessions::SyncSession::Delegate* delegate); |
| 102 | 98 |
| 103 // Limit the batch size of commit operations to a specified number of items. | 99 // Limit the batch size of commit operations to a specified number of items. |
| 104 void set_max_commit_batch_size(int x) { max_commit_batch_size_ = x; } | 100 void set_max_commit_batch_size(int x) { max_commit_batch_size_ = x; } |
| 105 | 101 |
| 106 ConflictResolver* conflict_resolver() { return &resolver_; } | |
| 107 | |
| 108 PathString account_name() { return account_name_; } | |
| 109 | |
| 110 SyncerEventChannel* channel() const { return syncer_event_channel_.get(); } | |
| 111 | |
| 112 ShutdownChannel* shutdown_channel() const { return shutdown_channel_.get(); } | 102 ShutdownChannel* shutdown_channel() const { return shutdown_channel_.get(); } |
| 113 | 103 |
| 114 ModelSafeWorker* model_safe_worker() { return model_safe_worker_; } | |
| 115 | |
| 116 // Syncer will take ownership of this channel and it will be destroyed along | 104 // Syncer will take ownership of this channel and it will be destroyed along |
| 117 // with the Syncer instance. | 105 // with the Syncer instance. |
| 118 void set_shutdown_channel(ShutdownChannel* channel) { | 106 void set_shutdown_channel(ShutdownChannel* channel) { |
| 119 shutdown_channel_.reset(channel); | 107 shutdown_channel_.reset(channel); |
| 120 } | 108 } |
| 121 | 109 |
| 122 void set_command_channel(ClientCommandChannel* channel) { | |
| 123 command_channel_ = channel; | |
| 124 } | |
| 125 | |
| 126 // Volatile reader for the source member of the syncer session object. The | 110 // Volatile reader for the source member of the syncer session object. The |
| 127 // value is set to the SYNC_CYCLE_CONTINUATION value to signal that it has | 111 // value is set to the SYNC_CYCLE_CONTINUATION value to signal that it has |
| 128 // been read. | 112 // been read. |
| 129 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE TestAndSetUpdatesSource() { | 113 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE TestAndSetUpdatesSource() { |
| 130 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE old_source = | 114 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE old_source = |
| 131 updates_source_; | 115 updates_source_; |
| 132 set_updates_source(sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION); | 116 set_updates_source(sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION); |
| 133 return old_source; | 117 return old_source; |
| 134 } | 118 } |
| 135 | 119 |
| 136 void set_updates_source( | 120 void set_updates_source( |
| 137 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE source) { | 121 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE source) { |
| 138 updates_source_ = source; | 122 updates_source_ = source; |
| 139 } | 123 } |
| 140 | 124 |
| 141 bool notifications_enabled() const { | |
| 142 return notifications_enabled_; | |
| 143 } | |
| 144 | |
| 145 void set_notifications_enabled(bool state) { | |
| 146 notifications_enabled_ = state; | |
| 147 } | |
| 148 | |
| 149 base::TimeTicks silenced_until() const { return silenced_until_; } | |
| 150 bool is_silenced() const { return !silenced_until_.is_null(); } | |
| 151 private: | 125 private: |
| 152 void RequestNudge(int milliseconds); | 126 void RequestNudge(int milliseconds); |
| 153 | 127 |
| 154 // Implements the PROCESS_CLIENT_COMMAND syncer step. | 128 // Implements the PROCESS_CLIENT_COMMAND syncer step. |
| 155 void ProcessClientCommand(SyncerSession *session); | 129 void ProcessClientCommand(sessions::SyncSession *session); |
| 156 | 130 |
| 157 void SyncShare(SyncerSession* session); | 131 // Resets transient state and runs from SYNCER_BEGIN to SYNCER_END. |
| 158 void SyncShare(SyncerSession* session, | 132 bool SyncShare(sessions::SyncSession* session); |
| 133 |
| 134 // This is the bottom-most SyncShare variant, and does not cause transient |
| 135 // state to be reset in session. |
| 136 void SyncShare(sessions::SyncSession* session, |
| 159 SyncerStep first_step, | 137 SyncerStep first_step, |
| 160 SyncerStep last_step); | 138 SyncerStep last_step); |
| 161 | 139 |
| 162 PathString account_name_; | |
| 163 bool early_exit_requested_; | 140 bool early_exit_requested_; |
| 164 | 141 |
| 165 int32 max_commit_batch_size_; | 142 int32 max_commit_batch_size_; |
| 166 | 143 |
| 167 ServerConnectionManager* connection_manager_; | 144 ConflictResolver resolver_; |
| 145 scoped_ptr<SyncerEventChannel> syncer_event_channel_; |
| 146 sessions::ScopedSessionContextConflictResolver resolver_scoper_; |
| 147 sessions::ScopedSessionContextSyncerEventChannel event_channel_scoper_; |
| 148 sessions::SyncSessionContext* context_; |
| 168 | 149 |
| 169 ConflictResolver resolver_; | |
| 170 syncable::DirectoryManager* const dirman_; | |
| 171 | |
| 172 // When we're over bandwidth quota, we don't update until past this time. | |
| 173 base::TimeTicks silenced_until_; | |
| 174 | |
| 175 scoped_ptr<SyncerEventChannel> syncer_event_channel_; | |
| 176 scoped_ptr<ShutdownChannel> shutdown_channel_; | 150 scoped_ptr<ShutdownChannel> shutdown_channel_; |
| 177 ClientCommandChannel* command_channel_; | |
| 178 | |
| 179 // A worker capable of processing work closures on a thread that is | |
| 180 // guaranteed to be safe for model modifications. This is created and owned | |
| 181 // by the SyncerThread that created us. | |
| 182 ModelSafeWorker* model_safe_worker_; | |
| 183 | |
| 184 // We use this to stuff extensions activity into CommitMessages so the server | |
| 185 // can correlate commit traffic with extension-related bookmark mutations. | |
| 186 ExtensionsActivityMonitor* extensions_monitor_; | |
| 187 | 151 |
| 188 // The source of the last nudge. | 152 // The source of the last nudge. |
| 189 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE updates_source_; | 153 sync_pb::GetUpdatesCallerInfo::GET_UPDATES_SOURCE updates_source_; |
| 190 | 154 |
| 191 // True only if the notification channel is authorized and open. | |
| 192 bool notifications_enabled_; | |
| 193 | |
| 194 // A callback hook used in unittests to simulate changes between conflict set | 155 // A callback hook used in unittests to simulate changes between conflict set |
| 195 // building and conflict resolution. | 156 // building and conflict resolution. |
| 196 Closure* pre_conflict_resolution_closure_; | 157 Closure* pre_conflict_resolution_closure_; |
| 197 | 158 |
| 159 friend class SyncerTest; |
| 160 FRIEND_TEST(SyncerTest, NameClashWithResolver); |
| 161 FRIEND_TEST(SyncerTest, IllegalAndLegalUpdates); |
| 198 FRIEND_TEST(SusanDeletingTest, | 162 FRIEND_TEST(SusanDeletingTest, |
| 199 NewServerItemInAFolderHierarchyWeHaveDeleted3); | 163 NewServerItemInAFolderHierarchyWeHaveDeleted3); |
| 200 FRIEND_TEST(SyncerTest, TestCommitListOrderingAndNewParent); | 164 FRIEND_TEST(SyncerTest, TestCommitListOrderingAndNewParent); |
| 201 FRIEND_TEST(SyncerTest, TestCommitListOrderingAndNewParentAndChild); | 165 FRIEND_TEST(SyncerTest, TestCommitListOrderingAndNewParentAndChild); |
| 202 FRIEND_TEST(SyncerTest, TestCommitListOrderingCounterexample); | 166 FRIEND_TEST(SyncerTest, TestCommitListOrderingCounterexample); |
| 203 FRIEND_TEST(SyncerTest, TestCommitListOrderingWithNesting); | 167 FRIEND_TEST(SyncerTest, TestCommitListOrderingWithNesting); |
| 204 FRIEND_TEST(SyncerTest, TestCommitListOrderingWithNewItems); | 168 FRIEND_TEST(SyncerTest, TestCommitListOrderingWithNewItems); |
| 205 FRIEND_TEST(SyncerTest, TestGetUnsyncedAndSimpleCommit); | 169 FRIEND_TEST(SyncerTest, TestGetUnsyncedAndSimpleCommit); |
| 170 FRIEND_TEST(SyncerTest, UnappliedUpdateDuringCommit); |
| 171 FRIEND_TEST(SyncerTest, DeletingEntryInFolder); |
| 172 FRIEND_TEST(SyncerTest, LongChangelistCreatesFakeOrphanedEntries); |
| 173 FRIEND_TEST(SyncerTest, QuicklyMergeDualCreatedHierarchy); |
| 174 FRIEND_TEST(SyncerTest, LongChangelistWithApplicationConflict); |
| 206 | 175 |
| 207 DISALLOW_COPY_AND_ASSIGN(Syncer); | 176 DISALLOW_COPY_AND_ASSIGN(Syncer); |
| 208 }; | 177 }; |
| 209 | 178 |
| 210 // Inline utility functions. | 179 // Inline utility functions. |
| 211 | 180 |
| 212 // Given iterator ranges from two collections sorted according to a common | 181 // Given iterator ranges from two collections sorted according to a common |
| 213 // strict weak ordering, return true if the two ranges contain any common | 182 // strict weak ordering, return true if the two ranges contain any common |
| 214 // items, and false if they do not. This function is in this header so that it | 183 // items, and false if they do not. This function is in this header so that it |
| 215 // can be tested. | 184 // can be tested. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 234 void CopyServerFields(syncable::Entry* src, syncable::MutableEntry* dest); | 203 void CopyServerFields(syncable::Entry* src, syncable::MutableEntry* dest); |
| 235 void ClearServerData(syncable::MutableEntry* entry); | 204 void ClearServerData(syncable::MutableEntry* entry); |
| 236 | 205 |
| 237 // Get update contents as a string. Intended for logging, and intended | 206 // Get update contents as a string. Intended for logging, and intended |
| 238 // to have a smaller footprint than the protobuf's built-in pretty printer. | 207 // to have a smaller footprint than the protobuf's built-in pretty printer. |
| 239 std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); | 208 std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); |
| 240 | 209 |
| 241 } // namespace browser_sync | 210 } // namespace browser_sync |
| 242 | 211 |
| 243 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ | 212 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_H_ |
| OLD | NEW |