• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import rpc from '@ohos.rpc';
17import { HiLog } from '../../../common/HiLog';
18import Constants from '../../../common/constant';
19import { emitter } from '@kit.BasicServicesKit';
20import OpeningDialogService from '../service/OpeningDialogService';
21
22const TAG = 'OpeningDialogStub';
23
24export default class OpeningDialogStub extends rpc.RemoteObject {
25  constructor(des: string) {
26    super(des);
27  }
28
29  asObject(): rpc.IRemoteObject {
30    return this;
31  }
32
33  private checkParams(code: number, data: rpc.MessageSequence): boolean {
34    if (!code || !data) {
35      HiLog.error(TAG, 'OpeningDialogStub params is invalid');
36      return false;
37    }
38    HiLog.info(TAG, `OpeningDialogStub onRemoteMessageRequest called, code = ${code}`);
39    try {
40      if (data.readInterfaceToken() !== Constants.DLP_MGR_VIEW_ABILITY_TOKEN) {
41        HiLog.error(TAG, 'OpeningDialogStub InterfaceToken unmatched.');
42        return false;
43      }
44    } catch (error) {
45      HiLog.wrapError(TAG, error, 'OpeningDialogStub read data exception');
46      return false;
47    }
48    return true;
49  }
50
51  async onRemoteMessageRequest(code: number, data: rpc.MessageSequence): Promise<boolean> {
52    if (!this.checkParams(code, data)) {
53      HiLog.error(TAG, 'onRemoteMessageRequest checkParams false');
54      return false;
55    }
56    switch (code) {
57      case Constants.COMMAND_SHOW_DIALOG: {
58        HiLog.info(TAG, 'command show dialog');
59        let showDialog: boolean = false;
60        let requestId: string = '';
61        try {
62          showDialog = data.readBoolean();
63          requestId = data.readString();
64        } catch (error) {
65          HiLog.wrapError(TAG, error, 'read boolean or string exception');
66          return false;
67        }
68        const eventData: emitter.EventData = {
69          data: {
70            'showDialog': showDialog,
71            'requestId': requestId
72          }
73        };
74        emitter.emit(Constants.SHOW_DIALOG_EVENT, eventData);
75        return true;
76      }
77      case Constants.COMMAND_DISCONNECT_WITH_TIMEOUT: {
78        await OpeningDialogService.getInstance().disconnectViewAbilityWithTimeout();
79        await OpeningDialogService.getInstance().terminateOpeningDialog();
80        return true;
81      }
82      case Constants.COMMAND_DISCONNECT: {
83        await OpeningDialogService.getInstance().disconnectViewAbility();
84        await OpeningDialogService.getInstance().terminateOpeningDialog();
85        return true;
86      }
87      default: {
88        HiLog.error(TAG, `OpeningDialogStub invalid request code: ${code}`);
89        break;
90      }
91    }
92    return false;
93  }
94}