• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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"
10 #include "base/values.h"
11 #include "chrome/browser/sync/js_arg_list.h"
12 #include "chrome/browser/sync/js_event_router.h"
13 #include "chrome/browser/sync/sessions/session_state.h"
14 #include "chrome/browser/sync/syncable/model_type.h"
15 
16 namespace browser_sync {
17 
JsSyncManagerObserver(JsEventRouter * parent_router)18 JsSyncManagerObserver::JsSyncManagerObserver(JsEventRouter* parent_router)
19     : parent_router_(parent_router) {
20   DCHECK(parent_router_);
21 }
22 
~JsSyncManagerObserver()23 JsSyncManagerObserver::~JsSyncManagerObserver() {}
24 
OnChangesApplied(syncable::ModelType model_type,const sync_api::BaseTransaction * trans,const sync_api::SyncManager::ChangeRecord * changes,int change_count)25 void JsSyncManagerObserver::OnChangesApplied(
26     syncable::ModelType model_type,
27     const sync_api::BaseTransaction* trans,
28     const sync_api::SyncManager::ChangeRecord* changes,
29     int change_count) {
30   ListValue return_args;
31   return_args.Append(Value::CreateStringValue(
32       syncable::ModelTypeToString(model_type)));
33   ListValue* change_values = new ListValue();
34   return_args.Append(change_values);
35   for (int i = 0; i < change_count; ++i) {
36     change_values->Append(changes[i].ToValue(trans));
37   }
38   parent_router_->RouteJsEvent("onChangesApplied",
39                                JsArgList(return_args), NULL);
40 }
41 
OnChangesComplete(syncable::ModelType model_type)42 void JsSyncManagerObserver::OnChangesComplete(
43     syncable::ModelType model_type) {
44   ListValue return_args;
45   return_args.Append(Value::CreateStringValue(
46       syncable::ModelTypeToString(model_type)));
47   parent_router_->RouteJsEvent("onChangesComplete",
48                                JsArgList(return_args), NULL);
49 }
50 
OnSyncCycleCompleted(const sessions::SyncSessionSnapshot * snapshot)51 void JsSyncManagerObserver::OnSyncCycleCompleted(
52     const sessions::SyncSessionSnapshot* snapshot) {
53   ListValue return_args;
54   return_args.Append(snapshot->ToValue());
55   parent_router_->RouteJsEvent("onSyncCycleCompleted",
56                                JsArgList(return_args), NULL);
57 }
58 
OnAuthError(const GoogleServiceAuthError & auth_error)59 void JsSyncManagerObserver::OnAuthError(
60     const GoogleServiceAuthError& auth_error) {
61   ListValue return_args;
62   return_args.Append(auth_error.ToValue());
63   parent_router_->RouteJsEvent("onAuthError",
64                                JsArgList(return_args), NULL);
65 }
66 
OnUpdatedToken(const std::string & token)67 void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) {
68   ListValue return_args;
69   return_args.Append(Value::CreateStringValue("<redacted>"));
70   parent_router_->RouteJsEvent("onUpdatedToken",
71                                JsArgList(return_args), NULL);
72 }
73 
OnPassphraseRequired(bool for_decryption)74 void JsSyncManagerObserver::OnPassphraseRequired(bool for_decryption) {
75   ListValue return_args;
76   return_args.Append(Value::CreateBooleanValue(for_decryption));
77   parent_router_->RouteJsEvent("onPassphraseRequired",
78                                JsArgList(return_args), NULL);
79 }
80 
OnPassphraseFailed()81 void JsSyncManagerObserver::OnPassphraseFailed() {
82   parent_router_->RouteJsEvent("onPassphraseFailed", JsArgList(), NULL);
83 }
84 
OnPassphraseAccepted(const std::string & bootstrap_token)85 void JsSyncManagerObserver::OnPassphraseAccepted(
86     const std::string& bootstrap_token) {
87   ListValue return_args;
88   return_args.Append(Value::CreateStringValue("<redacted>"));
89   parent_router_->RouteJsEvent("onPassphraseAccepted",
90                                JsArgList(return_args), NULL);
91 }
92 
OnEncryptionComplete(const syncable::ModelTypeSet & encrypted_types)93 void JsSyncManagerObserver::OnEncryptionComplete(
94     const syncable::ModelTypeSet& encrypted_types) {
95   ListValue return_args;
96   return_args.Append(syncable::ModelTypeSetToValue(encrypted_types));
97   parent_router_->RouteJsEvent("onEncryptionComplete",
98                                JsArgList(return_args), NULL);
99 }
100 
OnMigrationNeededForTypes(const syncable::ModelTypeSet & types)101 void JsSyncManagerObserver::OnMigrationNeededForTypes(
102     const syncable::ModelTypeSet& types) {
103   ListValue return_args;
104   return_args.Append(syncable::ModelTypeSetToValue(types));
105   parent_router_->RouteJsEvent("onMigrationNeededForTypes",
106                                JsArgList(return_args), NULL);
107   // TODO(akalin): Bug 79247. Hook up JS boiler plate!
108 }
109 
OnInitializationComplete()110 void JsSyncManagerObserver::OnInitializationComplete() {
111   parent_router_->RouteJsEvent("onInitializationComplete",
112                                JsArgList(), NULL);
113 }
114 
OnStopSyncingPermanently()115 void JsSyncManagerObserver::OnStopSyncingPermanently() {
116   parent_router_->RouteJsEvent("onStopSyncingPermanently",
117                                JsArgList(), NULL);
118 }
119 
OnClearServerDataSucceeded()120 void JsSyncManagerObserver::OnClearServerDataSucceeded() {
121   parent_router_->RouteJsEvent("onClearServerDataSucceeded",
122                                JsArgList(), NULL);
123 }
124 
OnClearServerDataFailed()125 void JsSyncManagerObserver::OnClearServerDataFailed() {
126   parent_router_->RouteJsEvent("onClearServerDataFailed", JsArgList(), NULL);
127 }
128 
129 }  // namespace browser_sync
130