add C wrapper for libmetrics

TEST=unit tests + build&test crosmetrics plugin for flimflam

Review URL: http://codereview.chromium.org/2832008
diff --git a/metrics/Makefile b/metrics/Makefile
index c8ba545..fa68a40 100644
--- a/metrics/Makefile
+++ b/metrics/Makefile
@@ -32,6 +32,7 @@
 	metrics_daemon.o \
 	metrics_daemon_test.o
 LIB_OBJS = \
+	c_metrics_library.o \
 	metrics_library.o
 TESTLIB_OBJS = \
 	metrics_library.o \
diff --git a/metrics/c_metrics_library.cc b/metrics/c_metrics_library.cc
new file mode 100644
index 0000000..91e9ca6
--- /dev/null
+++ b/metrics/c_metrics_library.cc
@@ -0,0 +1,44 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//
+// C wrapper to libmetrics
+//
+
+#include "c_metrics_library.h"
+#include "metrics_library.h"
+
+extern "C" CMetricsLibrary CMetricsLibraryNew(void) {
+  MetricsLibrary* lib = new MetricsLibrary;
+  return reinterpret_cast<CMetricsLibrary>(lib);
+}
+
+extern "C" void CMetricsLibraryDelete(CMetricsLibrary handle) {
+  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
+  delete lib;
+}
+
+extern "C" void CMetricsLibraryInit(CMetricsLibrary handle) {
+  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
+  if (lib != NULL)
+    lib->Init();
+}
+
+extern "C" int CMetricsLibrarySendToUMA(CMetricsLibrary handle,
+					const char* name, int sample,
+					int min, int max, int nbuckets) {
+  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
+  if (lib == NULL)
+    return 0;
+  return lib->SendToUMA(std::string(name), sample, min, max, nbuckets);
+}
+
+extern "C" int CMetricsLibrarySendEnumToUMA(CMetricsLibrary handle,
+					    const char* name, int sample,
+					    int max) {
+  MetricsLibrary* lib = reinterpret_cast<MetricsLibrary*>(handle);
+  if (lib == NULL)
+    return 0;
+  return lib->SendEnumToUMA(std::string(name), sample, max);
+}
diff --git a/metrics/c_metrics_library.h b/metrics/c_metrics_library.h
new file mode 100644
index 0000000..6d1e6f9
--- /dev/null
+++ b/metrics/c_metrics_library.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2010 The Chromium OS 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 C_METRICS_LIBRARY_H_
+#define C_METRICS_LIBRARY_H_
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+typedef struct CMetricsLibraryOpaque* CMetricsLibrary;
+
+// C wrapper for MetricsLibrary::MetricsLibrary.
+CMetricsLibrary CMetricsLibraryNew(void);
+
+// C wrapper for MetricsLibrary::~MetricsLibrary.
+void CMetricsLibraryDelete(CMetricsLibrary handle);
+
+// C wrapper for MetricsLibrary::Init.
+void CMetricsLibraryInit(CMetricsLibrary handle);
+
+// C wrapper for MetricsLibrary::SendToUMA.
+int CMetricsLibrarySendToUMA(CMetricsLibrary handle,
+			     const char* name, int sample,
+			     int min, int max, int nbuckets);
+
+// C wrapper for MetricsLibrary::SendEnumToUMA.
+int CMetricsLibrarySendEnumToUMA(CMetricsLibrary handle,
+				 const char* name, int sample, int max);
+#if defined(__cplusplus)
+}
+#endif
+#endif	// C_METRICS_LIBRARY_H_
diff --git a/metrics/metrics_library.h b/metrics/metrics_library.h
index a9ac0bb..80bea70 100644
--- a/metrics/metrics_library.h
+++ b/metrics/metrics_library.h
@@ -56,6 +56,7 @@
   static bool SendToAutotest(const std::string& name, int value);
 
  private:
+  friend class CMetricsLibraryTest;
   friend class MetricsLibraryTest;
   FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
   FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
diff --git a/metrics/metrics_library_test.cc b/metrics/metrics_library_test.cc
index f585d35..596abd3 100644
--- a/metrics/metrics_library_test.cc
+++ b/metrics/metrics_library_test.cc
@@ -2,13 +2,14 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "metrics_library.h"
-
 #include <cstring>
 
 #include <base/file_util.h>
 #include <gtest/gtest.h>
 
+#include "c_metrics_library.h"
+#include "metrics_library.h"
+
 static const FilePath kTestUMAEventsFile("test-uma-events");
 
 class MetricsLibraryTest : public testing::Test {
@@ -83,6 +84,48 @@
   EXPECT_EQ(0, memcmp(exp, buf, kLen));
 }
 
+class CMetricsLibraryTest : public testing::Test {
+ protected:
+  virtual void SetUp() {
+    lib_ = CMetricsLibraryNew();
+    MetricsLibrary& ml = *reinterpret_cast<MetricsLibrary*>(lib_);
+    EXPECT_EQ(NULL, ml.uma_events_file_);
+    CMetricsLibraryInit(lib_);
+    EXPECT_TRUE(NULL != ml.uma_events_file_);
+    ml.uma_events_file_ = kTestUMAEventsFile.value().c_str();
+  }
+
+  virtual void TearDown() {
+    CMetricsLibraryDelete(lib_);
+    file_util::Delete(kTestUMAEventsFile, false);
+  }
+
+  CMetricsLibrary lib_;
+};
+
+TEST_F(CMetricsLibraryTest, SendEnumToUMA) {
+  char buf[100];
+  const int kLen = 40;
+  EXPECT_TRUE(CMetricsLibrarySendEnumToUMA(lib_, "Test.EnumMetric", 1, 3));
+  EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100));
+
+  char exp[kLen];
+  sprintf(exp, "%c%c%c%clinearhistogram%cTest.EnumMetric 1 3",
+          kLen, 0, 0, 0, 0);
+  EXPECT_EQ(0, memcmp(exp, buf, kLen));
+}
+
+TEST_F(CMetricsLibraryTest, SendToUMA) {
+  char buf[100];
+  const int kLen = 37;
+  EXPECT_TRUE(CMetricsLibrarySendToUMA(lib_, "Test.Metric", 2, 1, 100, 50));
+  EXPECT_EQ(kLen, file_util::ReadFile(kTestUMAEventsFile, buf, 100));
+
+  char exp[kLen];
+  sprintf(exp, "%c%c%c%chistogram%cTest.Metric 2 1 100 50", kLen, 0, 0, 0, 0);
+  EXPECT_EQ(0, memcmp(exp, buf, kLen));
+}
+
 int main(int argc, char** argv) {
   testing::InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();