NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / sync_internals_message_handler.cc
blobecd38eb94f842d4560b457c28b8da54731e1de50
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"
7 #include <vector>
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() {
30 if (js_controller_)
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();
39 if (service)
40 js_controller_ = service->GetJsController();
41 if (js_controller_)
42 js_controller_->AddJsEventHandler(this);
44 web_ui()->RegisterMessageCallback(
45 "getAboutInfo",
46 base::Bind(&SyncInternalsMessageHandler::OnGetAboutInfo,
47 base::Unretained(this)));
49 web_ui()->RegisterMessageCallback(
50 "getListOfTypes",
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",
66 *value);
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",
80 type_list);
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(),
89 args.Get().end());
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),
100 details.Get());
103 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
104 const std::string& name) {
105 web_ui()->RegisterMessageCallback(
106 name,
107 base::Bind(&SyncInternalsMessageHandler::ForwardToJsController,
108 base::Unretained(this),
109 name));
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(
119 name, js_arg_list,
120 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
121 } else {
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());