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 #include <regex>
16 #include "system_ability_definition.h"
17 #include "if_system_ability_manager.h"
18 #include "ability_delegator_registry.h"
19 #include "ability_runtime/js_ability.h"
20
21 #include "ability_runtime/js_ability_context.h"
22 #include "ability_start_setting.h"
23 #include "connection_manager.h"
24 #include "hilog_wrapper.h"
25 #include "js_data_struct_converter.h"
26 #include "js_runtime.h"
27 #include "js_runtime_utils.h"
28 #include "napi_common_configuration.h"
29 #ifdef SUPPORT_GRAPHICS
30 #include "js_window_stage.h"
31 #endif
32 #include "napi_common_want.h"
33 #include "napi_remote_object.h"
34 #include "string_wrapper.h"
35
36 namespace OHOS {
37 namespace AbilityRuntime {
38 #ifdef SUPPORT_GRAPHICS
39 const std::string PAGE_STACK_PROPERTY_NAME = "pageStack";
40 #endif
Create(const std::unique_ptr<Runtime> & runtime)41 Ability *JsAbility::Create(const std::unique_ptr<Runtime> &runtime)
42 {
43 return new JsAbility(static_cast<JsRuntime &>(*runtime));
44 }
45
JsAbility(JsRuntime & jsRuntime)46 JsAbility::JsAbility(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime)
47 {}
48
~JsAbility()49 JsAbility::~JsAbility() {
50 auto &engine = jsRuntime_.GetNativeEngine();
51 uv_loop_t *loop = engine.GetUVLoop();
52 if (loop == nullptr) {
53 return;
54 }
55
56 uv_work_t *work = new (std::nothrow) uv_work_t;
57 if (work == nullptr) {
58 return;
59 }
60
61 auto cb = new (std::nothrow)JsAbilityDeleterObject();
62 if (cb == nullptr) {
63 delete work;
64 work = nullptr;
65 return;
66 }
67
68 if (jsAbilityObj_) {
69 cb->jsAbilityObj_ = std::move(jsAbilityObj_);
70 }
71 if (shellContextRef_) {
72 cb->shellContextRef_ = std::move(shellContextRef_);
73 }
74 if (abilityContext_) {
75 cb->abilityContext_ = std::move(abilityContext_);
76 }
77 work->data = reinterpret_cast<void *>(cb);
78
79 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {},
80 [](uv_work_t *work, int status) {
81 if (work == nullptr) {
82 return;
83 }
84 if (work->data == nullptr) {
85 delete work;
86 work = nullptr;
87 return;
88 }
89 delete reinterpret_cast<JsAbilityDeleterObject *>(work->data);
90 work->data = nullptr;
91 delete work;
92 work = nullptr;
93 });
94
95 if (ret != 0) {
96 delete reinterpret_cast<JsAbilityDeleterObject *>(work->data);
97 work->data = nullptr;
98 delete work;
99 work = nullptr;
100 }
101 }
102
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)103 void JsAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
104 const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
105 const sptr<IRemoteObject> &token)
106 {
107 Ability::Init(abilityInfo, application, handler, token);
108
109 if (!abilityInfo) {
110 HILOG_ERROR("abilityInfo is nullptr");
111 return;
112 }
113
114 std::string srcPath(abilityInfo->package);
115 if (!abilityInfo->isModuleJson) {
116 /* temporary compatibility api8 + config.json */
117 srcPath.append("/assets/js/");
118 if (!abilityInfo->srcPath.empty()) {
119 srcPath.append(abilityInfo->srcPath);
120 }
121 srcPath.append("/").append(abilityInfo->name).append(".abc");
122 } else {
123 if (abilityInfo->srcEntrance.empty()) {
124 HILOG_ERROR("abilityInfo srcEntrance is empty");
125 return;
126 }
127 srcPath.append("/");
128 srcPath.append(abilityInfo->srcEntrance);
129 srcPath.erase(srcPath.rfind("."));
130 srcPath.append(".abc");
131 HILOG_INFO("JsAbility srcPath is %{public}s", srcPath.c_str());
132 }
133
134 std::string moduleName(abilityInfo->moduleName);
135 moduleName.append("::").append(abilityInfo->name);
136
137 HandleScope handleScope(jsRuntime_);
138 auto &engine = jsRuntime_.GetNativeEngine();
139
140 jsAbilityObj_ = jsRuntime_.LoadModule(moduleName, srcPath);
141
142 NativeObject *obj = ConvertNativeValueTo<NativeObject>(jsAbilityObj_->Get());
143 if (obj == nullptr) {
144 HILOG_ERROR("Failed to get AbilityStage object");
145 return;
146 }
147
148 auto context = GetAbilityContext();
149 NativeValue *contextObj = CreateJsAbilityContext(engine, context);
150 shellContextRef_ = std::shared_ptr<NativeReference>(
151 jsRuntime_.LoadSystemModule("application.AbilityContext", &contextObj, 1).release());
152 contextObj = shellContextRef_->Get();
153
154 context->Bind(jsRuntime_, shellContextRef_.get());
155 obj->SetProperty("context", contextObj);
156
157 auto nativeObj = ConvertNativeValueTo<NativeObject>(contextObj);
158 if (nativeObj == nullptr) {
159 HILOG_ERROR("Failed to get ability native object");
160 return;
161 }
162
163 HILOG_INFO("Set ability context pointer: %{public}p", context.get());
164
165 nativeObj->SetNativePointer(
166 new std::weak_ptr<AbilityRuntime::Context>(context),
167 [](NativeEngine *, void *data, void *) {
168 HILOG_INFO("Finalizer for weak_ptr ability context is called");
169 delete static_cast<std::weak_ptr<AbilityRuntime::Context> *>(data);
170 },
171 nullptr);
172 }
173
OnStart(const Want & want)174 void JsAbility::OnStart(const Want &want)
175 {
176 Ability::OnStart(want);
177
178 if (!jsAbilityObj_) {
179 HILOG_WARN("Not found Ability.js");
180 return;
181 }
182
183 HandleScope handleScope(jsRuntime_);
184 auto &nativeEngine = jsRuntime_.GetNativeEngine();
185
186 NativeValue *value = jsAbilityObj_->Get();
187 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
188 if (obj == nullptr) {
189 HILOG_ERROR("Failed to get Ability object");
190 return;
191 }
192
193 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(&nativeEngine), want);
194 NativeValue *jsWant = reinterpret_cast<NativeValue *>(napiWant);
195
196 obj->SetProperty("launchWant", jsWant);
197 obj->SetProperty("lastRequestWant", jsWant);
198
199 NativeValue *argv[] = {
200 jsWant,
201 CreateJsLaunchParam(nativeEngine, GetLaunchParam()),
202 };
203 CallObjectMethod("onCreate", argv, ArraySize(argv));
204
205 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
206 if (delegator) {
207 HILOG_INFO("Call AbilityDelegator::PostPerformStart");
208 delegator->PostPerformStart(CreateADelegatorAbilityProperty());
209 }
210 }
211
OnStop()212 void JsAbility::OnStop()
213 {
214 Ability::OnStop();
215
216 CallObjectMethod("onDestroy");
217
218 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
219 if (delegator) {
220 HILOG_INFO("Call AbilityDelegator::PostPerformStop");
221 delegator->PostPerformStop(CreateADelegatorAbilityProperty());
222 }
223
224 bool ret = ConnectionManager::GetInstance().DisconnectCaller(AbilityContext::token_);
225 if (ret) {
226 ConnectionManager::GetInstance().ReportConnectionLeakEvent(getpid(), gettid());
227 HILOG_INFO("The service connection is not disconnected.");
228 }
229 }
230
231 #ifdef SUPPORT_GRAPHICS
OnSceneCreated()232 void JsAbility::OnSceneCreated()
233 {
234 Ability::OnSceneCreated();
235 HILOG_INFO("OnSceneCreated");
236 auto jsAppWindowStage = CreateAppWindowStage();
237 if (jsAppWindowStage == nullptr) {
238 HILOG_ERROR("Failed to create jsAppWindowStage object by LoadSystemModule");
239 return;
240 }
241 NativeValue *argv[] = {jsAppWindowStage->Get()};
242 CallObjectMethod("onWindowStageCreate", argv, ArraySize(argv));
243
244 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
245 if (delegator) {
246 HILOG_INFO("Call AbilityDelegator::PostPerformScenceCreated");
247 delegator->PostPerformScenceCreated(CreateADelegatorAbilityProperty());
248 }
249 }
250
OnSceneRestored()251 void JsAbility::OnSceneRestored()
252 {
253 Ability::OnSceneRestored();
254 HILOG_INFO("OnSceneRestored");
255 auto jsAppWindowStage = CreateAppWindowStage();
256 if (jsAppWindowStage == nullptr) {
257 HILOG_ERROR("Failed to create jsAppWindowStage object by LoadSystemModule");
258 return;
259 }
260 NativeValue *argv[] = {jsAppWindowStage->Get()};
261 CallObjectMethod("onWindowStageRestore", argv, ArraySize(argv));
262
263 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
264 if (delegator) {
265 HILOG_INFO("Call AbilityDelegator::PostPerformScenceRestored");
266 delegator->PostPerformScenceRestored(CreateADelegatorAbilityProperty());
267 }
268 }
269
onSceneDestroyed()270 void JsAbility::onSceneDestroyed()
271 {
272 Ability::onSceneDestroyed();
273
274 CallObjectMethod("onWindowStageDestroy");
275
276 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
277 if (delegator) {
278 HILOG_INFO("Call AbilityDelegator::PostPerformScenceDestroyed");
279 delegator->PostPerformScenceDestroyed(CreateADelegatorAbilityProperty());
280 }
281 }
282
OnForeground(const Want & want)283 void JsAbility::OnForeground(const Want &want)
284 {
285 Ability::OnForeground(want);
286
287 HandleScope handleScope(jsRuntime_);
288 auto &nativeEngine = jsRuntime_.GetNativeEngine();
289
290 NativeValue *value = jsAbilityObj_->Get();
291 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
292 if (obj == nullptr) {
293 HILOG_ERROR("Failed to get Ability object");
294 return;
295 }
296
297 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(&nativeEngine), want);
298 NativeValue *jsWant = reinterpret_cast<NativeValue *>(napiWant);
299
300 obj->SetProperty("lastRequestWant", jsWant);
301
302 CallObjectMethod("onForeground", &jsWant, 1);
303
304 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
305 if (delegator) {
306 HILOG_INFO("Call AbilityDelegator::PostPerformForeground");
307 delegator->PostPerformForeground(CreateADelegatorAbilityProperty());
308 }
309 }
310
OnBackground()311 void JsAbility::OnBackground()
312 {
313 Ability::OnBackground();
314
315 CallObjectMethod("onBackground");
316
317 auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator();
318 if (delegator) {
319 HILOG_INFO("Call AbilityDelegator::PostPerformBackground");
320 delegator->PostPerformBackground(CreateADelegatorAbilityProperty());
321 }
322 }
323 #endif
324
OnContinue(WantParams & wantParams)325 int32_t JsAbility::OnContinue(WantParams &wantParams)
326 {
327 HandleScope handleScope(jsRuntime_);
328 auto &nativeEngine = jsRuntime_.GetNativeEngine();
329
330 NativeValue *value = jsAbilityObj_->Get();
331 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
332 if (obj == nullptr) {
333 HILOG_ERROR("Failed to get Ability object");
334 return false;
335 }
336
337 NativeValue *methodOnCreate = obj->GetProperty("onContinue");
338 if (methodOnCreate == nullptr) {
339 HILOG_ERROR("Failed to get 'onContinue' from Ability object");
340 return false;
341 }
342
343 napi_value napiWantParams = OHOS::AppExecFwk::WrapWantParams(reinterpret_cast<napi_env>(&nativeEngine), wantParams);
344 NativeValue *jsWantParams = reinterpret_cast<NativeValue *>(napiWantParams);
345
346 NativeValue *result = nativeEngine.CallFunction(value, methodOnCreate, &jsWantParams, 1);
347
348 napi_value new_napiWantParams = reinterpret_cast<napi_value>(jsWantParams);
349 OHOS::AppExecFwk::UnwrapWantParams(reinterpret_cast<napi_env>(&nativeEngine), new_napiWantParams, wantParams);
350
351 NativeNumber *numberResult = ConvertNativeValueTo<NativeNumber>(result);
352 if (numberResult == nullptr) {
353 return false;
354 }
355
356 return *numberResult;
357 }
358
OnConfigurationUpdated(const Configuration & configuration)359 void JsAbility::OnConfigurationUpdated(const Configuration &configuration)
360 {
361 Ability::OnConfigurationUpdated(configuration);
362 HILOG_INFO("%{public}s called.", __func__);
363
364 HandleScope handleScope(jsRuntime_);
365 auto& nativeEngine = jsRuntime_.GetNativeEngine();
366 auto fullConfig = GetAbilityContext()->GetConfiguration();
367 if (!fullConfig) {
368 HILOG_ERROR("configuration is nullptr.");
369 return;
370 }
371
372 JsAbilityContext::ConfigurationUpdated(&nativeEngine, shellContextRef_, fullConfig);
373 napi_value napiConfiguration = OHOS::AppExecFwk::WrapConfiguration(
374 reinterpret_cast<napi_env>(&nativeEngine), *fullConfig);
375 NativeValue* jsConfiguration = reinterpret_cast<NativeValue*>(napiConfiguration);
376 CallObjectMethod("onConfigurationUpdated", &jsConfiguration, 1);
377 }
378
UpdateContextConfiguration()379 void JsAbility::UpdateContextConfiguration()
380 {
381 HILOG_INFO("%{public}s called.", __func__);
382 HandleScope handleScope(jsRuntime_);
383 auto& nativeEngine = jsRuntime_.GetNativeEngine();
384 JsAbilityContext::ConfigurationUpdated(&nativeEngine, shellContextRef_, GetAbilityContext()->GetConfiguration());
385 }
386
OnNewWant(const Want & want)387 void JsAbility::OnNewWant(const Want &want)
388 {
389 HILOG_INFO("%{public}s begin.", __func__);
390 Ability::OnNewWant(want);
391
392 HandleScope handleScope(jsRuntime_);
393 auto &nativeEngine = jsRuntime_.GetNativeEngine();
394
395 NativeValue *value = jsAbilityObj_->Get();
396 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
397 if (obj == nullptr) {
398 HILOG_ERROR("Failed to get Ability object");
399 return;
400 }
401
402 napi_value napiWant = OHOS::AppExecFwk::WrapWant(reinterpret_cast<napi_env>(&nativeEngine), want);
403 NativeValue *jsWant = reinterpret_cast<NativeValue *>(napiWant);
404
405 obj->SetProperty("lastRequestWant", jsWant);
406
407 CallObjectMethod("onNewWant", &jsWant, 1);
408 HILOG_INFO("%{public}s end.", __func__);
409 }
410
OnAbilityResult(int requestCode,int resultCode,const Want & resultData)411 void JsAbility::OnAbilityResult(int requestCode, int resultCode, const Want &resultData)
412 {
413 HILOG_INFO("%{public}s begin.", __func__);
414 Ability::OnAbilityResult(requestCode, resultCode, resultData);
415 std::shared_ptr<AbilityRuntime::AbilityContext> context = GetAbilityContext();
416 if (context == nullptr) {
417 HILOG_WARN("JsAbility not attached to any runtime context!");
418 return;
419 }
420 context->OnAbilityResult(requestCode, resultCode, resultData);
421 HILOG_INFO("%{public}s end.", __func__);
422 }
423
CallRequest()424 sptr<IRemoteObject> JsAbility::CallRequest()
425 {
426 HILOG_INFO("JsAbility::CallRequest begin.");
427 if (jsAbilityObj_ == nullptr) {
428 HILOG_WARN("JsAbility::CallRequest Obj is nullptr");
429 return nullptr;
430 }
431
432 if (remoteCallee_ != nullptr) {
433 HILOG_INFO("JsAbility::CallRequest get Callee remoteObj.");
434 return remoteCallee_;
435 }
436
437 HandleScope handleScope(jsRuntime_);
438 HILOG_DEBUG("JsAbility::CallRequest set runtime scope.");
439 auto& nativeEngine = jsRuntime_.GetNativeEngine();
440 auto value = jsAbilityObj_->Get();
441 if (value == nullptr) {
442 HILOG_ERROR("JsAbility::CallRequest value is nullptr");
443 return nullptr;
444 }
445
446 NativeObject* obj = ConvertNativeValueTo<NativeObject>(value);
447 if (obj == nullptr) {
448 HILOG_ERROR("JsAbility::CallRequest obj is nullptr");
449 return nullptr;
450 }
451
452 auto method = obj->GetProperty("onCallRequest");
453 if (method == nullptr || !method->IsCallable()) {
454 HILOG_ERROR("JsAbility::CallRequest method is %{public}s", method == nullptr ? "nullptr" : "not func");
455 return nullptr;
456 }
457
458 auto remoteJsObj = nativeEngine.CallFunction(value, method, nullptr, 0);
459 if (remoteJsObj == nullptr) {
460 HILOG_ERROR("JsAbility::CallRequest JsObj is nullptr");
461 return nullptr;
462 }
463
464 auto remoteObj = NAPI_ohos_rpc_getNativeRemoteObject(
465 reinterpret_cast<napi_env>(&nativeEngine), reinterpret_cast<napi_value>(remoteJsObj));
466 if (remoteObj == nullptr) {
467 HILOG_ERROR("JsAbility::CallRequest obj is nullptr");
468 }
469
470 remoteCallee_ = remoteObj;
471 HILOG_INFO("JsAbility::CallRequest end.");
472 return remoteCallee_;
473 }
474
OnRequestPermissionsFromUserResult(int requestCode,const std::vector<std::string> & permissions,const std::vector<int> & grantResults)475 void JsAbility::OnRequestPermissionsFromUserResult(
476 int requestCode, const std::vector<std::string> &permissions, const std::vector<int> &grantResults)
477 {
478 HILOG_INFO("%{public}s called.", __func__);
479 std::shared_ptr<AbilityRuntime::AbilityContext> context = GetAbilityContext();
480 if (context == nullptr) {
481 HILOG_WARN("JsAbility not attached to any runtime context!");
482 return;
483 }
484 context->OnRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
485 HILOG_INFO("%{public}s end.", __func__);
486 }
487
CallObjectMethod(const char * name,NativeValue * const * argv,size_t argc)488 void JsAbility::CallObjectMethod(const char *name, NativeValue *const *argv, size_t argc)
489 {
490 HILOG_INFO("JsAbility::CallObjectMethod(%{public}s", name);
491
492 if (!jsAbilityObj_) {
493 HILOG_WARN("Not found Ability.js");
494 return;
495 }
496
497 HandleScope handleScope(jsRuntime_);
498 auto &nativeEngine = jsRuntime_.GetNativeEngine();
499
500 NativeValue *value = jsAbilityObj_->Get();
501 NativeObject *obj = ConvertNativeValueTo<NativeObject>(value);
502 if (obj == nullptr) {
503 HILOG_ERROR("Failed to get Ability object");
504 return;
505 }
506
507 NativeValue *methodOnCreate = obj->GetProperty(name);
508 if (methodOnCreate == nullptr) {
509 HILOG_ERROR("Failed to get '%{public}s' from Ability object", name);
510 return;
511 }
512 nativeEngine.CallFunction(value, methodOnCreate, argv, argc);
513 }
514
515 #ifdef SUPPORT_GRAPHICS
CreateAppWindowStage()516 std::unique_ptr<NativeReference> JsAbility::CreateAppWindowStage()
517 {
518 HandleScope handleScope(jsRuntime_);
519 auto &engine = jsRuntime_.GetNativeEngine();
520 NativeValue *jsWindowStage = Rosen::CreateJsWindowStage(engine, GetScene());
521 if (jsWindowStage == nullptr) {
522 HILOG_ERROR("Failed to create jsWindowSatge object");
523 return nullptr;
524 }
525 return jsRuntime_.LoadSystemModule("application.WindowStage", &jsWindowStage, 1);
526 }
527
GetPageStackFromWant(const Want & want,std::string & pageStack)528 void JsAbility::GetPageStackFromWant(const Want &want, std::string &pageStack)
529 {
530 auto stringObj = AAFwk::IString::Query(want.GetParams().GetParam(PAGE_STACK_PROPERTY_NAME));
531 if (stringObj != nullptr) {
532 pageStack = AAFwk::String::Unbox(stringObj);
533 }
534 }
535
DoOnForeground(const Want & want)536 void JsAbility::DoOnForeground(const Want &want)
537 {
538 if (scene_ == nullptr) {
539 if ((abilityContext_ == nullptr) || (sceneListener_ == nullptr)) {
540 HILOG_ERROR("Ability::OnForeground error. abilityContext_ or sceneListener_ is nullptr!");
541 return;
542 }
543 scene_ = std::make_shared<Rosen::WindowScene>();
544 if (scene_ == nullptr) {
545 HILOG_ERROR("%{public}s error. failed to create WindowScene instance!", __func__);
546 return;
547 }
548 int32_t displayId = Rosen::WindowScene::DEFAULT_DISPLAY_ID;
549 if (setting_ != nullptr) {
550 std::string strDisplayId =
551 setting_->GetProperty(OHOS::AppExecFwk::AbilityStartSetting::WINDOW_DISPLAY_ID_KEY);
552 std::regex formatRegex("[0-9]{0,9}$");
553 std::smatch sm;
554 bool flag = std::regex_match(strDisplayId, sm, formatRegex);
555 if (flag && !strDisplayId.empty()) {
556 displayId = std::stoi(strDisplayId);
557 HILOG_INFO("%{public}s success. displayId is %{public}d", __func__, displayId);
558 } else {
559 HILOG_INFO("%{public}s failed to formatRegex:[%{public}s]", __func__, strDisplayId.c_str());
560 }
561 }
562 auto option = GetWindowOption(want);
563 Rosen::WMError ret = scene_->Init(displayId, abilityContext_, sceneListener_, option);
564 if (ret != Rosen::WMError::WM_OK) {
565 HILOG_ERROR("%{public}s error. failed to init window scene!", __func__);
566 return;
567 }
568
569 // multi-instance ability continuation
570 HILOG_INFO("lauch reason = %{public}d", launchParam_.launchReason);
571 if (IsRestoredInContinuation()) {
572 std::string pageStack;
573 GetPageStackFromWant(want, pageStack);
574 HandleScope handleScope(jsRuntime_);
575 auto &engine = jsRuntime_.GetNativeEngine();
576 if (abilityContext_->GetContentStorage()) {
577 scene_->GetMainWindow()->SetUIContent(pageStack, &engine,
578 abilityContext_->GetContentStorage()->Get(), true);
579 } else {
580 HILOG_ERROR("restore: content storage is nullptr");
581 }
582 OnSceneRestored();
583 WaitingDistributedObjectSyncComplete(want);
584 } else {
585 OnSceneCreated();
586 }
587 } else {
588 auto window = scene_->GetMainWindow();
589 if (window != nullptr && want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
590 auto windowMode = want.GetIntParam(Want::PARAM_RESV_WINDOW_MODE,
591 AAFwk::AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
592 window->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
593 HILOG_INFO("set window mode = %{public}d.", windowMode);
594 }
595 }
596
597 auto window = scene_->GetMainWindow();
598 if (window) {
599 HILOG_INFO("Call RegisterDisplayMoveListener, windowId: %{public}d", window->GetWindowId());
600 OHOS::sptr<OHOS::Rosen::IDisplayMoveListener> displayMoveListener(this);
601 window->RegisterDisplayMoveListener(displayMoveListener);
602 }
603
604 HILOG_INFO("%{public}s begin scene_->GoForeground, sceneFlag_:%{public}d.", __func__, Ability::sceneFlag_);
605 scene_->GoForeground(Ability::sceneFlag_);
606 HILOG_INFO("%{public}s end scene_->GoForeground.", __func__);
607 }
608 #endif
609
CreateADelegatorAbilityProperty()610 std::shared_ptr<AppExecFwk::ADelegatorAbilityProperty> JsAbility::CreateADelegatorAbilityProperty()
611 {
612 auto property = std::make_shared<AppExecFwk::ADelegatorAbilityProperty>();
613 property->token_ = GetAbilityContext()->GetToken();
614 property->name_ = GetAbilityName();
615 property->lifecycleState_ = GetState();
616
617 return property;
618 }
619
620 #ifdef SUPPORT_GRAPHICS
RequsetFocus(const Want & want)621 void JsAbility::RequsetFocus(const Want &want)
622 {
623 HILOG_INFO("%{public}s called.", __func__);
624 if (scene_ == nullptr) {
625 return;
626 }
627 auto window = scene_->GetMainWindow();
628 if (window != nullptr && want.HasParameter(Want::PARAM_RESV_WINDOW_MODE)) {
629 auto windowMode = want.GetIntParam(Want::PARAM_RESV_WINDOW_MODE,
630 AAFwk::AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_UNDEFINED);
631 window->SetWindowMode(static_cast<Rosen::WindowMode>(windowMode));
632 HILOG_INFO("set window mode = %{public}d.", windowMode);
633 }
634 scene_->GoForeground(Ability::sceneFlag_);
635 }
636 #endif
637 } // namespace AbilityRuntime
638 } // namespace OHOS
639