• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_wallpaper_extension.h"
17 
18 #include "ability_info.h"
19 #include "hilog_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "js_runtime.h"
22 #include "js_runtime_utils.h"
23 #include "js_wallpaper_extension_context.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "napi_common_util.h"
27 #include "napi_common_want.h"
28 #include "napi_remote_object.h"
29 #include "uv_queue.h"
30 #include "wallpaper_manager.h"
31 #include "wallpaper_extension_stub.h"
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 namespace {
36 constexpr size_t ARGC_ONE = 1;
37 }
38 struct WorkData {
39     NativeEngine *nativeEng_;
40     int wallpaperType_;
WorkDataOHOS::AbilityRuntime::WorkData41     WorkData(NativeEngine *nativeEng, int wallpaperType) : nativeEng_(nativeEng), wallpaperType_(wallpaperType)
42     {
43     }
44 };
45 
46 JsWallpaperExtension* JsWallpaperExtension::jsWallpaperExtension = NULL;
47 using namespace OHOS::AppExecFwk;
48 using namespace OHOS::MiscServices;
Create(const std::unique_ptr<Runtime> & runtime)49 JsWallpaperExtension* JsWallpaperExtension::Create(const std::unique_ptr<Runtime>& runtime)
50 {
51     HILOG_INFO("jws JsWallpaperExtension begin Create");
52     jsWallpaperExtension = new JsWallpaperExtension(static_cast<JsRuntime&>(*runtime));
53     return jsWallpaperExtension;
54 }
55 
JsWallpaperExtension(JsRuntime & jsRuntime)56 JsWallpaperExtension::JsWallpaperExtension(JsRuntime& jsRuntime) : jsRuntime_(jsRuntime) {}
~JsWallpaperExtension()57 JsWallpaperExtension::~JsWallpaperExtension()
58 {
59     jsRuntime_.FreeNativeReference(std::move(jsObj_));
60 }
61 
Init(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)62 void JsWallpaperExtension::Init(const std::shared_ptr<AbilityLocalRecord> &record,
63     const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
64     const sptr<IRemoteObject> &token)
65 {
66     HILOG_INFO("jws JsWallpaperExtension begin Init");
67     WallpaperExtension::Init(record, application, handler, token);
68     std::string srcPath = "";
69     GetSrcPath(srcPath);
70     if (srcPath.empty()) {
71         HILOG_ERROR("Failed to get srcPath");
72         return;
73     }
74 
75     std::string moduleName(Extension::abilityInfo_->moduleName);
76     moduleName.append("::").append(abilityInfo_->name);
77     HILOG_INFO("JsWallpaperExtension::Init module:%{public}s,srcPath:%{public}s.", moduleName.c_str(), srcPath.c_str());
78     HandleScope handleScope(jsRuntime_);
79     auto& engine = jsRuntime_.GetNativeEngine();
80 
81     jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->hapPath);
82     if (jsObj_ == nullptr) {
83         HILOG_ERROR("Failed to get jsObj_");
84         return;
85     }
86     HILOG_INFO("JsWallpaperExtension::Init ConvertNativeValueTo.");
87     NativeObject* obj = ConvertNativeValueTo<NativeObject>(jsObj_->Get());
88     if (obj == nullptr) {
89         HILOG_ERROR("Failed to get JsWallpaperExtension object");
90         return;
91     }
92 
93     auto context = GetContext();
94     if (context == nullptr) {
95         HILOG_ERROR("Failed to get context");
96         return;
97     }
98     HILOG_INFO("JsWallpaperExtension::Init CreateJsWallpaperExtensionContext.");
99     NativeValue* contextObj = CreateJsWallpaperExtensionContext(engine, context);
100     auto shellContextRef = jsRuntime_.LoadSystemModule("WallpaperExtensionContext", &contextObj, ARGC_ONE);
101     contextObj = shellContextRef->Get();
102     HILOG_INFO("JsWallpaperExtension::Init Bind.");
103     context->Bind(jsRuntime_, shellContextRef.release());
104     HILOG_INFO("JsWallpaperExtension::SetProperty.");
105     obj->SetProperty("context", contextObj);
106 
107     auto nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
108     if (nativeObj == nullptr) {
109         HILOG_ERROR("Failed to get wallpaper extension native object");
110         return;
111     }
112 
113     HILOG_INFO("Set wallpaper extension");
114 
115     nativeObj->SetNativePointer(new std::weak_ptr<AbilityRuntime::Context>(context),
116         [](NativeEngine*, void* data, void*) {
117             HILOG_INFO("Finalizer for weak_ptr wallpaper extension context is called");
118             delete static_cast<std::weak_ptr<AbilityRuntime::Context>*>(data);
119         }, nullptr);
120 
121     HILOG_INFO("JsWallpaperExtension::Init end.");
122 }
123 
OnStart(const AAFwk::Want & want)124 void JsWallpaperExtension::OnStart(const AAFwk::Want &want)
125 {
126     StartAsyncTrace(HITRACE_TAG_MISC, "OnStart", static_cast<int32_t>(TraceTaskId::ONSTART_EXTENSION));
127     StartAsyncTrace(
128         HITRACE_TAG_MISC, "Extension::OnStart", static_cast<int32_t>(TraceTaskId::ONSTART_MIDDLE_EXTENSION));
129     Extension::OnStart(want);
130     FinishAsyncTrace(
131         HITRACE_TAG_MISC, "Extension::OnStart", static_cast<int32_t>(TraceTaskId::ONSTART_MIDDLE_EXTENSION));
132     HILOG_INFO("jws JsWallpaperExtension OnStart begin..");
133     HandleScope handleScope(jsRuntime_);
134     NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
135     napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(nativeEngine), want);
136     NativeValue* nativeWant = reinterpret_cast<NativeValue*>(napiWant);
137     NativeValue* argv[] = {nativeWant};
138     StartAsyncTrace(HITRACE_TAG_MISC, "onCreated", static_cast<int32_t>(TraceTaskId::ONCREATE_EXTENSION));
139     CallObjectMethod("onCreated", argv, ARGC_ONE);
140     FinishAsyncTrace(HITRACE_TAG_MISC, "onCreated", static_cast<int32_t>(TraceTaskId::ONCREATE_EXTENSION));
141     CallObjectMethod("createWallpaperWin");
142     WallpaperMgrService::WallpaperManagerkits::GetInstance().RegisterWallpaperCallback([](int wallpaperType) -> bool {
143         HILOG_INFO("jsWallpaperExtension->CallObjectMethod");
144         NativeEngine *nativeEng = &(jsWallpaperExtension->jsRuntime_).GetNativeEngine();
145         WorkData *workData = new (std::nothrow) WorkData(nativeEng, wallpaperType);
146         if (workData == nullptr) {
147             return false;
148         }
149         uv_after_work_cb afterCallback = [](uv_work_t *work, int32_t status) {
150             WorkData *workData = reinterpret_cast<WorkData *>(work->data);
151             napi_value type = OHOS::AppExecFwk::WrapInt32ToJS(
152                 reinterpret_cast<napi_env>(workData->nativeEng_), workData->wallpaperType_);
153             NativeValue *nativeType = reinterpret_cast<NativeValue *>(type);
154             NativeValue *arg[] = { nativeType };
155             jsWallpaperExtension->CallObjectMethod("onWallpaperChanged", arg, ARGC_ONE);
156             delete workData;
157             delete work;
158         };
159         UvQueue::Call(reinterpret_cast<napi_env>(nativeEng), workData, afterCallback);
160         return true;
161     });
162     HILOG_INFO("%{public}s end.", __func__);
163     FinishAsyncTrace(HITRACE_TAG_MISC, "onCreated", static_cast<int32_t>(TraceTaskId::ONSTART_EXTENSION));
164 }
165 
OnStop()166 void JsWallpaperExtension::OnStop()
167 {
168     WallpaperExtension::OnStop();
169     HILOG_INFO("jws JsWallpaperExtension OnStop begin.");
170     CallObjectMethod("onDestroy");
171     bool ret = ConnectionManager::GetInstance().DisconnectCaller(GetContext()->GetToken());
172     if (ret) {
173         HILOG_INFO("The wallpaper extension connection is not disconnected.");
174     }
175     HILOG_INFO("%{public}s end.", __func__);
176 }
177 
OnConnect(const AAFwk::Want & want)178 sptr<IRemoteObject> JsWallpaperExtension::OnConnect(const AAFwk::Want &want)
179 {
180     HILOG_INFO("jws JsWallpaperExtension OnConnect begin.");
181     Extension::OnConnect(want);
182     auto remoteObj = new (std::nothrow) WallpaperMgrService::WallpaperExtensionStub();
183 
184     if (remoteObj == nullptr) {
185         HILOG_ERROR("failed to create IWallpaperExtension");
186         return nullptr;
187     }
188     return remoteObj;
189 }
190 
OnDisconnect(const AAFwk::Want & want)191 void JsWallpaperExtension::OnDisconnect(const AAFwk::Want &want)
192 {
193     HILOG_INFO("jws JsWallpaperExtension OnDisconnect begin.");
194     Extension::OnDisconnect(want);
195 }
196 
OnCommand(const AAFwk::Want & want,bool restart,int startId)197 void JsWallpaperExtension::OnCommand(const AAFwk::Want &want, bool restart, int startId)
198 {
199     HILOG_INFO("jws JsWallpaperExtension OnCommand begin.");
200     Extension::OnCommand(want, restart, startId);
201     HILOG_INFO("%{public}s begin restart=%{public}s,startId=%{public}d.",
202         __func__,
203         restart ? "true" : "false",
204         startId);
205     HILOG_INFO("%{public}s end.", __func__);
206 }
207 
CallObjectMethod(const char * name,NativeValue * const * argv,size_t argc)208 NativeValue* JsWallpaperExtension::CallObjectMethod(const char* name, NativeValue* const* argv, size_t argc)
209 {
210     HILOG_INFO("jws JsWallpaperExtension::CallObjectMethod(%{public}s), begin", name);
211 
212     if (!jsObj_) {
213         HILOG_WARN("Not found WallpaperExtension.js");
214         return nullptr;
215     }
216 
217     HandleScope handleScope(jsRuntime_);
218     auto& nativeEngine = jsRuntime_.GetNativeEngine();
219 
220     NativeValue* value = jsObj_->Get();
221     NativeObject* obj = ConvertNativeValueTo<NativeObject>(value);
222     if (obj == nullptr) {
223         HILOG_ERROR("Failed to get WallpaperExtension object");
224         return nullptr;
225     }
226 
227     NativeValue* method = obj->GetProperty(name);
228     if (method == nullptr) {
229         HILOG_ERROR("Failed to get '%{public}s' from WallpaperExtension object", name);
230         return nullptr;
231     }
232     HILOG_INFO("JsWallpaperExtension::CallFunction(%{public}s), success", name);
233     return nativeEngine.CallFunction(value, method, argv, argc);
234 }
235 
GetSrcPath(std::string & srcPath)236 void JsWallpaperExtension::GetSrcPath(std::string &srcPath)
237 {
238     HILOG_INFO("jws JsWallpaperExtension GetSrcPath begin.");
239     if (!Extension::abilityInfo_->isModuleJson) {
240         /* temporary compatibility api8 + config.json */
241         srcPath.append(Extension::abilityInfo_->package);
242         srcPath.append("/assets/js/");
243         if (!Extension::abilityInfo_->srcPath.empty()) {
244             srcPath.append(Extension::abilityInfo_->srcPath);
245         }
246         srcPath.append("/").append(Extension::abilityInfo_->name).append(".abc");
247         return;
248     }
249 
250     if (!Extension::abilityInfo_->srcEntrance.empty()) {
251         srcPath.append(Extension::abilityInfo_->moduleName + "/");
252         srcPath.append(Extension::abilityInfo_->srcEntrance);
253         srcPath.erase(srcPath.rfind('.'));
254         srcPath.append(".abc");
255     }
256 }
257 }
258 }
259