summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/js_mutation_event_observer.cc
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-21 09:59:42 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-21 09:59:42 +0000
commit91835ca82c6ff7f1aeb68e314a3842da1aca6841 (patch)
treec7e8119bd0fb2a8e1d6910c969e1d3f62dbff1f8 /sync/internal_api/js_mutation_event_observer.cc
parent7983f3bae6ab9c86aea81816e6bb1bd15ab114fe (diff)
downloadchromium_src-91835ca82c6ff7f1aeb68e314a3842da1aca6841.zip
chromium_src-91835ca82c6ff7f1aeb68e314a3842da1aca6841.tar.gz
chromium_src-91835ca82c6ff7f1aeb68e314a3842da1aca6841.tar.bz2
[Sync] Move 'syncapi_core' and 'sync_unit_tests' targets to sync/
Also move related test files. Lock down deps for sync/internal_api. Clean up some deps on chrome/browser/sync. BUG=117585 TEST= Review URL: https://chromiumcodereview.appspot.com/10147003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133349 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync/internal_api/js_mutation_event_observer.cc')
-rw-r--r--sync/internal_api/js_mutation_event_observer.cc112
1 files changed, 112 insertions, 0 deletions
diff --git a/sync/internal_api/js_mutation_event_observer.cc b/sync/internal_api/js_mutation_event_observer.cc
new file mode 100644
index 0000000..9833b09
--- /dev/null
+++ b/sync/internal_api/js_mutation_event_observer.cc
@@ -0,0 +1,112 @@
+// 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/js_mutation_event_observer.h"
+
+#include <string>
+
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/string_number_conversions.h"
+#include "base/values.h"
+#include "sync/js/js_event_details.h"
+#include "sync/js/js_event_handler.h"
+
+namespace browser_sync {
+
+JsMutationEventObserver::JsMutationEventObserver()
+ : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
+
+JsMutationEventObserver::~JsMutationEventObserver() {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+}
+
+base::WeakPtr<JsMutationEventObserver> JsMutationEventObserver::AsWeakPtr() {
+ return weak_ptr_factory_.GetWeakPtr();
+}
+
+void JsMutationEventObserver::InvalidateWeakPtrs() {
+ weak_ptr_factory_.InvalidateWeakPtrs();
+}
+
+void JsMutationEventObserver::SetJsEventHandler(
+ const WeakHandle<JsEventHandler>& event_handler) {
+ event_handler_ = event_handler;
+}
+
+namespace {
+
+// Max number of changes we attempt to convert to values (to avoid
+// running out of memory).
+const size_t kChangeLimit = 100;
+
+} // namespace
+
+void JsMutationEventObserver::OnChangesApplied(
+ syncable::ModelType model_type,
+ int64 write_transaction_id,
+ const sync_api::ImmutableChangeRecordList& changes) {
+ if (!event_handler_.IsInitialized()) {
+ return;
+ }
+ DictionaryValue details;
+ details.SetString("modelType", syncable::ModelTypeToString(model_type));
+ details.SetString("writeTransactionId",
+ base::Int64ToString(write_transaction_id));
+ base::Value* changes_value = NULL;
+ const size_t changes_size = changes.Get().size();
+ if (changes_size <= kChangeLimit) {
+ ListValue* changes_list = new ListValue();
+ for (sync_api::ChangeRecordList::const_iterator it =
+ changes.Get().begin(); it != changes.Get().end(); ++it) {
+ changes_list->Append(it->ToValue());
+ }
+ changes_value = changes_list;
+ } else {
+ changes_value =
+ Value::CreateStringValue(
+ base::Uint64ToString(static_cast<uint64>(changes_size)) +
+ " changes");
+ }
+ details.Set("changes", changes_value);
+ HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details));
+}
+
+void JsMutationEventObserver::OnChangesComplete(
+ syncable::ModelType model_type) {
+ if (!event_handler_.IsInitialized()) {
+ return;
+ }
+ DictionaryValue details;
+ details.SetString("modelType", syncable::ModelTypeToString(model_type));
+ HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details));
+}
+
+void JsMutationEventObserver::OnTransactionWrite(
+ const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
+ syncable::ModelTypeSet models_with_changes) {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ if (!event_handler_.IsInitialized()) {
+ return;
+ }
+ DictionaryValue details;
+ details.Set("writeTransactionInfo",
+ write_transaction_info.Get().ToValue(kChangeLimit));
+ details.Set("modelsWithChanges",
+ syncable::ModelTypeSetToValue(models_with_changes));
+ HandleJsEvent(FROM_HERE, "onTransactionWrite", JsEventDetails(&details));
+}
+
+void JsMutationEventObserver::HandleJsEvent(
+ const tracked_objects::Location& from_here,
+ const std::string& name, const JsEventDetails& details) {
+ if (!event_handler_.IsInitialized()) {
+ NOTREACHED();
+ return;
+ }
+ event_handler_.Call(from_here,
+ &JsEventHandler::HandleJsEvent, name, details);
+}
+
+} // namespace browser_sync