| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 JsSyncManagerObserver::JsSyncManagerObserver() {} | 24 JsSyncManagerObserver::JsSyncManagerObserver() {} |
| 25 | 25 |
| 26 JsSyncManagerObserver::~JsSyncManagerObserver() {} | 26 JsSyncManagerObserver::~JsSyncManagerObserver() {} |
| 27 | 27 |
| 28 void JsSyncManagerObserver::SetJsEventHandler( | 28 void JsSyncManagerObserver::SetJsEventHandler( |
| 29 const WeakHandle<JsEventHandler>& event_handler) { | 29 const WeakHandle<JsEventHandler>& event_handler) { |
| 30 event_handler_ = event_handler; | 30 event_handler_ = event_handler; |
| 31 } | 31 } |
| 32 | 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 | |
| 41 void JsSyncManagerObserver::OnChangesApplied( | |
| 42 syncable::ModelType model_type, | |
| 43 int64 write_transaction_id, | |
| 44 const sync_api::ImmutableChangeRecordList& changes) { | |
| 45 if (!event_handler_.IsInitialized()) { | |
| 46 return; | |
| 47 } | |
| 48 DictionaryValue details; | |
| 49 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | |
| 50 details.SetString("writeTransactionId", | |
| 51 base::Int64ToString(write_transaction_id)); | |
| 52 Value* changes_value = NULL; | |
| 53 const size_t changes_size = changes.Get().size(); | |
| 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"); | |
| 66 } | |
| 67 details.Set("changes", changes_value); | |
| 68 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); | |
| 69 } | |
| 70 | |
| 71 void JsSyncManagerObserver::OnChangesComplete( | |
| 72 syncable::ModelType model_type) { | |
| 73 if (!event_handler_.IsInitialized()) { | |
| 74 return; | |
| 75 } | |
| 76 DictionaryValue details; | |
| 77 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | |
| 78 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); | |
| 79 } | |
| 80 | |
| 81 void JsSyncManagerObserver::OnSyncCycleCompleted( | 33 void JsSyncManagerObserver::OnSyncCycleCompleted( |
| 82 const sessions::SyncSessionSnapshot* snapshot) { | 34 const sessions::SyncSessionSnapshot* snapshot) { |
| 83 if (!event_handler_.IsInitialized()) { | 35 if (!event_handler_.IsInitialized()) { |
| 84 return; | 36 return; |
| 85 } | 37 } |
| 86 DictionaryValue details; | 38 DictionaryValue details; |
| 87 details.Set("snapshot", snapshot->ToValue()); | 39 details.Set("snapshot", snapshot->ToValue()); |
| 88 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); | 40 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); |
| 89 } | 41 } |
| 90 | 42 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 const std::string& name, const JsEventDetails& details) { | 139 const std::string& name, const JsEventDetails& details) { |
| 188 if (!event_handler_.IsInitialized()) { | 140 if (!event_handler_.IsInitialized()) { |
| 189 NOTREACHED(); | 141 NOTREACHED(); |
| 190 return; | 142 return; |
| 191 } | 143 } |
| 192 event_handler_.Call(from_here, | 144 event_handler_.Call(from_here, |
| 193 &JsEventHandler::HandleJsEvent, name, details); | 145 &JsEventHandler::HandleJsEvent, name, details); |
| 194 } | 146 } |
| 195 | 147 |
| 196 } // namespace browser_sync | 148 } // namespace browser_sync |
| OLD | NEW |