blob: 4c2ff6a7290dc3c79c8997ef51980bba76037954 [file] [log] [blame]
[email protected]88af96012011-02-11 20:50:171// 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]fa134872011-08-02 18:39:5510#include "base/tracked.h"
[email protected]88af96012011-02-11 20:50:1711#include "base/values.h"
12#include "chrome/browser/sync/js_arg_list.h"
[email protected]ec5263a2011-05-10 09:23:3913#include "chrome/browser/sync/js_event_details.h"
[email protected]fa134872011-08-02 18:39:5514#include "chrome/browser/sync/js_event_handler.h"
[email protected]88af96012011-02-11 20:50:1715#include "chrome/browser/sync/sessions/session_state.h"
16#include "chrome/browser/sync/syncable/model_type.h"
[email protected]88af96012011-02-11 20:50:1717
18namespace browser_sync {
19
[email protected]fa134872011-08-02 18:39:5520JsSyncManagerObserver::JsSyncManagerObserver() {}
[email protected]88af96012011-02-11 20:50:1721
22JsSyncManagerObserver::~JsSyncManagerObserver() {}
23
[email protected]fa134872011-08-02 18:39:5524void JsSyncManagerObserver::SetJsEventHandler(
25 const WeakHandle<JsEventHandler>& event_handler) {
26 event_handler_ = event_handler;
27}
28
[email protected]88af96012011-02-11 20:50:1729void 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]fa134872011-08-02 18:39:5534 if (!event_handler_.IsInitialized()) {
35 return;
36 }
[email protected]ec5263a2011-05-10 09:23:3937 DictionaryValue details;
38 details.SetString("modelType", syncable::ModelTypeToString(model_type));
[email protected]88af96012011-02-11 20:50:1739 ListValue* change_values = new ListValue();
[email protected]ec5263a2011-05-10 09:23:3940 details.Set("changes", change_values);
[email protected]88af96012011-02-11 20:50:1741 for (int i = 0; i < change_count; ++i) {
[email protected]ab58e422011-02-18 09:57:0442 change_values->Append(changes[i].ToValue(trans));
[email protected]88af96012011-02-11 20:50:1743 }
[email protected]fa134872011-08-02 18:39:5544 HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1745}
46
47void JsSyncManagerObserver::OnChangesComplete(
48 syncable::ModelType model_type) {
[email protected]fa134872011-08-02 18:39:5549 if (!event_handler_.IsInitialized()) {
50 return;
51 }
[email protected]ec5263a2011-05-10 09:23:3952 DictionaryValue details;
53 details.SetString("modelType", syncable::ModelTypeToString(model_type));
[email protected]fa134872011-08-02 18:39:5554 HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1755}
56
57void JsSyncManagerObserver::OnSyncCycleCompleted(
58 const sessions::SyncSessionSnapshot* snapshot) {
[email protected]fa134872011-08-02 18:39:5559 if (!event_handler_.IsInitialized()) {
60 return;
61 }
[email protected]ec5263a2011-05-10 09:23:3962 DictionaryValue details;
63 details.Set("snapshot", snapshot->ToValue());
[email protected]fa134872011-08-02 18:39:5564 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1765}
66
67void JsSyncManagerObserver::OnAuthError(
68 const GoogleServiceAuthError& auth_error) {
[email protected]fa134872011-08-02 18:39:5569 if (!event_handler_.IsInitialized()) {
70 return;
71 }
[email protected]ec5263a2011-05-10 09:23:3972 DictionaryValue details;
73 details.Set("authError", auth_error.ToValue());
[email protected]fa134872011-08-02 18:39:5574 HandleJsEvent(FROM_HERE, "onAuthError", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1775}
76
77void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) {
[email protected]fa134872011-08-02 18:39:5578 if (!event_handler_.IsInitialized()) {
79 return;
80 }
[email protected]ec5263a2011-05-10 09:23:3981 DictionaryValue details;
82 details.SetString("token", "<redacted>");
[email protected]fa134872011-08-02 18:39:5583 HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1784}
85
[email protected]43c48fb92011-05-02 19:47:4386void JsSyncManagerObserver::OnPassphraseRequired(
87 sync_api::PassphraseRequiredReason reason) {
[email protected]fa134872011-08-02 18:39:5588 if (!event_handler_.IsInitialized()) {
89 return;
90 }
[email protected]ec5263a2011-05-10 09:23:3991 DictionaryValue details;
92 details.SetString("reason",
93 sync_api::PassphraseRequiredReasonToString(reason));
[email protected]fa134872011-08-02 18:39:5594 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:1795}
96
97void JsSyncManagerObserver::OnPassphraseAccepted(
98 const std::string& bootstrap_token) {
[email protected]fa134872011-08-02 18:39:5599 if (!event_handler_.IsInitialized()) {
100 return;
101 }
[email protected]ec5263a2011-05-10 09:23:39102 DictionaryValue details;
103 details.SetString("bootstrapToken", "<redacted>");
[email protected]fa134872011-08-02 18:39:55104 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details));
[email protected]88af96012011-02-11 20:50:17105}
106
[email protected]3fa964af2011-02-24 18:54:39107void JsSyncManagerObserver::OnEncryptionComplete(
108 const syncable::ModelTypeSet& encrypted_types) {
[email protected]fa134872011-08-02 18:39:55109 if (!event_handler_.IsInitialized()) {
110 return;
111 }
[email protected]ec5263a2011-05-10 09:23:39112 DictionaryValue details;
113 details.Set("encryptedTypes",
114 syncable::ModelTypeSetToValue(encrypted_types));
[email protected]fa134872011-08-02 18:39:55115 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails(&details));
[email protected]3fa964af2011-02-24 18:54:39116}
117
[email protected]dc556252011-04-20 01:59:35118void JsSyncManagerObserver::OnMigrationNeededForTypes(
119 const syncable::ModelTypeSet& types) {
[email protected]fa134872011-08-02 18:39:55120 if (!event_handler_.IsInitialized()) {
121 return;
122 }
[email protected]ec5263a2011-05-10 09:23:39123 DictionaryValue details;
124 details.Set("types", syncable::ModelTypeSetToValue(types));
[email protected]fa134872011-08-02 18:39:55125 HandleJsEvent(FROM_HERE, "onMigrationNeededForTypes",
126 JsEventDetails(&details));
[email protected]dc556252011-04-20 01:59:35127}
128
[email protected]fa134872011-08-02 18:39:55129void 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]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
146void JsSyncManagerObserver::OnClearServerDataSucceeded() {
[email protected]fa134872011-08-02 18:39:55147 if (!event_handler_.IsInitialized()) {
148 return;
149 }
150 HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails());
[email protected]88af96012011-02-11 20:50:17151}
152
153void JsSyncManagerObserver::OnClearServerDataFailed() {
[email protected]fa134872011-08-02 18:39:55154 if (!event_handler_.IsInitialized()) {
155 return;
156 }
157 HandleJsEvent(FROM_HERE, "onClearServerDataFailed", JsEventDetails());
158}
159
160void 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]88af96012011-02-11 20:50:17169}
170
171} // namespace browser_sync