• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "js_enterprise_admin_extension.h"
17 
18 #include <string>
19 
20 #include "ability_handler.h"
21 #include "edm_log.h"
22 #include "enterprise_admin_extension.h"
23 #include "enterprise_admin_stub_impl.h"
24 #include "js_enterprise_admin_extension_context.h"
25 #include "js_runtime.h"
26 #include "js_runtime_utils.h"
27 #include "runtime.h"
28 
29 namespace OHOS {
30 namespace EDM {
31 constexpr size_t JS_NAPI_ARGC_ZERO = 0;
32 constexpr size_t JS_NAPI_ARGC_ONE = 1;
33 
Create(const std::unique_ptr<AbilityRuntime::Runtime> & runtime)34 JsEnterpriseAdminExtension* JsEnterpriseAdminExtension::Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime)
35 {
36     return new JsEnterpriseAdminExtension(static_cast<AbilityRuntime::JsRuntime&>(*runtime));
37 }
38 
JsEnterpriseAdminExtension(AbilityRuntime::JsRuntime & jsRuntime)39 JsEnterpriseAdminExtension::JsEnterpriseAdminExtension(AbilityRuntime::JsRuntime& jsRuntime) : jsRuntime_(jsRuntime) {}
~JsEnterpriseAdminExtension()40 JsEnterpriseAdminExtension::~JsEnterpriseAdminExtension()
41 {
42     EDMLOGD("Js enterprise admin extension destructor.");
43     jsRuntime_.FreeNativeReference(std::move(jsObj_));
44 }
45 
Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> & record,const std::shared_ptr<AppExecFwk::OHOSApplication> & application,std::shared_ptr<AppExecFwk::AbilityHandler> & handler,const sptr<IRemoteObject> & token)46 void JsEnterpriseAdminExtension::Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>& record,
47     const std::shared_ptr<AppExecFwk::OHOSApplication>& application,
48     std::shared_ptr<AppExecFwk::AbilityHandler>& handler,
49     const sptr<IRemoteObject>& token)
50 {
51     EnterpriseAdminExtension::Init(record, application, handler, token);
52     std::string srcPath;
53     GetSrcPath(srcPath);
54     if (srcPath.empty()) {
55         EDMLOGI("JsEnterpriseAdminExtension Failed to get srcPath");
56         return;
57     }
58 
59     std::string moduleName(Extension::abilityInfo_->moduleName);
60     moduleName.append("::").append(abilityInfo_->name);
61     EDMLOGI("JsEnterpriseAdminExtension::Init moduleName:%{public}s,srcPath:%{public}s.",
62         moduleName.c_str(), srcPath.c_str());
63     AbilityRuntime::HandleScope handleScope(jsRuntime_);
64 
65     jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->hapPath,
66         Extension::abilityInfo_->compileMode == AbilityRuntime::CompileMode::ES_MODULE);
67     if (jsObj_ == nullptr) {
68         EDMLOGI("JsEnterpriseAdminExtension Failed to get jsObj_");
69         return;
70     }
71     EDMLOGI("JsEnterpriseAdminExtension::Init ConvertNativeValueTo.");
72     JsEnterpriseAdminExtensionContextInit();
73 }
74 
JsEnterpriseAdminExtensionContextInit()75 void JsEnterpriseAdminExtension::JsEnterpriseAdminExtensionContextInit()
76 {
77     napi_value obj = jsObj_->GetNapiValue();
78 
79     auto env = jsRuntime_.GetNapiEnv();
80     auto context = GetContext();
81     if (context == nullptr) {
82         EDMLOGI("JsEnterpriseAdminExtension Failed to get context");
83         return;
84     }
85     EDMLOGI("JsEnterpriseAdminExtension::Init CreateJsEnterpriseAdminExtensionContext.");
86     napi_value contextObj = CreateJsEnterpriseAdminExtensionContext(env, context);
87     auto shellContextRef = jsRuntime_.LoadSystemModule("enterprise.EnterpriseAdminExtensionContext",
88         &contextObj, JS_NAPI_ARGC_ONE);
89     contextObj = shellContextRef->GetNapiValue();
90     EDMLOGI("JsEnterpriseAdminExtension::Init Bind.");
91     context->Bind(jsRuntime_, shellContextRef.release());
92     EDMLOGI("JsEnterpriseAdminExtension::SetProperty.");
93     napi_set_named_property(env, obj, "context", contextObj);
94 
95     EDMLOGI("Set enterprise admin extension context");
96 
97     napi_wrap(env, contextObj, new std::weak_ptr<AbilityRuntime::Context>(context),
98         [](napi_env env, void* data, void*) {
99             EDMLOGI("Finalizer for weak_ptr service extension context is called");
100             delete static_cast<std::weak_ptr<AbilityRuntime::Context>*>(data);
101         }, nullptr, nullptr);
102 
103     EDMLOGI("JsEnterpriseAdminExtension::Init end.");
104 }
105 
OnStart(const AAFwk::Want & want)106 void JsEnterpriseAdminExtension::OnStart(const AAFwk::Want& want)
107 {
108     EDMLOGI("JsEnterpriseAdminExtension OnStart begin");
109     AbilityRuntime::Extension::OnStart(want);
110     auto task = [this]() {
111         CallObjectMethod("onStart", nullptr, JS_NAPI_ARGC_ZERO);
112     };
113     handler_->PostTask(task);
114 }
115 
OnStop()116 void JsEnterpriseAdminExtension::OnStop()
117 {
118     AbilityRuntime::Extension::OnStop();
119     EDMLOGI("JsEnterpriseAdminExtension %{public}s end.", __func__);
120 }
121 
OnConnect(const AAFwk::Want & want)122 sptr<IRemoteObject> JsEnterpriseAdminExtension::OnConnect(const AAFwk::Want& want)
123 {
124     AbilityRuntime::Extension::OnConnect(want);
125     EDMLOGI("EnterpriseAdminExtension %{public}s begin.", __func__);
126     sptr<EnterpriseAdminStubImpl> remoteObject = new (std::nothrow) EnterpriseAdminStubImpl(
127         std::static_pointer_cast<JsEnterpriseAdminExtension>(shared_from_this()));
128 
129     if (remoteObject == nullptr) {
130         EDMLOGI("OnConnect get null");
131         return remoteObject;
132     }
133     EDMLOGI("JsEnterpriseAdminExtension %{public}s end. ", __func__);
134     return remoteObject->AsObject();
135 }
136 
OnDisconnect(const AAFwk::Want & want)137 void JsEnterpriseAdminExtension::OnDisconnect(const AAFwk::Want& want)
138 {
139     EDMLOGI("JsEnterpriseAdminExtension %{public}s begin.", __func__);
140     AbilityRuntime::Extension::OnDisconnect(want);
141 }
142 
OnAdminEnabled()143 void JsEnterpriseAdminExtension::OnAdminEnabled()
144 {
145     EDMLOGI("JsEnterpriseAdminExtension::OnAdminEnabled");
146     auto task = [this]() {
147         CallObjectMethod("onAdminEnabled", nullptr, JS_NAPI_ARGC_ZERO);
148     };
149     handler_->PostTask(task);
150 }
151 
OnAdminDisabled()152 void JsEnterpriseAdminExtension::OnAdminDisabled()
153 {
154     EDMLOGI("JsEnterpriseAdminExtension::OnAdminDisabled");
155     auto task = [this]() {
156         CallObjectMethod("onAdminDisabled", nullptr, JS_NAPI_ARGC_ZERO);
157     };
158     handler_->PostTask(task);
159 }
160 
OnBundleAdded(const std::string & bundleName)161 void JsEnterpriseAdminExtension::OnBundleAdded(const std::string &bundleName)
162 {
163     EDMLOGI("JsEnterpriseAdminExtension::OnBundleAdded");
164     auto task = [bundleName, this]() {
165         auto env = jsRuntime_.GetNapiEnv();
166         napi_value argv[] = { AbilityRuntime::CreateJsValue(env, bundleName) };
167         CallObjectMethod("onBundleAdded", argv, JS_NAPI_ARGC_ONE);
168     };
169     handler_->PostTask(task);
170 }
171 
OnBundleRemoved(const std::string & bundleName)172 void JsEnterpriseAdminExtension::OnBundleRemoved(const std::string &bundleName)
173 {
174     EDMLOGI("JsEnterpriseAdminExtension::OnBundleRemoved");
175     auto task = [bundleName, this]() {
176         auto env = jsRuntime_.GetNapiEnv();
177         napi_value argv[] = { AbilityRuntime::CreateJsValue(env, bundleName) };
178         CallObjectMethod("onBundleRemoved", argv, JS_NAPI_ARGC_ONE);
179     };
180     handler_->PostTask(task);
181 }
182 
OnAppStart(const std::string & bundleName)183 void JsEnterpriseAdminExtension::OnAppStart(const std::string &bundleName)
184 {
185     EDMLOGI("JsEnterpriseAdminExtension::OnAppStart");
186     auto task = [bundleName, this]() {
187         auto env = jsRuntime_.GetNapiEnv();
188         napi_value argv[] = { AbilityRuntime::CreateJsValue(env, bundleName) };
189         CallObjectMethod("onAppStart", argv, JS_NAPI_ARGC_ONE);
190     };
191     handler_->PostTask(task);
192 }
193 
OnAppStop(const std::string & bundleName)194 void JsEnterpriseAdminExtension::OnAppStop(const std::string &bundleName)
195 {
196     EDMLOGI("JsEnterpriseAdminExtension::OnAppStop");
197     auto task = [bundleName, this]() {
198         auto env = jsRuntime_.GetNapiEnv();
199         napi_value argv[] = { AbilityRuntime::CreateJsValue(env, bundleName) };
200         CallObjectMethod("onAppStop", argv, JS_NAPI_ARGC_ONE);
201     };
202     handler_->PostTask(task);
203 }
204 
OnSystemUpdate(const UpdateInfo & updateInfo)205 void JsEnterpriseAdminExtension::OnSystemUpdate(const UpdateInfo &updateInfo)
206 {
207     EDMLOGI("JsEnterpriseAdminExtension::OnSystemUpdate");
208     auto task = [updateInfo, this]() {
209         auto env = jsRuntime_.GetNapiEnv();
210         napi_value argv[] = { CreateUpdateInfoObject(env, updateInfo) };
211         CallObjectMethod("onSystemUpdate", argv, JS_NAPI_ARGC_ONE);
212     };
213     handler_->PostTask(task);
214 }
215 
CreateUpdateInfoObject(napi_env env,const UpdateInfo & updateInfo)216 napi_value JsEnterpriseAdminExtension::CreateUpdateInfoObject(napi_env env, const UpdateInfo &updateInfo)
217 {
218     napi_value nSystemUpdateInfo = nullptr;
219     NAPI_CALL(env, napi_create_object(env, &nSystemUpdateInfo));
220 
221     napi_value nVersionName = nullptr;
222     NAPI_CALL(env, napi_create_string_utf8(env, updateInfo.version.c_str(), NAPI_AUTO_LENGTH, &nVersionName));
223     NAPI_CALL(env, napi_set_named_property(env, nSystemUpdateInfo, "versionName", nVersionName));
224 
225     napi_value nFirstReceivedTime = nullptr;
226     NAPI_CALL(env, napi_create_int64(env, updateInfo.firstReceivedTime, &nFirstReceivedTime));
227     NAPI_CALL(env, napi_set_named_property(env, nSystemUpdateInfo, "firstReceivedTime", nFirstReceivedTime));
228 
229     napi_value nPackageType = nullptr;
230     NAPI_CALL(env, napi_create_string_utf8(env, updateInfo.packageType.c_str(), NAPI_AUTO_LENGTH, &nPackageType));
231     NAPI_CALL(env, napi_set_named_property(env, nSystemUpdateInfo, "packageType", nPackageType));
232 
233     return nSystemUpdateInfo;
234 }
235 
CallObjectMethod(const char * name,napi_value * argv,size_t argc)236 napi_value JsEnterpriseAdminExtension::CallObjectMethod(const char* name, napi_value* argv, size_t argc)
237 {
238     EDMLOGI("JsEnterpriseAdminExtension::CallObjectMethod(%{public}s), begin", name);
239 
240     if (!jsObj_) {
241         EDMLOGW("Not found EnterpriseAdminExtension.js");
242         return nullptr;
243     }
244 
245     AbilityRuntime::HandleScope handleScope(jsRuntime_);
246     auto env = jsRuntime_.GetNapiEnv();
247 
248     napi_value value = jsObj_->GetNapiValue();
249     if (value == nullptr) {
250         EDMLOGE("Failed to get EnterpriseAdminExtension object");
251         return nullptr;
252     }
253     napi_value method = nullptr;
254     napi_get_named_property(env, value, name, &method);
255 
256     if (method == nullptr) {
257         EDMLOGE("Failed to get '%{public}s' from EnterpriseAdminExtension object", name);
258         return nullptr;
259     }
260     napi_valuetype valueType = napi_undefined;
261     napi_typeof(env, method, &valueType);
262     if (valueType != napi_function) {
263         EDMLOGE("'%{public}s' is not function", name);
264         return nullptr;
265     }
266 
267     EDMLOGI("JsEnterpriseAdminExtension::CallFunction(%{public}s), success", name);
268     napi_value result = nullptr;
269     napi_status status = napi_call_function(env, value, method, argc, argv, &result);
270     if (status != napi_ok) {
271         EDMLOGE("Failed to call function");
272     }
273     return result;
274 }
275 
GetSrcPath(std::string & srcPath)276 void JsEnterpriseAdminExtension::GetSrcPath(std::string& srcPath)
277 {
278     if (!Extension::abilityInfo_->srcEntrance.empty()) {
279         srcPath.append(Extension::abilityInfo_->moduleName + "/");
280         srcPath.append(Extension::abilityInfo_->srcEntrance);
281         srcPath.erase(srcPath.rfind('.'));
282         srcPath.append(".abc");
283     }
284 }
285 } // namespace EDM
286 } // namespace OHOS