Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / sync_internals_message_handler.cc
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.
4
5 #include "chrome/browser/ui/webui/sync_internals_message_handler.h"
6
7 #include <vector>
8
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"
19
20 using syncer::JsArgList;
21 using syncer::JsEventDetails;
22 using syncer::JsReplyHandler;
23 using syncer::ModelTypeSet;
24 using syncer::WeakHandle;
25
26 SyncInternalsMessageHandler::SyncInternalsMessageHandler()
27     : weak_ptr_factory_(this) {}
28
29 SyncInternalsMessageHandler::~SyncInternalsMessageHandler() {
30   if (js_controller_)
31     js_controller_->RemoveJsEventHandler(this);
32
33   ProfileSyncService* service = GetProfileSyncService();
34   if (service && service->HasObserver(this)) {
35     service->RemoveObserver(this);
36   }
37 }
38
39 void SyncInternalsMessageHandler::RegisterMessages() {
40   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
41
42   // Register for ProfileSyncService events.
43   ProfileSyncService* service = GetProfileSyncService();
44   if (service) {
45     service->AddObserver(this);
46     js_controller_ = service->GetJsController();
47     js_controller_->AddJsEventHandler(this);
48   }
49
50   web_ui()->RegisterMessageCallback(
51       "requestUpdatedAboutInfo",
52       base::Bind(&SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo,
53                  base::Unretained(this)));
54
55   web_ui()->RegisterMessageCallback(
56       "requestListOfTypes",
57       base::Bind(&SyncInternalsMessageHandler::HandleRequestListOfTypes,
58                  base::Unretained(this)));
59
60   RegisterJsControllerCallback("getNotificationState");
61   RegisterJsControllerCallback("getNotificationInfo");
62   RegisterJsControllerCallback("getAllNodes");
63   RegisterJsControllerCallback("getClientServerTraffic");
64 }
65
66 void SyncInternalsMessageHandler::HandleRequestUpdatedAboutInfo(
67     const base::ListValue* args) {
68   DCHECK(args->empty());
69   SendAboutInfo();
70 }
71
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())));
81   }
82   event_details.Set("types", type_list.release());
83   web_ui()->CallJavascriptFunction(
84       "chrome.sync.dispatchEvent",
85       base::StringValue("onReceivedListOfTypes"),
86       event_details);
87 }
88
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(),
95                                            args.Get().end());
96   web_ui()->CallJavascriptFunction(reply_handler, arg_list);
97 }
98
99 void SyncInternalsMessageHandler::OnStateChanged() {
100   SendAboutInfo();
101 }
102
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),
110                                    details.Get());
111 }
112
113 void SyncInternalsMessageHandler::RegisterJsControllerCallback(
114     const std::string& name) {
115   web_ui()->RegisterMessageCallback(
116       name,
117       base::Bind(&SyncInternalsMessageHandler::ForwardToJsController,
118                  base::Unretained(this),
119                  name));
120 }
121
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"),
128       *value);
129 }
130
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(
138         name, js_arg_list,
139         MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
140   } else {
141     DLOG(WARNING) << "No sync service; dropping message " << name;
142   }
143 }
144
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());
151 }
152