Chromium Code Reviews| 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 SetCheckinInfos( | |
| 29 const std::vector<gcm::GCMStatsRecorder::CheckinActivity>& checkins, | |
| 30 base::ListValue* checkinInfo) { | |
| 31 for (std::vector<gcm::GCMStatsRecorder::CheckinActivity>::const_iterator it = | |
| 32 checkins.begin(); | |
| 33 it < checkins.end(); | |
|
jianli
2014/03/18 23:53:23
nit: !=
juyik
2014/03/20 01:09:53
Done.
| |
| 34 ++it) { | |
| 35 base::ListValue* row = new base::ListValue(); | |
| 36 checkinInfo->Append(row); | |
| 37 | |
| 38 row->AppendDouble(it->time.ToJsTime()); | |
| 39 row->AppendString(it->GetTypeString()); | |
| 40 row->AppendString(base::StringPrintf("%" PRId64, it->android_id)); | |
| 41 row->AppendString(it->details); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void SetRegisterInfo( | |
| 46 const std::vector<gcm::GCMStatsRecorder::RegisterActivity>& registers, | |
| 47 base::ListValue* registerInfo) { | |
| 48 for (std::vector<gcm::GCMStatsRecorder::RegisterActivity>::const_iterator it = | |
| 49 registers.begin(); | |
| 50 it < registers.end(); | |
| 51 ++it) { | |
| 52 base::ListValue* row = new base::ListValue(); | |
| 53 registerInfo->Append(row); | |
| 54 | |
| 55 row->AppendDouble(it->time.ToJsTime()); | |
| 56 row->AppendString(it->GetTypeString()); | |
| 57 row->AppendString(it->app_id); | |
| 58 row->AppendString(it->sender_id); | |
| 59 row->AppendString(it->details); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void SetSendInfo( | |
| 64 const std::vector<gcm::GCMStatsRecorder::SendMessageActivity>& sends, | |
| 65 base::ListValue* sendInfo) { | |
| 66 std::vector<gcm::GCMStatsRecorder::SendMessageActivity>::const_iterator it = | |
| 67 sends.begin(); | |
| 68 for (; it < sends.end(); ++it) { | |
| 69 base::ListValue* row = new base::ListValue(); | |
| 70 sendInfo->Append(row); | |
| 71 | |
| 72 row->AppendDouble(it->time.ToJsTime()); | |
| 73 row->AppendString(it->GetTypeString()); | |
| 74 row->AppendString(it->app_id); | |
| 75 row->AppendString(it->receiver_id); | |
| 76 row->AppendString(it->message_id); | |
| 77 row->AppendString(it->details); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void SetReceiveInfo( | |
| 82 const std::vector<gcm::GCMStatsRecorder::ReceiveMessageActivity>& receives, | |
| 83 base::ListValue* sendInfo) { | |
| 84 std::vector<gcm::GCMStatsRecorder::ReceiveMessageActivity>::const_iterator | |
| 85 it = receives.begin(); | |
| 86 for (; it < receives.end(); ++it) { | |
| 87 base::ListValue* row = new base::ListValue(); | |
| 88 sendInfo->Append(row); | |
| 89 | |
| 90 row->AppendDouble(it->time.ToJsTime()); | |
| 91 row->AppendString(it->GetTypeString()); | |
| 92 row->AppendString(it->sender_id); | |
| 93 row->AppendString(it->app_id); | |
| 94 row->AppendInteger(it->message_size); | |
| 95 row->AppendString(it->details); | |
| 96 } | |
| 97 } | |
| 98 | |
| 26 // Class acting as a controller of the chrome://gcm-internals WebUI. | 99 // Class acting as a controller of the chrome://gcm-internals WebUI. |
| 27 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { | 100 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler { |
| 28 public: | 101 public: |
| 29 GcmInternalsUIMessageHandler(); | 102 GcmInternalsUIMessageHandler(); |
| 30 virtual ~GcmInternalsUIMessageHandler(); | 103 virtual ~GcmInternalsUIMessageHandler(); |
| 31 | 104 |
| 32 // WebUIMessageHandler implementation. | 105 // WebUIMessageHandler implementation. |
| 33 virtual void RegisterMessages() OVERRIDE; | 106 virtual void RegisterMessages() OVERRIDE; |
| 34 | 107 |
| 35 private: | 108 private: |
| 36 // Return all of the GCM related infos to the gcm-internals page by calling | 109 // Return all of the GCM related infos to the gcm-internals page by calling |
| 37 // Javascript callback function | 110 // Javascript callback function |
| 38 // |gcm-internals.returnInfo()|. | 111 // |gcm-internals.returnInfo()|. |
| 39 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, | 112 void ReturnResults(Profile* profile, gcm::GCMProfileService* profile_service, |
| 40 const gcm::GCMClient::GCMStatistics* stats) const; | 113 const gcm::GCMClient::GCMStatistics* stats) const; |
| 41 | 114 |
| 42 // Request all of the GCM related infos through gcm profile service. | 115 // Request all of the GCM related infos through gcm profile service. |
| 43 void RequestAllInfo(const base::ListValue* args); | 116 void RequestAllInfo(const base::ListValue* args); |
| 44 | 117 |
| 118 // Enables/disables GCM activity recording through gcm profile service. | |
| 119 void SetRecording(const base::ListValue* args); | |
| 120 | |
| 45 // Callback function of the request for all gcm related infos. | 121 // Callback function of the request for all gcm related infos. |
| 46 void RequestGCMStatisticsFinished( | 122 void RequestGCMStatisticsFinished( |
| 47 const gcm::GCMClient::GCMStatistics& args) const; | 123 const gcm::GCMClient::GCMStatistics& args) const; |
| 48 | 124 |
| 49 // Factory for creating references in callbacks. | 125 // Factory for creating references in callbacks. |
| 50 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; | 126 base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_; |
| 51 | 127 |
| 52 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); | 128 DISALLOW_COPY_AND_ASSIGN(GcmInternalsUIMessageHandler); |
| 53 }; | 129 }; |
| 54 | 130 |
| 55 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() | 131 GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() |
| 56 : weak_ptr_factory_(this) {} | 132 : weak_ptr_factory_(this) {} |
| 57 | 133 |
| 58 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {} | 134 GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() {} |
| 59 | 135 |
| 60 void GcmInternalsUIMessageHandler::ReturnResults( | 136 void GcmInternalsUIMessageHandler::ReturnResults( |
| 61 Profile* profile, | 137 Profile* profile, |
| 62 gcm::GCMProfileService* profile_service, | 138 gcm::GCMProfileService* profile_service, |
| 63 const gcm::GCMClient::GCMStatistics* stats) const { | 139 const gcm::GCMClient::GCMStatistics* stats) const { |
| 64 base::DictionaryValue results; | 140 base::DictionaryValue results; |
| 141 results.SetBoolean("isRecording", stats->is_recording); | |
| 65 base::DictionaryValue* device_info = new base::DictionaryValue(); | 142 base::DictionaryValue* device_info = new base::DictionaryValue(); |
| 66 results.Set("deviceInfo", device_info); | 143 results.Set("deviceInfo", device_info); |
| 67 | 144 |
| 68 device_info->SetBoolean("profileServiceCreated", profile_service != NULL); | 145 device_info->SetBoolean("profileServiceCreated", profile_service != NULL); |
| 69 device_info->SetString("gcmEnabledState", | 146 device_info->SetString("gcmEnabledState", |
| 70 gcm::GCMProfileService::GetGCMEnabledStateString( | 147 gcm::GCMProfileService::GetGCMEnabledStateString( |
| 71 gcm::GCMProfileService::GetGCMEnabledState(profile))); | 148 gcm::GCMProfileService::GetGCMEnabledState(profile))); |
| 72 if (profile_service) { | 149 if (profile_service) { |
| 73 device_info->SetString("signedInUserName", | 150 device_info->SetString("signedInUserName", |
| 74 profile_service->SignedInUserName()); | 151 profile_service->SignedInUserName()); |
| 75 device_info->SetBoolean("gcmClientReady", | 152 device_info->SetBoolean("gcmClientReady", |
| 76 profile_service->IsGCMClientReady()); | 153 profile_service->IsGCMClientReady()); |
| 77 } | 154 } |
| 78 if (stats) { | 155 if (stats) { |
| 79 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); | 156 device_info->SetBoolean("gcmClientCreated", stats->gcm_client_created); |
| 80 device_info->SetString("gcmClientState", stats->gcm_client_state); | 157 device_info->SetString("gcmClientState", stats->gcm_client_state); |
| 81 device_info->SetBoolean("connectionClientCreated", | 158 device_info->SetBoolean("connectionClientCreated", |
| 82 stats->connection_client_created); | 159 stats->connection_client_created); |
| 160 device_info->SetString("appIdsCached", stats->app_ids_cached); | |
| 83 if (stats->connection_client_created) | 161 if (stats->connection_client_created) |
| 84 device_info->SetString("connectionState", stats->connection_state); | 162 device_info->SetString("connectionState", stats->connection_state); |
| 85 if (stats->android_id > 0) { | 163 if (stats->android_id > 0) { |
| 86 device_info->SetString("androidId", | 164 device_info->SetString("androidId", |
| 87 base::StringPrintf("0x%" PRIx64, stats->android_id)); | 165 base::StringPrintf("0x%" PRIx64, stats->android_id)); |
| 88 } | 166 } |
| 167 if (stats->send_queue_size > 0) | |
| 168 device_info->SetInteger("sendQueueSize", stats->send_queue_size); | |
| 169 if (stats->unacked_queue_size > 0) | |
| 170 device_info->SetInteger("unackedQueueSize", stats->unacked_queue_size); | |
| 171 | |
| 172 if (stats->checkins.size() > 0) { | |
| 173 base::ListValue* checkinInfo = new base::ListValue(); | |
| 174 results.Set("checkinInfo", checkinInfo); | |
| 175 SetCheckinInfos(stats->checkins, checkinInfo); | |
| 176 } | |
| 177 if (stats->registers.size() > 0) { | |
| 178 base::ListValue* registerInfo = new base::ListValue(); | |
| 179 results.Set("registerInfo", registerInfo); | |
| 180 SetRegisterInfo(stats->registers, registerInfo); | |
| 181 } | |
| 182 if (stats->sent_messages.size() > 0) { | |
| 183 base::ListValue* sendInfo = new base::ListValue(); | |
| 184 results.Set("sendInfo", sendInfo); | |
| 185 SetSendInfo(stats->sent_messages, sendInfo); | |
| 186 } | |
| 187 if (stats->received_messages.size() > 0) { | |
| 188 base::ListValue* receiveInfo = new base::ListValue(); | |
| 189 results.Set("receiveInfo", receiveInfo); | |
| 190 SetReceiveInfo(stats->received_messages, receiveInfo); | |
| 191 } | |
| 89 } | 192 } |
| 90 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo", | 193 web_ui()->CallJavascriptFunction("gcmInternals.setGcmInternalsInfo", |
| 91 results); | 194 results); |
| 92 } | 195 } |
| 93 | 196 |
| 94 void GcmInternalsUIMessageHandler::RequestAllInfo( | 197 void GcmInternalsUIMessageHandler::RequestAllInfo( |
| 95 const base::ListValue* args) { | 198 const base::ListValue* args) { |
| 199 if (args->GetSize() != 1) { | |
| 200 NOTREACHED(); | |
| 201 return; | |
| 202 } | |
| 203 bool clear_logs = false; | |
| 204 if (!args->GetBoolean(0, &clear_logs)) { | |
| 205 NOTREACHED(); | |
| 206 return; | |
| 207 } | |
| 208 | |
| 96 Profile* profile = Profile::FromWebUI(web_ui()); | 209 Profile* profile = Profile::FromWebUI(web_ui()); |
| 97 gcm::GCMProfileService* profile_service = | 210 gcm::GCMProfileService* profile_service = |
| 98 gcm::GCMProfileServiceFactory::GetForProfile(profile); | 211 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
| 212 | |
| 213 if (!profile_service) { | |
|
fgorski
2014/03/18 21:28:37
this is the place we identified as a problem.
juyik
2014/03/20 01:09:53
I looked into it deeper and now it doesn't seemed
| |
| 214 ReturnResults(profile, NULL, NULL); | |
| 215 } else { | |
| 216 profile_service->RequestGCMStatistics( | |
| 217 base::Bind(&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | |
| 218 weak_ptr_factory_.GetWeakPtr()), | |
| 219 clear_logs); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 void GcmInternalsUIMessageHandler::SetRecording(const base::ListValue* args) { | |
| 224 if (args->GetSize() != 1) { | |
| 225 NOTREACHED(); | |
| 226 return; | |
| 227 } | |
| 228 bool recording = false; | |
| 229 if (!args->GetBoolean(0, &recording)) { | |
| 230 NOTREACHED(); | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 235 gcm::GCMProfileService* profile_service = | |
| 236 gcm::GCMProfileServiceFactory::GetForProfile(profile); | |
| 99 | 237 |
| 100 if (!profile_service) { | 238 if (!profile_service) { |
| 101 ReturnResults(profile, NULL, NULL); | 239 ReturnResults(profile, NULL, NULL); |
| 102 } else { | 240 } else { |
| 103 profile_service->RequestGCMStatistics(base::Bind( | 241 // Get fresh stats after changing recording setting. |
| 104 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | 242 profile_service->SetGCMRecording( |
| 105 weak_ptr_factory_.GetWeakPtr())); | 243 base::Bind( |
| 244 &GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished, | |
| 245 weak_ptr_factory_.GetWeakPtr()), | |
| 246 recording); | |
| 106 } | 247 } |
| 107 } | 248 } |
| 108 | 249 |
| 109 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( | 250 void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished( |
| 110 const gcm::GCMClient::GCMStatistics& stats) const { | 251 const gcm::GCMClient::GCMStatistics& stats) const { |
| 111 Profile* profile = Profile::FromWebUI(web_ui()); | 252 Profile* profile = Profile::FromWebUI(web_ui()); |
| 112 DCHECK(profile); | 253 DCHECK(profile); |
| 113 gcm::GCMProfileService* profile_service = | 254 gcm::GCMProfileService* profile_service = |
| 114 gcm::GCMProfileServiceFactory::GetForProfile(profile); | 255 gcm::GCMProfileServiceFactory::GetForProfile(profile); |
| 115 DCHECK(profile_service); | 256 DCHECK(profile_service); |
| 116 ReturnResults(profile, profile_service, &stats); | 257 ReturnResults(profile, profile_service, &stats); |
| 117 } | 258 } |
| 118 | 259 |
| 119 void GcmInternalsUIMessageHandler::RegisterMessages() { | 260 void GcmInternalsUIMessageHandler::RegisterMessages() { |
| 120 web_ui()->RegisterMessageCallback( | 261 web_ui()->RegisterMessageCallback( |
| 121 "getGcmInternalsInfo", | 262 "getGcmInternalsInfo", |
| 122 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, | 263 base::Bind(&GcmInternalsUIMessageHandler::RequestAllInfo, |
| 123 base::Unretained(this))); | 264 base::Unretained(this))); |
| 265 web_ui()->RegisterMessageCallback( | |
| 266 "setGcmInternalsRecording", | |
| 267 base::Bind(&GcmInternalsUIMessageHandler::SetRecording, | |
| 268 base::Unretained(this))); | |
| 124 } | 269 } |
| 125 | 270 |
| 126 } // namespace | 271 } // namespace |
| 127 | 272 |
| 128 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) | 273 GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui) |
| 129 : content::WebUIController(web_ui) { | 274 : content::WebUIController(web_ui) { |
| 130 // Set up the chrome://gcm-internals source. | 275 // Set up the chrome://gcm-internals source. |
| 131 content::WebUIDataSource* html_source = | 276 content::WebUIDataSource* html_source = |
| 132 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); | 277 content::WebUIDataSource::Create(chrome::kChromeUIGCMInternalsHost); |
| 133 html_source->SetUseJsonJSFormatV2(); | 278 html_source->SetUseJsonJSFormatV2(); |
| 134 | 279 |
| 135 html_source->SetJsonPath("strings.js"); | 280 html_source->SetJsonPath("strings.js"); |
| 136 | 281 |
| 137 // Add required resources. | 282 // Add required resources. |
| 138 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); | 283 html_source->AddResourcePath("gcm_internals.css", IDR_GCM_INTERNALS_CSS); |
| 139 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); | 284 html_source->AddResourcePath("gcm_internals.js", IDR_GCM_INTERNALS_JS); |
| 140 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); | 285 html_source->SetDefaultResource(IDR_GCM_INTERNALS_HTML); |
| 141 | 286 |
| 142 Profile* profile = Profile::FromWebUI(web_ui); | 287 Profile* profile = Profile::FromWebUI(web_ui); |
| 143 content::WebUIDataSource::Add(profile, html_source); | 288 content::WebUIDataSource::Add(profile, html_source); |
| 144 | 289 |
| 145 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); | 290 web_ui->AddMessageHandler(new GcmInternalsUIMessageHandler()); |
| 146 } | 291 } |
| 147 | 292 |
| 148 GCMInternalsUI::~GCMInternalsUI() {} | 293 GCMInternalsUI::~GCMInternalsUI() {} |
| OLD | NEW |