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);
33 ProfileSyncService* service = GetProfileSyncService();
34 if (service && service->HasObserver(this)) {
35 service->RemoveObserver(this);
39 void SyncInternalsMessageHandler::RegisterMessages() {
40 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
42 // Register for ProfileSyncService events.
43 ProfileSyncService* service = GetProfileSyncService();
45 service->AddObserver(this);
46 js_controller_ = service->GetJsController();
47 js_controller_->AddJsEventHandler(this);
50 web_ui()->RegisterMessageCallback(
51 "requestUpdatedAboutInfo",
52 base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo,
53 base::Unretained(this)));
55 web_ui()->RegisterMessageCallback(
57 base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes,
58 base::Unretained(this)));
60 RegisterJsControllerCallback("getNotificationState");
61 RegisterJsControllerCallback("getNotificationInfo");
62 RegisterJsControllerCallback("getAllNodes");
63 RegisterJsControllerCallback("getClientServerTraffic");
66 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
67 const base::ListValue* args) {
68 DCHECK(args->empty());
72 void SyncInternalsMessageHandler::HandleRequestListOfTypes(
73 const base::ListValue* args) {
74 DCHECK(args->empty());
75 base::DictionaryValue event_details;
76 scoped_ptr<base::ListValue> type_list(new base::ListValue());
77 ModelTypeSet protocol_types = syncer::ProtocolTypes();
78 for (ModelTypeSet::Iterator it = protocol_types.First();
79 it.Good(); it.Inc()) {
80 type_list->Append(new base::StringValue(ModelTypeToString(it.Get())));
82 event_details.Set("types", type_list.release());
83 web_ui()->CallJavascriptFunction(
84 "chrome.sync.dispatchEvent",
85 base::StringValue("onReceivedListOfTypes"),
89 void SyncInternalsMessageHandler::HandleJsReply(
90 const std::string& name, const JsArgList& args) {
91 DVLOG(1) << "Handling reply for " << name << " message"
92 << " with args " << args.ToString();
93 const std::string& reply_handler = "chrome.sync." + name + ".handleReply";
94 std::vector<const base::Value*> arg_list(args.Get().begin(),
96 web_ui()->CallJavascriptFunction(reply_handler, arg_list);
99 void SyncInternalsMessageHandler::OnStateChanged() {
103 void SyncInternalsMessageHandler::HandleJsEvent(
104 const std::string& name,
105 const JsEventDetails& details) {
106 DVLOG(1) << "Handling event: " << name
107 << " with details " << details.ToString();
108 web_ui()->CallJavascriptFunction("chrome.sync.dispatchEvent",
109 base::StringValue(name),
113 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
114 const std::string& name) {
115 web_ui()->RegisterMessageCallback(
117 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController,
118 base::Unretained(this),
122 void SyncInternalsMessageHandler::SendAboutInfo() {
123 scoped_ptr<base::DictionaryValue> value =
124 sync_ui_util::ConstructAboutInformation(GetProfileSyncService());
125 web_ui()->CallJavascriptFunction(
126 "chrome.sync.dispatchEvent",
127 base::StringValue("onAboutInfoUpdated"),
131 void SyncInternalsMessageHandler::ForwardToJsController(
132 const std::string& name,
133 const base::ListValue* args) {
134 if (js_controller_) {
135 scoped_ptr<base::ListValue> args_copy(args->DeepCopy());
136 JsArgList js_arg_list(args_copy.get());
137 js_controller_->ProcessJsMessage(
139 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
141 DLOG(WARNING) << "No sync service; dropping message " << name;
145 // Gets the ProfileSyncService of the underlying original profile.
146 // May return NULL (e.g., if sync is disabled on the command line).
147 ProfileSyncService* SyncInternalsMessageHandler::GetProfileSyncService() {
148 Profile* profile = Profile::FromWebUI(web_ui());
149 ProfileSyncServiceFactory* factory = ProfileSyncServiceFactory::GetInstance();
150 return factory->GetForProfile(profile->GetOriginalProfile());