OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/gcm_internals_ui.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
10 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
16 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
17 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
18 #include "content/public/browser/web_ui_controller.h" | 20 #include "content/public/browser/web_ui_controller.h" |
19 #include "content/public/browser/web_ui_data_source.h" | 21 #include "content/public/browser/web_ui_data_source.h" |
20 #include "content/public/browser/web_ui_message_handler.h" | 22 #include "content/public/browser/web_ui_message_handler.h" |
21 #include "google_apis/gcm/gcm_client.h" | 23 #include "google_apis/gcm/gcm_client.h" |
22 #include "grit/browser_resources.h" | 24 #include "grit/browser_resources.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
28 void SetSendingInfo( | |
29 const std::vector<gcm::GCMStatsRecorder::SendingActivity>& sends, | |
30 base::ListValue* sendInfo) { | |
jianli
2014/03/21 18:25:27
nit: send_info
juyik
2014/03/26 04:06:03
Done.
| |
31 std::vector<gcm::GCMStatsRecorder::SendingActivity>::const_iterator it = | |
32 sends.begin(); | |
33 for (; it < sends.end(); ++it) { | |
34 base::ListValue* row = new base::ListValue(); | |
35 sendInfo->Append(row); | |
36 | |
37 row->AppendDouble(it->time.ToJsTime()); | |
38 row->AppendString(it->app_id); | |
39 row->AppendString(it->receiver_id); | |
40 row->AppendString(it->message_id); | |
41 row->AppendString(it->event); | |
42 row->AppendString(it->details); | |
43 } | |
44 } | |
45 | |
26 // Class acting as a controller of the chrome://gcm-internals WebUI. | 46 // Class acting as a controller of the chrome://gcm-internals WebUI. |
27 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { | 47 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { |
28 public: | 48 public: |
29 GcmInternalsUIMessageHandler(); | 49 GcmInternalsUIMessageHandler(); |
30 virtual ~GcmInternalsUIMessageHandler(); | 50 virtual ~GcmInternalsUIMessageHandler(); |
31 | 51 |
32 // WebUIMessageHandler implementation. | 52 // WebUIMessageHandler implementation. |
33 virtual void RegisterMessages() OVERRIDE; | 53 virtual void RegisterMessages() OVERRIDE; |
34 | 54 |
35 private: | 55 private: |
36 // Return all of the GCM related infos to the gcm-internals page by calling | 56 // Return all of the GCM related infos to the gcm-internals page by calling |
37 // Javascript callback function | 57 // Javascript callback function |
38 // |gcm-internals.returnInfo()|. | 58 // |gcm-internals.returnInfo()|. |
39 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, | 59 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, |
40 const gcm::GCMClient::GCMStatistics* stats) const; | 60 const gcm::GCMClient::GCMStatistics* stats) const; |
41 | 61 |
42 // Request all of the GCM related infos through gcm profile service. | 62 // Request all of the GCM related infos through gcm profile service. |
43 void RequestAllInfo(const base::ListValue* args); | 63 void RequestAllInfo(const base::ListValue* args); |
44 | 64 |
65 // Enables/disables GCM activity recording through gcm profile service. | |
66 void SetRecording(const base::ListValue* args); | |
67 | |
45 // Callback function of the request for all gcm related infos. | 68 // Callback function of the request for all gcm related infos. |
46 void RequestGCMStatisticsFinished( | 69 void RequestGCMStatisticsFinished( |
47 const gcm::GCMClient::GCMStatistics& args) const; | 70 const gcm::GCMClient::GCMStatistics& args) const; |
48 | 71 |
49 // Factory for creating references in callbacks. | 72 // Factory for creating references in callbacks. |
50 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; | 73 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; |
51 | 74 |
52 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); | 75 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); |
53 }; | 76 }; |
54 | 77 |
(...skipping 14 matching lines...) Expand all Loading... | |
69 device_info->SetString("gcmEnabledState", | 92 device_info->SetString("gcmEnabledState", |
70 gcm::GCMProfileService::GetGCMEnabledStateString( | 93 gcm::GCMProfileService::GetGCMEnabledStateString( |
71 gcm::GCMProfileService::GetGCMEnabledState(profile))); | 94 gcm::GCMProfileService::GetGCMEnabledState(profile))); |
72 if (profile_service) { | 95 if (profile_service) { |
73 device_info->SetString("signedInUserName", | 96 device_info->SetString("signedInUserName", |
74 profile_service->SignedInUserName()); | 97 profile_service->SignedInUserName()); |
75 device_info->SetBoolean("gcmClientReady", | 98 device_info->SetBoolean("gcmClientReady", |
76 profile_service->IsGCMClientReady()); | 99 profile_service->IsGCMClientReady()); |
77 } | 100 } |
78 if (stats) { | 101 if (stats) { |
102 results.SetBoolean("isRecording", stats->is_recording); | |
79 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); | 103 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); |
80 device_info->SetString("gcmClientState", stats->gcm_client_state); | 104 device_info->SetString("gcmClientState", stats->gcm_client_state); |
81 device_info->SetBoolean("connectionClientCreated", | 105 device_info->SetBoolean("connectionClientCreated", |
82 stats->connection_client_created); | 106 stats->connection_client_created); |
107 device_info->SetString("appIdsCached", stats->app_ids_cached); | |
83 if (stats->connection_client_created) | 108 if (stats->connection_client_created) |
84 device_info->SetString("connectionState", stats->connection_state); | 109 device_info->SetString("connectionState", stats->connection_state); |
85 if (stats->android_id > 0) { | 110 if (stats->android_id > 0) { |
86 device_info->SetString("androidId", | 111 device_info->SetString("androidId", |
87 base::StringPrintf("0x%" PRIx64, stats->android_id)); | 112 base::StringPrintf("0x%" PRIx64, stats->android_id)); |
88 } | 113 } |
114 if (stats->send_queue_size > 0) | |
115 device_info->SetInteger("sendQueueSize", stats->send_queue_size); | |
116 if (stats->unacked_queue_size > 0) | |
117 device_info->SetInteger("unackedQueueSize", stats->unacked_queue_size); | |
118 | |
119 if (stats->sending.size() > 0) { | |
120 base::ListValue* sendInfo = new base::ListValue(); | |
jianli
2014/03/21 18:25:27
nit: send_info
juyik
2014/03/26 04:06:03
Done.
| |
121 results.Set("sendInfo", sendInfo); | |
122 SetSendingInfo(stats->sending, sendInfo); | |
123 } | |
89 } | 124 } |
90 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo", | 125 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo", |
91 results); | 126 results); |
92 } | 127 } |
93 | 128 |
94 void GcmInternalsUIMessageHandler::RequestAllInfo( | 129 void GcmInternalsUIMessageHandler::RequestAllInfo( |
95 const base::ListValue* args) { | 130 const base::ListValue* args) { |
131 if (args->GetSize() != 1) { | |
132 NOTREACHED(); | |
133 return; | |
134 } | |
135 bool clear_logs = false; | |
136 if (!args->GetBoolean(0, &clear_logs)) { | |
137 NOTREACHED(); | |
138 return; | |
139 } | |
140 | |
96 Profile* profile = Profile::FromWebUI(web_ui()); | 141 Profile* profile = Profile::FromWebUI(web_ui()); |
97 gcm::GCMProfileService* profile_service = | 142 gcm::GCMProfileService* profile_service = |
98 gcm::GCMProfileServiceFactory::GetForProfile(profile); | 143 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
99 | 144 |
100 if (!profile_service) { | 145 if (!profile_service) { |
101 ReturnResults(profile, NULL, NULL); | 146 ReturnResults(profile, NULL, NULL); |
147 } else if (profile_service->SignedInUserName().empty()) { | |
148 ReturnResults(profile, profile_service, NULL); | |
102 } else { | 149 } else { |
103 profile_service->RequestGCMStatistics(base::Bind( | 150 profile_service->RequestGCMStatistics( |
104 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | 151 base::Bind(&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, |
105 weak_ptr_factory_.GetWeakPtr())); | 152 weak_ptr_factory_.GetWeakPtr()), |
153 clear_logs); | |
106 } | 154 } |
107 } | 155 } |
108 | 156 |
157 void GcmInternalsUIMessageHandler::SetRecording(const base::ListValue* args) { | |
158 if (args->GetSize() != 1) { | |
159 NOTREACHED(); | |
160 return; | |
161 } | |
162 bool recording = false; | |
163 if (!args->GetBoolean(0, &recording)) { | |
164 NOTREACHED(); | |
165 return; | |
166 } | |
167 | |
168 Profile* profile = Profile::FromWebUI(web_ui()); | |
169 gcm::GCMProfileService* profile_service = | |
170 gcm::GCMProfileServiceFactory::GetForProfile(profile); | |
jianli
2014/03/21 18:25:27
nit: indentation
juyik
2014/03/26 04:06:03
Done.
| |
171 | |
172 if (!profile_service) { | |
173 ReturnResults(profile, NULL, NULL); | |
174 } else if (profile_service->SignedInUserName().empty()) { | |
175 ReturnResults(profile, profile_service, NULL); | |
176 } else { | |
177 // Get fresh stats after changing recording setting. | |
178 profile_service->SetGCMRecording( | |
179 base::Bind( | |
180 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | |
181 weak_ptr_factory_.GetWeakPtr()), | |
182 recording); | |
183 } | |
184 } | |
185 | |
109 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( | 186 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( |
110 const gcm::GCMClient::GCMStatistics& stats) const { | 187 const gcm::GCMClient::GCMStatistics& stats) const { |
111 Profile* profile = Profile::FromWebUI(web_ui()); | 188 Profile* profile = Profile::FromWebUI(web_ui()); |
112 DCHECK(profile); | 189 DCHECK(profile); |
113 gcm::GCMProfileService* profile_service = | 190 gcm::GCMProfileService* profile_service = |
114 gcm::GCMProfileServiceFactory::GetForProfile(profile); | 191 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
115 DCHECK(profile_service); | 192 DCHECK(profile_service); |
116 ReturnResults(profile, profile_service, &stats); | 193 ReturnResults(profile, profile_service, &stats); |
117 } | 194 } |
118 | 195 |
119 void GcmInternalsUIMessageHandler::RegisterMessages() { | 196 void GcmInternalsUIMessageHandler::RegisterMessages() { |
120 web_ui()->RegisterMessageCallback( | 197 web_ui()->RegisterMessageCallback( |
121 "getGcmInternalsInfo", | 198 "getGcmInternalsInfo", |
122 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, | 199 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, |
123 base::Unretained(this))); | 200 base::Unretained(this))); |
201 web_ui()->RegisterMessageCallback( | |
202 "setGcmInternalsRecording", | |
203 base::Bind(&GcmInternalsUIMessageHandler::SetRecording, | |
204 base::Unretained(this))); | |
jianli
2014/03/21 18:25:27
Please use weak_ptr_factory_.
juyik
2014/03/26 04:06:03
Done.
| |
124 } | 205 } |
125 | 206 |
126 } // namespace | 207 } // namespace |
127 | 208 |
128 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) | 209 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) |
129 : content::WebUIController(web_ui) { | 210 : content::WebUIController(web_ui) { |
130 // Set up the chrome://gcm-internals source. | 211 // Set up the chrome://gcm-internals source. |
131 content::WebUIDataSource* html_source = | 212 content::WebUIDataSource* html_source = |
132 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); | 213 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); |
133 html_source->SetUseJsonJSFormatV2(); | 214 html_source->SetUseJsonJSFormatV2(); |
134 | 215 |
135 html_source->SetJsonPath("strings.js"); | 216 html_source->SetJsonPath("strings.js"); |
136 | 217 |
137 // Add required resources. | 218 // Add required resources. |
138 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); | 219 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); |
139 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); | 220 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); |
140 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); | 221 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); |
141 | 222 |
142 Profile* profile = Profile::FromWebUI(web_ui); | 223 Profile* profile = Profile::FromWebUI(web_ui); |
143 content::WebUIDataSource::Add(profile, html_source); | 224 content::WebUIDataSource::Add(profile, html_source); |
144 | 225 |
145 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); | 226 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); |
146 } | 227 } |
147 | 228 |
148 GCMInternalsUI::~GCMInternalsUI() {} | 229 GCMInternalsUI::~GCMInternalsUI() {} |
OLD | NEW |