| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync/core_impl/js_mutation_event_observer.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "components/sync/js/js_event_details.h" | |
| 14 #include "components/sync/js/js_event_handler.h" | |
| 15 | |
| 16 namespace syncer { | |
| 17 | |
| 18 JsMutationEventObserver::JsMutationEventObserver() : weak_ptr_factory_(this) {} | |
| 19 | |
| 20 JsMutationEventObserver::~JsMutationEventObserver() { | |
| 21 DCHECK(CalledOnValidThread()); | |
| 22 } | |
| 23 | |
| 24 base::WeakPtr<JsMutationEventObserver> JsMutationEventObserver::AsWeakPtr() { | |
| 25 return weak_ptr_factory_.GetWeakPtr(); | |
| 26 } | |
| 27 | |
| 28 void JsMutationEventObserver::InvalidateWeakPtrs() { | |
| 29 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 30 } | |
| 31 | |
| 32 void JsMutationEventObserver::SetJsEventHandler( | |
| 33 const WeakHandle<JsEventHandler>& event_handler) { | |
| 34 event_handler_ = event_handler; | |
| 35 } | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 // Max number of changes we attempt to convert to values (to avoid | |
| 40 // running out of memory). | |
| 41 const size_t kChangeLimit = 100; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 void JsMutationEventObserver::OnChangesApplied( | |
| 46 ModelType model_type, | |
| 47 int64_t write_transaction_id, | |
| 48 const ImmutableChangeRecordList& changes) { | |
| 49 if (!event_handler_.IsInitialized()) { | |
| 50 return; | |
| 51 } | |
| 52 base::DictionaryValue details; | |
| 53 details.SetString("modelType", ModelTypeToString(model_type)); | |
| 54 details.SetString("writeTransactionId", | |
| 55 base::Int64ToString(write_transaction_id)); | |
| 56 base::Value* changes_value = NULL; | |
| 57 const size_t changes_size = changes.Get().size(); | |
| 58 if (changes_size <= kChangeLimit) { | |
| 59 base::ListValue* changes_list = new base::ListValue(); | |
| 60 for (ChangeRecordList::const_iterator it = changes.Get().begin(); | |
| 61 it != changes.Get().end(); ++it) { | |
| 62 changes_list->Append(it->ToValue()); | |
| 63 } | |
| 64 changes_value = changes_list; | |
| 65 } else { | |
| 66 changes_value = | |
| 67 new base::StringValue(base::SizeTToString(changes_size) + " changes"); | |
| 68 } | |
| 69 details.Set("changes", changes_value); | |
| 70 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); | |
| 71 } | |
| 72 | |
| 73 void JsMutationEventObserver::OnChangesComplete(ModelType model_type) { | |
| 74 if (!event_handler_.IsInitialized()) { | |
| 75 return; | |
| 76 } | |
| 77 base::DictionaryValue details; | |
| 78 details.SetString("modelType", ModelTypeToString(model_type)); | |
| 79 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); | |
| 80 } | |
| 81 | |
| 82 void JsMutationEventObserver::OnTransactionWrite( | |
| 83 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | |
| 84 ModelTypeSet models_with_changes) { | |
| 85 DCHECK(CalledOnValidThread()); | |
| 86 if (!event_handler_.IsInitialized()) { | |
| 87 return; | |
| 88 } | |
| 89 base::DictionaryValue details; | |
| 90 details.Set("writeTransactionInfo", | |
| 91 write_transaction_info.Get().ToValue(kChangeLimit)); | |
| 92 details.Set("modelsWithChanges", ModelTypeSetToValue(models_with_changes)); | |
| 93 HandleJsEvent(FROM_HERE, "onTransactionWrite", JsEventDetails(&details)); | |
| 94 } | |
| 95 | |
| 96 void JsMutationEventObserver::HandleJsEvent( | |
| 97 const tracked_objects::Location& from_here, | |
| 98 const std::string& name, | |
| 99 const JsEventDetails& details) { | |
| 100 if (!event_handler_.IsInitialized()) { | |
| 101 NOTREACHED(); | |
| 102 return; | |
| 103 } | |
| 104 event_handler_.Call(from_here, &JsEventHandler::HandleJsEvent, name, details); | |
| 105 } | |
| 106 | |
| 107 } // namespace syncer | |
| OLD | NEW |