| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/js/js_sync_manager_observer.h" | 5 #include "chrome/browser/sync/js/js_sync_manager_observer.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/sync/internal_api/change_record.h" | 13 #include "chrome/browser/sync/internal_api/change_record.h" |
| 13 #include "chrome/browser/sync/js/js_arg_list.h" | 14 #include "chrome/browser/sync/js/js_arg_list.h" |
| 14 #include "chrome/browser/sync/js/js_event_details.h" | 15 #include "chrome/browser/sync/js/js_event_details.h" |
| 15 #include "chrome/browser/sync/js/js_event_handler.h" | 16 #include "chrome/browser/sync/js/js_event_handler.h" |
| 16 #include "chrome/browser/sync/sessions/session_state.h" | 17 #include "chrome/browser/sync/sessions/session_state.h" |
| 17 #include "chrome/browser/sync/syncable/model_type.h" | 18 #include "chrome/browser/sync/syncable/model_type.h" |
| 18 | 19 |
| 19 namespace browser_sync { | 20 namespace browser_sync { |
| 20 | 21 |
| 21 using browser_sync::SyncProtocolError; | 22 using browser_sync::SyncProtocolError; |
| 22 | 23 |
| 23 JsSyncManagerObserver::JsSyncManagerObserver() {} | 24 JsSyncManagerObserver::JsSyncManagerObserver() {} |
| 24 | 25 |
| 25 JsSyncManagerObserver::~JsSyncManagerObserver() {} | 26 JsSyncManagerObserver::~JsSyncManagerObserver() {} |
| 26 | 27 |
| 27 void JsSyncManagerObserver::SetJsEventHandler( | 28 void JsSyncManagerObserver::SetJsEventHandler( |
| 28 const WeakHandle<JsEventHandler>& event_handler) { | 29 const WeakHandle<JsEventHandler>& event_handler) { |
| 29 event_handler_ = event_handler; | 30 event_handler_ = event_handler; |
| 30 } | 31 } |
| 31 | 32 |
| 33 namespace { |
| 34 |
| 35 // Max number of changes we attempt to convert to values (to avoid |
| 36 // running out of memory). |
| 37 const size_t kChangeLimit = 300; |
| 38 |
| 39 } // namespace |
| 40 |
| 32 void JsSyncManagerObserver::OnChangesApplied( | 41 void JsSyncManagerObserver::OnChangesApplied( |
| 33 syncable::ModelType model_type, | 42 syncable::ModelType model_type, |
| 34 const sync_api::BaseTransaction* trans, | 43 int64 write_transaction_id, |
| 35 const sync_api::ImmutableChangeRecordList& changes) { | 44 const sync_api::ImmutableChangeRecordList& changes) { |
| 36 if (!event_handler_.IsInitialized()) { | 45 if (!event_handler_.IsInitialized()) { |
| 37 return; | 46 return; |
| 38 } | 47 } |
| 39 DictionaryValue details; | 48 DictionaryValue details; |
| 40 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | 49 details.SetString("modelType", syncable::ModelTypeToString(model_type)); |
| 41 ListValue* change_values = new ListValue(); | 50 details.SetString("writeTransactionId", |
| 42 details.Set("changes", change_values); | 51 base::Int64ToString(write_transaction_id)); |
| 43 for (sync_api::ChangeRecordList::const_iterator it = | 52 Value* changes_value = NULL; |
| 44 changes.Get().begin(); it != changes.Get().end(); ++it) { | 53 const size_t changes_size = changes.Get().size(); |
| 45 change_values->Append(it->ToValue(trans)); | 54 if (changes_size <= kChangeLimit) { |
| 55 ListValue* changes_list = new ListValue(); |
| 56 for (sync_api::ChangeRecordList::const_iterator it = |
| 57 changes.Get().begin(); it != changes.Get().end(); ++it) { |
| 58 changes_list->Append(it->ToValue()); |
| 59 } |
| 60 changes_value = changes_list; |
| 61 } else { |
| 62 changes_value = |
| 63 Value::CreateStringValue( |
| 64 base::Uint64ToString(static_cast<uint64>(changes_size)) + |
| 65 " changes"); |
| 46 } | 66 } |
| 67 details.Set("changes", changes_value); |
| 47 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); | 68 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); |
| 48 } | 69 } |
| 49 | 70 |
| 50 void JsSyncManagerObserver::OnChangesComplete( | 71 void JsSyncManagerObserver::OnChangesComplete( |
| 51 syncable::ModelType model_type) { | 72 syncable::ModelType model_type) { |
| 52 if (!event_handler_.IsInitialized()) { | 73 if (!event_handler_.IsInitialized()) { |
| 53 return; | 74 return; |
| 54 } | 75 } |
| 55 DictionaryValue details; | 76 DictionaryValue details; |
| 56 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | 77 details.SetString("modelType", syncable::ModelTypeToString(model_type)); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 const std::string& name, const JsEventDetails& details) { | 187 const std::string& name, const JsEventDetails& details) { |
| 167 if (!event_handler_.IsInitialized()) { | 188 if (!event_handler_.IsInitialized()) { |
| 168 NOTREACHED(); | 189 NOTREACHED(); |
| 169 return; | 190 return; |
| 170 } | 191 } |
| 171 event_handler_.Call(from_here, | 192 event_handler_.Call(from_here, |
| 172 &JsEventHandler::HandleJsEvent, name, details); | 193 &JsEventHandler::HandleJsEvent, name, details); |
| 173 } | 194 } |
| 174 | 195 |
| 175 } // namespace browser_sync | 196 } // namespace browser_sync |
| OLD | NEW |