/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ let cert = requireInternal('security.cert'); let webview = requireInternal('web.webview'); let accessControl = requireNapi('abilityAccessCtrl'); const PARAM_CHECK_ERROR = 401; const ERROR_MSG_INVALID_PARAM = 'Invalid input parameter'; let errMsgMap = new Map(); errMsgMap.set(PARAM_CHECK_ERROR, ERROR_MSG_INVALID_PARAM); class BusinessError extends Error { constructor(code) { let msg = errMsgMap.get(code); super(msg); this.code = code; } } function getCertificatePromise(certChainData) { let x509CertArray = []; if (!(certChainData instanceof Array)) { console.log('failed, cert chain data type is not array'); return Promise.all(x509CertArray); } for (let i = 0; i < certChainData.length; i++) { let encodeBlobData = { data: certChainData[i], encodingFormat: cert.EncodingFormat.FORMAT_DER }; x509CertArray[i] = cert.createX509Cert(encodeBlobData); } return Promise.all(x509CertArray); } Object.defineProperty(webview.WebviewController.prototype, 'getCertificate', { value: function (callback) { if (arguments.length !== 0 && arguments.length !== 1) { throw new BusinessError(PARAM_CHECK_ERROR); } let certChainData = this.innerGetCertificate(); if (callback === undefined) { console.log('get certificate promise'); return getCertificatePromise(certChainData); } else { console.log('get certificate async callback'); if (typeof callback !== 'function') { throw new BusinessError(PARAM_CHECK_ERROR); } getCertificatePromise(certChainData).then(x509CertArray => { callback(undefined, x509CertArray); }).catch(error => { callback(error, undefined); }); } } }); Object.defineProperty(webview.WebviewController.prototype, 'requestPermissionsFromUserWeb', { value: function (callback) { let accessManger = accessControl.createAtManager(); let abilityContext = getContext(this); accessManger.requestPermissionsFromUser(abilityContext, ['ohos.permission.READ_PASTEBOARD']) .then((PermissionRequestResult) => { if (PermissionRequestResult.authResults[0] === 0) { console.log('requestPermissionsFromUserWeb is allowed'); callback.request.grant(callback.request.getAccessibleResource()); } else { console.log('requestPermissionsFromUserWeb is refused'); callback.request.deny(); } }) .catch((error) => { callback.request.deny(); }); } }); export default webview;