• 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 #include "package.h"
16 #include <string>
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "bundle_mgr_host.h"
21 #include "bundle_mgr_interface.h"
22 #include "if_system_ability_manager.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 
28 #include "securec.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 using namespace OHOS;
34 using namespace OHOS::AAFwk;
35 using namespace OHOS::AppExecFwk;
36 namespace {
37 constexpr size_t ARGS_SIZE_ONE = 1;
38 constexpr size_t ARGS_SIZE_TWO = 2;
39 constexpr int32_t PARAM0 = 0;
40 constexpr int32_t INVALID_PARAM = 2;
41 constexpr int32_t INVALID_NUMBER = 202;
42 }
43 
~CheckPackageHasInstalledOptions()44 CheckPackageHasInstalledOptions::~CheckPackageHasInstalledOptions()
45 {
46     if (jsSuccessRef) {
47         APP_LOGD("CheckPackageHasInstalledOptions::~CheckPackageHasInstalledOptions delete successRef");
48         jsSuccessRef = nullptr;
49     }
50     if (jsFailRef) {
51         APP_LOGD("CheckPackageHasInstalledOptions::~CheckPackageHasInstalledOptions delete failRef");
52         jsFailRef = nullptr;
53     }
54     if (jsCompleteRef) {
55         APP_LOGD("CheckPackageHasInstalledOptions::~CheckPackageHasInstalledOptions delete completeRef");
56         jsCompleteRef = nullptr;
57     }
58 }
59 
GetBundleMgr()60 static OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr()
61 {
62     auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63     if (systemAbilityManager == nullptr) {
64         APP_LOGE("GetBundleMgr GetSystemAbilityManager is null");
65         return nullptr;
66     }
67     auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
68     if (bundleMgrSa == nullptr) {
69         APP_LOGE("GetBundleMgr GetSystemAbility is null");
70         return nullptr;
71     }
72     auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
73     if (bundleMgr == nullptr) {
74         APP_LOGE("GetBundleMgr iface_cast get null");
75     }
76     return bundleMgr;
77 }
78 
InnerHasInstalled(std::string bundleName)79 static bool InnerHasInstalled(std::string bundleName)
80 {
81     if (bundleName.empty()) {
82         APP_LOGE("bundleName is invalid param");
83         return false;
84     }
85     auto iBundleMgr = GetBundleMgr();
86     if (iBundleMgr == nullptr) {
87         APP_LOGE("can not get iBundleMgr");
88         return false;
89     }
90     BundleInfo bundleInfo;
91     bool ret = iBundleMgr->GetBundleInfo(bundleName, 0, bundleInfo);
92     if (!ret) {
93         APP_LOGE("bundleInfo is not find, bundleName=%{public}s.", bundleName.c_str());
94     }
95     return ret;
96 }
97 
Finalizer(NativeEngine * engine,void * data,void * hint)98 void JsPackage::Finalizer(NativeEngine *engine, void *data, void *hint)
99 {
100     APP_LOGE("JsPackage::Finalizer is called");
101     std::unique_ptr<JsPackage>(static_cast<JsPackage*>(data));
102 }
103 
HasInstalled(NativeEngine * engine,NativeCallbackInfo * info)104 NativeValue* JsPackage::HasInstalled(NativeEngine *engine, NativeCallbackInfo *info)
105 {
106     JsPackage* me = OHOS::AbilityRuntime::CheckParamsAndGetThis<JsPackage>(engine, info);
107     return (me != nullptr) ? me->OnHasInstalled(*engine, *info) : nullptr;
108 }
109 
JsParseCheckPackageHasInstalledOptions(NativeEngine & engine,const NativeCallbackInfo & info,std::shared_ptr<CheckPackageHasInstalledOptions> hasInstalledOptions)110 void JsPackage::JsParseCheckPackageHasInstalledOptions(NativeEngine &engine, const NativeCallbackInfo &info,
111     std::shared_ptr<CheckPackageHasInstalledOptions> hasInstalledOptions)
112 {
113     if (hasInstalledOptions == nullptr) {
114         APP_LOGE("hasInstalledOptions is nullptr");
115         return;
116     }
117 
118     auto param = info.argv[0];
119     if (param == nullptr) {
120         APP_LOGI("param is nullptr");
121         return;
122     }
123 
124     NativeObject *object = AbilityRuntime::ConvertNativeValueTo<NativeObject>(param);
125     NativeValue *jsBundleName = object->GetProperty("bundleName");
126     if (jsBundleName->TypeOf() == NATIVE_STRING) {
127         if (!AbilityRuntime::ConvertFromJsValue(engine, jsBundleName, hasInstalledOptions->bundleName)) {
128             APP_LOGI("Convert the Js value error.");
129             return;
130         }
131         hasInstalledOptions->isString = true;
132     } else {
133         hasInstalledOptions->isString = false;
134     }
135 
136     NativeValue *jsFunction = nullptr;
137     jsFunction = object->GetProperty("success");
138     if (jsFunction->IsCallable()) {
139         hasInstalledOptions->jsSuccessRef.reset(engine.CreateReference(jsFunction, 1));
140     }
141 
142     jsFunction = object->GetProperty("fail");
143     if (jsFunction->IsCallable()) {
144         hasInstalledOptions->jsFailRef.reset(engine.CreateReference(jsFunction, 1));
145     }
146 
147     jsFunction = object->GetProperty("complete");
148     if (jsFunction->IsCallable()) {
149         hasInstalledOptions->jsCompleteRef.reset(engine.CreateReference(jsFunction, 1));
150     }
151 }
152 
OnHasInstalled(NativeEngine & engine,NativeCallbackInfo & info)153 NativeValue* JsPackage::OnHasInstalled(NativeEngine &engine, NativeCallbackInfo &info)
154 {
155     APP_LOGI("%{public}s called.", __func__);
156     int32_t errCode = 0;
157 
158     std::shared_ptr<CheckPackageHasInstalledOptions> asyncCallbackInfo =
159         std::make_shared<CheckPackageHasInstalledOptions>();
160     if (info.argc < ARGS_SIZE_ONE || info.argc > ARGS_SIZE_TWO) {
161         APP_LOGI("input params is not object!");
162         return engine.CreateUndefined();
163     }
164 
165     if (info.argv[PARAM0]->TypeOf() == NATIVE_OBJECT) {
166         JsParseCheckPackageHasInstalledOptions(engine, info, asyncCallbackInfo);
167     } else {
168         errCode = INVALID_PARAM;
169     }
170 
171     if (!errCode && asyncCallbackInfo->isString && asyncCallbackInfo->jsSuccessRef) {
172         asyncCallbackInfo->response.result = InnerHasInstalled(asyncCallbackInfo->bundleName);
173     }
174 
175     if (!asyncCallbackInfo->isString) {
176         if (asyncCallbackInfo->jsFailRef) {
177             std::string data = "value is not an available number";
178             NativeValue *args[] = {AbilityRuntime::CreateJsValue(engine, data),
179                 AbilityRuntime::CreateJsValue(engine, INVALID_NUMBER)};
180             NativeValue *value = asyncCallbackInfo->jsFailRef->Get();
181             NativeValue *callback = asyncCallbackInfo->jsFailRef->Get();
182             engine.CallFunction(value, callback, args, ARGS_SIZE_TWO);
183         }
184     } else {
185         if (asyncCallbackInfo->jsSuccessRef) {
186             NativeValue *objValue = engine.CreateObject();
187             NativeObject *object = AbilityRuntime::ConvertNativeValueTo<NativeObject>(objValue);
188             object->SetProperty("result", AbilityRuntime::CreateJsValue(engine, asyncCallbackInfo->response.result));
189 
190             NativeValue *args[] = {objValue};
191             NativeValue *value = asyncCallbackInfo->jsSuccessRef->Get();
192             NativeValue *callback = asyncCallbackInfo->jsSuccessRef->Get();
193             engine.CallFunction(value, callback, args, ARGS_SIZE_ONE);
194         }
195     }
196     if (asyncCallbackInfo->jsCompleteRef) {
197         NativeValue *args[] = {engine.CreateUndefined()};
198         NativeValue *value = asyncCallbackInfo->jsCompleteRef->Get();
199         NativeValue *callback = asyncCallbackInfo->jsCompleteRef->Get();
200         engine.CallFunction(value, callback, args, ARGS_SIZE_ONE);
201     }
202     return engine.CreateUndefined();
203 }
204 }  // namespace AppExecFwk
205 }  // namespace OHOS