• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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
16let Context = requireNapi('application.Context');
17let Caller = requireNapi('application.Caller');
18let hilog = requireNapi('hilog');
19
20let domainID = 0xD001320;
21let TAG = 'JSENV';
22
23const ERROR_CODE_INVALID_PARAM = 401;
24const ERROR_MSG_INVALID_PARAM = 'Invalid input parameter.';
25class ParamError extends Error {
26  constructor(msg) {
27    super(msg);
28    this.code = ERROR_CODE_INVALID_PARAM;
29  }
30}
31
32class AbilityContext extends Context {
33  constructor(obj) {
34    super(obj);
35    this.abilityInfo = obj.abilityInfo;
36    this.currentHapModuleInfo = obj.currentHapModuleInfo;
37    this.config = obj.config;
38    this.windowStage = obj.windowStage;
39  }
40
41  onUpdateConfiguration(config) {
42    this.config = config;
43  }
44
45  startAbility(want, options, callback) {
46    return this.__context_impl__.startAbility(want, options, callback);
47  }
48
49  openLink(link, options, callback) {
50    return this.__context_impl__.openLink(link, options, callback);
51  }
52
53  startAbilityAsCaller(want, options, callback) {
54    return this.__context_impl__.startAbilityAsCaller(want, options, callback);
55  }
56
57  startRecentAbility(want, options, callback) {
58    return this.__context_impl__.startRecentAbility(want, options, callback);
59  }
60
61  startAbilityWithAccount(want, accountId, options, callback) {
62    return this.__context_impl__.startAbilityWithAccount(want, accountId, options, callback);
63  }
64
65  startAbilityByCall(want) {
66    return new Promise(async (resolve, reject) => {
67      if (typeof want !== 'object' || want == null) {
68        hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall input param error');
69        reject(new ParamError('Parse param want failed, want must be Want'));
70        return;
71      }
72
73      let callee = null;
74      try {
75        callee = await this.__context_impl__.startAbilityByCall(want);
76      } catch (error) {
77        hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall Obtain remoteObject failed');
78        reject(error);
79        return;
80      }
81
82      resolve(new Caller(callee));
83      hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall success');
84      return;
85    });
86  }
87
88  startAbilityByCallWithAccount(want, accountId) {
89    return new Promise(async (resolve, reject) => {
90      if (typeof want !== 'object' || want == null || typeof accountId !== 'number') {
91        hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall With accountId input param error');
92        reject(new ParamError(
93          'Parse param want or accountId failed, want must be Want and accountId must be number'));
94        return;
95      }
96
97      let callee = null;
98      try {
99        callee = await this.__context_impl__.startAbilityByCall(want, accountId);
100      } catch (error) {
101        hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall With accountId Obtain remoteObject failed');
102        reject(error);
103        return;
104      }
105
106      resolve(new Caller(callee));
107      hilog.sLogI(domainID, TAG, 'AbilityContext::startAbilityByCall With accountId success');
108      return;
109    });
110  }
111
112  startAbilityForResult(want, options, callback) {
113    return this.__context_impl__.startAbilityForResult(want, options, callback);
114  }
115
116  startAbilityForResultWithAccount(want, accountId, options, callback) {
117    return this.__context_impl__.startAbilityForResultWithAccount(want, accountId, options, callback);
118  }
119
120  startUIServiceExtensionAbility(want, callback) {
121    return this.__context_impl__.startUIServiceExtensionAbility(want, callback);
122  }
123
124  connectUIServiceExtensionAbility(want, callback) {
125    return this.__context_impl__.connectUIServiceExtensionAbility(want, callback);
126  }
127
128  disconnectUIServiceExtensionAbility(proxy) {
129    return this.__context_impl__.disconnectUIServiceExtensionAbility(proxy);
130  }
131
132  startServiceExtensionAbility(want, callback) {
133    return this.__context_impl__.startServiceExtensionAbility(want, callback);
134  }
135
136  startServiceExtensionAbilityWithAccount(want, accountId, callback) {
137    return this.__context_impl__.startServiceExtensionAbilityWithAccount(want, accountId, callback);
138  }
139
140  stopServiceExtensionAbility(want, callback) {
141    return this.__context_impl__.stopServiceExtensionAbility(want, callback);
142  }
143
144  stopServiceExtensionAbilityWithAccount(want, accountId, callback) {
145    return this.__context_impl__.stopServiceExtensionAbilityWithAccount(want, accountId, callback);
146  }
147
148  connectServiceExtensionAbility(want, options) {
149    return this.__context_impl__.connectServiceExtensionAbility(want, options);
150  }
151
152  connectAbilityWithAccount(want, accountId, options) {
153    return this.__context_impl__.connectAbilityWithAccount(want, accountId, options);
154  }
155
156  connectServiceExtensionAbilityWithAccount(want, accountId, options) {
157    return this.__context_impl__.connectServiceExtensionAbilityWithAccount(want, accountId, options);
158  }
159
160  disconnectAbility(connection, callback) {
161    return this.__context_impl__.disconnectAbility(connection, callback);
162  }
163
164  disconnectServiceExtensionAbility(connection, callback) {
165    return this.__context_impl__.disconnectServiceExtensionAbility(connection, callback);
166  }
167
168  terminateSelf(callback) {
169    return this.__context_impl__.terminateSelf(callback);
170  }
171
172  isTerminating() {
173    return this.__context_impl__.isTerminating();
174  }
175
176  terminateSelfWithResult(abilityResult, callback) {
177    return this.__context_impl__.terminateSelfWithResult(abilityResult, callback);
178  }
179
180  backToCallerAbilityWithResult(abilityResult, requestCode) {
181    return this.__context_impl__.backToCallerAbilityWithResult(abilityResult, requestCode);
182  }
183
184  restoreWindowStage(contentStorage) {
185    return this.__context_impl__.restoreWindowStage(contentStorage);
186  }
187
188  setMissionContinueState(state, callback) {
189    return this.__context_impl__.setMissionContinueState(state, callback);
190  }
191
192  setMissionLabel(label, callback) {
193    return this.__context_impl__.setMissionLabel(label, callback);
194  }
195
196  setMissionIcon(icon, callback) {
197    return this.__context_impl__.setMissionIcon(icon, callback);
198  }
199
200  setAbilityInstanceInfo(label, callback) {
201    return this.__context_impl__.setAbilityInstanceInfo(label, callback);
202  }
203
204  requestDialogService(want, resultCallback) {
205    return this.__context_impl__.requestDialogService(want, resultCallback);
206  }
207
208  reportDrawnCompleted(callback) {
209    return this.__context_impl__.reportDrawnCompleted(callback);
210  }
211
212  startAbilityByType(type, wantParam, abilityStartCallback, callback) {
213    return this.__context_impl__.startAbilityByType(type, wantParam, abilityStartCallback, callback);
214  }
215
216  requestModalUIExtension(want, callback) {
217    return this.__context_impl__.requestModalUIExtension(want, callback);
218  }
219
220  showAbility() {
221    return this.__context_impl__.showAbility();
222  }
223
224  hideAbility() {
225    return this.__context_impl__.hideAbility();
226  }
227
228  openAtomicService(appId, options, callback) {
229    return this.__context_impl__.openAtomicService(appId, options, callback);
230  }
231
232  moveAbilityToBackground(callback) {
233    return this.__context_impl__.moveAbilityToBackground(callback);
234  }
235
236  setRestoreEnabled(enabled) {
237    this.__context_impl__.setRestoreEnabled(enabled);
238  }
239
240  setColorMode(colorMode) {
241    this.__context_impl__.setColorMode(colorMode);
242  };
243
244  revokeDelegator() {
245    return this.__context_impl__.revokeDelegator();
246  }
247}
248
249export default AbilityContext;
250