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 "mem_mgr_client.h"
17 #include "memmgr_log.h"
18
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 namespace OHOS {
24 namespace Memory {
25 namespace {
26 const std::string TAG = "MemMgrClient";
27 }
28
29 IMPLEMENT_SINGLE_INSTANCE(MemMgrClient);
30
GetBundlePriorityList(BundlePriorityList & bundlePrioList)31 int32_t MemMgrClient::GetBundlePriorityList(BundlePriorityList &bundlePrioList)
32 {
33 HILOGE("called");
34 auto dps = GetMemMgrService();
35 if (dps == nullptr) {
36 HILOGE("MemMgrService is null");
37 return -1;
38 }
39 return dps->GetBundlePriorityList(bundlePrioList);
40 }
41
NotifyDistDevStatus(int32_t pid,int32_t uid,const std::string & name,bool connected)42 int32_t MemMgrClient::NotifyDistDevStatus(int32_t pid, int32_t uid, const std::string &name, bool connected)
43 {
44 HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
45 connected);
46 auto dps = GetMemMgrService();
47 if (dps == nullptr) {
48 HILOGE("MemMgrService is null");
49 return -1;
50 }
51 return dps->NotifyDistDevStatus(pid, uid, name, connected);
52 }
53
54
GetMemMgrService()55 sptr<IMemMgr> MemMgrClient::GetMemMgrService()
56 {
57 HILOGI("called");
58 std::lock_guard<std::mutex> lock(mutex_);
59 if (dpProxy_ != nullptr) {
60 return dpProxy_;
61 }
62
63 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (samgrProxy == nullptr) {
65 HILOGE("get samgr failed");
66 return nullptr;
67 }
68 auto object = samgrProxy->GetSystemAbility(MEMORY_MANAGER_SA_ID);
69 if (object == nullptr) {
70 HILOGE("get service failed");
71 return nullptr;
72 }
73 HILOGI("get service succeed");
74 dpProxy_ = iface_cast<IMemMgr>(object);
75 return dpProxy_;
76 }
77 } // namespace Memory
78 } // namespace OHOS
79