Componentize the GCM info collection for gcm-internals.

This CL componentizes the function
GcmInternalsUIMessageHandler::ReturnResults
and the related code, so that it can be shared on iOS.

TBR=dbeam

Review URL: https://codereview.chromium.org/1437133002

Cr-Commit-Position: refs/heads/master@{#359814}
diff --git a/chrome/browser/ui/webui/gcm_internals_ui.cc b/chrome/browser/ui/webui/gcm_internals_ui.cc
index 79e45e8..0529985b 100644
--- a/chrome/browser/ui/webui/gcm_internals_ui.cc
+++ b/chrome/browser/ui/webui/gcm_internals_ui.cc
@@ -8,11 +8,7 @@
 
 #include "base/bind.h"
 #include "base/bind_helpers.h"
-#include "base/format_macros.h"
 #include "base/memory/weak_ptr.h"
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/string_util.h"
-#include "base/strings/stringprintf.h"
 #include "base/values.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
@@ -20,6 +16,7 @@
 #include "components/gcm_driver/gcm_client.h"
 #include "components/gcm_driver/gcm_driver.h"
 #include "components/gcm_driver/gcm_internals_constants.h"
+#include "components/gcm_driver/gcm_internals_helper.h"
 #include "components/gcm_driver/gcm_profile_service.h"
 #include "content/public/browser/web_ui.h"
 #include "content/public/browser/web_ui_controller.h"
@@ -29,85 +26,6 @@
 
 namespace {
 
-void SetCheckinInfo(
-    const std::vector<gcm::CheckinActivity>& checkins,
-    base::ListValue* checkin_info) {
-  std::vector<gcm::CheckinActivity>::const_iterator it = checkins.begin();
-  for (; it < checkins.end(); ++it) {
-    base::ListValue* row = new base::ListValue();
-    checkin_info->Append(row);
-
-    row->AppendDouble(it->time.ToJsTime());
-    row->AppendString(it->event);
-    row->AppendString(it->details);
-  }
-}
-
-void SetConnectionInfo(
-    const std::vector<gcm::ConnectionActivity>& connections,
-    base::ListValue* connection_info) {
-  std::vector<gcm::ConnectionActivity>::const_iterator it = connections.begin();
-  for (; it < connections.end(); ++it) {
-    base::ListValue* row = new base::ListValue();
-    connection_info->Append(row);
-
-    row->AppendDouble(it->time.ToJsTime());
-    row->AppendString(it->event);
-    row->AppendString(it->details);
-  }
-}
-
-void SetRegistrationInfo(
-    const std::vector<gcm::RegistrationActivity>& registrations,
-    base::ListValue* registration_info) {
-  std::vector<gcm::RegistrationActivity>::const_iterator it =
-      registrations.begin();
-  for (; it < registrations.end(); ++it) {
-    base::ListValue* row = new base::ListValue();
-    registration_info->Append(row);
-
-    row->AppendDouble(it->time.ToJsTime());
-    row->AppendString(it->app_id);
-    row->AppendString(it->source);
-    row->AppendString(it->event);
-    row->AppendString(it->details);
-  }
-}
-
-void SetReceivingInfo(
-    const std::vector<gcm::ReceivingActivity>& receives,
-    base::ListValue* receive_info) {
-  std::vector<gcm::ReceivingActivity>::const_iterator it = receives.begin();
-  for (; it < receives.end(); ++it) {
-    base::ListValue* row = new base::ListValue();
-    receive_info->Append(row);
-
-    row->AppendDouble(it->time.ToJsTime());
-    row->AppendString(it->app_id);
-    row->AppendString(it->from);
-    row->AppendString(base::IntToString(it->message_byte_size));
-    row->AppendString(it->event);
-    row->AppendString(it->details);
-  }
-}
-
-void SetSendingInfo(
-    const std::vector<gcm::SendingActivity>& sends,
-    base::ListValue* send_info) {
-  std::vector<gcm::SendingActivity>::const_iterator it = sends.begin();
-  for (; it < sends.end(); ++it) {
-    base::ListValue* row = new base::ListValue();
-    send_info->Append(row);
-
-    row->AppendDouble(it->time.ToJsTime());
-    row->AppendString(it->app_id);
-    row->AppendString(it->receiver_id);
-    row->AppendString(it->message_id);
-    row->AppendString(it->event);
-    row->AppendString(it->details);
-  }
-}
-
 // Class acting as a controller of the chrome://gcm-internals WebUI.
 class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler {
  public:
@@ -150,66 +68,8 @@
     gcm::GCMProfileService* profile_service,
     const gcm::GCMClient::GCMStatistics* stats) const {
   base::DictionaryValue results;
-  base::DictionaryValue* device_info = new base::DictionaryValue();
-  results.Set(gcm_driver::kDeviceInfo, device_info);
-
-  device_info->SetBoolean(gcm_driver::kProfileServiceCreated,
-                          profile_service != NULL);
-  device_info->SetBoolean(
-      gcm_driver::kGcmEnabled,
-      gcm::GCMProfileService::IsGCMEnabled(profile->GetPrefs()));
-  if (stats) {
-    results.SetBoolean(gcm_driver::kIsRecording, stats->is_recording);
-    device_info->SetBoolean(gcm_driver::kGcmClientCreated,
-                            stats->gcm_client_created);
-    device_info->SetString(gcm_driver::kGcmClientState,
-                           stats->gcm_client_state);
-    device_info->SetBoolean(gcm_driver::kConnectionClientCreated,
-                            stats->connection_client_created);
-    device_info->SetString(gcm_driver::kRegisteredAppIds,
-                           base::JoinString(stats->registered_app_ids, ","));
-    if (stats->connection_client_created)
-      device_info->SetString(gcm_driver::kConnectionState,
-                             stats->connection_state);
-    if (stats->android_id > 0) {
-      device_info->SetString(
-          gcm_driver::kAndroidId,
-          base::StringPrintf("0x%" PRIx64, stats->android_id));
-    }
-    device_info->SetInteger(gcm_driver::kSendQueueSize, stats->send_queue_size);
-    device_info->SetInteger(gcm_driver::kResendQueueSize,
-                            stats->resend_queue_size);
-
-    if (stats->recorded_activities.checkin_activities.size() > 0) {
-      base::ListValue* checkin_info = new base::ListValue();
-      results.Set(gcm_driver::kCheckinInfo, checkin_info);
-      SetCheckinInfo(stats->recorded_activities.checkin_activities,
-                     checkin_info);
-    }
-    if (stats->recorded_activities.connection_activities.size() > 0) {
-      base::ListValue* connection_info = new base::ListValue();
-      results.Set(gcm_driver::kConnectionInfo, connection_info);
-      SetConnectionInfo(stats->recorded_activities.connection_activities,
-                        connection_info);
-    }
-    if (stats->recorded_activities.registration_activities.size() > 0) {
-      base::ListValue* registration_info = new base::ListValue();
-      results.Set(gcm_driver::kRegistrationInfo, registration_info);
-      SetRegistrationInfo(stats->recorded_activities.registration_activities,
-                          registration_info);
-    }
-    if (stats->recorded_activities.receiving_activities.size() > 0) {
-      base::ListValue* receive_info = new base::ListValue();
-      results.Set(gcm_driver::kReceiveInfo, receive_info);
-      SetReceivingInfo(stats->recorded_activities.receiving_activities,
-                       receive_info);
-    }
-    if (stats->recorded_activities.sending_activities.size() > 0) {
-      base::ListValue* send_info = new base::ListValue();
-      results.Set(gcm_driver::kSendInfo, send_info);
-      SetSendingInfo(stats->recorded_activities.sending_activities, send_info);
-    }
-  }
+  gcm_driver::SetGCMInternalsInfo(stats, profile_service, profile->GetPrefs(),
+                                  &results);
   web_ui()->CallJavascriptFunction(gcm_driver::kSetGcmInternalsInfo, results);
 }
 
diff --git a/components/gcm_driver.gypi b/components/gcm_driver.gypi
index 7ac49c9..9c5bb23d 100644
--- a/components/gcm_driver.gypi
+++ b/components/gcm_driver.gypi
@@ -81,6 +81,8 @@
         'gcm_driver/gcm_driver_desktop.h',
         'gcm_driver/gcm_internals_constants.cc',
         'gcm_driver/gcm_internals_constants.h',
+        'gcm_driver/gcm_internals_helper.cc',
+        'gcm_driver/gcm_internals_helper.h',
         'gcm_driver/gcm_profile_service.cc',
         'gcm_driver/gcm_profile_service.h',
         'gcm_driver/gcm_stats_recorder_impl.cc',
diff --git a/components/gcm_driver/BUILD.gn b/components/gcm_driver/BUILD.gn
index 2e852433..46c35eb 100644
--- a/components/gcm_driver/BUILD.gn
+++ b/components/gcm_driver/BUILD.gn
@@ -45,6 +45,8 @@
     "gcm_driver_desktop.h",
     "gcm_internals_constants.cc",
     "gcm_internals_constants.h",
+    "gcm_internals_helper.cc",
+    "gcm_internals_helper.h",
     "gcm_profile_service.cc",
     "gcm_profile_service.h",
     "gcm_stats_recorder_impl.cc",
diff --git a/components/gcm_driver/gcm_internals_helper.cc b/components/gcm_driver/gcm_internals_helper.cc
new file mode 100644
index 0000000..c66d3d7
--- /dev/null
+++ b/components/gcm_driver/gcm_internals_helper.cc
@@ -0,0 +1,150 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/gcm_internals_helper.h"
+
+#include "base/format_macros.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "base/values.h"
+#include "components/gcm_driver/gcm_activity.h"
+#include "components/gcm_driver/gcm_internals_constants.h"
+#include "components/gcm_driver/gcm_profile_service.h"
+
+namespace gcm_driver {
+
+namespace {
+
+void SetCheckinInfo(const std::vector<gcm::CheckinActivity>& checkins,
+                    base::ListValue* checkin_info) {
+  for (const gcm::CheckinActivity& checkin : checkins) {
+        base::ListValue* row = new base::ListValue();
+    checkin_info->Append(row);
+
+    row->AppendDouble(checkin.time.ToJsTime());
+    row->AppendString(checkin.event);
+    row->AppendString(checkin.details);
+  }
+}
+
+void SetConnectionInfo(const std::vector<gcm::ConnectionActivity>& connections,
+                       base::ListValue* connection_info) {
+  for (const gcm::ConnectionActivity& connection : connections) {
+    base::ListValue* row = new base::ListValue();
+    connection_info->Append(row);
+
+    row->AppendDouble(connection.time.ToJsTime());
+    row->AppendString(connection.event);
+    row->AppendString(connection.details);
+  }
+}
+
+void SetRegistrationInfo(
+    const std::vector<gcm::RegistrationActivity>& registrations,
+    base::ListValue* registration_info) {
+  for (const gcm::RegistrationActivity& registration : registrations) {
+    base::ListValue* row = new base::ListValue();
+    registration_info->Append(row);
+
+    row->AppendDouble(registration.time.ToJsTime());
+    row->AppendString(registration.app_id);
+    row->AppendString(registration.source);
+    row->AppendString(registration.event);
+    row->AppendString(registration.details);
+  }
+}
+
+void SetReceivingInfo(const std::vector<gcm::ReceivingActivity>& receives,
+                      base::ListValue* receive_info) {
+  for (const gcm::ReceivingActivity& receive : receives) {
+    base::ListValue* row = new base::ListValue();
+    receive_info->Append(row);
+
+    row->AppendDouble(receive.time.ToJsTime());
+    row->AppendString(receive.app_id);
+    row->AppendString(receive.from);
+    row->AppendString(base::IntToString(receive.message_byte_size));
+    row->AppendString(receive.event);
+    row->AppendString(receive.details);
+  }
+}
+
+void SetSendingInfo(const std::vector<gcm::SendingActivity>& sends,
+                    base::ListValue* send_info) {
+  for (const gcm::SendingActivity& send : sends) {
+    base::ListValue* row = new base::ListValue();
+    send_info->Append(row);
+
+    row->AppendDouble(send.time.ToJsTime());
+    row->AppendString(send.app_id);
+    row->AppendString(send.receiver_id);
+    row->AppendString(send.message_id);
+    row->AppendString(send.event);
+    row->AppendString(send.details);
+  }
+}
+
+}  // namespace
+
+void SetGCMInternalsInfo(const gcm::GCMClient::GCMStatistics* stats,
+                         gcm::GCMProfileService* profile_service,
+                         PrefService* prefs,
+                         base::DictionaryValue* results) {
+  base::DictionaryValue* device_info = new base::DictionaryValue();
+  results->Set(kDeviceInfo, device_info);
+
+  device_info->SetBoolean(kProfileServiceCreated, profile_service != NULL);
+  device_info->SetBoolean(kGcmEnabled,
+                          gcm::GCMProfileService::IsGCMEnabled(prefs));
+  if (stats) {
+    results->SetBoolean(kIsRecording, stats->is_recording);
+    device_info->SetBoolean(kGcmClientCreated, stats->gcm_client_created);
+    device_info->SetString(kGcmClientState, stats->gcm_client_state);
+    device_info->SetBoolean(kConnectionClientCreated,
+                            stats->connection_client_created);
+    device_info->SetString(kRegisteredAppIds,
+                           base::JoinString(stats->registered_app_ids, ","));
+    if (stats->connection_client_created)
+      device_info->SetString(kConnectionState, stats->connection_state);
+    if (stats->android_id > 0) {
+      device_info->SetString(
+          kAndroidId, base::StringPrintf("0x%" PRIx64, stats->android_id));
+    }
+    device_info->SetInteger(kSendQueueSize, stats->send_queue_size);
+    device_info->SetInteger(kResendQueueSize, stats->resend_queue_size);
+
+    if (stats->recorded_activities.checkin_activities.size() > 0) {
+      base::ListValue* checkin_info = new base::ListValue();
+      results->Set(kCheckinInfo, checkin_info);
+      SetCheckinInfo(stats->recorded_activities.checkin_activities,
+                     checkin_info);
+    }
+    if (stats->recorded_activities.connection_activities.size() > 0) {
+      base::ListValue* connection_info = new base::ListValue();
+      results->Set(kConnectionInfo, connection_info);
+      SetConnectionInfo(stats->recorded_activities.connection_activities,
+                        connection_info);
+    }
+    if (stats->recorded_activities.registration_activities.size() > 0) {
+      base::ListValue* registration_info = new base::ListValue();
+      results->Set(kRegistrationInfo, registration_info);
+      SetRegistrationInfo(stats->recorded_activities.registration_activities,
+                          registration_info);
+    }
+    if (stats->recorded_activities.receiving_activities.size() > 0) {
+      base::ListValue* receive_info = new base::ListValue();
+      results->Set(kReceiveInfo, receive_info);
+      SetReceivingInfo(stats->recorded_activities.receiving_activities,
+                       receive_info);
+    }
+    if (stats->recorded_activities.sending_activities.size() > 0) {
+      base::ListValue* send_info = new base::ListValue();
+      results->Set(kSendInfo, send_info);
+      SetSendingInfo(stats->recorded_activities.sending_activities, send_info);
+    }
+  }
+}
+
+}  // namespace gcm_driver
diff --git a/components/gcm_driver/gcm_internals_helper.h b/components/gcm_driver/gcm_internals_helper.h
new file mode 100644
index 0000000..ee89602
--- /dev/null
+++ b/components/gcm_driver/gcm_internals_helper.h
@@ -0,0 +1,32 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_GCM_DRIVER_GCM_INTERNALS_HELPER_H_
+#define COMPONENTS_GCM_DRIVER_GCM_INTERNALS_HELPER_H_
+
+#include <vector>
+
+#include "components/gcm_driver/gcm_client.h"
+
+class PrefService;
+
+namespace base {
+class DictionaryValue;
+}
+
+namespace gcm {
+class GCMProfileService;
+}
+
+namespace gcm_driver {
+
+// Sets the GCM infos for the gcm-internals WebUI in |results|.
+void SetGCMInternalsInfo(const gcm::GCMClient::GCMStatistics* stats,
+                         gcm::GCMProfileService* profile_service,
+                         PrefService* prefs,
+                         base::DictionaryValue* results);
+
+}  // namespace gcm_driver
+
+#endif  // COMPONENTS_GCM_DRIVER_GCM_INTERNALS_HELPER_H_