diff options
Diffstat (limited to 'sync/internal_api/public/engine')
7 files changed, 331 insertions, 0 deletions
diff --git a/sync/internal_api/public/engine/model_safe_worker.cc b/sync/internal_api/public/engine/model_safe_worker.cc new file mode 100644 index 0000000..2718acc --- /dev/null +++ b/sync/internal_api/public/engine/model_safe_worker.cc @@ -0,0 +1,75 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/engine/model_safe_worker.h" + +#include "base/json/json_writer.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" + +namespace browser_sync { + +base::DictionaryValue* ModelSafeRoutingInfoToValue( + const ModelSafeRoutingInfo& routing_info) { + base::DictionaryValue* dict = new base::DictionaryValue(); + for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin(); + it != routing_info.end(); ++it) { + dict->SetString(syncable::ModelTypeToString(it->first), + ModelSafeGroupToString(it->second)); + } + return dict; +} + +std::string ModelSafeRoutingInfoToString( + const ModelSafeRoutingInfo& routing_info) { + scoped_ptr<DictionaryValue> dict(ModelSafeRoutingInfoToValue(routing_info)); + std::string json; + base::JSONWriter::Write(dict.get(), &json); + return json; +} + +syncable::ModelTypeSet GetRoutingInfoTypes( + const ModelSafeRoutingInfo& routing_info) { + syncable::ModelTypeSet types; + for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin(); + it != routing_info.end(); ++it) { + types.Put(it->first); + } + return types; +} + +ModelSafeGroup GetGroupForModelType(const syncable::ModelType type, + const ModelSafeRoutingInfo& routes) { + ModelSafeRoutingInfo::const_iterator it = routes.find(type); + if (it == routes.end()) { + if (type != syncable::UNSPECIFIED && type != syncable::TOP_LEVEL_FOLDER) + LOG(WARNING) << "Entry does not belong to active ModelSafeGroup!"; + return GROUP_PASSIVE; + } + return it->second; +} + +std::string ModelSafeGroupToString(ModelSafeGroup group) { + switch (group) { + case GROUP_UI: + return "GROUP_UI"; + case GROUP_DB: + return "GROUP_DB"; + case GROUP_FILE: + return "GROUP_FILE"; + case GROUP_HISTORY: + return "GROUP_HISTORY"; + case GROUP_PASSIVE: + return "GROUP_PASSIVE"; + case GROUP_PASSWORD: + return "GROUP_PASSWORD"; + default: + NOTREACHED(); + return "INVALID"; + } +} + +ModelSafeWorker::~ModelSafeWorker() {} + +} // namespace browser_sync diff --git a/sync/internal_api/public/engine/model_safe_worker.h b/sync/internal_api/public/engine/model_safe_worker.h new file mode 100644 index 0000000..1a6c371 --- /dev/null +++ b/sync/internal_api/public/engine/model_safe_worker.h @@ -0,0 +1,87 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ +#define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ +#pragma once + +#include <map> +#include <string> +#include <vector> + +#include "base/callback.h" +#include "base/memory/ref_counted.h" +#include "sync/internal_api/public/syncable/model_type.h" +#include "sync/internal_api/public/util/syncer_error.h" + +namespace base { +class DictionaryValue; +} // namespace + +namespace browser_sync { + +typedef base::Callback<enum SyncerError(void)> WorkCallback; + +enum ModelSafeGroup { + GROUP_PASSIVE = 0, // Models that are just "passively" being synced; e.g. + // changes to these models don't need to be pushed to a + // native model. + GROUP_UI, // Models that live on UI thread and are being synced. + GROUP_DB, // Models that live on DB thread and are being synced. + GROUP_FILE, // Models that live on FILE thread and are being synced. + GROUP_HISTORY, // Models that live on history thread and are being + // synced. + GROUP_PASSWORD, // Models that live on the password thread and are + // being synced. On windows and linux, this runs on the + // DB thread. + MODEL_SAFE_GROUP_COUNT, +}; + +std::string ModelSafeGroupToString(ModelSafeGroup group); + +// The Syncer uses a ModelSafeWorker for all tasks that could potentially +// modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker +// only knows how to do one thing, and that is take some work (in a fully +// pre-bound callback) and have it performed (as in Run()) from a thread which +// is guaranteed to be "model-safe", where "safe" refers to not allowing us to +// cause an embedding application model to fall out of sync with the +// syncable::Directory due to a race. +class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { + public: + // Any time the Syncer performs model modifications (e.g employing a + // WriteTransaction), it should be done by this method to ensure it is done + // from a model-safe thread. + virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; + + virtual ModelSafeGroup GetModelSafeGroup() = 0; + + protected: + virtual ~ModelSafeWorker(); + + private: + friend class base::RefCountedThreadSafe<ModelSafeWorker>; +}; + +// A map that details which ModelSafeGroup each syncable::ModelType +// belongs to. Routing info can change in response to the user enabling / +// disabling sync for certain types, as well as model association completions. +typedef std::map<syncable::ModelType, ModelSafeGroup> + ModelSafeRoutingInfo; + +// Caller takes ownership of return value. +base::DictionaryValue* ModelSafeRoutingInfoToValue( + const ModelSafeRoutingInfo& routing_info); + +std::string ModelSafeRoutingInfoToString( + const ModelSafeRoutingInfo& routing_info); + +syncable::ModelTypeSet GetRoutingInfoTypes( + const ModelSafeRoutingInfo& routing_info); + +ModelSafeGroup GetGroupForModelType(const syncable::ModelType type, + const ModelSafeRoutingInfo& routes); + +} // namespace browser_sync + +#endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ diff --git a/sync/internal_api/public/engine/model_safe_worker_unittest.cc b/sync/internal_api/public/engine/model_safe_worker_unittest.cc new file mode 100644 index 0000000..5304c13 --- /dev/null +++ b/sync/internal_api/public/engine/model_safe_worker_unittest.cc @@ -0,0 +1,55 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/engine/model_safe_worker.h" + +#include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace browser_sync { +namespace { + +class ModelSafeWorkerTest : public ::testing::Test { +}; + +TEST_F(ModelSafeWorkerTest, ModelSafeRoutingInfoToValue) { + ModelSafeRoutingInfo routing_info; + routing_info[syncable::BOOKMARKS] = GROUP_PASSIVE; + routing_info[syncable::NIGORI] = GROUP_UI; + routing_info[syncable::PREFERENCES] = GROUP_DB; + DictionaryValue expected_value; + expected_value.SetString("Bookmarks", "GROUP_PASSIVE"); + expected_value.SetString("Encryption keys", "GROUP_UI"); + expected_value.SetString("Preferences", "GROUP_DB"); + scoped_ptr<DictionaryValue> value( + ModelSafeRoutingInfoToValue(routing_info)); + EXPECT_TRUE(value->Equals(&expected_value)); +} + +TEST_F(ModelSafeWorkerTest, ModelSafeRoutingInfoToString) { + ModelSafeRoutingInfo routing_info; + routing_info[syncable::BOOKMARKS] = GROUP_PASSIVE; + routing_info[syncable::NIGORI] = GROUP_UI; + routing_info[syncable::PREFERENCES] = GROUP_DB; + EXPECT_EQ( + "{\"Bookmarks\":\"GROUP_PASSIVE\",\"Encryption keys\":\"GROUP_UI\"," + "\"Preferences\":\"GROUP_DB\"}", + ModelSafeRoutingInfoToString(routing_info)); +} + +TEST_F(ModelSafeWorkerTest, GetRoutingInfoTypes) { + ModelSafeRoutingInfo routing_info; + routing_info[syncable::BOOKMARKS] = GROUP_PASSIVE; + routing_info[syncable::NIGORI] = GROUP_UI; + routing_info[syncable::PREFERENCES] = GROUP_DB; + const syncable::ModelTypeSet expected_types( + syncable::BOOKMARKS, + syncable::NIGORI, + syncable::PREFERENCES); + EXPECT_TRUE(GetRoutingInfoTypes(routing_info).Equals(expected_types)); +} + +} // namespace +} // namespace browser_sync diff --git a/sync/internal_api/public/engine/passive_model_worker.cc b/sync/internal_api/public/engine/passive_model_worker.cc new file mode 100644 index 0000000..26fccc1 --- /dev/null +++ b/sync/internal_api/public/engine/passive_model_worker.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/engine/passive_model_worker.h" + +#include "base/message_loop.h" + +namespace browser_sync { + +PassiveModelWorker::PassiveModelWorker(const MessageLoop* sync_loop) + : sync_loop_(sync_loop) {} + +PassiveModelWorker::~PassiveModelWorker() { +} + +SyncerError PassiveModelWorker::DoWorkAndWaitUntilDone( + const WorkCallback& work) { + DCHECK_EQ(MessageLoop::current(), sync_loop_); + // Simply do the work on the current thread. + return work.Run(); +} + +ModelSafeGroup PassiveModelWorker::GetModelSafeGroup() { + return GROUP_PASSIVE; +} + +} // namespace browser_sync diff --git a/sync/internal_api/public/engine/passive_model_worker.h b/sync/internal_api/public/engine/passive_model_worker.h new file mode 100644 index 0000000..7c0ddf3 --- /dev/null +++ b/sync/internal_api/public/engine/passive_model_worker.h @@ -0,0 +1,40 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_PASSIVE_MODEL_WORKER_H_ +#define SYNC_INTERNAL_API_PUBLIC_ENGINE_PASSIVE_MODEL_WORKER_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "sync/internal_api/public/engine/model_safe_worker.h" +#include "sync/internal_api/public/util/syncer_error.h" + +class MessageLoop; + +namespace browser_sync { + +// Implementation of ModelSafeWorker for passive types. All work is +// done on the same thread DoWorkAndWaitUntilDone (i.e., the sync +// thread). +class PassiveModelWorker : public ModelSafeWorker { + public: + explicit PassiveModelWorker(const MessageLoop* sync_loop); + + // ModelSafeWorker implementation. Called on the sync thread. + virtual SyncerError DoWorkAndWaitUntilDone( + const WorkCallback& work) OVERRIDE; + virtual ModelSafeGroup GetModelSafeGroup() OVERRIDE; + + private: + virtual ~PassiveModelWorker(); + + const MessageLoop* const sync_loop_; + + DISALLOW_COPY_AND_ASSIGN(PassiveModelWorker); +}; + +} // namespace browser_sync + +#endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_PASSIVE_MODEL_WORKER_H_ diff --git a/sync/internal_api/public/engine/polling_constants.cc b/sync/internal_api/public/engine/polling_constants.cc new file mode 100644 index 0000000..d7fe753 --- /dev/null +++ b/sync/internal_api/public/engine/polling_constants.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/basictypes.h" +#include "sync/internal_api/public/engine/polling_constants.h" + +namespace browser_sync { + +// Server can overwrite these values via client commands. +// Standard short poll. This is used when XMPP is off. +// We use high values here to ensure that failure to receive poll updates from +// the server doesn't result in rapid-fire polling from the client due to low +// local limits. +const int64 kDefaultShortPollIntervalSeconds = 3600 * 8; +// Long poll is used when XMPP is on. +const int64 kDefaultLongPollIntervalSeconds = 3600 * 12; + +// Maximum interval for exponential backoff. +const int64 kMaxBackoffSeconds = 60 * 60 * 4; // 4 hours. + +// Backoff interval randomization factor. +const int kBackoffRandomizationFactor = 2; + +} // namespace browser_sync + diff --git a/sync/internal_api/public/engine/polling_constants.h b/sync/internal_api/public/engine/polling_constants.h new file mode 100644 index 0000000..9adfe92 --- /dev/null +++ b/sync/internal_api/public/engine/polling_constants.h @@ -0,0 +1,20 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Constants used by SyncScheduler when polling servers for updates. + +#ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_POLLING_CONSTANTS_H_ +#define SYNC_INTERNAL_API_PUBLIC_ENGINE_POLLING_CONSTANTS_H_ +#pragma once + +namespace browser_sync { + +extern const int64 kDefaultShortPollIntervalSeconds; +extern const int64 kDefaultLongPollIntervalSeconds; +extern const int64 kMaxBackoffSeconds; +extern const int kBackoffRandomizationFactor; + +} // namespace browser_sync + +#endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_POLLING_CONSTANTS_H_ |