1 // Copyright 2014 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 "chrome/browser/ui/webui/sync_internals_message_handler.h"
9 #include "base/logging.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/about_sync_util.h"
12 #include "chrome/browser/sync/profile_sync_service.h"
13 #include "chrome/browser/sync/profile_sync_service_factory.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_ui.h"
16 #include "sync/internal_api/public/events/protocol_event.h"
17 #include "sync/internal_api/public/sessions/commit_counters.h"
18 #include "sync/internal_api/public/sessions/status_counters.h"
19 #include "sync/internal_api/public/sessions/update_counters.h"
20 #include "sync/internal_api/public/util/weak_handle.h"
21 #include "sync/js/js_event_details.h"
23 using syncer::JsEventDetails
;
24 using syncer::ModelTypeSet
;
25 using syncer::WeakHandle
;
27 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
28 : is_registered_(false),
29 is_registered_for_counters_(false),
30 weak_ptr_factory_(this) {
33 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
35 js_controller_
->RemoveJsEventHandler(this);
37 ProfileSyncService
* service
= GetProfileSyncService();
38 if (service
&& service
->HasObserver(this)) {
39 service
->RemoveObserver(this);
40 service
->RemoveProtocolEventObserver(this);
43 if (service
&& is_registered_for_counters_
) {
44 service
->RemoveTypeDebugInfoObserver(this);
48 void SyncInternalsMessageHandler::RegisterMessages() {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
51 web_ui()->RegisterMessageCallback(
53 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForEvents
,
54 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback(
57 "registerForPerTypeCounters",
58 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters
,
59 base::Unretained(this)));
61 web_ui()->RegisterMessageCallback(
62 "requestUpdatedAboutInfo",
63 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo
,
64 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback(
68 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes
,
69 base::Unretained(this)));
71 web_ui()->RegisterMessageCallback(
73 base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes
,
74 base::Unretained(this)));
77 void SyncInternalsMessageHandler::HandleRegisterForEvents(
78 const base::ListValue
* args
) {
79 DCHECK(args
->empty());
81 // is_registered_ flag protects us from double-registering. This could
82 // happen on a page refresh, where the JavaScript gets re-run but the
83 // message handler remains unchanged.
84 ProfileSyncService
* service
= GetProfileSyncService();
85 if (service
&& !is_registered_
) {
86 service
->AddObserver(this);
87 service
->AddProtocolEventObserver(this);
88 js_controller_
= service
->GetJsController();
89 js_controller_
->AddJsEventHandler(this);
90 is_registered_
= true;
94 void SyncInternalsMessageHandler::HandleRegisterForPerTypeCounters(
95 const base::ListValue
* args
) {
96 DCHECK(args
->empty());
98 if (ProfileSyncService
* service
= GetProfileSyncService()) {
99 if (!is_registered_for_counters_
) {
100 service
->AddTypeDebugInfoObserver(this);
101 is_registered_for_counters_
= true;
103 // Re-register to ensure counters get re-emitted.
104 service
->RemoveTypeDebugInfoObserver(this);
105 service
->AddTypeDebugInfoObserver(this);
110 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
111 const base::ListValue
* args
) {
112 DCHECK(args
->empty());
116 void SyncInternalsMessageHandler::HandleRequestListOfTypes(
117 const base::ListValue
* args
) {
118 DCHECK(args
->empty());
119 base::DictionaryValue event_details
;
120 scoped_ptr
<base::ListValue
> type_list(new base::ListValue());
121 ModelTypeSet protocol_types
= syncer::ProtocolTypes();
122 for (ModelTypeSet::Iterator it
= protocol_types
.First();
123 it
.Good(); it
.Inc()) {
124 type_list
->Append(new base::StringValue(ModelTypeToString(it
.Get())));
126 event_details
.Set("types", type_list
.release());
127 web_ui()->CallJavascriptFunction(
128 "chrome.sync.dispatchEvent",
129 base::StringValue("onReceivedListOfTypes"),
133 void SyncInternalsMessageHandler::HandleGetAllNodes(
134 const base::ListValue
* args
) {
135 DCHECK_EQ(1U, args
->GetSize());
137 bool success
= args
->GetInteger(0, &request_id
);
140 ProfileSyncService
* service
= GetProfileSyncService();
142 service
->GetAllNodes(
143 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes
,
144 weak_ptr_factory_
.GetWeakPtr(), request_id
));
148 void SyncInternalsMessageHandler::OnReceivedAllNodes(
150 scoped_ptr
<base::ListValue
> nodes
) {
151 base::FundamentalValue
id(request_id
);
152 web_ui()->CallJavascriptFunction("chrome.sync.getAllNodesCallback",
156 void SyncInternalsMessageHandler::OnStateChanged() {
160 void SyncInternalsMessageHandler::OnProtocolEvent(
161 const syncer::ProtocolEvent
& event
) {
162 scoped_ptr
<base::DictionaryValue
> value(
163 syncer::ProtocolEvent::ToValue(event
));
164 web_ui()->CallJavascriptFunction(
165 "chrome.sync.dispatchEvent",
166 base::StringValue("onProtocolEvent"),
170 void SyncInternalsMessageHandler::OnCommitCountersUpdated(
171 syncer::ModelType type
,
172 const syncer::CommitCounters
& counters
) {
173 EmitCounterUpdate(type
, "commit", counters
.ToValue());
176 void SyncInternalsMessageHandler::OnUpdateCountersUpdated(
177 syncer::ModelType type
,
178 const syncer::UpdateCounters
& counters
) {
179 EmitCounterUpdate(type
, "update", counters
.ToValue());
182 void SyncInternalsMessageHandler::OnStatusCountersUpdated(
183 syncer::ModelType type
,
184 const syncer::StatusCounters
& counters
) {
185 EmitCounterUpdate(type
, "status", counters
.ToValue());
188 void SyncInternalsMessageHandler::EmitCounterUpdate(
189 syncer::ModelType type
,
190 const std::string
& counter_type
,
191 scoped_ptr
<base::DictionaryValue
> value
) {
192 scoped_ptr
<base::DictionaryValue
> details(new base::DictionaryValue());
193 details
->SetString("modelType", ModelTypeToString(type
));
194 details
->SetString("counterType", counter_type
);
195 details
->Set("counters", value
.release());
196 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
197 base::StringValue("onCountersUpdated"),
201 void SyncInternalsMessageHandler::HandleJsEvent(
202 const std::string
& name
,
203 const JsEventDetails
& details
) {
204 DVLOG(1) << "Handling event: " << name
205 << " with details " << details
.ToString();
206 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
207 base::StringValue(name
),
211 void SyncInternalsMessageHandler::SendAboutInfo() {
212 scoped_ptr
<base::DictionaryValue
> value
=
213 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
214 web_ui()->CallJavascriptFunction(
215 "chrome.sync.dispatchEvent",
216 base::StringValue("onAboutInfoUpdated"),
220 // Gets the ProfileSyncService of the underlying original profile.
221 // May return NULL (e.g., if sync is disabled on the command line).
222 ProfileSyncService
* SyncInternalsMessageHandler::GetProfileSyncService() {
223 Profile
* profile
= Profile::FromWebUI(web_ui());
224 return ProfileSyncServiceFactory::GetForProfile(
225 profile
->GetOriginalProfile());