Adds aidl definitions and their implementations for binder transfer of
statsd entries to clients. This change only includes changes on statds
side and does not include java library for clients to import. Java
library will be a separate change as it requires system api review.

Test: statsd, statsd_test
Change-Id: I306c6e9687801668cc0145b12d38406bfe634775
diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h
index 449a2b8..7f04658 100644
--- a/cmds/statsd/src/StatsService.h
+++ b/cmds/statsd/src/StatsService.h
@@ -24,6 +24,7 @@
 #include "packages/UidMap.h"
 
 #include <android/os/BnStatsManager.h>
+#include <android/os/IStatsCallbacks.h>
 #include <android/os/IStatsCompanionService.h>
 #include <binder/IResultReceiver.h>
 #include <binder/IShellCallback.h>
@@ -42,7 +43,7 @@
 namespace os {
 namespace statsd {
 
-class StatsService : public BnStatsManager, public LogListener {
+class StatsService : public BnStatsManager, public LogListener, public IBinder::DeathRecipient {
 public:
     StatsService(const sp<Looper>& handlerLooper);
     virtual ~StatsService();
@@ -70,6 +71,22 @@
      */
     virtual void OnLogEvent(const LogEvent& event);
 
+    /**
+     * Binder call to force trigger pushLog. This would be called by callback
+     * clients.
+     */
+    virtual Status requestPush() override;
+
+    /**
+     * Pushes stats log entries from statsd to callback clients.
+     */
+    Status pushLog(const vector<uint8_t>& log);
+
+    /**
+     * Binder call to listen to statsd to send stats log entries.
+     */
+    virtual Status subscribeStatsLog(const sp<IStatsCallbacks>& callbacks) override;
+
     // TODO: public for testing since statsd doesn't run when system starts. Change to private
     // later.
     /** Inform statsCompanion that statsd is ready. */
@@ -78,6 +95,9 @@
     /** Fetches and returns the StatsCompanionService. */
     static sp<IStatsCompanionService> getStatsCompanionService();
 
+    /** IBinder::DeathRecipient */
+    virtual void binderDied(const wp<IBinder>& who) override;
+
 private:
     /**
      * Load system properties at init.
@@ -159,6 +179,16 @@
      * Whether this is an eng build.
      */
     bool mEngBuild;
+
+    /**
+     * Lock for callback handling.
+     */
+    std::mutex mLock;
+
+    /**
+     * Vector maintaining the list of callbacks for clients.
+     */
+    Vector< sp<IStatsCallbacks> > mCallbacks;
 };
 
 }  // namespace statsd