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/util/weak_handle.h"
17 #include "sync/js/js_arg_list.h"
18 #include "sync/js/js_event_details.h"
20 using syncer::JsArgList
;
21 using syncer::JsEventDetails
;
22 using syncer::JsReplyHandler
;
23 using syncer::ModelTypeSet
;
24 using syncer::WeakHandle
;
26 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
27 : weak_ptr_factory_(this) {}
29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
31 js_controller_
->RemoveJsEventHandler(this);
34 void SyncInternalsMessageHandler::RegisterMessages() {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
37 // Init our link to the JsController.
38 ProfileSyncService
* service
= GetProfileSyncService();
40 js_controller_
= service
->GetJsController();
42 js_controller_
->AddJsEventHandler(this);
44 web_ui()->RegisterMessageCallback(
46 base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo
,
47 base::Unretained(this)));
49 web_ui()->RegisterMessageCallback(
51 base::Bind(&SyncInternalsMessageHandler::OnGetListOfTypes
,
52 base::Unretained(this)));
54 RegisterJsControllerCallback("getNotificationState");
55 RegisterJsControllerCallback("getNotificationInfo");
56 RegisterJsControllerCallback("getAllNodes");
57 RegisterJsControllerCallback("getClientServerTraffic");
60 void SyncInternalsMessageHandler::OnGetAboutInfo(const base::ListValue
* args
) {
61 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431.
62 scoped_ptr
<base::DictionaryValue
> value
=
63 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
64 web_ui()->CallJavascriptFunction(
65 "chrome.sync.getAboutInfo.handleReply",
69 void SyncInternalsMessageHandler::OnGetListOfTypes(
70 const base::ListValue
* args
) {
71 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431.
72 base::ListValue type_list
;
73 ModelTypeSet protocol_types
= syncer::ProtocolTypes();
74 for (ModelTypeSet::Iterator it
= protocol_types
.First();
75 it
.Good(); it
.Inc()) {
76 type_list
.Append(new base::StringValue(ModelTypeToString(it
.Get())));
78 web_ui()->CallJavascriptFunction(
79 "chrome.sync.getListOfTypes.handleReply",
83 void SyncInternalsMessageHandler::HandleJsReply(
84 const std::string
& name
, const JsArgList
& args
) {
85 DVLOG(1) << "Handling reply for " << name
<< " message"
86 << " with args " << args
.ToString();
87 const std::string
& reply_handler
= "chrome.sync." + name
+ ".handleReply";
88 std::vector
<const base::Value
*> arg_list(args
.Get().begin(),
90 web_ui()->CallJavascriptFunction(reply_handler
, arg_list
);
93 void SyncInternalsMessageHandler::HandleJsEvent(
94 const std::string
& name
,
95 const JsEventDetails
& details
) {
96 DVLOG(1) << "Handling event: " << name
97 << " with details " << details
.ToString();
98 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
99 base::StringValue(name
),
103 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
104 const std::string
& name
) {
105 web_ui()->RegisterMessageCallback(
107 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController
,
108 base::Unretained(this),
112 void SyncInternalsMessageHandler::ForwardToJsController(
113 const std::string
& name
,
114 const base::ListValue
* args
) {
115 if (js_controller_
) {
116 scoped_ptr
<base::ListValue
> args_copy(args
->DeepCopy());
117 JsArgList
js_arg_list(args_copy
.get());
118 js_controller_
->ProcessJsMessage(
120 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
122 DLOG(WARNING
) << "No sync service; dropping message " << name
;
126 // Gets the ProfileSyncService of the underlying original profile.
127 // May return NULL (e.g., if sync is disabled on the command line).
128 ProfileSyncService
* SyncInternalsMessageHandler::GetProfileSyncService() {
129 Profile
* profile
= Profile::FromWebUI(web_ui());
130 ProfileSyncServiceFactory
* factory
= ProfileSyncServiceFactory::GetInstance();
131 return factory
->GetForProfile(profile
->GetOriginalProfile());