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