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