• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
16import extension from '@ohos.app.ability.ServiceExtensionAbility';
17import type { Permissions } from '@ohos.abilityAccessCtrl';
18import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
19import rpc from '@ohos.rpc';
20import GlobalContext from '../common/GlobalContext';
21import * as notice_sub from '../InjectNotice/InjectNoticeStub';
22import {NOTICE_ID, injectNoticeUtil } from '../InjectNotice/InjectNoticeUtil';
23
24const TAG = 'InjectNotice';
25const ACCESS_TYPE_MASK = 0b11;
26const SHIFT_DIGIT = 27;
27const TOKEN_NATIVE = 1;
28
29export default class InjectNoticeAbility extends extension {
30  /**
31   * Lifecycle function, called back when a service extension is started for initialization.
32   */
33  onCreate(want): void {
34    try {
35      console.debug(TAG, 'InjectNoticeAbility onCreate' + JSON.stringify(want));
36      GlobalContext.getContext().setObject('appcontext', this.context.getApplicationContext());
37      injectNoticeUtil.init();
38    } catch (e) {
39      console.error(TAG, 'InjectNoticeAbility onCreate', e);
40    }
41    console.debug(TAG, 'InjectNoticeAbility onCreate end');
42  }
43
44  onConnect(want): rpc.RemoteObject {
45    let ret: rpc.RemoteObject = null;
46    try {
47      console.debug(TAG, 'onConnect want: ' + JSON.stringify(want));
48      ret = new notice_sub.InjectNoticeStub('InjectNoticeStub');
49    } catch (e) {
50      console.error(TAG, 'InjectNoticeAbility onConnect', e);
51    }
52    return ret;
53  }
54
55  onDisconnect(want): void {
56    console.info(TAG, 'onDisconnect');
57  }
58
59  /**
60   * Lifecycle function, called back when a service extension is started or recall.
61   */
62  onRequest(want, startId): void {
63    try {
64      console.debug(TAG, `onRequest startId: ${startId} want:${JSON.stringify(want)}`);
65      if ('noticeId' in want.parameters) {
66        console.debug(TAG, `onRequest noticeId has value`);
67        if (want.parameters.noticeId === NOTICE_ID) {
68          console.debug(TAG, `onRequest startId: ${startId} close notice`);
69          injectNoticeUtil.cancelAuthorization();
70          injectNoticeUtil.cancelNotificationById(NOTICE_ID);
71          return;
72        }
73      }
74    } catch (e) {
75      console.error(TAG, 'InjectNoticeAbility onRequest', e);
76    }
77  }
78  /**
79   * Lifecycle function, called back before a service extension is destroyed.
80   */
81  onDestroy(): void {
82    console.debug(TAG, 'onDestroy.');
83    injectNoticeUtil.unInit();
84  }
85
86  private isSystemAbility(callingTokenId: number): boolean {
87    let type: number = ACCESS_TYPE_MASK & (callingTokenId >> SHIFT_DIGIT);
88    console.debug(TAG, 'InjectNoticeAbility isSystemAbility, type:' + type);
89    return type === TOKEN_NATIVE;
90  }
91
92  private checkPermission(tokenID: number, permissionName: Permissions): boolean {
93    let aac = abilityAccessCtrl.createAtManager();
94    try {
95      let grantStatus = aac.verifyAccessTokenSync(tokenID, permissionName);
96      if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) {
97        console.error(TAG, `verify ${permissionName} fail`);
98      }
99    } catch (error) {
100      console.error(TAG, `verify ${permissionName}, ${error}`);
101    }
102    console.debug(TAG, `verify ${permissionName}, success`);
103    return true;
104  }
105}