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 return remoteObject->AsObject();
76 } catch (const exception &e) {
77 HILOG_ERROR("%{public}s", e.what());
78 return nullptr;
79 } catch (...) {
80 HILOG_ERROR("");
81 return nullptr;
82 }
83 }
84
OnDisconnect(const AAFwk::Want & want)85 void DistributedExtension::OnDisconnect(const AAFwk::Want &want)
86 {
87 try {
88 HILOG_INFO("begin disconnect");
89 sptr<DistributedExtensionService> distributedExtensionService = distributedExtensionService_.promote();
90 if (distributedExtensionService != nullptr) {
91 distributedExtensionService->Clear();
92 }
93 Extension::OnDisconnect(want);
94 HILOG_INFO("end");
95 } catch (const exception &e) {
96 HILOG_ERROR("%{public}s", e.what());
97 return;
98 } catch (...) {
99 HILOG_ERROR("");
100 return;
101 }
102 }
103
SetDistributedExtensionService(const wptr<DistributedExtensionService> & distributedExtensionService)104 void DistributedExtension::SetDistributedExtensionService(
105 const wptr<DistributedExtensionService> &distributedExtensionService)
106 {
107 distributedExtensionService_ = distributedExtensionService;
108 }
109
TriggerOnCreate(AAFwk::Want & want)110 int32_t DistributedExtension::TriggerOnCreate(AAFwk::Want& want)
111 {
112 HILOG_INFO("jDistributedExtension::TriggerOnCreate");
113 return ERR_OK;
114 }
115
TriggerOnDestroy()116 int32_t DistributedExtension::TriggerOnDestroy()
117 {
118 HILOG_INFO("DistributedExtension::TriggerOnDestroy");
119 return ERR_OK;
120 }
121
TriggerOnCollaborate(AAFwk::WantParams & wantParam)122 int32_t DistributedExtension::TriggerOnCollaborate(AAFwk::WantParams &wantParam)
123 {
124 HILOG_INFO("DistributedExtension::TriggerOnCollaborate");
125 return ERR_OK;
126 }
127 }
128 }
129