1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef TEST_TASK_RUNNER_THREAD_DELEGATES_H_
18 #define TEST_TASK_RUNNER_THREAD_DELEGATES_H_
19 
20 #include "perfetto/tracing/ipc/service_ipc_host.h"
21 #include "src/traced/probes/probes_producer.h"
22 #include "test/fake_producer.h"
23 #include "test/task_runner_thread.h"
24 
25 namespace perfetto {
26 // This is used only in daemon starting integrations tests.
27 class ServiceDelegate : public ThreadDelegate {
28  public:
29   ServiceDelegate(const std::string& producer_socket,
30                   const std::string& consumer_socket)
31       : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {}
32   ~ServiceDelegate() override = default;
33 
34   void Initialize(base::TaskRunner* task_runner) override {
35     svc_ = ServiceIPCHost::CreateInstance(task_runner);
36     unlink(producer_socket_.c_str());
37     unlink(consumer_socket_.c_str());
38     svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
39   }
40 
41  private:
42   std::string producer_socket_;
43   std::string consumer_socket_;
44   std::unique_ptr<ServiceIPCHost> svc_;
45 };
46 
47 // This is used only in daemon starting integrations tests.
48 class ProbesProducerDelegate : public ThreadDelegate {
49  public:
50   ProbesProducerDelegate(const std::string& producer_socket)
51       : producer_socket_(producer_socket) {}
52   ~ProbesProducerDelegate() override = default;
53 
54   void Initialize(base::TaskRunner* task_runner) override {
55     producer_.reset(new ProbesProducer);
56     producer_->ConnectWithRetries(producer_socket_.c_str(), task_runner);
57   }
58 
59  private:
60   std::string producer_socket_;
61   std::unique_ptr<ProbesProducer> producer_;
62 };
63 
64 class FakeProducerDelegate : public ThreadDelegate {
65  public:
66   FakeProducerDelegate(const std::string& producer_socket,
67                        std::function<void()> connect_callback)
68       : producer_socket_(producer_socket),
69         connect_callback_(std::move(connect_callback)) {}
70   ~FakeProducerDelegate() override = default;
71 
72   void Initialize(base::TaskRunner* task_runner) override {
73     producer_.reset(new FakeProducer("android.perfetto.FakeProducer"));
74     producer_->Connect(producer_socket_.c_str(), task_runner,
75                        std::move(connect_callback_));
76   }
77 
78   FakeProducer* producer() { return producer_.get(); }
79 
80  private:
81   std::string producer_socket_;
82   std::unique_ptr<FakeProducer> producer_;
83   std::function<void()> connect_callback_;
84 };
85 }  // namespace perfetto
86 
87 #endif  // TEST_TASK_RUNNER_THREAD_DELEGATES_H_
88