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"
10 #include "base/bind_helpers.h"
11 #include "base/format_macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/values.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service.h"
18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "components/gcm_driver/gcm_client.h"
21 #include "components/gcm_driver/gcm_driver.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_controller.h"
24 #include "content/public/browser/web_ui_data_source.h"
25 #include "content/public/browser/web_ui_message_handler.h"
26 #include "grit/browser_resources.h"
31 const std::vector
<gcm::CheckinActivity
>& checkins
,
32 base::ListValue
* checkin_info
) {
33 std::vector
<gcm::CheckinActivity
>::const_iterator it
= checkins
.begin();
34 for (; it
< checkins
.end(); ++it
) {
35 base::ListValue
* row
= new base::ListValue();
36 checkin_info
->Append(row
);
38 row
->AppendDouble(it
->time
.ToJsTime());
39 row
->AppendString(it
->event
);
40 row
->AppendString(it
->details
);
44 void SetConnectionInfo(
45 const std::vector
<gcm::ConnectionActivity
>& connections
,
46 base::ListValue
* connection_info
) {
47 std::vector
<gcm::ConnectionActivity
>::const_iterator it
= connections
.begin();
48 for (; it
< connections
.end(); ++it
) {
49 base::ListValue
* row
= new base::ListValue();
50 connection_info
->Append(row
);
52 row
->AppendDouble(it
->time
.ToJsTime());
53 row
->AppendString(it
->event
);
54 row
->AppendString(it
->details
);
58 void SetRegistrationInfo(
59 const std::vector
<gcm::RegistrationActivity
>& registrations
,
60 base::ListValue
* registration_info
) {
61 std::vector
<gcm::RegistrationActivity
>::const_iterator it
=
62 registrations
.begin();
63 for (; it
< registrations
.end(); ++it
) {
64 base::ListValue
* row
= new base::ListValue();
65 registration_info
->Append(row
);
67 row
->AppendDouble(it
->time
.ToJsTime());
68 row
->AppendString(it
->app_id
);
69 row
->AppendString(it
->sender_ids
);
70 row
->AppendString(it
->event
);
71 row
->AppendString(it
->details
);
75 void SetReceivingInfo(
76 const std::vector
<gcm::ReceivingActivity
>& receives
,
77 base::ListValue
* receive_info
) {
78 std::vector
<gcm::ReceivingActivity
>::const_iterator it
= receives
.begin();
79 for (; it
< receives
.end(); ++it
) {
80 base::ListValue
* row
= new base::ListValue();
81 receive_info
->Append(row
);
83 row
->AppendDouble(it
->time
.ToJsTime());
84 row
->AppendString(it
->app_id
);
85 row
->AppendString(it
->from
);
86 row
->AppendString(base::StringPrintf("%d", it
->message_byte_size
));
87 row
->AppendString(it
->event
);
88 row
->AppendString(it
->details
);
93 const std::vector
<gcm::SendingActivity
>& sends
,
94 base::ListValue
* send_info
) {
95 std::vector
<gcm::SendingActivity
>::const_iterator it
= sends
.begin();
96 for (; it
< sends
.end(); ++it
) {
97 base::ListValue
* row
= new base::ListValue();
98 send_info
->Append(row
);
100 row
->AppendDouble(it
->time
.ToJsTime());
101 row
->AppendString(it
->app_id
);
102 row
->AppendString(it
->receiver_id
);
103 row
->AppendString(it
->message_id
);
104 row
->AppendString(it
->event
);
105 row
->AppendString(it
->details
);
109 // Class acting as a controller of the chrome://gcm-internals WebUI.
110 class GcmInternalsUIMessageHandler
: public content::WebUIMessageHandler
{
112 GcmInternalsUIMessageHandler();
113 ~GcmInternalsUIMessageHandler() override
;
115 // WebUIMessageHandler implementation.
116 void RegisterMessages() override
;
119 // Return all of the GCM related infos to the gcm-internals page by calling
120 // Javascript callback function
121 // |gcm-internals.returnInfo()|.
122 void ReturnResults(Profile
* profile
, gcm::GCMProfileService
* profile_service
,
123 const gcm::GCMClient::GCMStatistics
* stats
) const;
125 // Request all of the GCM related infos through gcm profile service.
126 void RequestAllInfo(const base::ListValue
* args
);
128 // Enables/disables GCM activity recording through gcm profile service.
129 void SetRecording(const base::ListValue
* args
);
131 // Callback function of the request for all gcm related infos.
132 void RequestGCMStatisticsFinished(
133 const gcm::GCMClient::GCMStatistics
& args
) const;
135 // Factory for creating references in callbacks.
136 base::WeakPtrFactory
<GcmInternalsUIMessageHandler
> weak_ptr_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler
);
141 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler()
142 : weak_ptr_factory_(this) {}
144 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {}
146 void GcmInternalsUIMessageHandler::ReturnResults(
148 gcm::GCMProfileService
* profile_service
,
149 const gcm::GCMClient::GCMStatistics
* stats
) const {
150 base::DictionaryValue results
;
151 base::DictionaryValue
* device_info
= new base::DictionaryValue();
152 results
.Set("deviceInfo", device_info
);
154 device_info
->SetBoolean("profileServiceCreated", profile_service
!= NULL
);
155 device_info
->SetBoolean("gcmEnabled",
156 gcm::GCMProfileService::IsGCMEnabled(profile
));
158 results
.SetBoolean("isRecording", stats
->is_recording
);
159 device_info
->SetBoolean("gcmClientCreated", stats
->gcm_client_created
);
160 device_info
->SetString("gcmClientState", stats
->gcm_client_state
);
161 device_info
->SetBoolean("connectionClientCreated",
162 stats
->connection_client_created
);
163 device_info
->SetString("registeredAppIds",
164 JoinString(stats
->registered_app_ids
, ","));
165 if (stats
->connection_client_created
)
166 device_info
->SetString("connectionState", stats
->connection_state
);
167 if (stats
->android_id
> 0) {
168 device_info
->SetString("androidId",
169 base::StringPrintf("0x%" PRIx64
, stats
->android_id
));
171 device_info
->SetInteger("sendQueueSize", stats
->send_queue_size
);
172 device_info
->SetInteger("resendQueueSize", stats
->resend_queue_size
);
174 if (stats
->recorded_activities
.checkin_activities
.size() > 0) {
175 base::ListValue
* checkin_info
= new base::ListValue();
176 results
.Set("checkinInfo", checkin_info
);
177 SetCheckinInfo(stats
->recorded_activities
.checkin_activities
,
180 if (stats
->recorded_activities
.connection_activities
.size() > 0) {
181 base::ListValue
* connection_info
= new base::ListValue();
182 results
.Set("connectionInfo", connection_info
);
183 SetConnectionInfo(stats
->recorded_activities
.connection_activities
,
186 if (stats
->recorded_activities
.registration_activities
.size() > 0) {
187 base::ListValue
* registration_info
= new base::ListValue();
188 results
.Set("registrationInfo", registration_info
);
189 SetRegistrationInfo(stats
->recorded_activities
.registration_activities
,
192 if (stats
->recorded_activities
.receiving_activities
.size() > 0) {
193 base::ListValue
* receive_info
= new base::ListValue();
194 results
.Set("receiveInfo", receive_info
);
195 SetReceivingInfo(stats
->recorded_activities
.receiving_activities
,
198 if (stats
->recorded_activities
.sending_activities
.size() > 0) {
199 base::ListValue
* send_info
= new base::ListValue();
200 results
.Set("sendInfo", send_info
);
201 SetSendingInfo(stats
->recorded_activities
.sending_activities
, send_info
);
204 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo",
208 void GcmInternalsUIMessageHandler::RequestAllInfo(
209 const base::ListValue
* args
) {
210 if (args
->GetSize() != 1) {
214 bool clear_logs
= false;
215 if (!args
->GetBoolean(0, &clear_logs
)) {
220 Profile
* profile
= Profile::FromWebUI(web_ui());
221 gcm::GCMProfileService
* profile_service
=
222 gcm::GCMProfileServiceFactory::GetForProfile(profile
);
224 if (!profile_service
|| !profile_service
->driver()) {
225 ReturnResults(profile
, NULL
, NULL
);
227 profile_service
->driver()->GetGCMStatistics(
228 base::Bind(&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished
,
229 weak_ptr_factory_
.GetWeakPtr()),
234 void GcmInternalsUIMessageHandler::SetRecording(const base::ListValue
* args
) {
235 if (args
->GetSize() != 1) {
239 bool recording
= false;
240 if (!args
->GetBoolean(0, &recording
)) {
245 Profile
* profile
= Profile::FromWebUI(web_ui());
246 gcm::GCMProfileService
* profile_service
=
247 gcm::GCMProfileServiceFactory::GetForProfile(profile
);
249 if (!profile_service
) {
250 ReturnResults(profile
, NULL
, NULL
);
253 // Get fresh stats after changing recording setting.
254 profile_service
->driver()->SetGCMRecording(
256 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished
,
257 weak_ptr_factory_
.GetWeakPtr()),
261 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished(
262 const gcm::GCMClient::GCMStatistics
& stats
) const {
263 Profile
* profile
= Profile::FromWebUI(web_ui());
265 gcm::GCMProfileService
* profile_service
=
266 gcm::GCMProfileServiceFactory::GetForProfile(profile
);
267 DCHECK(profile_service
);
268 ReturnResults(profile
, profile_service
, &stats
);
271 void GcmInternalsUIMessageHandler::RegisterMessages() {
272 web_ui()->RegisterMessageCallback(
273 "getGcmInternalsInfo",
274 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo
,
275 weak_ptr_factory_
.GetWeakPtr()));
276 web_ui()->RegisterMessageCallback(
277 "setGcmInternalsRecording",
278 base::Bind(&GcmInternalsUIMessageHandler::SetRecording
,
279 weak_ptr_factory_
.GetWeakPtr()));
284 GCMInternalsUI::GCMInternalsUI(content::WebUI
* web_ui
)
285 : content::WebUIController(web_ui
) {
286 // Set up the chrome://gcm-internals source.
287 content::WebUIDataSource
* html_source
=
288 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost
);
290 html_source
->SetJsonPath("strings.js");
292 // Add required resources.
293 html_source
->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS
);
294 html_source
->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS
);
295 html_source
->SetDefaultResource(IDR_GCM_INTERNALS_HTML
);
297 Profile
* profile
= Profile::FromWebUI(web_ui
);
298 content::WebUIDataSource::Add(profile
, html_source
);
300 web_ui
->AddMessageHandler(new GcmInternalsUIMessageHandler());
303 GCMInternalsUI::~GCMInternalsUI() {}