[email protected] | 12bb3e11 | 2012-01-18 08:58:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 91835ca | 2012-04-21 09:59:42 | [diff] [blame] | 5 | #include "sync/internal_api/js_sync_manager_observer.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 6 | |
| 7 | #include <cstddef> |
| 8 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 9 | #include "base/location.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 0f328d5 | 2011-09-21 21:24:51 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | 002d3919 | 2012-07-03 01:30:56 | [diff] [blame] | 13 | #include "sync/internal_api/public/base/model_type.h" |
[email protected] | 406203d | 2012-06-17 01:07:19 | [diff] [blame] | 14 | #include "sync/internal_api/public/change_record.h" |
[email protected] | 3f8556a | 2012-06-07 17:50:19 | [diff] [blame] | 15 | #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
[email protected] | ceffdf0 | 2012-07-17 20:12:15 | [diff] [blame^] | 16 | #include "sync/internal_api/public/util/sync_string_conversions.h" |
[email protected] | c1c32c8 | 2012-03-15 09:35:42 | [diff] [blame] | 17 | #include "sync/js/js_arg_list.h" |
| 18 | #include "sync/js/js_event_details.h" |
| 19 | #include "sync/js/js_event_handler.h" |
| 20 | #include "sync/sessions/session_state.h" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 21 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 22 | namespace syncer { |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 23 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 24 | using syncer::SyncProtocolError; |
[email protected] | cb5199b | 2011-09-04 01:55:52 | [diff] [blame] | 25 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 26 | JsSyncManagerObserver::JsSyncManagerObserver() {} |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 27 | |
| 28 | JsSyncManagerObserver::~JsSyncManagerObserver() {} |
| 29 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 30 | void JsSyncManagerObserver::SetJsEventHandler( |
| 31 | const WeakHandle<JsEventHandler>& event_handler) { |
| 32 | event_handler_ = event_handler; |
| 33 | } |
| 34 | |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 35 | void JsSyncManagerObserver::OnSyncCycleCompleted( |
[email protected] | 90ce61b5 | 2012-04-24 19:32:28 | [diff] [blame] | 36 | const sessions::SyncSessionSnapshot& snapshot) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 37 | if (!event_handler_.IsInitialized()) { |
| 38 | return; |
| 39 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 40 | DictionaryValue details; |
[email protected] | 90ce61b5 | 2012-04-24 19:32:28 | [diff] [blame] | 41 | details.Set("snapshot", snapshot.ToValue()); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 42 | HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 6e205989 | 2012-03-08 02:22:24 | [diff] [blame] | 45 | void JsSyncManagerObserver::OnConnectionStatusChange( |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 46 | syncer::ConnectionStatus status) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 47 | if (!event_handler_.IsInitialized()) { |
| 48 | return; |
| 49 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 50 | DictionaryValue details; |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 51 | details.SetString("status", syncer::ConnectionStatusToString(status)); |
[email protected] | 6e205989 | 2012-03-08 02:22:24 | [diff] [blame] | 52 | HandleJsEvent(FROM_HERE, |
| 53 | "onConnectionStatusChange", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 57 | if (!event_handler_.IsInitialized()) { |
| 58 | return; |
| 59 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 60 | DictionaryValue details; |
| 61 | details.SetString("token", "<redacted>"); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 62 | HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 43c48fb9 | 2011-05-02 19:47:43 | [diff] [blame] | 65 | void JsSyncManagerObserver::OnPassphraseRequired( |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 66 | syncer::PassphraseRequiredReason reason, |
[email protected] | 12bb3e11 | 2012-01-18 08:58:01 | [diff] [blame] | 67 | const sync_pb::EncryptedData& pending_keys) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 68 | if (!event_handler_.IsInitialized()) { |
| 69 | return; |
| 70 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 71 | DictionaryValue details; |
| 72 | details.SetString("reason", |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 73 | syncer::PassphraseRequiredReasonToString(reason)); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 74 | HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | a76abe7 | 2012-02-03 00:57:52 | [diff] [blame] | 77 | void JsSyncManagerObserver::OnPassphraseAccepted() { |
| 78 | if (!event_handler_.IsInitialized()) { |
| 79 | return; |
| 80 | } |
| 81 | DictionaryValue details; |
| 82 | HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); |
| 83 | } |
| 84 | |
| 85 | void JsSyncManagerObserver::OnBootstrapTokenUpdated( |
| 86 | const std::string& boostrap_token) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 87 | if (!event_handler_.IsInitialized()) { |
| 88 | return; |
| 89 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 90 | DictionaryValue details; |
| 91 | details.SetString("bootstrapToken", "<redacted>"); |
[email protected] | a76abe7 | 2012-02-03 00:57:52 | [diff] [blame] | 92 | HandleJsEvent(FROM_HERE, "OnBootstrapTokenUpdated", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | f42a7f9 | 2011-10-24 20:50:00 | [diff] [blame] | 95 | void JsSyncManagerObserver::OnEncryptedTypesChanged( |
[email protected] | a4a14765 | 2012-07-03 23:41:32 | [diff] [blame] | 96 | syncer::ModelTypeSet encrypted_types, |
[email protected] | f42a7f9 | 2011-10-24 20:50:00 | [diff] [blame] | 97 | bool encrypt_everything) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 98 | if (!event_handler_.IsInitialized()) { |
| 99 | return; |
| 100 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 101 | DictionaryValue details; |
| 102 | details.Set("encryptedTypes", |
[email protected] | a4a14765 | 2012-07-03 23:41:32 | [diff] [blame] | 103 | syncer::ModelTypeSetToValue(encrypted_types)); |
[email protected] | f42a7f9 | 2011-10-24 20:50:00 | [diff] [blame] | 104 | details.SetBoolean("encryptEverything", encrypt_everything); |
| 105 | HandleJsEvent(FROM_HERE, |
| 106 | "onEncryptedTypesChanged", JsEventDetails(&details)); |
| 107 | } |
| 108 | |
| 109 | void JsSyncManagerObserver::OnEncryptionComplete() { |
| 110 | if (!event_handler_.IsInitialized()) { |
| 111 | return; |
| 112 | } |
| 113 | DictionaryValue details; |
| 114 | HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails()); |
[email protected] | 3fa964af | 2011-02-24 18:54:39 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | cb5199b | 2011-09-04 01:55:52 | [diff] [blame] | 117 | void JsSyncManagerObserver::OnActionableError( |
| 118 | const SyncProtocolError& sync_error) { |
| 119 | if (!event_handler_.IsInitialized()) { |
| 120 | return; |
| 121 | } |
| 122 | DictionaryValue details; |
| 123 | details.Set("syncError", sync_error.ToValue()); |
| 124 | HandleJsEvent(FROM_HERE, "onActionableError", |
| 125 | JsEventDetails(&details)); |
| 126 | } |
| 127 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 128 | void JsSyncManagerObserver::OnInitializationComplete( |
[email protected] | da0832b | 2011-09-08 17:00:04 | [diff] [blame] | 129 | const WeakHandle<JsBackend>& js_backend, |
| 130 | bool success) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 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 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 146 | void JsSyncManagerObserver::HandleJsEvent( |
| 147 | const tracked_objects::Location& from_here, |
| 148 | const std::string& name, const JsEventDetails& details) { |
| 149 | if (!event_handler_.IsInitialized()) { |
| 150 | NOTREACHED(); |
| 151 | return; |
| 152 | } |
| 153 | event_handler_.Call(from_here, |
| 154 | &JsEventHandler::HandleJsEvent, name, details); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 157 | } // namespace syncer |