[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" |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 20 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 21 | namespace syncer { |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 22 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 23 | JsSyncManagerObserver::JsSyncManagerObserver() {} |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 24 | |
| 25 | JsSyncManagerObserver::~JsSyncManagerObserver() {} |
| 26 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 27 | void JsSyncManagerObserver::SetJsEventHandler( |
| 28 | const WeakHandle<JsEventHandler>& event_handler) { |
| 29 | event_handler_ = event_handler; |
| 30 | } |
| 31 | |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 32 | void JsSyncManagerObserver::OnSyncCycleCompleted( |
[email protected] | 90ce61b5 | 2012-04-24 19:32:28 | [diff] [blame] | 33 | const sessions::SyncSessionSnapshot& snapshot) { |
[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; |
[email protected] | 90ce61b5 | 2012-04-24 19:32:28 | [diff] [blame] | 38 | details.Set("snapshot", snapshot.ToValue()); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 39 | HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 42 | void JsSyncManagerObserver::OnConnectionStatusChange(ConnectionStatus status) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 43 | if (!event_handler_.IsInitialized()) { |
| 44 | return; |
| 45 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 46 | DictionaryValue details; |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 47 | details.SetString("status", ConnectionStatusToString(status)); |
[email protected] | 6e205989 | 2012-03-08 02:22:24 | [diff] [blame] | 48 | HandleJsEvent(FROM_HERE, |
| 49 | "onConnectionStatusChange", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 53 | if (!event_handler_.IsInitialized()) { |
| 54 | return; |
| 55 | } |
[email protected] | ec5263a | 2011-05-10 09:23:39 | [diff] [blame] | 56 | DictionaryValue details; |
| 57 | details.SetString("token", "<redacted>"); |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 58 | HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | cb5199b | 2011-09-04 01:55:52 | [diff] [blame] | 61 | void JsSyncManagerObserver::OnActionableError( |
| 62 | const SyncProtocolError& sync_error) { |
| 63 | if (!event_handler_.IsInitialized()) { |
| 64 | return; |
| 65 | } |
| 66 | DictionaryValue details; |
| 67 | details.Set("syncError", sync_error.ToValue()); |
| 68 | HandleJsEvent(FROM_HERE, "onActionableError", |
| 69 | JsEventDetails(&details)); |
| 70 | } |
| 71 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 72 | void JsSyncManagerObserver::OnInitializationComplete( |
[email protected] | da0832b | 2011-09-08 17:00:04 | [diff] [blame] | 73 | const WeakHandle<JsBackend>& js_backend, |
[email protected] | 12a11e2 | 2012-11-08 06:26:30 | [diff] [blame^] | 74 | const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener, |
[email protected] | 62c28b9 | 2012-07-30 04:37:48 | [diff] [blame] | 75 | bool success, syncer::ModelTypeSet restored_types) { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 76 | if (!event_handler_.IsInitialized()) { |
| 77 | return; |
| 78 | } |
| 79 | // Ignore the |js_backend| argument; it's not really convertible to |
| 80 | // JSON anyway. |
[email protected] | 62c28b9 | 2012-07-30 04:37:48 | [diff] [blame] | 81 | |
[email protected] | 32e757a | 2012-07-30 23:09:36 | [diff] [blame] | 82 | DictionaryValue details; |
| 83 | details.Set("restoredTypes", ModelTypeSetToValue(restored_types)); |
[email protected] | 62c28b9 | 2012-07-30 04:37:48 | [diff] [blame] | 84 | |
[email protected] | 32e757a | 2012-07-30 23:09:36 | [diff] [blame] | 85 | HandleJsEvent(FROM_HERE, |
| 86 | "onInitializationComplete", |
| 87 | JsEventDetails(&details)); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 90 | void JsSyncManagerObserver::OnStopSyncingPermanently() { |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 91 | if (!event_handler_.IsInitialized()) { |
| 92 | return; |
| 93 | } |
| 94 | HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | fa13487 | 2011-08-02 18:39:55 | [diff] [blame] | 97 | void JsSyncManagerObserver::HandleJsEvent( |
| 98 | const tracked_objects::Location& from_here, |
| 99 | const std::string& name, const JsEventDetails& details) { |
| 100 | if (!event_handler_.IsInitialized()) { |
| 101 | NOTREACHED(); |
| 102 | return; |
| 103 | } |
| 104 | event_handler_.Call(from_here, |
| 105 | &JsEventHandler::HandleJsEvent, name, details); |
[email protected] | 88af9601 | 2011-02-11 20:50:17 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | 65f17355 | 2012-06-28 22:43:58 | [diff] [blame] | 108 | } // namespace syncer |