| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/js_sync_manager_observer.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/tracked.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/sync/js_arg_list.h" | |
| 13 #include "chrome/browser/sync/js_event_details.h" | |
| 14 #include "chrome/browser/sync/js_event_handler.h" | |
| 15 #include "chrome/browser/sync/sessions/session_state.h" | |
| 16 #include "chrome/browser/sync/syncable/model_type.h" | |
| 17 | |
| 18 namespace browser_sync { | |
| 19 | |
| 20 JsSyncManagerObserver::JsSyncManagerObserver() {} | |
| 21 | |
| 22 JsSyncManagerObserver::~JsSyncManagerObserver() {} | |
| 23 | |
| 24 void JsSyncManagerObserver::SetJsEventHandler( | |
| 25 const WeakHandle<JsEventHandler>& event_handler) { | |
| 26 event_handler_ = event_handler; | |
| 27 } | |
| 28 | |
| 29 void JsSyncManagerObserver::OnChangesApplied( | |
| 30 syncable::ModelType model_type, | |
| 31 const sync_api::BaseTransaction* trans, | |
| 32 const sync_api::SyncManager::ChangeRecord* changes, | |
| 33 int change_count) { | |
| 34 if (!event_handler_.IsInitialized()) { | |
| 35 return; | |
| 36 } | |
| 37 DictionaryValue details; | |
| 38 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | |
| 39 ListValue* change_values = new ListValue(); | |
| 40 details.Set("changes", change_values); | |
| 41 for (int i = 0; i < change_count; ++i) { | |
| 42 change_values->Append(changes[i].ToValue(trans)); | |
| 43 } | |
| 44 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); | |
| 45 } | |
| 46 | |
| 47 void JsSyncManagerObserver::OnChangesComplete( | |
| 48 syncable::ModelType model_type) { | |
| 49 if (!event_handler_.IsInitialized()) { | |
| 50 return; | |
| 51 } | |
| 52 DictionaryValue details; | |
| 53 details.SetString("modelType", syncable::ModelTypeToString(model_type)); | |
| 54 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); | |
| 55 } | |
| 56 | |
| 57 void JsSyncManagerObserver::OnSyncCycleCompleted( | |
| 58 const sessions::SyncSessionSnapshot* snapshot) { | |
| 59 if (!event_handler_.IsInitialized()) { | |
| 60 return; | |
| 61 } | |
| 62 DictionaryValue details; | |
| 63 details.Set("snapshot", snapshot->ToValue()); | |
| 64 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); | |
| 65 } | |
| 66 | |
| 67 void JsSyncManagerObserver::OnAuthError( | |
| 68 const GoogleServiceAuthError& auth_error) { | |
| 69 if (!event_handler_.IsInitialized()) { | |
| 70 return; | |
| 71 } | |
| 72 DictionaryValue details; | |
| 73 details.Set("authError", auth_error.ToValue()); | |
| 74 HandleJsEvent(FROM_HERE, "onAuthError", JsEventDetails(&details)); | |
| 75 } | |
| 76 | |
| 77 void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { | |
| 78 if (!event_handler_.IsInitialized()) { | |
| 79 return; | |
| 80 } | |
| 81 DictionaryValue details; | |
| 82 details.SetString("token", "<redacted>"); | |
| 83 HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); | |
| 84 } | |
| 85 | |
| 86 void JsSyncManagerObserver::OnPassphraseRequired( | |
| 87 sync_api::PassphraseRequiredReason reason) { | |
| 88 if (!event_handler_.IsInitialized()) { | |
| 89 return; | |
| 90 } | |
| 91 DictionaryValue details; | |
| 92 details.SetString("reason", | |
| 93 sync_api::PassphraseRequiredReasonToString(reason)); | |
| 94 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); | |
| 95 } | |
| 96 | |
| 97 void JsSyncManagerObserver::OnPassphraseAccepted( | |
| 98 const std::string& bootstrap_token) { | |
| 99 if (!event_handler_.IsInitialized()) { | |
| 100 return; | |
| 101 } | |
| 102 DictionaryValue details; | |
| 103 details.SetString("bootstrapToken", "<redacted>"); | |
| 104 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); | |
| 105 } | |
| 106 | |
| 107 void JsSyncManagerObserver::OnEncryptionComplete( | |
| 108 const syncable::ModelTypeSet& encrypted_types) { | |
| 109 if (!event_handler_.IsInitialized()) { | |
| 110 return; | |
| 111 } | |
| 112 DictionaryValue details; | |
| 113 details.Set("encryptedTypes", | |
| 114 syncable::ModelTypeSetToValue(encrypted_types)); | |
| 115 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails(&details)); | |
| 116 } | |
| 117 | |
| 118 void JsSyncManagerObserver::OnMigrationNeededForTypes( | |
| 119 const syncable::ModelTypeSet& types) { | |
| 120 if (!event_handler_.IsInitialized()) { | |
| 121 return; | |
| 122 } | |
| 123 DictionaryValue details; | |
| 124 details.Set("types", syncable::ModelTypeSetToValue(types)); | |
| 125 HandleJsEvent(FROM_HERE, "onMigrationNeededForTypes", | |
| 126 JsEventDetails(&details)); | |
| 127 } | |
| 128 | |
| 129 void JsSyncManagerObserver::OnInitializationComplete( | |
| 130 const WeakHandle<JsBackend>& js_backend) { | |
| 131 if (!event_handler_.IsInitialized()) { | |
| 132 return; | |
| 133 } | |
| 134 // Ignore the |js_backend| argument; it's not really convertible to | |
| 135 // JSON anyway. | |
| 136 HandleJsEvent(FROM_HERE, "onInitializationComplete", JsEventDetails()); | |
| 137 } | |
| 138 | |
| 139 void JsSyncManagerObserver::OnStopSyncingPermanently() { | |
| 140 if (!event_handler_.IsInitialized()) { | |
| 141 return; | |
| 142 } | |
| 143 HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); | |
| 144 } | |
| 145 | |
| 146 void JsSyncManagerObserver::OnClearServerDataSucceeded() { | |
| 147 if (!event_handler_.IsInitialized()) { | |
| 148 return; | |
| 149 } | |
| 150 HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails()); | |
| 151 } | |
| 152 | |
| 153 void JsSyncManagerObserver::OnClearServerDataFailed() { | |
| 154 if (!event_handler_.IsInitialized()) { | |
| 155 return; | |
| 156 } | |
| 157 HandleJsEvent(FROM_HERE, "onClearServerDataFailed", JsEventDetails()); | |
| 158 } | |
| 159 | |
| 160 void JsSyncManagerObserver::HandleJsEvent( | |
| 161 const tracked_objects::Location& from_here, | |
| 162 const std::string& name, const JsEventDetails& details) { | |
| 163 if (!event_handler_.IsInitialized()) { | |
| 164 NOTREACHED(); | |
| 165 return; | |
| 166 } | |
| 167 event_handler_.Call(from_here, | |
| 168 &JsEventHandler::HandleJsEvent, name, details); | |
| 169 } | |
| 170 | |
| 171 } // namespace browser_sync | |
| OLD | NEW |