/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include "AccessControl.h" #include "HidlService.h" namespace android { namespace hidl { namespace manager { namespace implementation { using ::android::hardware::hidl_death_recipient; using ::android::hardware::hidl_vec; using ::android::hardware::hidl_string; using ::android::hardware::Return; using ::android::hardware::Void; using ::android::hidl::base::V1_0::IBase; using ::android::hidl::manager::V1_0::IServiceNotification; using ::android::hidl::manager::V1_2::IClientCallback; using ::android::sp; using ::android::wp; struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient { // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. Return> get(const hidl_string& fqName, const hidl_string& name) override; Return add(const hidl_string& name, const sp& service) override; Return getTransport(const hidl_string& fqName, const hidl_string& name); Return list(list_cb _hidl_cb) override; Return listByInterface(const hidl_string& fqInstanceName, listByInterface_cb _hidl_cb) override; Return registerForNotifications(const hidl_string& fqName, const hidl_string& name, const sp& callback) override; Return debugDump(debugDump_cb _cb) override; Return registerPassthroughClient(const hidl_string &fqName, const hidl_string &name) override; // Methods from ::android::hidl::manager::V1_1::IServiceManager follow. Return unregisterForNotifications(const hidl_string& fqName, const hidl_string& name, const sp& callback) override; // Methods from ::android::hidl::manager::V1_2::IServiceManager follow. Return registerClientCallback(const hidl_string& fqName, const hidl_string& name, const sp& server, const sp& cb) override; Return unregisterClientCallback(const sp& server, const sp& cb) override; Return addWithChain(const hidl_string& name, const sp& service, const hidl_vec& chain) override; Return listManifestByInterface(const hidl_string& fqInstanceName, listManifestByInterface_cb _hidl_cb) override; Return tryUnregister(const hidl_string& fqName, const hidl_string& name, const sp& service) override; void handleClientCallbacks(); virtual void serviceDied(uint64_t cookie, const wp& who); private: bool addImpl(const std::string& name, const sp& service, const hidl_vec& interfaceChain, const AccessControl::CallingContext& callingContext); // if restrictToInstanceName is nullptr, remove all, otherwise only those services // which match this instance name. Returns whether all instances were removed. bool removeService(const wp& who, const std::string* restrictToInstanceName); bool removePackageListener(const wp& who); bool removeServiceListener(const wp& who); size_t countExistingService() const; // true = continue, false = break void forEachExistingService(std::function f) const; void forEachExistingService(std::function f); void forEachServiceEntry(std::function f) const; void forEachServiceEntry(std::function f); HidlService* lookup(const std::string& fqName, const std::string& name); using InstanceMap = std::map< std::string, // instance name e.x. "manager" std::unique_ptr >; struct PackageInterfaceMap { InstanceMap &getInstanceMap(); const InstanceMap &getInstanceMap() const; /** * Finds a HidlService with the desired name. If none, * returns nullptr. HidlService::getService() might also be nullptr * if there are registered IServiceNotification objects for it. Return * value should be treated as a temporary reference. */ HidlService *lookup( const std::string &name); const HidlService *lookup( const std::string &name) const; void insertService(std::unique_ptr &&service); void addPackageListener(sp listener); bool removePackageListener(const wp& who); bool removeServiceListener(const wp& who); void sendPackageRegistrationNotification( const hidl_string &fqName, const hidl_string &instanceName); private: InstanceMap mInstanceMap{}; std::vector> mPackageListeners{}; }; AccessControl mAcl; /** * Access to this map doesn't need to be locked, since hwservicemanager * is single-threaded. * * e.x. * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"] * -> HidlService object */ std::map< std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager" PackageInterfaceMap > mServiceMap; }; } // namespace implementation } // namespace manager } // namespace hidl } // namespace android