Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 19 | #include "ipc/host.h" |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 20 | #include "perfetto_base/logging.h" |
| 21 | #include "perfetto_base/task_runner.h" |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 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 | |
| 26 | namespace 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. |
| 32 | std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance( |
| 33 | base::TaskRunner* task_runner) { |
| 34 | return std::unique_ptr<ServiceIPCHost>(new ServiceIPCHostImpl(task_runner)); |
| 35 | } |
| 36 | |
| 37 | ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner) |
| 38 | : task_runner_(task_runner) {} |
| 39 | |
| 40 | ServiceIPCHostImpl::~ServiceIPCHostImpl() {} |
| 41 | |
| 42 | bool 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 | |
| 64 | Service* ServiceIPCHostImpl::service_for_testing() const { |
| 65 | return svc_.get(); |
| 66 | } |
| 67 | |
| 68 | // Definitions for the base class ctor/dtor. |
| 69 | ServiceIPCHost::ServiceIPCHost() = default; |
| 70 | ServiceIPCHost::~ServiceIPCHost() = default; |
| 71 | |
| 72 | } // namespace perfetto |