• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef SERVICE_REGISTRY_INCLUDE_H
17 #define SERVICE_REGISTRY_INCLUDE_H
18 
19 #include "mock_system_ability_manager.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 
24 class SystemAbilityManagerClient {
25 public:
26     static bool nullptrFlag;
27 
28     static SystemAbilityManagerClient& GetInstance();
29 
30     /**
31      * get system ability manager.
32      */
33     sptr<MockSystemAbilityManager> GetSystemAbilityManager();
34 
35 private:
36     SystemAbilityManagerClient() = default;
37     ~SystemAbilityManagerClient() = default;
38 
39     sptr<MockSystemAbilityManager> systemAbilityManager_;
40     std::mutex systemAbilityManagerLock_;
41 };
42 
43 bool SystemAbilityManagerClient::nullptrFlag = false;
44 
GetInstance()45 SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance()
46 {
47     static auto instance = new SystemAbilityManagerClient();
48     return *instance;
49 }
50 
GetSystemAbilityManager()51 sptr<MockSystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
52 {
53     std::lock_guard<std::mutex> lock(systemAbilityManagerLock_);
54     if (nullptrFlag) {
55         return nullptr;
56     }
57     if (systemAbilityManager_ != nullptr) {
58         return systemAbilityManager_;
59     }
60     systemAbilityManager_ = new MockSystemAbilityManager();
61     return systemAbilityManager_;
62 }
63 }  // namespace AAFwk
64 }  // namespace OHOS
65 #endif // SERVICE_REGISTRY_INCLUDE_H