blob: 4e98fd901a8e820fb9b4c8a7848d576779060235 [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"
20#include "sync/sessions/session_state.h"
[email protected]88af96012011-02-11 20:50:1721
[email protected]65f173552012-06-28 22:43:5822namespace syncer {
[email protected]88af96012011-02-11 20:50:1723
[email protected]65f173552012-06-28 22:43:5824using syncer::SyncProtocolError;
[email protected]cb5199b2011-09-04 01:55:5225
[email protected]fa134872011-08-02 18:39:5526JsSyncManagerObserver::JsSyncManagerObserver() {}
[email protected]88af96012011-02-11 20:50:1727
28JsSyncManagerObserver::~JsSyncManagerObserver() {}
29
[email protected]fa134872011-08-02 18:39:5530void JsSyncManagerObserver::SetJsEventHandler(
31 const WeakHandle<JsEventHandler>& event_handler) {
32 event_handler_ = event_handler;
33}
34
[email protected]88af96012011-02-11 20:50:1735void JsSyncManagerObserver::OnSyncCycleCompleted(
[email protected]90ce61b52012-04-24 19:32:2836 const sessions::SyncSessionSnapshot& snapshot) {
[email protected]fa134872011-08-02 18:39:5537 if (!event_handler_.IsInitialized()) {
38 return;
39 }
[email protected]ec5263a2011-05-10 09:23:3940 DictionaryValue details;
[email protected]90ce61b52012-04-24 19:32:2841 details.Set("snapshot", snapshot.ToValue());
[email protected]fa134872011-08-02 18:39:5542 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1743}
44
[email protected]6e2059892012-03-08 02:22:2445void JsSyncManagerObserver::OnConnectionStatusChange(
[email protected]65f173552012-06-28 22:43:5846 syncer::ConnectionStatus status) {
[email protected]fa134872011-08-02 18:39:5547 if (!event_handler_.IsInitialized()) {
48 return;
49 }
[email protected]ec5263a2011-05-10 09:23:3950 DictionaryValue details;
[email protected]65f173552012-06-28 22:43:5851 details.SetString("status", syncer::ConnectionStatusToString(status));
[email protected]6e2059892012-03-08 02:22:2452 HandleJsEvent(FROM_HERE,
53 "onConnectionStatusChange", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1754}
55
56void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) {
[email protected]fa134872011-08-02 18:39:5557 if (!event_handler_.IsInitialized()) {
58 return;
59 }
[email protected]ec5263a2011-05-10 09:23:3960 DictionaryValue details;
61 details.SetString("token", "<redacted>");
[email protected]fa134872011-08-02 18:39:5562 HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1763}
64
[email protected]43c48fb92011-05-02 19:47:4365void JsSyncManagerObserver::OnPassphraseRequired(
[email protected]65f173552012-06-28 22:43:5866 syncer::PassphraseRequiredReason reason,
[email protected]12bb3e112012-01-18 08:58:0167 const sync_pb::EncryptedData& pending_keys) {
[email protected]fa134872011-08-02 18:39:5568 if (!event_handler_.IsInitialized()) {
69 return;
70 }
[email protected]ec5263a2011-05-10 09:23:3971 DictionaryValue details;
72 details.SetString("reason",
[email protected]65f173552012-06-28 22:43:5873 syncer::PassphraseRequiredReasonToString(reason));
[email protected]fa134872011-08-02 18:39:5574 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1775}
76
[email protected]a76abe72012-02-03 00:57:5277void JsSyncManagerObserver::OnPassphraseAccepted() {
78 if (!event_handler_.IsInitialized()) {
79 return;
80 }
81 DictionaryValue details;
82 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details));
83}
84
85void JsSyncManagerObserver::OnBootstrapTokenUpdated(
86 const std::string& boostrap_token) {
[email protected]fa134872011-08-02 18:39:5587 if (!event_handler_.IsInitialized()) {
88 return;
89 }
[email protected]ec5263a2011-05-10 09:23:3990 DictionaryValue details;
91 details.SetString("bootstrapToken", "<redacted>");
[email protected]a76abe72012-02-03 00:57:5292 HandleJsEvent(FROM_HERE, "OnBootstrapTokenUpdated", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1793}
94
[email protected]f42a7f92011-10-24 20:50:0095void JsSyncManagerObserver::OnEncryptedTypesChanged(
[email protected]a4a147652012-07-03 23:41:3296 syncer::ModelTypeSet encrypted_types,
[email protected]f42a7f92011-10-24 20:50:0097 bool encrypt_everything) {
[email protected]fa134872011-08-02 18:39:5598 if (!event_handler_.IsInitialized()) {
99 return;
100 }
[email protected]ec5263a2011-05-10 09:23:39101 DictionaryValue details;
102 details.Set("encryptedTypes",
[email protected]a4a147652012-07-03 23:41:32103 syncer::ModelTypeSetToValue(encrypted_types));
[email protected]f42a7f92011-10-24 20:50:00104 details.SetBoolean("encryptEverything", encrypt_everything);
105 HandleJsEvent(FROM_HERE,
106 "onEncryptedTypesChanged", JsEventDetails(&details));
107}
108
109void JsSyncManagerObserver::OnEncryptionComplete() {
110 if (!event_handler_.IsInitialized()) {
111 return;
112 }
113 DictionaryValue details;
114 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails());
[email protected]3fa964af2011-02-24 18:54:39115}
116
[email protected]cb5199b2011-09-04 01:55:52117void 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]fa134872011-08-02 18:39:55128void JsSyncManagerObserver::OnInitializationComplete(
[email protected]da0832b2011-09-08 17:00:04129 const WeakHandle<JsBackend>& js_backend,
130 bool success) {
[email protected]fa134872011-08-02 18:39:55131 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]88af96012011-02-11 20:50:17137}
138
[email protected]88af96012011-02-11 20:50:17139void JsSyncManagerObserver::OnStopSyncingPermanently() {
[email protected]fa134872011-08-02 18:39:55140 if (!event_handler_.IsInitialized()) {
141 return;
142 }
143 HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails());
[email protected]88af96012011-02-11 20:50:17144}
145
[email protected]fa134872011-08-02 18:39:55146void 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]88af96012011-02-11 20:50:17155}
156
[email protected]65f173552012-06-28 22:43:58157} // namespace syncer