[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 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" |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 10 | #include "base/tracked.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 11 | #include "base/values.h" |
| 12 | #include "chrome/browser/sync/js_arg_list.h" |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 13 | #include "chrome/browser/sync/js_event_details.h" |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 14 | #include "chrome/browser/sync/js_event_handler.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 15 | #include "chrome/browser/sync/sessions/session_state.h" |
| 16 | #include "chrome/browser/sync/syncable/model_type.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 17 | |
| 18 | namespace browser_sync { |
| 19 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 20 | JsSyncManagerObserver::JsSyncManagerObserver() {} |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 21 | |
| 22 | JsSyncManagerObserver::~JsSyncManagerObserver() {} |
| 23 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 24 | void JsSyncManagerObserver::SetJsEventHandler( |
| 25 | const WeakHandle<JsEventHandler>& event_handler) { |
| 26 | event_handler_ = event_handler; |
| 27 | } |
| 28 | |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 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) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 34 | if (!event_handler_.IsInitialized()) { |
| 35 | return; |
| 36 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 37 | DictionaryValue details; |
| 38 | details.SetString("modelType", syncable::ModelTypeToString(model_type)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 39 | ListValue* change_values = new ListValue(); |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 40 | details.Set("changes", change_values); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 41 | for (int i = 0; i < change_count; ++i) { |
[email protected] | ab58e42 | 2011-02-18 09:57:04 | [diff] [blame] | 42 | change_values->Append(changes[i].ToValue(trans)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 43 | } |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 44 | HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void JsSyncManagerObserver::OnChangesComplete( |
| 48 | syncable::ModelType model_type) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 49 | if (!event_handler_.IsInitialized()) { |
| 50 | return; |
| 51 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 52 | DictionaryValue details; |
| 53 | details.SetString("modelType", syncable::ModelTypeToString(model_type)); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 54 | HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void JsSyncManagerObserver::OnSyncCycleCompleted( |
| 58 | const sessions::SyncSessionSnapshot* snapshot) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 59 | if (!event_handler_.IsInitialized()) { |
| 60 | return; |
| 61 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 62 | DictionaryValue details; |
| 63 | details.Set("snapshot", snapshot->ToValue()); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 64 | HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void JsSyncManagerObserver::OnAuthError( |
| 68 | const GoogleServiceAuthError& auth_error) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 69 | if (!event_handler_.IsInitialized()) { |
| 70 | return; |
| 71 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 72 | DictionaryValue details; |
| 73 | details.Set("authError", auth_error.ToValue()); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 74 | HandleJsEvent(FROM_HERE, "onAuthError", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 78 | if (!event_handler_.IsInitialized()) { |
| 79 | return; |
| 80 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 81 | DictionaryValue details; |
| 82 | details.SetString("token", "<redacted>"); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 83 | HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 43c48fb9 | 2011-05-02 19:47:43 | [diff] [blame] | 86 | void JsSyncManagerObserver::OnPassphraseRequired( |
| 87 | sync_api::PassphraseRequiredReason reason) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 88 | if (!event_handler_.IsInitialized()) { |
| 89 | return; |
| 90 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 91 | DictionaryValue details; |
| 92 | details.SetString("reason", |
| 93 | sync_api::PassphraseRequiredReasonToString(reason)); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 94 | HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void JsSyncManagerObserver::OnPassphraseAccepted( |
| 98 | const std::string& bootstrap_token) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 99 | if (!event_handler_.IsInitialized()) { |
| 100 | return; |
| 101 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 102 | DictionaryValue details; |
| 103 | details.SetString("bootstrapToken", "<redacted>"); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 104 | HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 3fa964af | 2011-02-24 18:54:39 | [diff] [blame] | 107 | void JsSyncManagerObserver::OnEncryptionComplete( |
| 108 | const syncable::ModelTypeSet& encrypted_types) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 109 | if (!event_handler_.IsInitialized()) { |
| 110 | return; |
| 111 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 112 | DictionaryValue details; |
| 113 | details.Set("encryptedTypes", |
| 114 | syncable::ModelTypeSetToValue(encrypted_types)); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 115 | HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails(&details)); |
[email protected] | 3fa964af | 2011-02-24 18:54:39 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | dc55625 | 2011-04-20 01:59:35 | [diff] [blame] | 118 | void JsSyncManagerObserver::OnMigrationNeededForTypes( |
| 119 | const syncable::ModelTypeSet& types) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 120 | if (!event_handler_.IsInitialized()) { |
| 121 | return; |
| 122 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 123 | DictionaryValue details; |
| 124 | details.Set("types", syncable::ModelTypeSetToValue(types)); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 125 | HandleJsEvent(FROM_HERE, "onMigrationNeededForTypes", |
| 126 | JsEventDetails(&details)); |
[email protected] | dc55625 | 2011-04-20 01:59:35 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 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()); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 139 | void JsSyncManagerObserver::OnStopSyncingPermanently() { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 140 | if (!event_handler_.IsInitialized()) { |
| 141 | return; |
| 142 | } |
| 143 | HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void JsSyncManagerObserver::OnClearServerDataSucceeded() { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 147 | if (!event_handler_.IsInitialized()) { |
| 148 | return; |
| 149 | } |
| 150 | HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails()); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void JsSyncManagerObserver::OnClearServerDataFailed() { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame^] | 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); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace browser_sync |