Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / webui / gcm_internals_ui.cc
blob490d98604d920de61554395600e24267cc005a0a
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/gcm_internals_ui.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/format_macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/services/gcm/gcm_profile_service.h"
19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
20 #include "chrome/common/url_constants.h"
21 #include "components/gcm_driver/gcm_client.h"
22 #include "components/gcm_driver/gcm_driver.h"
23 #include "components/gcm_driver/gcm_internals_constants.h"
24 #include "content/public/browser/web_ui.h"
25 #include "content/public/browser/web_ui_controller.h"
26 #include "content/public/browser/web_ui_data_source.h"
27 #include "content/public/browser/web_ui_message_handler.h"
28 #include "grit/components_resources.h"
30 namespace {
32 void SetCheckinInfo(
33 const std::vector<gcm::CheckinActivity>& checkins,
34 base::ListValue* checkin_info) {
35 std::vector<gcm::CheckinActivity>::const_iterator it = checkins.begin();
36 for (; it < checkins.end(); ++it) {
37 base::ListValue* row = new base::ListValue();
38 checkin_info->Append(row);
40 row->AppendDouble(it->time.ToJsTime());
41 row->AppendString(it->event);
42 row->AppendString(it->details);
46 void SetConnectionInfo(
47 const std::vector<gcm::ConnectionActivity>& connections,
48 base::ListValue* connection_info) {
49 std::vector<gcm::ConnectionActivity>::const_iterator it = connections.begin();
50 for (; it < connections.end(); ++it) {
51 base::ListValue* row = new base::ListValue();
52 connection_info->Append(row);
54 row->AppendDouble(it->time.ToJsTime());
55 row->AppendString(it->event);
56 row->AppendString(it->details);
60 void SetRegistrationInfo(
61 const std::vector<gcm::RegistrationActivity>& registrations,
62 base::ListValue* registration_info) {
63 std::vector<gcm::RegistrationActivity>::const_iterator it =
64 registrations.begin();
65 for (; it < registrations.end(); ++it) {
66 base::ListValue* row = new base::ListValue();
67 registration_info->Append(row);
69 row->AppendDouble(it->time.ToJsTime());
70 row->AppendString(it->app_id);
71 row->AppendString(it->source);
72 row->AppendString(it->event);
73 row->AppendString(it->details);
77 void SetReceivingInfo(
78 const std::vector<gcm::ReceivingActivity>& receives,
79 base::ListValue* receive_info) {
80 std::vector<gcm::ReceivingActivity>::const_iterator it = receives.begin();
81 for (; it < receives.end(); ++it) {
82 base::ListValue* row = new base::ListValue();
83 receive_info->Append(row);
85 row->AppendDouble(it->time.ToJsTime());
86 row->AppendString(it->app_id);
87 row->AppendString(it->from);
88 row->AppendString(base::IntToString(it->message_byte_size));
89 row->AppendString(it->event);
90 row->AppendString(it->details);
94 void SetSendingInfo(
95 const std::vector<gcm::SendingActivity>& sends,
96 base::ListValue* send_info) {
97 std::vector<gcm::SendingActivity>::const_iterator it = sends.begin();
98 for (; it < sends.end(); ++it) {
99 base::ListValue* row = new base::ListValue();
100 send_info->Append(row);
102 row->AppendDouble(it->time.ToJsTime());
103 row->AppendString(it->app_id);
104 row->AppendString(it->receiver_id);
105 row->AppendString(it->message_id);
106 row->AppendString(it->event);
107 row->AppendString(it->details);
111 // Class acting as a controller of the chrome://gcm-internals WebUI.
112 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler {
113 public:
114 GcmInternalsUIMessageHandler();
115 ~GcmInternalsUIMessageHandler() override;
117 // WebUIMessageHandler implementation.
118 void RegisterMessages() override;
120 private:
121 // Return all of the GCM related infos to the gcm-internals page by calling
122 // Javascript callback function
123 // |gcm-internals.returnInfo()|.
124 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service,
125 const gcm::GCMClient::GCMStatistics* stats) const;
127 // Request all of the GCM related infos through gcm profile service.
128 void RequestAllInfo(const base::ListValue* args);
130 // Enables/disables GCM activity recording through gcm profile service.
131 void SetRecording(const base::ListValue* args);
133 // Callback function of the request for all gcm related infos.
134 void RequestGCMStatisticsFinished(
135 const gcm::GCMClient::GCMStatistics& args) const;
137 // Factory for creating references in callbacks.
138 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_;
140 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler);
143 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler()
144 : weak_ptr_factory_(this) {}
146 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {}
148 void GcmInternalsUIMessageHandler::ReturnResults(
149 Profile* profile,
150 gcm::GCMProfileService* profile_service,
151 const gcm::GCMClient::GCMStatistics* stats) const {
152 base::DictionaryValue results;
153 base::DictionaryValue* device_info = new base::DictionaryValue();
154 results.Set(gcm_driver::kDeviceInfo, device_info);
156 device_info->SetBoolean(gcm_driver::kProfileServiceCreated,
157 profile_service != NULL);
158 device_info->SetBoolean(gcm_driver::kGcmEnabled,
159 gcm::GCMProfileService::IsGCMEnabled(profile));
160 if (stats) {
161 results.SetBoolean(gcm_driver::kIsRecording, stats->is_recording);
162 device_info->SetBoolean(gcm_driver::kGcmClientCreated,
163 stats->gcm_client_created);
164 device_info->SetString(gcm_driver::kGcmClientState,
165 stats->gcm_client_state);
166 device_info->SetBoolean(gcm_driver::kConnectionClientCreated,
167 stats->connection_client_created);
168 device_info->SetString(gcm_driver::kRegisteredAppIds,
169 base::JoinString(stats->registered_app_ids, ","));
170 if (stats->connection_client_created)
171 device_info->SetString(gcm_driver::kConnectionState,
172 stats->connection_state);
173 if (stats->android_id > 0) {
174 device_info->SetString(
175 gcm_driver::kAndroidId,
176 base::StringPrintf("0x%" PRIx64, stats->android_id));
178 device_info->SetInteger(gcm_driver::kSendQueueSize, stats->send_queue_size);
179 device_info->SetInteger(gcm_driver::kResendQueueSize,
180 stats->resend_queue_size);
182 if (stats->recorded_activities.checkin_activities.size() > 0) {
183 base::ListValue* checkin_info = new base::ListValue();
184 results.Set(gcm_driver::kCheckinInfo, checkin_info);
185 SetCheckinInfo(stats->recorded_activities.checkin_activities,
186 checkin_info);
188 if (stats->recorded_activities.connection_activities.size() > 0) {
189 base::ListValue* connection_info = new base::ListValue();
190 results.Set(gcm_driver::kConnectionInfo, connection_info);
191 SetConnectionInfo(stats->recorded_activities.connection_activities,
192 connection_info);
194 if (stats->recorded_activities.registration_activities.size() > 0) {
195 base::ListValue* registration_info = new base::ListValue();
196 results.Set(gcm_driver::kRegistrationInfo, registration_info);
197 SetRegistrationInfo(stats->recorded_activities.registration_activities,
198 registration_info);
200 if (stats->recorded_activities.receiving_activities.size() > 0) {
201 base::ListValue* receive_info = new base::ListValue();
202 results.Set(gcm_driver::kReceiveInfo, receive_info);
203 SetReceivingInfo(stats->recorded_activities.receiving_activities,
204 receive_info);
206 if (stats->recorded_activities.sending_activities.size() > 0) {
207 base::ListValue* send_info = new base::ListValue();
208 results.Set(gcm_driver::kSendInfo, send_info);
209 SetSendingInfo(stats->recorded_activities.sending_activities, send_info);
212 web_ui()->CallJavascriptFunction(gcm_driver::kSetGcmInternalsInfo, results);
215 void GcmInternalsUIMessageHandler::RequestAllInfo(
216 const base::ListValue* args) {
217 if (args->GetSize() != 1) {
218 NOTREACHED();
219 return;
221 bool clear_logs = false;
222 if (!args->GetBoolean(0, &clear_logs)) {
223 NOTREACHED();
224 return;
227 Profile* profile = Profile::FromWebUI(web_ui());
228 gcm::GCMProfileService* profile_service =
229 gcm::GCMProfileServiceFactory::GetForProfile(profile);
231 if (!profile_service || !profile_service->driver()) {
232 ReturnResults(profile, NULL, NULL);
233 } else {
234 profile_service->driver()->GetGCMStatistics(
235 base::Bind(&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
236 weak_ptr_factory_.GetWeakPtr()),
237 clear_logs);
241 void GcmInternalsUIMessageHandler::SetRecording(const base::ListValue* args) {
242 if (args->GetSize() != 1) {
243 NOTREACHED();
244 return;
246 bool recording = false;
247 if (!args->GetBoolean(0, &recording)) {
248 NOTREACHED();
249 return;
252 Profile* profile = Profile::FromWebUI(web_ui());
253 gcm::GCMProfileService* profile_service =
254 gcm::GCMProfileServiceFactory::GetForProfile(profile);
256 if (!profile_service) {
257 ReturnResults(profile, NULL, NULL);
258 return;
260 // Get fresh stats after changing recording setting.
261 profile_service->driver()->SetGCMRecording(
262 base::Bind(
263 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
264 weak_ptr_factory_.GetWeakPtr()),
265 recording);
268 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished(
269 const gcm::GCMClient::GCMStatistics& stats) const {
270 Profile* profile = Profile::FromWebUI(web_ui());
271 DCHECK(profile);
272 gcm::GCMProfileService* profile_service =
273 gcm::GCMProfileServiceFactory::GetForProfile(profile);
274 DCHECK(profile_service);
275 ReturnResults(profile, profile_service, &stats);
278 void GcmInternalsUIMessageHandler::RegisterMessages() {
279 web_ui()->RegisterMessageCallback(
280 gcm_driver::kGetGcmInternalsInfo,
281 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo,
282 weak_ptr_factory_.GetWeakPtr()));
283 web_ui()->RegisterMessageCallback(
284 gcm_driver::kSetGcmInternalsRecording,
285 base::Bind(&GcmInternalsUIMessageHandler::SetRecording,
286 weak_ptr_factory_.GetWeakPtr()));
289 } // namespace
291 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui)
292 : content::WebUIController(web_ui) {
293 // Set up the chrome://gcm-internals source.
294 content::WebUIDataSource* html_source =
295 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost);
297 html_source->SetJsonPath("strings.js");
299 // Add required resources.
300 html_source->AddResourcePath(gcm_driver::kGcmInternalsCSS,
301 IDR_GCM_DRIVER_GCM_INTERNALS_CSS);
302 html_source->AddResourcePath(gcm_driver::kGcmInternalsJS,
303 IDR_GCM_DRIVER_GCM_INTERNALS_JS);
304 html_source->SetDefaultResource(IDR_GCM_DRIVER_GCM_INTERNALS_HTML);
306 Profile* profile = Profile::FromWebUI(web_ui);
307 content::WebUIDataSource::Add(profile, html_source);
309 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler());
312 GCMInternalsUI::~GCMInternalsUI() {}