1/* 2 * Copyright (c) 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 cert = requireInternal('security.cert'); 17let webview = requireInternal('web.webview'); 18let accessControl = requireNapi('abilityAccessCtrl'); 19const PARAM_CHECK_ERROR = 401; 20 21const ERROR_MSG_INVALID_PARAM = 'Invalid input parameter'; 22 23let errMsgMap = new Map(); 24errMsgMap.set(PARAM_CHECK_ERROR, ERROR_MSG_INVALID_PARAM); 25 26class BusinessError extends Error { 27 constructor(code) { 28 let msg = errMsgMap.get(code); 29 super(msg); 30 this.code = code; 31 } 32} 33 34function getCertificatePromise(certChainData) { 35 let x509CertArray = []; 36 if (!(certChainData instanceof Array)) { 37 console.log('failed, cert chain data type is not array'); 38 return Promise.all(x509CertArray); 39 } 40 41 for (let i = 0; i < certChainData.length; i++) { 42 let encodeBlobData = { 43 data: certChainData[i], 44 encodingFormat: cert.EncodingFormat.FORMAT_DER 45 }; 46 x509CertArray[i] = cert.createX509Cert(encodeBlobData); 47 } 48 49 return Promise.all(x509CertArray); 50} 51 52Object.defineProperty(webview.WebviewController.prototype, 'getCertificate', { 53 value: function (callback) { 54 if (arguments.length !== 0 && arguments.length !== 1) { 55 throw new BusinessError(PARAM_CHECK_ERROR); 56 } 57 58 let certChainData = this.innerGetCertificate(); 59 if (callback === undefined) { 60 console.log('get certificate promise'); 61 return getCertificatePromise(certChainData); 62 } else { 63 console.log('get certificate async callback'); 64 if (typeof callback !== 'function') { 65 throw new BusinessError(PARAM_CHECK_ERROR); 66 } 67 getCertificatePromise(certChainData).then(x509CertArray => { 68 callback(undefined, x509CertArray); 69 }).catch(error => { 70 callback(error, undefined); 71 }); 72 } 73 } 74}); 75 76Object.defineProperty(webview.WebviewController.prototype, 'requestPermissionsFromUserWeb', { 77 value: function (callback) { 78 let accessManger = accessControl.createAtManager(); 79 let abilityContext = getContext(this); 80 accessManger.requestPermissionsFromUser(abilityContext, ['ohos.permission.READ_PASTEBOARD']) 81 .then((PermissionRequestResult) => { 82 if (PermissionRequestResult.authResults[0] === 0) { 83 console.log('requestPermissionsFromUserWeb is allowed'); 84 callback.request.grant(callback.request.getAccessibleResource()); 85 } 86 else { 87 console.log('requestPermissionsFromUserWeb is refused'); 88 callback.request.deny(); 89 } 90 }) 91 .catch((error) => { 92 callback.request.deny(); 93 }); 94 } 95}); 96 97export default webview; 98