1 /*
2 * Copyright (c) 2025 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 "distributed_extension.h"
17
18 #include "distributed_extension_js.h"
19 #include "distributed_extension_service.h"
20 #include "dtbschedmgr_log.h"
21
22 namespace OHOS {
23 namespace DistributedSchedule {
24 const std::string TAG = "DistributedExtension";
25
26 using namespace std;
27
Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> & record,const shared_ptr<AbilityRuntime::OHOSApplication> & application,shared_ptr<AbilityRuntime::AbilityHandler> & handler,const sptr<IRemoteObject> & token)28 void DistributedExtension::Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> &record,
29 const shared_ptr<AbilityRuntime::OHOSApplication> &application,
30 shared_ptr<AbilityRuntime::AbilityHandler> &handler,
31 const sptr<IRemoteObject> &token)
32 {
33 HILOG_INFO("Init the DistributedExtensionAbility(Base)");
34 AbilityRuntime::ExtensionBase<DistributedExtensionContext>::Init(record, application, handler, token);
35 }
36
Create(const unique_ptr<AbilityRuntime::Runtime> & runtime)37 DistributedExtension *DistributedExtension::Create(const unique_ptr<AbilityRuntime::Runtime> &runtime)
38 {
39 if (!runtime) {
40 HILOG_DEBUG("Create as BackupExtensionAbility(base)");
41 return new DistributedExtension();
42 }
43
44 switch (runtime->GetLanguage()) {
45 case AbilityRuntime::Runtime::Language::JS:
46 HILOG_INFO("Create as DistributedExtensionAbility(JS)");
47 return DistributedExtensionJs::Create(runtime);
48
49 default:
50 HILOG_INFO("Create as DistributedExtensionAbility(base)");
51 return new DistributedExtension();
52 }
53 }
54
OnStart(const AAFwk::Want & want)55 void DistributedExtension::OnStart(const AAFwk::Want &want)
56 {
57 HILOG_INFO("DistributedExtensionAbility was started");
58 Extension::OnStart(want);
59 }
60
OnCommand(const AAFwk::Want & want,bool restart,int startId)61 void DistributedExtension::OnCommand(const AAFwk::Want &want, bool restart, int startId)
62 {
63 HILOG_INFO("DistributedExtensionAbility was invoked. restart=%{public}d, startId=%{public}d", restart, startId);
64 }
65
OnConnect(const AAFwk::Want & want)66 sptr<IRemoteObject> DistributedExtension::OnConnect(const AAFwk::Want &want)
67 {
68 try {
69 HILOG_INFO("DistributedExtension::OnConnect begin connect");
70 Extension::OnConnect(want);
71
72 auto remoteObject = sptr<DistributedExtensionService>(
73 new DistributedExtensionService(std::static_pointer_cast<DistributedExtension>(shared_from_this()),
74 want.GetBundle()));
75 if (remoteObject == nullptr) {
76 HILOG_ERROR("remoteObject is null");
77 return nullptr;
78 }
79 return remoteObject->AsObject();
80 } catch (const exception &e) {
81 HILOG_ERROR("%{public}s", e.what());
82 return nullptr;
83 } catch (...) {
84 HILOG_ERROR("");
85 return nullptr;
86 }
87 }
88
OnDisconnect(const AAFwk::Want & want)89 void DistributedExtension::OnDisconnect(const AAFwk::Want &want)
90 {
91 try {
92 HILOG_INFO("begin disconnect");
93 sptr<DistributedExtensionService> distributedExtensionService = distributedExtensionService_.promote();
94 if (distributedExtensionService != nullptr) {
95 distributedExtensionService->Clear();
96 }
97 Extension::OnDisconnect(want);
98 HILOG_INFO("end");
99 } catch (const exception &e) {
100 HILOG_ERROR("%{public}s", e.what());
101 return;
102 } catch (...) {
103 HILOG_ERROR("");
104 return;
105 }
106 }
107
SetDistributedExtensionService(const wptr<DistributedExtensionService> & distributedExtensionService)108 void DistributedExtension::SetDistributedExtensionService(
109 const wptr<DistributedExtensionService> &distributedExtensionService)
110 {
111 distributedExtensionService_ = distributedExtensionService;
112 }
113
TriggerOnCreate(AAFwk::Want & want)114 int32_t DistributedExtension::TriggerOnCreate(AAFwk::Want& want)
115 {
116 HILOG_INFO("jDistributedExtension::TriggerOnCreate");
117 return ERR_OK;
118 }
119
TriggerOnDestroy()120 int32_t DistributedExtension::TriggerOnDestroy()
121 {
122 HILOG_INFO("DistributedExtension::TriggerOnDestroy");
123 return ERR_OK;
124 }
125
TriggerOnCollaborate(AAFwk::WantParams & wantParam)126 int32_t DistributedExtension::TriggerOnCollaborate(AAFwk::WantParams &wantParam)
127 {
128 HILOG_INFO("DistributedExtension::TriggerOnCollaborate");
129 return ERR_OK;
130 }
131 }
132 }
133