1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <binder/IServiceManager.h>
20 
21 #include <map>
22 #include <mutex>
23 #include <optional>
24 #include <vector>
25 
26 namespace android {
27 
28 /**
29  * A local host simple implementation of IServiceManager, that does not
30  * communicate over binder.
31 */
32 class FakeServiceManager : public IServiceManager {
33 public:
34     FakeServiceManager();
35 
36     sp<IBinder> getService( const String16& name) const override;
37 
38     sp<IBinder> checkService( const String16& name) const override;
39 
40     status_t addService(const String16& name, const sp<IBinder>& service,
41                         bool allowIsolated = false,
42                         int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT) override;
43 
44     Vector<String16> listServices(int dumpsysFlags = 0) override;
45 
46     IBinder* onAsBinder() override;
47 
48     sp<IBinder> waitForService(const String16& name) override;
49 
50     bool isDeclared(const String16& name) override;
51 
52     Vector<String16> getDeclaredInstances(const String16& iface) override;
53 
54     std::optional<String16> updatableViaApex(const String16& name) override;
55 
56     Vector<String16> getUpdatableNames(const String16& apexName) override;
57 
58     std::optional<IServiceManager::ConnectionInfo> getConnectionInfo(const String16& name) override;
59 
60     status_t registerForNotifications(const String16& name,
61                                       const sp<LocalRegistrationCallback>& callback) override;
62 
63     status_t unregisterForNotifications(const String16& name,
64                                         const sp<LocalRegistrationCallback>& callback) override;
65 
66     std::vector<IServiceManager::ServiceDebugInfo> getServiceDebugInfo() override;
67 
enableAddServiceCache(bool)68     void enableAddServiceCache(bool /*value*/) override {}
69     // Clear all of the registered services
70     void clear();
71 
72 private:
73     mutable std::mutex mMutex;
74     std::map<String16, sp<IBinder>> mNameToService;
75 };
76 
77 }  // namespace android
78