| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/internal_api/js_mutation_event_observer.h" | 5 #include "sync/internal_api/js_mutation_event_observer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 // Max number of changes we attempt to convert to values (to avoid | 40 // Max number of changes we attempt to convert to values (to avoid |
| 41 // running out of memory). | 41 // running out of memory). |
| 42 const size_t kChangeLimit = 100; | 42 const size_t kChangeLimit = 100; |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 void JsMutationEventObserver::OnChangesApplied( | 46 void JsMutationEventObserver::OnChangesApplied( |
| 47 syncable::ModelType model_type, | 47 syncer::ModelType model_type, |
| 48 int64 write_transaction_id, | 48 int64 write_transaction_id, |
| 49 const syncer::ImmutableChangeRecordList& changes) { | 49 const syncer::ImmutableChangeRecordList& changes) { |
| 50 if (!event_handler_.IsInitialized()) { | 50 if (!event_handler_.IsInitialized()) { |
| 51 return; | 51 return; |
| 52 } | 52 } |
| 53 DictionaryValue details; | 53 DictionaryValue details; |
| 54 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | 54 details.SetString("modelType", syncer::ModelTypeToString(model_type)); |
| 55 details.SetString("writeTransactionId", | 55 details.SetString("writeTransactionId", |
| 56 base::Int64ToString(write_transaction_id)); | 56 base::Int64ToString(write_transaction_id)); |
| 57 base::Value* changes_value = NULL; | 57 base::Value* changes_value = NULL; |
| 58 const size_t changes_size = changes.Get().size(); | 58 const size_t changes_size = changes.Get().size(); |
| 59 if (changes_size <= kChangeLimit) { | 59 if (changes_size <= kChangeLimit) { |
| 60 ListValue* changes_list = new ListValue(); | 60 ListValue* changes_list = new ListValue(); |
| 61 for (syncer::ChangeRecordList::const_iterator it = | 61 for (syncer::ChangeRecordList::const_iterator it = |
| 62 changes.Get().begin(); it != changes.Get().end(); ++it) { | 62 changes.Get().begin(); it != changes.Get().end(); ++it) { |
| 63 changes_list->Append(it->ToValue()); | 63 changes_list->Append(it->ToValue()); |
| 64 } | 64 } |
| 65 changes_value = changes_list; | 65 changes_value = changes_list; |
| 66 } else { | 66 } else { |
| 67 changes_value = | 67 changes_value = |
| 68 Value::CreateStringValue( | 68 Value::CreateStringValue( |
| 69 base::Uint64ToString(static_cast<uint64>(changes_size)) + | 69 base::Uint64ToString(static_cast<uint64>(changes_size)) + |
| 70 " changes"); | 70 " changes"); |
| 71 } | 71 } |
| 72 details.Set("changes", changes_value); | 72 details.Set("changes", changes_value); |
| 73 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); | 73 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void JsMutationEventObserver::OnChangesComplete( | 76 void JsMutationEventObserver::OnChangesComplete( |
| 77 syncable::ModelType model_type) { | 77 syncer::ModelType model_type) { |
| 78 if (!event_handler_.IsInitialized()) { | 78 if (!event_handler_.IsInitialized()) { |
| 79 return; | 79 return; |
| 80 } | 80 } |
| 81 DictionaryValue details; | 81 DictionaryValue details; |
| 82 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | 82 details.SetString("modelType", syncer::ModelTypeToString(model_type)); |
| 83 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); | 83 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void JsMutationEventObserver::OnTransactionWrite( | 86 void JsMutationEventObserver::OnTransactionWrite( |
| 87 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, | 87 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, |
| 88 syncable::ModelTypeSet models_with_changes) { | 88 syncer::ModelTypeSet models_with_changes) { |
| 89 DCHECK(CalledOnValidThread()); | 89 DCHECK(CalledOnValidThread()); |
| 90 if (!event_handler_.IsInitialized()) { | 90 if (!event_handler_.IsInitialized()) { |
| 91 return; | 91 return; |
| 92 } | 92 } |
| 93 DictionaryValue details; | 93 DictionaryValue details; |
| 94 details.Set("writeTransactionInfo", | 94 details.Set("writeTransactionInfo", |
| 95 write_transaction_info.Get().ToValue(kChangeLimit)); | 95 write_transaction_info.Get().ToValue(kChangeLimit)); |
| 96 details.Set("modelsWithChanges", | 96 details.Set("modelsWithChanges", |
| 97 syncable::ModelTypeSetToValue(models_with_changes)); | 97 syncer::ModelTypeSetToValue(models_with_changes)); |
| 98 HandleJsEvent(FROM_HERE, "onTransactionWrite", JsEventDetails(&details)); | 98 HandleJsEvent(FROM_HERE, "onTransactionWrite", JsEventDetails(&details)); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void JsMutationEventObserver::HandleJsEvent( | 101 void JsMutationEventObserver::HandleJsEvent( |
| 102 const tracked_objects::Location& from_here, | 102 const tracked_objects::Location& from_here, |
| 103 const std::string& name, const JsEventDetails& details) { | 103 const std::string& name, const JsEventDetails& details) { |
| 104 if (!event_handler_.IsInitialized()) { | 104 if (!event_handler_.IsInitialized()) { |
| 105 NOTREACHED(); | 105 NOTREACHED(); |
| 106 return; | 106 return; |
| 107 } | 107 } |
| 108 event_handler_.Call(from_here, | 108 event_handler_.Call(from_here, |
| 109 &JsEventHandler::HandleJsEvent, name, details); | 109 &JsEventHandler::HandleJsEvent, name, details); |
| 110 } | 110 } |
| 111 | 111 |
| 112 } // namespace syncer | 112 } // namespace syncer |
| OLD | NEW |