Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / sync / internal_api / js_sync_encryption_handler_observer.cc
blob7924d31f283a4877988aacd1b602914e7135b1fe
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/strings/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_event_details.h"
16 #include "sync/js/js_event_handler.h"
17 #include "sync/util/cryptographer.h"
18 #include "sync/util/time.h"
20 namespace syncer {
22 JsSyncEncryptionHandlerObserver::JsSyncEncryptionHandlerObserver() {}
24 JsSyncEncryptionHandlerObserver::~JsSyncEncryptionHandlerObserver() {}
26 void JsSyncEncryptionHandlerObserver::SetJsEventHandler(
27 const WeakHandle<JsEventHandler>& event_handler) {
28 event_handler_ = event_handler;
31 void JsSyncEncryptionHandlerObserver::OnPassphraseRequired(
32 PassphraseRequiredReason reason,
33 const sync_pb::EncryptedData& pending_keys) {
34 if (!event_handler_.IsInitialized()) {
35 return;
37 base::DictionaryValue details;
38 details.SetString("reason",
39 PassphraseRequiredReasonToString(reason));
40 HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details));
43 void JsSyncEncryptionHandlerObserver::OnPassphraseAccepted() {
44 if (!event_handler_.IsInitialized()) {
45 return;
47 base::DictionaryValue details;
48 HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details));
51 void JsSyncEncryptionHandlerObserver::OnBootstrapTokenUpdated(
52 const std::string& boostrap_token,
53 BootstrapTokenType type) {
54 if (!event_handler_.IsInitialized()) {
55 return;
57 base::DictionaryValue details;
58 details.SetString("bootstrapToken", "<redacted>");
59 details.SetString("type", BootstrapTokenTypeToString(type));
60 HandleJsEvent(FROM_HERE, "onBootstrapTokenUpdated", JsEventDetails(&details));
63 void JsSyncEncryptionHandlerObserver::OnEncryptedTypesChanged(
64 ModelTypeSet encrypted_types,
65 bool encrypt_everything) {
66 if (!event_handler_.IsInitialized()) {
67 return;
69 base::DictionaryValue details;
70 details.Set("encryptedTypes",
71 ModelTypeSetToValue(encrypted_types));
72 details.SetBoolean("encryptEverything", encrypt_everything);
73 HandleJsEvent(FROM_HERE,
74 "onEncryptedTypesChanged", JsEventDetails(&details));
77 void JsSyncEncryptionHandlerObserver::OnEncryptionComplete() {
78 if (!event_handler_.IsInitialized()) {
79 return;
81 base::DictionaryValue details;
82 HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails());
85 void JsSyncEncryptionHandlerObserver::OnCryptographerStateChanged(
86 Cryptographer* cryptographer) {
87 if (!event_handler_.IsInitialized()) {
88 return;
90 base::DictionaryValue details;
91 details.SetBoolean("ready",
92 cryptographer->is_ready());
93 details.SetBoolean("hasPendingKeys",
94 cryptographer->has_pending_keys());
95 HandleJsEvent(FROM_HERE,
96 "onCryptographerStateChanged",
97 JsEventDetails(&details));
100 void JsSyncEncryptionHandlerObserver::OnPassphraseTypeChanged(
101 PassphraseType type,
102 base::Time explicit_passphrase_time) {
103 if (!event_handler_.IsInitialized()) {
104 return;
106 base::DictionaryValue details;
107 details.SetString("passphraseType",
108 PassphraseTypeToString(type));
109 details.SetInteger("explicitPassphraseTime",
110 TimeToProtoTime(explicit_passphrase_time));
111 HandleJsEvent(FROM_HERE,
112 "onPassphraseTypeChanged",
113 JsEventDetails(&details));
116 void JsSyncEncryptionHandlerObserver::OnLocalSetPassphraseEncryption(
117 const SyncEncryptionHandler::NigoriState& nigori_state) {
120 void JsSyncEncryptionHandlerObserver::HandleJsEvent(
121 const tracked_objects::Location& from_here,
122 const std::string& name, const JsEventDetails& details) {
123 if (!event_handler_.IsInitialized()) {
124 NOTREACHED();
125 return;
127 event_handler_.Call(from_here,
128 &JsEventHandler::HandleJsEvent, name, details);
131 } // namespace syncer