• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "edm_sys_manager_mock.h"
17 #include "gmock/gmock.h"
18 #include "edm_log.h"
19 #include "iservice_registry.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 std::mutex EdmSysManager::saMutex_;
24 std::unordered_map<int32_t, sptr<IRemoteObject>> EdmSysManager::remoteServiceMap_;
GetRemoteObjectOfSystemAbility(int32_t systemAbilityId)25 sptr<IRemoteObject> EdmSysManager::GetRemoteObjectOfSystemAbility(int32_t systemAbilityId)
26 {
27     std::lock_guard<std::mutex> lock(saMutex_);
28     GTEST_LOG_(INFO) << "mock EdmSysMgrManager GetRemoteObjectOfSystemAbility start remoteServiceMap_ is nullptr :" <<
29         (remoteServiceMap_[systemAbilityId] == nullptr);
30     if (remoteServiceMap_[systemAbilityId] == nullptr) {
31         auto sysAbilityMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32         if (sysAbilityMgr == nullptr) {
33             GTEST_LOG_(INFO) << "mock EdmSysMgrManager GetRemoteObjectOfSystemAbility get system ability manager fail.";
34             return nullptr;
35         }
36 
37         auto object = sysAbilityMgr->GetSystemAbility(systemAbilityId);
38         if (object == nullptr) {
39             GTEST_LOG_(INFO) << "mock EdmSysMgrManager GetRemoteObjectOfSystemAbility get system ability fail.";
40             return nullptr;
41         }
42         remoteServiceMap_[systemAbilityId] = object;
43     }
44     return remoteServiceMap_[systemAbilityId];
45 }
46 
RegisterSystemAbilityOfRemoteObject(int32_t systemAbilityId,sptr<IRemoteObject> object)47 void EdmSysManager::RegisterSystemAbilityOfRemoteObject(int32_t systemAbilityId, sptr<IRemoteObject> object)
48 {
49     std::lock_guard<std::mutex> lock(saMutex_);
50     remoteServiceMap_[systemAbilityId] = object;
51 }
52 
UnregisterSystemAbilityOfRemoteObject(int32_t systemAbilityId)53 void EdmSysManager::UnregisterSystemAbilityOfRemoteObject(int32_t systemAbilityId)
54 {
55     std::lock_guard<std::mutex> lock(saMutex_);
56     remoteServiceMap_.erase(systemAbilityId);
57 }
58 } // namespace EDM
59 } // namespace OHOS
60