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/web_ui.h"
15 #include "sync/internal_api/public/util/weak_handle.h"
16 #include "sync/js/js_arg_list.h"
17 #include "sync/js/js_event_details.h"
19 using syncer::JsArgList
;
20 using syncer::JsEventDetails
;
21 using syncer::JsReplyHandler
;
22 using syncer::ModelTypeSet
;
23 using syncer::WeakHandle
;
25 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
26 : weak_ptr_factory_(this) {}
28 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
30 js_controller_
->RemoveJsEventHandler(this);
33 void SyncInternalsMessageHandler::RegisterMessages() {
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
36 // Init our link to the JsController.
37 ProfileSyncService
* service
= GetProfileSyncService();
39 js_controller_
= service
->GetJsController();
41 js_controller_
->AddJsEventHandler(this);
43 web_ui()->RegisterMessageCallback(
45 base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo
,
46 base::Unretained(this)));
48 web_ui()->RegisterMessageCallback(
50 base::Bind(&SyncInternalsMessageHandler::OnGetListOfTypes
,
51 base::Unretained(this)));
53 RegisterJsControllerCallback("getNotificationState");
54 RegisterJsControllerCallback("getNotificationInfo");
55 RegisterJsControllerCallback("getAllNodes");
56 RegisterJsControllerCallback("getClientServerTraffic");
59 void SyncInternalsMessageHandler::OnGetAboutInfo(const base::ListValue
* args
) {
60 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431.
61 scoped_ptr
<base::DictionaryValue
> value
=
62 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
63 web_ui()->CallJavascriptFunction(
64 "chrome.sync.getAboutInfo.handleReply",
68 void SyncInternalsMessageHandler::OnGetListOfTypes(
69 const base::ListValue
* args
) {
70 // TODO(rlarocque): We should DCHECK(!args) here. See crbug.com/334431.
71 base::ListValue type_list
;
72 ModelTypeSet protocol_types
= syncer::ProtocolTypes();
73 for (ModelTypeSet::Iterator it
= protocol_types
.First();
74 it
.Good(); it
.Inc()) {
75 type_list
.Append(new base::StringValue(ModelTypeToString(it
.Get())));
77 web_ui()->CallJavascriptFunction(
78 "chrome.sync.getListOfTypes.handleReply",
82 void SyncInternalsMessageHandler::HandleJsReply(
83 const std::string
& name
, const JsArgList
& args
) {
84 DVLOG(1) << "Handling reply for " << name
<< " message"
85 << " with args " << args
.ToString();
86 const std::string
& reply_handler
= "chrome.sync." + name
+ ".handleReply";
87 std::vector
<const base::Value
*> arg_list(args
.Get().begin(),
89 web_ui()->CallJavascriptFunction(reply_handler
, arg_list
);
92 void SyncInternalsMessageHandler::HandleJsEvent(
93 const std::string
& name
,
94 const JsEventDetails
& details
) {
95 DVLOG(1) << "Handling event: " << name
96 << " with details " << details
.ToString();
97 const std::string
& event_handler
= "chrome.sync." + name
+ ".fire";
98 std::vector
<const base::Value
*> arg_list(1, &details
.Get());
99 web_ui()->CallJavascriptFunction(event_handler
, arg_list
);
102 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
103 const std::string
& name
) {
104 web_ui()->RegisterMessageCallback(
106 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController
,
107 base::Unretained(this),
111 void SyncInternalsMessageHandler::ForwardToJsController(
112 const std::string
& name
,
113 const base::ListValue
* args
) {
114 if (js_controller_
) {
115 scoped_ptr
<base::ListValue
> args_copy(args
->DeepCopy());
116 JsArgList
js_arg_list(args_copy
.get());
117 js_controller_
->ProcessJsMessage(
119 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
121 DLOG(WARNING
) << "No sync service; dropping message " << name
;
125 // Gets the ProfileSyncService of the underlying original profile.
126 // May return NULL (e.g., if sync is disabled on the command line).
127 ProfileSyncService
* SyncInternalsMessageHandler::GetProfileSyncService() {
128 Profile
* profile
= Profile::FromWebUI(web_ui());
129 ProfileSyncServiceFactory
* factory
= ProfileSyncServiceFactory::GetInstance();
130 return factory
->GetForProfile(profile
->GetOriginalProfile());