blob: 96c78c9cd10aa95ac0025d31c25f4ba5fe901b80 [file] [log] [blame]
[email protected]12bb3e112012-01-18 08:58:011// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]88af96012011-02-11 20:50:172// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]91835ca2012-04-21 09:59:425#include "sync/internal_api/js_sync_manager_observer.h"
[email protected]88af96012011-02-11 20:50:176
7#include <cstddef>
8
[email protected]c62dd9d2011-09-21 18:05:419#include "base/location.h"
[email protected]88af96012011-02-11 20:50:1710#include "base/logging.h"
[email protected]0f328d52011-09-21 21:24:5111#include "base/string_number_conversions.h"
[email protected]88af96012011-02-11 20:50:1712#include "base/values.h"
[email protected]002d39192012-07-03 01:30:5613#include "sync/internal_api/public/base/model_type.h"
[email protected]406203d2012-06-17 01:07:1914#include "sync/internal_api/public/change_record.h"
[email protected]3f8556a2012-06-07 17:50:1915#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
[email protected]ceffdf02012-07-17 20:12:1516#include "sync/internal_api/public/util/sync_string_conversions.h"
[email protected]c1c32c82012-03-15 09:35:4217#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]88af96012011-02-11 20:50:1720
[email protected]65f173552012-06-28 22:43:5821namespace syncer {
[email protected]88af96012011-02-11 20:50:1722
[email protected]fa134872011-08-02 18:39:5523JsSyncManagerObserver::JsSyncManagerObserver() {}
[email protected]88af96012011-02-11 20:50:1724
25JsSyncManagerObserver::~JsSyncManagerObserver() {}
26
[email protected]fa134872011-08-02 18:39:5527void JsSyncManagerObserver::SetJsEventHandler(
28 const WeakHandle<JsEventHandler>& event_handler) {
29 event_handler_ = event_handler;
30}
31
[email protected]88af96012011-02-11 20:50:1732void JsSyncManagerObserver::OnSyncCycleCompleted(
[email protected]90ce61b52012-04-24 19:32:2833 const sessions::SyncSessionSnapshot& snapshot) {
[email protected]fa134872011-08-02 18:39:5534 if (!event_handler_.IsInitialized()) {
35 return;
36 }
[email protected]ec5263a2011-05-10 09:23:3937 DictionaryValue details;
[email protected]90ce61b52012-04-24 19:32:2838 details.Set("snapshot", snapshot.ToValue());
[email protected]fa134872011-08-02 18:39:5539 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1740}
41
[email protected]d45f0d92012-07-20 17:25:4142void JsSyncManagerObserver::OnConnectionStatusChange(ConnectionStatus status) {
[email protected]fa134872011-08-02 18:39:5543 if (!event_handler_.IsInitialized()) {
44 return;
45 }
[email protected]ec5263a2011-05-10 09:23:3946 DictionaryValue details;
[email protected]d45f0d92012-07-20 17:25:4147 details.SetString("status", ConnectionStatusToString(status));
[email protected]6e2059892012-03-08 02:22:2448 HandleJsEvent(FROM_HERE,
49 "onConnectionStatusChange", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1750}
51
52void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) {
[email protected]fa134872011-08-02 18:39:5553 if (!event_handler_.IsInitialized()) {
54 return;
55 }
[email protected]ec5263a2011-05-10 09:23:3956 DictionaryValue details;
57 details.SetString("token", "<redacted>");
[email protected]fa134872011-08-02 18:39:5558 HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1759}
60
[email protected]cb5199b2011-09-04 01:55:5261void 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]fa134872011-08-02 18:39:5572void JsSyncManagerObserver::OnInitializationComplete(
[email protected]da0832b2011-09-08 17:00:0473 const WeakHandle<JsBackend>& js_backend,
[email protected]12a11e22012-11-08 06:26:3074 const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener,
[email protected]62c28b92012-07-30 04:37:4875 bool success, syncer::ModelTypeSet restored_types) {
[email protected]fa134872011-08-02 18:39:5576 if (!event_handler_.IsInitialized()) {
77 return;
78 }
79 // Ignore the |js_backend| argument; it's not really convertible to
80 // JSON anyway.
[email protected]62c28b92012-07-30 04:37:4881
[email protected]32e757a2012-07-30 23:09:3682 DictionaryValue details;
83 details.Set("restoredTypes", ModelTypeSetToValue(restored_types));
[email protected]62c28b92012-07-30 04:37:4884
[email protected]32e757a2012-07-30 23:09:3685 HandleJsEvent(FROM_HERE,
86 "onInitializationComplete",
87 JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1788}
89
[email protected]88af96012011-02-11 20:50:1790void JsSyncManagerObserver::OnStopSyncingPermanently() {
[email protected]fa134872011-08-02 18:39:5591 if (!event_handler_.IsInitialized()) {
92 return;
93 }
94 HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails());
[email protected]88af96012011-02-11 20:50:1795}
96
[email protected]fa134872011-08-02 18:39:5597void 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]88af96012011-02-11 20:50:17106}
107
[email protected]65f173552012-06-28 22:43:58108} // namespace syncer