blob: d11f5d5b69752379ca109e24bb10fbca4b466ec7 [file] [log] [blame]
Primiano Tuccia6166482017-11-20 13:05:45 +00001/*
2 * Copyright (C) 2017 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#include "tracing/src/ipc/service/service_ipc_host_impl.h"
18
19#include "base/logging.h"
20#include "base/task_runner.h"
21#include "ipc/host.h"
22#include "tracing/core/service.h"
23#include "tracing/src/ipc/posix_shared_memory.h"
24#include "tracing/src/ipc/service/producer_ipc_service.h"
25
26namespace perfetto {
27
28// TODO: implement per-uid connection limit (b/69093705).
29
30// Implements the publicly exposed factory method declared in
31// include/tracing/posix_ipc/posix_service_host.h.
32std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
33 base::TaskRunner* task_runner) {
34 return std::unique_ptr<ServiceIPCHost>(new ServiceIPCHostImpl(task_runner));
35}
36
37ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner)
38 : task_runner_(task_runner) {}
39
40ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
41
42bool ServiceIPCHostImpl::Start(const char* producer_socket_name) {
43 PERFETTO_CHECK(!svc_); // Check if already started.
44
45 // Create and initialize the platform-independent tracing business logic.
46 std::unique_ptr<SharedMemory::Factory> shm_factory(
47 new PosixSharedMemory::Factory());
48 svc_ = Service::CreateInstance(std::move(shm_factory), task_runner_);
49
50 // Initialize the IPC transport.
51 producer_ipc_port_ =
52 ipc::Host::CreateInstance(producer_socket_name, task_runner_);
53 if (!producer_ipc_port_)
54 return false;
55
56 // TODO: add a test that destroyes the ServiceIPCHostImpl soon after Start()
57 // and checks that no spurious callbacks are issued.
58 bool producer_service_exposed = producer_ipc_port_->ExposeService(
59 std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
60 PERFETTO_CHECK(producer_service_exposed);
61 return true;
62}
63
64Service* ServiceIPCHostImpl::service_for_testing() const {
65 return svc_.get();
66}
67
68// Definitions for the base class ctor/dtor.
69ServiceIPCHost::ServiceIPCHost() = default;
70ServiceIPCHost::~ServiceIPCHost() = default;
71
72} // namespace perfetto