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/util/weak_handle.h"
18 #include "sync/js/js_event_details.h"
20 using syncer::JsEventDetails
;
21 using syncer::ModelTypeSet
;
22 using syncer::WeakHandle
;
24 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
25 : is_registered_(false),
26 weak_ptr_factory_(this) {}
28 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
30 js_controller_
->RemoveJsEventHandler(this);
32 ProfileSyncService
* service
= GetProfileSyncService();
33 if (service
&& service
->HasObserver(this)) {
34 service
->RemoveObserver(this);
35 service
->RemoveProtocolEventObserver(this);
39 void SyncInternalsMessageHandler::RegisterMessages() {
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
42 web_ui()->RegisterMessageCallback(
44 base::Bind(&SyncInternalsMessageHandler::HandleRegisterForEvents
,
45 base::Unretained(this)));
47 web_ui()->RegisterMessageCallback(
48 "requestUpdatedAboutInfo",
49 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo
,
50 base::Unretained(this)));
52 web_ui()->RegisterMessageCallback(
54 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes
,
55 base::Unretained(this)));
57 web_ui()->RegisterMessageCallback(
59 base::Bind(&SyncInternalsMessageHandler::HandleGetAllNodes
,
60 base::Unretained(this)));
63 void SyncInternalsMessageHandler::HandleRegisterForEvents(
64 const base::ListValue
* args
) {
65 DCHECK(args
->empty());
67 // is_registered_ flag protects us from double-registering. This could
68 // happen on a page refresh, where the JavaScript gets re-run but the
69 // message handler remains unchanged.
70 ProfileSyncService
* service
= GetProfileSyncService();
71 if (service
&& !is_registered_
) {
72 service
->AddObserver(this);
73 service
->AddProtocolEventObserver(this);
74 js_controller_
= service
->GetJsController();
75 js_controller_
->AddJsEventHandler(this);
76 is_registered_
= true;
80 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
81 const base::ListValue
* args
) {
82 DCHECK(args
->empty());
86 void SyncInternalsMessageHandler::HandleRequestListOfTypes(
87 const base::ListValue
* args
) {
88 DCHECK(args
->empty());
89 base::DictionaryValue event_details
;
90 scoped_ptr
<base::ListValue
> type_list(new base::ListValue());
91 ModelTypeSet protocol_types
= syncer::ProtocolTypes();
92 for (ModelTypeSet::Iterator it
= protocol_types
.First();
93 it
.Good(); it
.Inc()) {
94 type_list
->Append(new base::StringValue(ModelTypeToString(it
.Get())));
96 event_details
.Set("types", type_list
.release());
97 web_ui()->CallJavascriptFunction(
98 "chrome.sync.dispatchEvent",
99 base::StringValue("onReceivedListOfTypes"),
103 void SyncInternalsMessageHandler::HandleGetAllNodes(
104 const base::ListValue
* args
) {
105 DCHECK_EQ(1U, args
->GetSize());
107 bool success
= args
->GetInteger(0, &request_id
);
110 ProfileSyncService
* service
= GetProfileSyncService();
112 service
->GetAllNodes(
113 base::Bind(&SyncInternalsMessageHandler::OnReceivedAllNodes
,
114 weak_ptr_factory_
.GetWeakPtr(), request_id
));
118 void SyncInternalsMessageHandler::OnReceivedAllNodes(
120 scoped_ptr
<base::ListValue
> nodes
) {
121 base::FundamentalValue
id(request_id
);
122 web_ui()->CallJavascriptFunction("chrome.sync.getAllNodesCallback",
126 void SyncInternalsMessageHandler::OnStateChanged() {
130 void SyncInternalsMessageHandler::OnProtocolEvent(
131 const syncer::ProtocolEvent
& event
) {
132 scoped_ptr
<base::DictionaryValue
> value(
133 syncer::ProtocolEvent::ToValue(event
));
134 web_ui()->CallJavascriptFunction(
135 "chrome.sync.dispatchEvent",
136 base::StringValue("onProtocolEvent"),
140 void SyncInternalsMessageHandler::HandleJsEvent(
141 const std::string
& name
,
142 const JsEventDetails
& details
) {
143 DVLOG(1) << "Handling event: " << name
144 << " with details " << details
.ToString();
145 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
146 base::StringValue(name
),
150 void SyncInternalsMessageHandler::SendAboutInfo() {
151 scoped_ptr
<base::DictionaryValue
> value
=
152 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
153 web_ui()->CallJavascriptFunction(
154 "chrome.sync.dispatchEvent",
155 base::StringValue("onAboutInfoUpdated"),
159 // Gets the ProfileSyncService of the underlying original profile.
160 // May return NULL (e.g., if sync is disabled on the command line).
161 ProfileSyncService
* SyncInternalsMessageHandler::GetProfileSyncService() {
162 Profile
* profile
= Profile::FromWebUI(web_ui());
163 ProfileSyncServiceFactory
* factory
= ProfileSyncServiceFactory::GetInstance();
164 return factory
->GetForProfile(profile
->GetOriginalProfile());