Allow consumers to detach from tracing session

Introduce two methods to the consumer endpoint:
Detach(key) and Attach(key). Both take a key, which
is a random tag chosen by the consumer.
When detaching, the tracing session is allowed to
continue in background, without requiring the cmdline
client to outlive the session.
When re-attaching, the new consumer is bound to a
the previously detached session.
When re-attaching both the key and the UID of the
consumer must match the previous session.

This CL also introduces a new ObserveState IPC message.
This is require to know when the tracing session is
disabled after reattaching. Before this change that was
only possible as a IPC reply to EnableTracing.

Bug: 120607375
Change-Id: If1998c8ed228c02be69767aaff8b4f3d3f19f201
diff --git a/test/test_helper.h b/test/test_helper.h
index 599ce5d..89a2fde 100644
--- a/test/test_helper.h
+++ b/test/test_helper.h
@@ -17,6 +17,7 @@
 #ifndef TEST_TEST_HELPER_H_
 #define TEST_TEST_HELPER_H_
 
+#include "perfetto/base/scoped_file.h"
 #include "perfetto/tracing/core/consumer.h"
 #include "perfetto/tracing/core/trace_config.h"
 #include "perfetto/tracing/core/trace_packet.h"
@@ -38,14 +39,19 @@
   void OnDisconnect() override;
   void OnTracingDisabled() override;
   void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
+  void OnDetach(bool) override;
+  void OnAttach(bool, const TraceConfig&) override;
 
   void StartServiceIfRequired();
   FakeProducer* ConnectFakeProducer();
   void ConnectConsumer();
-  void StartTracing(const TraceConfig& config);
+  void StartTracing(const TraceConfig& config,
+                    base::ScopedFile = base::ScopedFile());
   void DisableTracing();
   void FlushAndWait(uint32_t timeout_ms);
   void ReadData(uint32_t read_count = 0);
+  void DetachConsumer(const std::string& key);
+  bool AttachConsumer(const std::string& key);
 
   void WaitForConsumerConnect();
   void WaitForProducerEnabled();
@@ -60,10 +66,13 @@
 
  private:
   base::TestTaskRunner* task_runner_ = nullptr;
+  int cur_consumer_num_ = 0;
 
   std::function<void()> on_connect_callback_;
   std::function<void()> on_packets_finished_callback_;
   std::function<void()> on_stop_tracing_callback_;
+  std::function<void()> on_detach_callback_;
+  std::function<void(bool)> on_attach_callback_;
 
   std::vector<protos::TracePacket> trace_;