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