1 /*
2 * Copyright (c) 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_media_control_extension.h"
17
18 #include "ability_info.h"
19 #include "ability_manager_client.h"
20 #include "hitrace_meter.h"
21 #include "hilog_wrapper.h"
22 #include "js_extension_common.h"
23 #include "js_extension_context.h"
24 #include "js_runtime.h"
25 #include "js_runtime_utils.h"
26 #include "js_media_control_extension_context.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 #include "napi_common_configuration.h"
30 #include "napi_common_want.h"
31 #include "napi_remote_object.h"
32 #include "ui_extension_window_command.h"
33
34 namespace OHOS {
35 namespace AbilityRuntime {
36 using namespace OHOS::AppExecFwk;
37 namespace {
38 constexpr size_t ARGC_ONE = 1;
39 constexpr size_t ARGC_TWO = 2;
40 }
41
AttachMediaControlExtensionContext(NativeEngine * engine,void * value,void *)42 NativeValue *AttachMediaControlExtensionContext(NativeEngine *engine, void *value, void *)
43 {
44 HILOG_DEBUG("AttachMediaControlExtensionContext");
45 if (value == nullptr) {
46 HILOG_ERROR("invalid parameter.");
47 return nullptr;
48 }
49
50 auto ptr = reinterpret_cast<std::weak_ptr<MediaControlExtensionContext> *>(value)->lock();
51 if (ptr == nullptr) {
52 HILOG_ERROR("invalid context.");
53 return nullptr;
54 }
55 NativeValue *object = JsMediaControlExtensionContext::CreateJsMediaControlExtensionContext(*engine, ptr);
56 auto contextObj = JsRuntime::LoadSystemModuleByEngine(engine, "application.MediaControlExtensionContext",
57 &object, 1)->Get();
58 if (contextObj == nullptr) {
59 HILOG_ERROR("load context error.");
60 return nullptr;
61 }
62 NativeObject *nObject = ConvertNativeValueTo<NativeObject>(contextObj);
63 nObject->ConvertToNativeBindingObject(engine, DetachCallbackFunc, AttachMediaControlExtensionContext,
64 value, nullptr);
65
66 auto workContext = new (std::nothrow) std::weak_ptr<MediaControlExtensionContext>(ptr);
67 nObject->SetNativePointer(workContext,
68 [](NativeEngine *, void *data, void *) {
69 HILOG_DEBUG("Finalizer for weak_ptr ui extension context is called");
70 delete static_cast<std::weak_ptr<MediaControlExtensionContext> *>(data);
71 }, nullptr);
72 return contextObj;
73 }
74
Create(const std::unique_ptr<Runtime> & runtime)75 JsMediaControlExtension* JsMediaControlExtension::Create(const std::unique_ptr<Runtime>& runtime)
76 {
77 return new JsMediaControlExtension(static_cast<JsRuntime&>(*runtime));
78 }
79
JsMediaControlExtension(JsRuntime & jsRuntime)80 JsMediaControlExtension::JsMediaControlExtension(JsRuntime& jsRuntime) : jsRuntime_(jsRuntime) {}
~JsMediaControlExtension()81 JsMediaControlExtension::~JsMediaControlExtension()
82 {
83 HILOG_DEBUG("Js ui extension destructor.");
84 auto context = GetContext();
85 if (context) {
86 context->Unbind();
87 }
88
89 jsRuntime_.FreeNativeReference(std::move(jsObj_));
90 jsRuntime_.FreeNativeReference(std::move(shellContextRef_));
91 }
92
Init(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)93 void JsMediaControlExtension::Init(const std::shared_ptr<AbilityLocalRecord> &record,
94 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
95 const sptr<IRemoteObject> &token)
96 {
97 HILOG_DEBUG("JsMediaControlExtension begin init");
98 MediaControlExtension::Init(record, application, handler, token);
99 if (Extension::abilityInfo_->srcEntrance.empty()) {
100 HILOG_ERROR("JsMediaControlExtension Init abilityInfo srcEntrance is empty");
101 return;
102 }
103 std::string srcPath(Extension::abilityInfo_->moduleName + "/");
104 srcPath.append(Extension::abilityInfo_->srcEntrance);
105 srcPath.erase(srcPath.rfind('.'));
106 srcPath.append(".abc");
107
108 std::string moduleName(Extension::abilityInfo_->moduleName);
109 moduleName.append("::").append(abilityInfo_->name);
110 HandleScope handleScope(jsRuntime_);
111 auto& engine = jsRuntime_.GetNativeEngine();
112
113 jsObj_ = jsRuntime_.LoadModule(
114 moduleName, srcPath, abilityInfo_->hapPath, abilityInfo_->compileMode == CompileMode::ES_MODULE);
115 if (jsObj_ == nullptr) {
116 HILOG_ERROR("Failed to get jsObj_");
117 return;
118 }
119
120 NativeObject* obj = ConvertNativeValueTo<NativeObject>(jsObj_->Get());
121 if (obj == nullptr) {
122 HILOG_ERROR("Failed to get JsMediaControlExtension object");
123 return;
124 }
125
126 BindContext(engine, obj);
127
128 SetExtensionCommon(
129 JsExtensionCommon::Create(jsRuntime_, static_cast<NativeReference&>(*jsObj_), shellContextRef_));
130 }
131
BindContext(NativeEngine & engine,NativeObject * obj)132 void JsMediaControlExtension::BindContext(NativeEngine& engine, NativeObject* obj)
133 {
134 auto context = GetContext();
135 if (context == nullptr) {
136 HILOG_ERROR("Failed to get context");
137 return;
138 }
139 HILOG_DEBUG("BindContext CreateJsMediaControlExtensionContext.");
140 NativeValue* contextObj = JsMediaControlExtensionContext::CreateJsMediaControlExtensionContext(engine, context);
141
142 if (contextObj == nullptr) {
143 HILOG_ERROR("Create js ui extension context error.");
144 return;
145 }
146
147 shellContextRef_ = JsRuntime::LoadSystemModuleByEngine(&engine, "application.MediaControlExtensionContext",
148 &contextObj, ARGC_ONE);
149 contextObj = shellContextRef_->Get();
150 NativeObject *nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
151 if (nativeObj == nullptr) {
152 HILOG_ERROR("Failed to get context native object");
153 return;
154 }
155 auto workContext = new (std::nothrow) std::weak_ptr<MediaControlExtensionContext>(context);
156 nativeObj->ConvertToNativeBindingObject(&engine, DetachCallbackFunc, AttachMediaControlExtensionContext,
157 workContext, nullptr);
158 context->Bind(jsRuntime_, shellContextRef_.get());
159 obj->SetProperty("context", contextObj);
160
161 nativeObj->SetNativePointer(workContext,
162 [](NativeEngine*, void* data, void*) {
163 HILOG_DEBUG("Finalizer for weak_ptr ui extension context is called");
164 delete static_cast<std::weak_ptr<MediaControlExtensionContext>*>(data);
165 }, nullptr);
166
167 HILOG_DEBUG("Init end.");
168 }
169
OnStart(const AAFwk::Want & want)170 void JsMediaControlExtension::OnStart(const AAFwk::Want &want)
171 {
172 HILOG_DEBUG("JsMediaControlExtension OnStart begin.");
173 Extension::OnStart(want);
174 HandleScope handleScope(jsRuntime_);
175 NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
176 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(nativeEngine), want);
177 NativeValue* nativeWant = reinterpret_cast<NativeValue*>(napiWant);
178 NativeValue* argv[] = {nativeWant};
179 CallObjectMethod("onCreate", argv, ARGC_ONE);
180 HILOG_DEBUG("JsMediaControlExtension OnStart end.");
181 }
182
OnStop()183 void JsMediaControlExtension::OnStop()
184 {
185 MediaControlExtension::OnStop();
186 HILOG_DEBUG("JsMediaControlExtension OnStop begin.");
187 HandleScope handleScope(jsRuntime_);
188 CallObjectMethod("onDestroy");
189 HILOG_DEBUG("JsMediaControlExtension OnStop end.");
190 }
191
OnConnect(const AAFwk::Want & want)192 sptr<IRemoteObject> JsMediaControlExtension::OnConnect(const AAFwk::Want &want)
193 {
194 HandleScope handleScope(jsRuntime_);
195 NativeValue *result = CallOnConnect(want);
196 NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
197 auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject(
198 reinterpret_cast<napi_env>(nativeEngine), reinterpret_cast<napi_value>(result));
199 if (remoteObj == nullptr) {
200 HILOG_ERROR("remoteObj is nullptr.");
201 }
202 return remoteObj;
203 }
204
OnDisconnect(const AAFwk::Want & want)205 void JsMediaControlExtension::OnDisconnect(const AAFwk::Want &want)
206 {
207 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
208 Extension::OnDisconnect(want);
209 HILOG_DEBUG("JsMediaControlExtension OnDisconnect begin.");
210 CallOnDisconnect(want, false);
211 HILOG_DEBUG("JsMediaControlExtension OnDisconnect end.");
212 }
213
OnCommandWindow(const AAFwk::Want & want,const sptr<AAFwk::SessionInfo> & sessionInfo,AAFwk::WindowCommand winCmd)214 void JsMediaControlExtension::OnCommandWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
215 AAFwk::WindowCommand winCmd)
216 {
217 if (sessionInfo == nullptr) {
218 HILOG_ERROR("sessionInfo is nullptr.");
219 return;
220 }
221 HILOG_DEBUG("begin. persistentId: %{private}d, winCmd: %{public}d", sessionInfo->persistentId, winCmd);
222 Extension::OnCommandWindow(want, sessionInfo, winCmd);
223 switch (winCmd) {
224 case AAFwk::WIN_CMD_FOREGROUND:
225 ForegroundWindow(want, sessionInfo);
226 break;
227 case AAFwk::WIN_CMD_BACKGROUND:
228 BackgroundWindow(sessionInfo);
229 break;
230 case AAFwk::WIN_CMD_DESTROY:
231 DestroyWindow(sessionInfo);
232 break;
233 default:
234 HILOG_DEBUG("unsupported cmd.");
235 break;
236 }
237 auto context = GetContext();
238 if (context == nullptr) {
239 HILOG_ERROR("Failed to get context");
240 return;
241 }
242 AAFwk::AbilityCommand abilityCmd;
243 if (uiWindowMap_.empty()) {
244 abilityCmd = AAFwk::ABILITY_CMD_DESTROY;
245 } else if (foregroundWindows_.empty()) {
246 abilityCmd = AAFwk::ABILITY_CMD_BACKGROUND;
247 } else {
248 abilityCmd = AAFwk::ABILITY_CMD_FOREGROUND;
249 }
250 AAFwk::AbilityManagerClient::GetInstance()->ScheduleCommandAbilityWindowDone(
251 context->GetToken(), sessionInfo, winCmd, abilityCmd);
252 HILOG_DEBUG("end.");
253 }
254
OnCommand(const AAFwk::Want & want,bool restart,int startId)255 void JsMediaControlExtension::OnCommand(const AAFwk::Want &want, bool restart, int startId)
256 {
257 Extension::OnCommand(want, restart, startId);
258 HILOG_DEBUG("JsMediaControlExtension OnCommand begin restart=%{public}s,startId=%{public}d.",
259 restart ? "true" : "false", startId);
260 // wrap want
261 HandleScope handleScope(jsRuntime_);
262 NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
263 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(nativeEngine), want);
264 NativeValue* nativeWant = reinterpret_cast<NativeValue*>(napiWant);
265 // wrap startId
266 napi_value napiStartId = nullptr;
267 napi_create_int32(reinterpret_cast<napi_env>(nativeEngine), startId, &napiStartId);
268 NativeValue* nativeStartId = reinterpret_cast<NativeValue*>(napiStartId);
269 NativeValue* argv[] = {nativeWant, nativeStartId};
270 CallObjectMethod("onRequest", argv, ARGC_TWO);
271 HILOG_DEBUG("JsMediaControlExtension OnCommand end.");
272 }
273
OnForeground(const Want & want)274 void JsMediaControlExtension::OnForeground(const Want &want)
275 {
276 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
277 HILOG_DEBUG("JsMediaControlExtension OnForeground begin.");
278 Extension::OnForeground(want);
279
280 HandleScope handleScope(jsRuntime_);
281 CallObjectMethod("onForeground");
282 HILOG_DEBUG("JsMediaControlExtension OnForeground end.");
283 }
284
OnBackground()285 void JsMediaControlExtension::OnBackground()
286 {
287 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
288 HILOG_DEBUG("JsMediaControlExtension OnBackground begin.");
289 HandleScope handleScope(jsRuntime_);
290 CallObjectMethod("onBackground");
291 Extension::OnBackground();
292 HILOG_DEBUG("JsMediaControlExtension OnBackground end.");
293 }
294
ForegroundWindow(const AAFwk::Want & want,const sptr<AAFwk::SessionInfo> & sessionInfo)295 void JsMediaControlExtension::ForegroundWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo)
296 {
297 HILOG_DEBUG("begin.");
298 if (sessionInfo == nullptr) {
299 HILOG_ERROR("SessionInfo is nullptr.");
300 return;
301 }
302 auto persistentId = sessionInfo->persistentId;
303 if (uiWindowMap_.find(persistentId) == uiWindowMap_.end()) {
304 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
305 auto context = GetContext();
306 if (context == nullptr || context->GetAbilityInfo() == nullptr) {
307 HILOG_ERROR("Failed to get context");
308 return;
309 }
310 option->SetWindowName(context->GetBundleName() + context->GetAbilityInfo()->name);
311 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_UI_EXTENSION);
312 option->SetWindowSessionType(Rosen::WindowSessionType::EXTENSION_SESSION);
313 auto uiWindow = Rosen::Window::Create(option, GetContext(), sessionInfo->sessionToken);
314 if (uiWindow == nullptr) {
315 HILOG_ERROR("create ui window error.");
316 return;
317 }
318 HandleScope handleScope(jsRuntime_);
319 NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
320 NativeValue* pathNative = CallObjectMethod("onLoadContent");
321 std::string contextPath;
322 if (!ConvertFromJsValue(*nativeEngine, pathNative, contextPath)) {
323 HILOG_ERROR("Failed to convert parameter to context path");
324 return;
325 }
326 HILOG_DEBUG("contextPath is %{private}s", contextPath.c_str());
327 uiWindow->SetUIContent(contextPath, nativeEngine, nullptr);
328 uiWindowMap_[persistentId] = uiWindow;
329 }
330 auto& uiWindow = uiWindowMap_[persistentId];
331 if (uiWindow) {
332 uiWindow->Show();
333 foregroundWindows_.emplace(persistentId);
334 }
335 HILOG_DEBUG("end.");
336 }
337
BackgroundWindow(const sptr<AAFwk::SessionInfo> & sessionInfo)338 void JsMediaControlExtension::BackgroundWindow(const sptr<AAFwk::SessionInfo> &sessionInfo)
339 {
340 HILOG_DEBUG("begin.");
341 if (sessionInfo == nullptr) {
342 HILOG_ERROR("SessionInfo is nullptr.");
343 return;
344 }
345 auto persistentId = sessionInfo->persistentId;
346 if (uiWindowMap_.find(persistentId) == uiWindowMap_.end()) {
347 HILOG_ERROR("Fail to find uiWindow, persistentId=%{private}d", persistentId);
348 return;
349 }
350 auto& uiWindow = uiWindowMap_[persistentId];
351 if (uiWindow) {
352 uiWindow->Hide();
353 foregroundWindows_.erase(persistentId);
354 }
355 HILOG_DEBUG("end.");
356 }
357
DestroyWindow(const sptr<AAFwk::SessionInfo> & sessionInfo)358 void JsMediaControlExtension::DestroyWindow(const sptr<AAFwk::SessionInfo> &sessionInfo)
359 {
360 HILOG_DEBUG("begin.");
361
362 if (sessionInfo == nullptr) {
363 HILOG_ERROR("SessionInfo is nullptr.");
364 return;
365 }
366 auto persistentId = sessionInfo->persistentId;
367 if (uiWindowMap_.find(persistentId) == uiWindowMap_.end()) {
368 HILOG_ERROR("Fail to find uiWindow, persistentId=%{private}d", persistentId);
369 return;
370 }
371 auto& uiWindow = uiWindowMap_[persistentId];
372 if (uiWindow) {
373 uiWindow->Destroy();
374 uiWindowMap_.erase(persistentId);
375 foregroundWindows_.erase(persistentId);
376 }
377 HILOG_DEBUG("end.");
378 }
379
CallObjectMethod(const char * name,NativeValue * const * argv,size_t argc)380 NativeValue* JsMediaControlExtension::CallObjectMethod(const char* name, NativeValue* const* argv, size_t argc)
381 {
382 HILOG_DEBUG("JsMediaControlExtension CallObjectMethod(%{public}s), begin", name);
383
384 if (!jsObj_) {
385 HILOG_ERROR("Not found MediaControlExtension.js");
386 return nullptr;
387 }
388
389 auto& nativeEngine = jsRuntime_.GetNativeEngine();
390 NativeValue* value = jsObj_->Get();
391 NativeObject* obj = ConvertNativeValueTo<NativeObject>(value);
392 if (obj == nullptr) {
393 HILOG_ERROR("Failed to get MediaControlExtension object");
394 return nullptr;
395 }
396
397 NativeValue* method = obj->GetProperty(name);
398 if (method == nullptr || method->TypeOf() != NATIVE_FUNCTION) {
399 HILOG_ERROR("Failed to get '%{public}s' from MediaControlExtension object", name);
400 return nullptr;
401 }
402 HILOG_DEBUG("JsMediaControlExtension CallFunction(%{public}s), success", name);
403 return nativeEngine.CallFunction(value, method, argv, argc);
404 }
405
CallOnConnect(const AAFwk::Want & want)406 NativeValue *JsMediaControlExtension::CallOnConnect(const AAFwk::Want &want)
407 {
408 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
409 Extension::OnConnect(want);
410 HILOG_DEBUG("JsMediaControlExtension CallOnConnect begin.");
411 HandleScope handleScope(jsRuntime_);
412 NativeEngine* nativeEngine = &jsRuntime_.GetNativeEngine();
413 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(nativeEngine), want);
414 auto* nativeWant = reinterpret_cast<NativeValue*>(napiWant);
415 NativeValue* argv[] = {nativeWant};
416 if (!jsObj_) {
417 HILOG_ERROR("Not found MediaControlExtension.js");
418 return nullptr;
419 }
420
421 NativeValue* value = jsObj_->Get();
422 auto* obj = ConvertNativeValueTo<NativeObject>(value);
423 if (obj == nullptr) {
424 HILOG_ERROR("Failed to get MediaControlExtension object");
425 return nullptr;
426 }
427
428 NativeValue* method = obj->GetProperty("onConnect");
429 if (method == nullptr) {
430 HILOG_ERROR("Failed to get onConnect from MediaControlExtension object");
431 return nullptr;
432 }
433 NativeValue* remoteNative = nativeEngine->CallFunction(value, method, argv, ARGC_ONE);
434 if (remoteNative == nullptr) {
435 HILOG_ERROR("remoteNative is nullptr.");
436 }
437 HILOG_DEBUG("JsMediaControlExtension CallOnConnect end.");
438 return remoteNative;
439 }
440
CallOnDisconnect(const AAFwk::Want & want,bool withResult)441 NativeValue *JsMediaControlExtension::CallOnDisconnect(const AAFwk::Want &want, bool withResult)
442 {
443 HandleEscape handleEscape(jsRuntime_);
444 NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine();
445 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(nativeEngine), want);
446 NativeValue *nativeWant = reinterpret_cast<NativeValue *>(napiWant);
447 NativeValue *argv[] = { nativeWant };
448 if (!jsObj_) {
449 HILOG_ERROR("Not found MediaControlExtension.js");
450 return nullptr;
451 }
452
453 NativeValue *value = jsObj_->Get();
454 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
455 if (obj == nullptr) {
456 HILOG_ERROR("Failed to get MediaControlExtension object");
457 return nullptr;
458 }
459
460 NativeValue *method = obj->GetProperty("onDisconnect");
461 if (method == nullptr) {
462 HILOG_ERROR("Failed to get onDisconnect from MediaControlExtension object");
463 return nullptr;
464 }
465
466 if (withResult) {
467 return handleEscape.Escape(nativeEngine->CallFunction(value, method, argv, ARGC_ONE));
468 } else {
469 nativeEngine->CallFunction(value, method, argv, ARGC_ONE);
470 return nullptr;
471 }
472 }
473
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)474 void JsMediaControlExtension::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration)
475 {
476 Extension::OnConfigurationUpdated(configuration);
477 HILOG_DEBUG("JsMediaControlExtension OnConfigurationUpdated called.");
478
479 HandleScope handleScope(jsRuntime_);
480 auto& nativeEngine = jsRuntime_.GetNativeEngine();
481
482 // Notify extension context
483 auto fullConfig = GetContext()->GetConfiguration();
484 if (!fullConfig) {
485 HILOG_ERROR("configuration is nullptr.");
486 return;
487 }
488 JsExtensionContext::ConfigurationUpdated(&nativeEngine, shellContextRef_, fullConfig);
489
490 napi_value napiConfiguration = OHOS::AppExecFwk::WrapConfiguration(
491 reinterpret_cast<napi_env>(&nativeEngine), *fullConfig);
492 NativeValue* jsConfiguration = reinterpret_cast<NativeValue*>(napiConfiguration);
493 CallObjectMethod("onConfigurationUpdate", &jsConfiguration, ARGC_ONE);
494 }
495
Dump(const std::vector<std::string> & params,std::vector<std::string> & info)496 void JsMediaControlExtension::Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info)
497 {
498 Extension::Dump(params, info);
499 HILOG_DEBUG("JsMediaControlExtension Dump called.");
500 HandleScope handleScope(jsRuntime_);
501 auto& nativeEngine = jsRuntime_.GetNativeEngine();
502 // create js array object of params
503 NativeValue* arrayValue = nativeEngine.CreateArray(params.size());
504 NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
505 uint32_t index = 0;
506 for (const auto ¶m : params) {
507 array->SetElement(index++, CreateJsValue(nativeEngine, param));
508 }
509 NativeValue* argv[] = { arrayValue };
510
511 if (!jsObj_) {
512 HILOG_ERROR("Not found MediaControlExtension.js");
513 return;
514 }
515
516 NativeValue* value = jsObj_->Get();
517 NativeObject* obj = ConvertNativeValueTo<NativeObject>(value);
518 if (obj == nullptr) {
519 HILOG_ERROR("Failed to get MediaControlExtension object");
520 return;
521 }
522
523 NativeValue* method = obj->GetProperty("onDump");
524 if (method == nullptr || method->TypeOf() != NATIVE_FUNCTION) {
525 method = obj->GetProperty("dump");
526 if (method == nullptr || method->TypeOf() != NATIVE_FUNCTION) {
527 HILOG_ERROR("Failed to get onDump from MediaControlExtension object");
528 return;
529 }
530 }
531 NativeValue* dumpInfo = nativeEngine.CallFunction(value, method, argv, ARGC_ONE);
532 if (dumpInfo == nullptr) {
533 HILOG_ERROR("dumpInfo is nullptr.");
534 return;
535 }
536 NativeArray* dumpInfoNative = ConvertNativeValueTo<NativeArray>(dumpInfo);
537 if (dumpInfoNative == nullptr) {
538 HILOG_ERROR("dumpInfoNative is nullptr.");
539 return;
540 }
541 for (uint32_t i = 0; i < dumpInfoNative->GetLength(); i++) {
542 std::string dumpInfoStr;
543 if (!ConvertFromJsValue(nativeEngine, dumpInfoNative->GetElement(i), dumpInfoStr)) {
544 HILOG_ERROR("Parse dumpInfoStr failed");
545 return;
546 }
547 info.push_back(dumpInfoStr);
548 }
549 HILOG_DEBUG("Dump info size: %{public}zu", info.size());
550 }
551 }
552 }
553