Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(839)

Side by Side Diff: components/gcm_driver/gcm_internals_helper.cc

Issue 1437133002: Componentize the GCM info collection for gcm-internals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/gcm_driver/gcm_internals_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/gcm_internals_ui.h" 5 #include "components/gcm_driver/gcm_internals_helper.h"
6 6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/format_macros.h" 7 #include "base/format_macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
16 #include "base/values.h" 11 #include "base/values.h"
17 #include "chrome/browser/profiles/profile.h" 12 #include "components/gcm_driver/gcm_activity.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 "components/gcm_driver/gcm_internals_constants.h" 13 #include "components/gcm_driver/gcm_internals_constants.h"
23 #include "components/gcm_driver/gcm_profile_service.h" 14 #include "components/gcm_driver/gcm_profile_service.h"
24 #include "content/public/browser/web_ui.h" 15
25 #include "content/public/browser/web_ui_controller.h" 16 namespace gcm_driver {
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"
29 17
30 namespace { 18 namespace {
31 19
32 void SetCheckinInfo( 20 void SetCheckinInfo(const std::vector<gcm::CheckinActivity>& checkins,
33 const std::vector<gcm::CheckinActivity>& checkins, 21 base::ListValue* checkin_info) {
34 base::ListValue* checkin_info) { 22 for (const gcm::CheckinActivity& checkin : checkins) {
35 std::vector<gcm::CheckinActivity>::const_iterator it = checkins.begin(); 23 base::ListValue* row = new base::ListValue();
36 for (; it < checkins.end(); ++it) {
37 base::ListValue* row = new base::ListValue();
38 checkin_info->Append(row); 24 checkin_info->Append(row);
39 25
40 row->AppendDouble(it->time.ToJsTime()); 26 row->AppendDouble(checkin.time.ToJsTime());
41 row->AppendString(it->event); 27 row->AppendString(checkin.event);
42 row->AppendString(it->details); 28 row->AppendString(checkin.details);
43 } 29 }
44 } 30 }
45 31
46 void SetConnectionInfo( 32 void SetConnectionInfo(const std::vector<gcm::ConnectionActivity>& connections,
47 const std::vector<gcm::ConnectionActivity>& connections, 33 base::ListValue* connection_info) {
48 base::ListValue* connection_info) { 34 for (const gcm::ConnectionActivity& connection : connections) {
49 std::vector<gcm::ConnectionActivity>::const_iterator it = connections.begin();
50 for (; it < connections.end(); ++it) {
51 base::ListValue* row = new base::ListValue(); 35 base::ListValue* row = new base::ListValue();
52 connection_info->Append(row); 36 connection_info->Append(row);
53 37
54 row->AppendDouble(it->time.ToJsTime()); 38 row->AppendDouble(connection.time.ToJsTime());
55 row->AppendString(it->event); 39 row->AppendString(connection.event);
56 row->AppendString(it->details); 40 row->AppendString(connection.details);
57 } 41 }
58 } 42 }
59 43
60 void SetRegistrationInfo( 44 void SetRegistrationInfo(
61 const std::vector<gcm::RegistrationActivity>& registrations, 45 const std::vector<gcm::RegistrationActivity>& registrations,
62 base::ListValue* registration_info) { 46 base::ListValue* registration_info) {
63 std::vector<gcm::RegistrationActivity>::const_iterator it = 47 for (const gcm::RegistrationActivity& registration : registrations) {
64 registrations.begin();
65 for (; it < registrations.end(); ++it) {
66 base::ListValue* row = new base::ListValue(); 48 base::ListValue* row = new base::ListValue();
67 registration_info->Append(row); 49 registration_info->Append(row);
68 50
69 row->AppendDouble(it->time.ToJsTime()); 51 row->AppendDouble(registration.time.ToJsTime());
70 row->AppendString(it->app_id); 52 row->AppendString(registration.app_id);
71 row->AppendString(it->source); 53 row->AppendString(registration.source);
72 row->AppendString(it->event); 54 row->AppendString(registration.event);
73 row->AppendString(it->details); 55 row->AppendString(registration.details);
74 } 56 }
75 } 57 }
76 58
77 void SetReceivingInfo( 59 void SetReceivingInfo(const std::vector<gcm::ReceivingActivity>& receives,
78 const std::vector<gcm::ReceivingActivity>& receives, 60 base::ListValue* receive_info) {
79 base::ListValue* receive_info) { 61 for (const gcm::ReceivingActivity& receive : receives) {
80 std::vector<gcm::ReceivingActivity>::const_iterator it = receives.begin();
81 for (; it < receives.end(); ++it) {
82 base::ListValue* row = new base::ListValue(); 62 base::ListValue* row = new base::ListValue();
83 receive_info->Append(row); 63 receive_info->Append(row);
84 64
85 row->AppendDouble(it->time.ToJsTime()); 65 row->AppendDouble(receive.time.ToJsTime());
86 row->AppendString(it->app_id); 66 row->AppendString(receive.app_id);
87 row->AppendString(it->from); 67 row->AppendString(receive.from);
88 row->AppendString(base::IntToString(it->message_byte_size)); 68 row->AppendString(base::IntToString(receive.message_byte_size));
89 row->AppendString(it->event); 69 row->AppendString(receive.event);
90 row->AppendString(it->details); 70 row->AppendString(receive.details);
91 } 71 }
92 } 72 }
93 73
94 void SetSendingInfo( 74 void SetSendingInfo(const std::vector<gcm::SendingActivity>& sends,
95 const std::vector<gcm::SendingActivity>& sends, 75 base::ListValue* send_info) {
96 base::ListValue* send_info) { 76 for (const gcm::SendingActivity& send : sends) {
97 std::vector<gcm::SendingActivity>::const_iterator it = sends.begin();
98 for (; it < sends.end(); ++it) {
99 base::ListValue* row = new base::ListValue(); 77 base::ListValue* row = new base::ListValue();
100 send_info->Append(row); 78 send_info->Append(row);
101 79
102 row->AppendDouble(it->time.ToJsTime()); 80 row->AppendDouble(send.time.ToJsTime());
103 row->AppendString(it->app_id); 81 row->AppendString(send.app_id);
104 row->AppendString(it->receiver_id); 82 row->AppendString(send.receiver_id);
105 row->AppendString(it->message_id); 83 row->AppendString(send.message_id);
106 row->AppendString(it->event); 84 row->AppendString(send.event);
107 row->AppendString(it->details); 85 row->AppendString(send.details);
108 } 86 }
109 } 87 }
110 88
111 // Class acting as a controller of the chrome://gcm-internals WebUI. 89 } // namespace
112 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler {
113 public:
114 GcmInternalsUIMessageHandler();
115 ~GcmInternalsUIMessageHandler() override;
116 90
117 // WebUIMessageHandler implementation. 91 void SetGCMInternalsInfo(const gcm::GCMClient::GCMStatistics* stats,
118 void RegisterMessages() override; 92 gcm::GCMProfileService* profile_service,
93 PrefService* prefs,
94 base::DictionaryValue* results) {
95 base::DictionaryValue* device_info = new base::DictionaryValue();
96 results->Set(kDeviceInfo, device_info);
119 97
120 private: 98 device_info->SetBoolean(kProfileServiceCreated, profile_service != NULL);
121 // Return all of the GCM related infos to the gcm-internals page by calling 99 device_info->SetBoolean(kGcmEnabled,
122 // Javascript callback function 100 gcm::GCMProfileService::IsGCMEnabled(prefs));
123 // |gcm-internals.returnInfo()|.
124 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service,
125 const gcm::GCMClient::GCMStatistics* stats) const;
126
127 // Request all of the GCM related infos through gcm profile service.
128 void RequestAllInfo(const base::ListValue* args);
129
130 // Enables/disables GCM activity recording through gcm profile service.
131 void SetRecording(const base::ListValue* args);
132
133 // Callback function of the request for all gcm related infos.
134 void RequestGCMStatisticsFinished(
135 const gcm::GCMClient::GCMStatistics& args) const;
136
137 // Factory for creating references in callbacks.
138 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_;
139
140 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler);
141 };
142
143 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler()
144 : weak_ptr_factory_(this) {}
145
146 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {}
147
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);
155
156 device_info->SetBoolean(gcm_driver::kProfileServiceCreated,
157 profile_service != NULL);
158 device_info->SetBoolean(
159 gcm_driver::kGcmEnabled,
160 gcm::GCMProfileService::IsGCMEnabled(profile->GetPrefs()));
161 if (stats) { 101 if (stats) {
162 results.SetBoolean(gcm_driver::kIsRecording, stats->is_recording); 102 results->SetBoolean(kIsRecording, stats->is_recording);
163 device_info->SetBoolean(gcm_driver::kGcmClientCreated, 103 device_info->SetBoolean(kGcmClientCreated, stats->gcm_client_created);
164 stats->gcm_client_created); 104 device_info->SetString(kGcmClientState, stats->gcm_client_state);
165 device_info->SetString(gcm_driver::kGcmClientState, 105 device_info->SetBoolean(kConnectionClientCreated,
166 stats->gcm_client_state);
167 device_info->SetBoolean(gcm_driver::kConnectionClientCreated,
168 stats->connection_client_created); 106 stats->connection_client_created);
169 device_info->SetString(gcm_driver::kRegisteredAppIds, 107 device_info->SetString(kRegisteredAppIds,
170 base::JoinString(stats->registered_app_ids, ",")); 108 base::JoinString(stats->registered_app_ids, ","));
171 if (stats->connection_client_created) 109 if (stats->connection_client_created)
172 device_info->SetString(gcm_driver::kConnectionState, 110 device_info->SetString(kConnectionState, stats->connection_state);
173 stats->connection_state);
174 if (stats->android_id > 0) { 111 if (stats->android_id > 0) {
175 device_info->SetString( 112 device_info->SetString(
176 gcm_driver::kAndroidId, 113 kAndroidId, base::StringPrintf("0x%" PRIx64, stats->android_id));
177 base::StringPrintf("0x%" PRIx64, stats->android_id));
178 } 114 }
179 device_info->SetInteger(gcm_driver::kSendQueueSize, stats->send_queue_size); 115 device_info->SetInteger(kSendQueueSize, stats->send_queue_size);
180 device_info->SetInteger(gcm_driver::kResendQueueSize, 116 device_info->SetInteger(kResendQueueSize, stats->resend_queue_size);
181 stats->resend_queue_size);
182 117
183 if (stats->recorded_activities.checkin_activities.size() > 0) { 118 if (stats->recorded_activities.checkin_activities.size() > 0) {
184 base::ListValue* checkin_info = new base::ListValue(); 119 base::ListValue* checkin_info = new base::ListValue();
185 results.Set(gcm_driver::kCheckinInfo, checkin_info); 120 results->Set(kCheckinInfo, checkin_info);
186 SetCheckinInfo(stats->recorded_activities.checkin_activities, 121 SetCheckinInfo(stats->recorded_activities.checkin_activities,
187 checkin_info); 122 checkin_info);
188 } 123 }
189 if (stats->recorded_activities.connection_activities.size() > 0) { 124 if (stats->recorded_activities.connection_activities.size() > 0) {
190 base::ListValue* connection_info = new base::ListValue(); 125 base::ListValue* connection_info = new base::ListValue();
191 results.Set(gcm_driver::kConnectionInfo, connection_info); 126 results->Set(kConnectionInfo, connection_info);
192 SetConnectionInfo(stats->recorded_activities.connection_activities, 127 SetConnectionInfo(stats->recorded_activities.connection_activities,
193 connection_info); 128 connection_info);
194 } 129 }
195 if (stats->recorded_activities.registration_activities.size() > 0) { 130 if (stats->recorded_activities.registration_activities.size() > 0) {
196 base::ListValue* registration_info = new base::ListValue(); 131 base::ListValue* registration_info = new base::ListValue();
197 results.Set(gcm_driver::kRegistrationInfo, registration_info); 132 results->Set(kRegistrationInfo, registration_info);
198 SetRegistrationInfo(stats->recorded_activities.registration_activities, 133 SetRegistrationInfo(stats->recorded_activities.registration_activities,
199 registration_info); 134 registration_info);
200 } 135 }
201 if (stats->recorded_activities.receiving_activities.size() > 0) { 136 if (stats->recorded_activities.receiving_activities.size() > 0) {
202 base::ListValue* receive_info = new base::ListValue(); 137 base::ListValue* receive_info = new base::ListValue();
203 results.Set(gcm_driver::kReceiveInfo, receive_info); 138 results->Set(kReceiveInfo, receive_info);
204 SetReceivingInfo(stats->recorded_activities.receiving_activities, 139 SetReceivingInfo(stats->recorded_activities.receiving_activities,
205 receive_info); 140 receive_info);
206 } 141 }
207 if (stats->recorded_activities.sending_activities.size() > 0) { 142 if (stats->recorded_activities.sending_activities.size() > 0) {
208 base::ListValue* send_info = new base::ListValue(); 143 base::ListValue* send_info = new base::ListValue();
209 results.Set(gcm_driver::kSendInfo, send_info); 144 results->Set(kSendInfo, send_info);
210 SetSendingInfo(stats->recorded_activities.sending_activities, send_info); 145 SetSendingInfo(stats->recorded_activities.sending_activities, send_info);
211 } 146 }
212 } 147 }
213 web_ui()->CallJavascriptFunction(gcm_driver::kSetGcmInternalsInfo, results);
214 } 148 }
215 149
216 void GcmInternalsUIMessageHandler::RequestAllInfo( 150 } // namespace gcm_driver
217 const base::ListValue* args) {
218 if (args->GetSize() != 1) {
219 NOTREACHED();
220 return;
221 }
222 bool clear_logs = false;
223 if (!args->GetBoolean(0, &clear_logs)) {
224 NOTREACHED();
225 return;
226 }
227
228 Profile* profile = Profile::FromWebUI(web_ui());
229 gcm::GCMProfileService* profile_service =
230 gcm::GCMProfileServiceFactory::GetForProfile(profile);
231
232 if (!profile_service || !profile_service->driver()) {
233 ReturnResults(profile, NULL, NULL);
234 } else {
235 profile_service->driver()->GetGCMStatistics(
236 base::Bind(&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
237 weak_ptr_factory_.GetWeakPtr()),
238 clear_logs);
239 }
240 }
241
242 void GcmInternalsUIMessageHandler::SetRecording(const base::ListValue* args) {
243 if (args->GetSize() != 1) {
244 NOTREACHED();
245 return;
246 }
247 bool recording = false;
248 if (!args->GetBoolean(0, &recording)) {
249 NOTREACHED();
250 return;
251 }
252
253 Profile* profile = Profile::FromWebUI(web_ui());
254 gcm::GCMProfileService* profile_service =
255 gcm::GCMProfileServiceFactory::GetForProfile(profile);
256
257 if (!profile_service) {
258 ReturnResults(profile, NULL, NULL);
259 return;
260 }
261 // Get fresh stats after changing recording setting.
262 profile_service->driver()->SetGCMRecording(
263 base::Bind(
264 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
265 weak_ptr_factory_.GetWeakPtr()),
266 recording);
267 }
268
269 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished(
270 const gcm::GCMClient::GCMStatistics& stats) const {
271 Profile* profile = Profile::FromWebUI(web_ui());
272 DCHECK(profile);
273 gcm::GCMProfileService* profile_service =
274 gcm::GCMProfileServiceFactory::GetForProfile(profile);
275 DCHECK(profile_service);
276 ReturnResults(profile, profile_service, &stats);
277 }
278
279 void GcmInternalsUIMessageHandler::RegisterMessages() {
280 web_ui()->RegisterMessageCallback(
281 gcm_driver::kGetGcmInternalsInfo,
282 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo,
283 weak_ptr_factory_.GetWeakPtr()));
284 web_ui()->RegisterMessageCallback(
285 gcm_driver::kSetGcmInternalsRecording,
286 base::Bind(&GcmInternalsUIMessageHandler::SetRecording,
287 weak_ptr_factory_.GetWeakPtr()));
288 }
289
290 } // namespace
291
292 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui)
293 : content::WebUIController(web_ui) {
294 // Set up the chrome://gcm-internals source.
295 content::WebUIDataSource* html_source =
296 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost);
297
298 html_source->SetJsonPath("strings.js");
299
300 // Add required resources.
301 html_source->AddResourcePath(gcm_driver::kGcmInternalsCSS,
302 IDR_GCM_DRIVER_GCM_INTERNALS_CSS);
303 html_source->AddResourcePath(gcm_driver::kGcmInternalsJS,
304 IDR_GCM_DRIVER_GCM_INTERNALS_JS);
305 html_source->SetDefaultResource(IDR_GCM_DRIVER_GCM_INTERNALS_HTML);
306
307 Profile* profile = Profile::FromWebUI(web_ui);
308 content::WebUIDataSource::Add(profile, html_source);
309
310 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler());
311 }
312
313 GCMInternalsUI::~GCMInternalsUI() {}
OLDNEW
« no previous file with comments | « components/gcm_driver/gcm_internals_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698