OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/internal_api/js_sync_manager_observer.h" | |
6 | |
7 #include <cstddef> | |
8 | |
9 #include "base/location.h" | |
10 #include "base/logging.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/sync/internal_api/change_record.h" | |
14 #include "sync/js/js_arg_list.h" | |
15 #include "sync/js/js_event_details.h" | |
16 #include "sync/js/js_event_handler.h" | |
17 #include "sync/sessions/session_state.h" | |
18 #include "sync/syncable/model_type.h" | |
19 | |
20 namespace browser_sync { | |
21 | |
22 using browser_sync::SyncProtocolError; | |
23 | |
24 JsSyncManagerObserver::JsSyncManagerObserver() {} | |
25 | |
26 JsSyncManagerObserver::~JsSyncManagerObserver() {} | |
27 | |
28 void JsSyncManagerObserver::SetJsEventHandler( | |
29 const WeakHandle<JsEventHandler>& event_handler) { | |
30 event_handler_ = event_handler; | |
31 } | |
32 | |
33 void JsSyncManagerObserver::OnSyncCycleCompleted( | |
34 const sessions::SyncSessionSnapshot* snapshot) { | |
35 if (!event_handler_.IsInitialized()) { | |
36 return; | |
37 } | |
38 DictionaryValue details; | |
39 details.Set("snapshot", snapshot->ToValue()); | |
40 HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); | |
41 } | |
42 | |
43 void JsSyncManagerObserver::OnConnectionStatusChange( | |
44 sync_api::ConnectionStatus status) { | |
45 if (!event_handler_.IsInitialized()) { | |
46 return; | |
47 } | |
48 DictionaryValue details; | |
49 details.SetString("status", sync_api::ConnectionStatusToString(status)); | |
50 HandleJsEvent(FROM_HERE, | |
51 "onConnectionStatusChange", JsEventDetails(&details)); | |
52 } | |
53 | |
54 void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { | |
55 if (!event_handler_.IsInitialized()) { | |
56 return; | |
57 } | |
58 DictionaryValue details; | |
59 details.SetString("token", "<redacted>"); | |
60 HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); | |
61 } | |
62 | |
63 void JsSyncManagerObserver::OnPassphraseRequired( | |
64 sync_api::PassphraseRequiredReason reason, | |
65 const sync_pb::EncryptedData& pending_keys) { | |
66 if (!event_handler_.IsInitialized()) { | |
67 return; | |
68 } | |
69 DictionaryValue details; | |
70 details.SetString("reason", | |
71 sync_api::PassphraseRequiredReasonToString(reason)); | |
72 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); | |
73 } | |
74 | |
75 void JsSyncManagerObserver::OnPassphraseAccepted() { | |
76 if (!event_handler_.IsInitialized()) { | |
77 return; | |
78 } | |
79 DictionaryValue details; | |
80 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); | |
81 } | |
82 | |
83 void JsSyncManagerObserver::OnBootstrapTokenUpdated( | |
84 const std::string& boostrap_token) { | |
85 if (!event_handler_.IsInitialized()) { | |
86 return; | |
87 } | |
88 DictionaryValue details; | |
89 details.SetString("bootstrapToken", "<redacted>"); | |
90 HandleJsEvent(FROM_HERE, "OnBootstrapTokenUpdated", JsEventDetails(&details)); | |
91 } | |
92 | |
93 void JsSyncManagerObserver::OnEncryptedTypesChanged( | |
94 syncable::ModelTypeSet encrypted_types, | |
95 bool encrypt_everything) { | |
96 if (!event_handler_.IsInitialized()) { | |
97 return; | |
98 } | |
99 DictionaryValue details; | |
100 details.Set("encryptedTypes", | |
101 syncable::ModelTypeSetToValue(encrypted_types)); | |
102 details.SetBoolean("encryptEverything", encrypt_everything); | |
103 HandleJsEvent(FROM_HERE, | |
104 "onEncryptedTypesChanged", JsEventDetails(&details)); | |
105 } | |
106 | |
107 void JsSyncManagerObserver::OnEncryptionComplete() { | |
108 if (!event_handler_.IsInitialized()) { | |
109 return; | |
110 } | |
111 DictionaryValue details; | |
112 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails()); | |
113 } | |
114 | |
115 void JsSyncManagerObserver::OnActionableError( | |
116 const SyncProtocolError& sync_error) { | |
117 if (!event_handler_.IsInitialized()) { | |
118 return; | |
119 } | |
120 DictionaryValue details; | |
121 details.Set("syncError", sync_error.ToValue()); | |
122 HandleJsEvent(FROM_HERE, "onActionableError", | |
123 JsEventDetails(&details)); | |
124 } | |
125 | |
126 void JsSyncManagerObserver::OnInitializationComplete( | |
127 const WeakHandle<JsBackend>& js_backend, | |
128 bool success) { | |
129 if (!event_handler_.IsInitialized()) { | |
130 return; | |
131 } | |
132 // Ignore the |js_backend| argument; it's not really convertible to | |
133 // JSON anyway. | |
134 HandleJsEvent(FROM_HERE, "onInitializationComplete", JsEventDetails()); | |
135 } | |
136 | |
137 void JsSyncManagerObserver::OnStopSyncingPermanently() { | |
138 if (!event_handler_.IsInitialized()) { | |
139 return; | |
140 } | |
141 HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); | |
142 } | |
143 | |
144 void JsSyncManagerObserver::OnClearServerDataSucceeded() { | |
145 if (!event_handler_.IsInitialized()) { | |
146 return; | |
147 } | |
148 HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails()); | |
149 } | |
150 | |
151 void JsSyncManagerObserver::OnClearServerDataFailed() { | |
152 if (!event_handler_.IsInitialized()) { | |
153 return; | |
154 } | |
155 HandleJsEvent(FROM_HERE, "onClearServerDataFailed", JsEventDetails()); | |
156 } | |
157 | |
158 void JsSyncManagerObserver::HandleJsEvent( | |
159 const tracked_objects::Location& from_here, | |
160 const std::string& name, const JsEventDetails& details) { | |
161 if (!event_handler_.IsInitialized()) { | |
162 NOTREACHED(); | |
163 return; | |
164 } | |
165 event_handler_.Call(from_here, | |
166 &JsEventHandler::HandleJsEvent, name, details); | |
167 } | |
168 | |
169 } // namespace browser_sync | |
OLD | NEW |