Bluetooth: fix issues with discovery API
[chromium-blink-merge.git] / sync / internal_api / js_sync_encryption_handler_observer.cc
blobd44bda7f5eaed5d9f498372ea678d341d159002d
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.
5 #include "sync/internal_api/js_sync_encryption_handler_observer.h"
7 #include <cstddef>
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/string_number_conversions.h"
12 #include "base/values.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/util/sync_string_conversions.h"
15 #include "sync/js/js_arg_list.h"
16 #include "sync/js/js_event_details.h"
17 #include "sync/js/js_event_handler.h"
18 #include "sync/util/cryptographer.h"
19 #include "sync/util/time.h"
21 namespace syncer {
23 JsSyncEncryptionHandlerObserver::JsSyncEncryptionHandlerObserver() {}
25 JsSyncEncryptionHandlerObserver::~JsSyncEncryptionHandlerObserver() {}
27 void JsSyncEncryptionHandlerObserver::SetJsEventHandler(
28 const WeakHandle<JsEventHandler>& event_handler) {
29 event_handler_ = event_handler;
32 void JsSyncEncryptionHandlerObserver::OnPassphraseRequired(
33 PassphraseRequiredReason reason,
34 const sync_pb::EncryptedData& pending_keys) {
35 if (!event_handler_.IsInitialized()) {
36 return;
38 DictionaryValue details;
39 details.SetString("reason",
40 PassphraseRequiredReasonToString(reason));
41 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details));
44 void JsSyncEncryptionHandlerObserver::OnPassphraseAccepted() {
45 if (!event_handler_.IsInitialized()) {
46 return;
48 DictionaryValue details;
49 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details));
52 void JsSyncEncryptionHandlerObserver::OnBootstrapTokenUpdated(
53 const std::string& boostrap_token,
54 BootstrapTokenType type) {
55 if (!event_handler_.IsInitialized()) {
56 return;
58 DictionaryValue details;
59 details.SetString("bootstrapToken", "<redacted>");
60 details.SetString("type", BootstrapTokenTypeToString(type));
61 HandleJsEvent(FROM_HERE, "onBootstrapTokenUpdated", JsEventDetails(&details));
64 void JsSyncEncryptionHandlerObserver::OnEncryptedTypesChanged(
65 ModelTypeSet encrypted_types,
66 bool encrypt_everything) {
67 if (!event_handler_.IsInitialized()) {
68 return;
70 DictionaryValue details;
71 details.Set("encryptedTypes",
72 ModelTypeSetToValue(encrypted_types));
73 details.SetBoolean("encryptEverything", encrypt_everything);
74 HandleJsEvent(FROM_HERE,
75 "onEncryptedTypesChanged", JsEventDetails(&details));
78 void JsSyncEncryptionHandlerObserver::OnEncryptionComplete() {
79 if (!event_handler_.IsInitialized()) {
80 return;
82 DictionaryValue details;
83 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails());
86 void JsSyncEncryptionHandlerObserver::OnCryptographerStateChanged(
87 Cryptographer* cryptographer) {
88 if (!event_handler_.IsInitialized()) {
89 return;
91 DictionaryValue details;
92 details.SetBoolean("ready",
93 cryptographer->is_ready());
94 details.SetBoolean("hasPendingKeys",
95 cryptographer->has_pending_keys());
96 HandleJsEvent(FROM_HERE,
97 "onCryptographerStateChanged",
98 JsEventDetails(&details));
101 void JsSyncEncryptionHandlerObserver::OnPassphraseTypeChanged(
102 PassphraseType type,
103 base::Time explicit_passphrase_time) {
104 if (!event_handler_.IsInitialized()) {
105 return;
107 DictionaryValue details;
108 details.SetString("passphraseType",
109 PassphraseTypeToString(type));
110 details.SetInteger("explicitPassphraseTime",
111 TimeToProtoTime(explicit_passphrase_time));
112 HandleJsEvent(FROM_HERE,
113 "onPassphraseTypeChanged",
114 JsEventDetails(&details));
117 void JsSyncEncryptionHandlerObserver::HandleJsEvent(
118 const tracked_objects::Location& from_here,
119 const std::string& name, const JsEventDetails& details) {
120 if (!event_handler_.IsInitialized()) {
121 NOTREACHED();
122 return;
124 event_handler_.Call(from_here,
125 &JsEventHandler::HandleJsEvent, name, details);
128 } // namespace syncer