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 */ 15import Want from '@ohos.app.ability.Want'; 16import { FileParseType } from '../../bean/data/FileParseType'; 17import Constants from '../../common/constant'; 18import { HiLog } from '../../common/HiLog'; 19import { isInvalidStr, isValidPath } from '../../common/FileUtils/utils'; 20 21const TAG: string = 'OpenDlpFileData'; 22const APPID_FOR_HOPE: string = '5765880207854533797'; 23const BUNDLE_NAME_FOR_HOPE: string = 'com.huawei.it.welink'; 24 25interface DlpConnectionPluginIdObj { 26 id: string 27} 28 29export interface DlpWantParam { 30 bundleName: string, 31 abilityName: string, 32 moduleName: string, 33 uri: string, 34 flags: number, 35 parameters: Record<string, Object> 36} 37 38export default class OpenDlpFileData { 39 private _want: Want; 40 private _startId: number; 41 private _uri: string = ''; 42 private _fileName: string = ''; 43 private _sandboxBundleName: string = ''; 44 private _callerBundleName: string = ''; 45 private _pluginId: string = ''; 46 private _callerAppId: string = ''; 47 private _sandboxAbilityName: string = ''; 48 private _sandboxModuleName: string = ''; 49 private _isFromPlugin: boolean = false; 50 private _requestId: string = ''; 51 private _needShowToast: boolean = false; 52 private _fileParse: FileParseType = FileParseType.ZIP; 53 54 constructor(want: Want, startId: number) { 55 this._want = want; 56 this._startId = startId; 57 } 58 59 public set want(value: Want) { 60 this._want = value; 61 } 62 63 public get want(): Want { 64 return this._want; 65 } 66 67 public set startId(value: number) { 68 this._startId = value; 69 } 70 71 public get startId(): number { 72 return this._startId; 73 } 74 75 public set uri(value: string) { 76 this._uri = value; 77 } 78 79 public get uri(): string { 80 return this._uri; 81 } 82 83 public set fileName(value: string) { 84 this._fileName = value; 85 } 86 87 public get fileName(): string { 88 return this._fileName; 89 } 90 91 public set sandboxBundleName(value: string) { 92 this._sandboxBundleName = value; 93 } 94 95 public get sandboxBundleName(): string { 96 return this._sandboxBundleName; 97 } 98 99 public set callerBundleName(value: string) { 100 this._callerBundleName = value; 101 } 102 103 public get callerBundleName(): string { 104 return this._callerBundleName; 105 } 106 107 public set pluginId(value: string) { 108 this._pluginId = value; 109 } 110 111 public get pluginId(): string { 112 return this._pluginId; 113 } 114 115 public set callerAppId(value: string) { 116 this._callerAppId = value; 117 } 118 119 public get callerAppId(): string { 120 return this._callerAppId; 121 } 122 123 public set sandboxAbilityName(value: string) { 124 this._sandboxAbilityName = value; 125 } 126 127 public get sandboxAbilityName(): string { 128 return this._sandboxAbilityName; 129 } 130 131 public set sandboxModuleName(value: string) { 132 this._sandboxModuleName = value; 133 } 134 135 public get sandboxModuleName(): string { 136 return this._sandboxModuleName; 137 } 138 139 public set isFromPlugin(value: boolean) { 140 this._isFromPlugin = value; 141 } 142 143 public get isFromPlugin(): boolean { 144 return this._isFromPlugin; 145 } 146 147 public set requestId(value: string) { 148 this._requestId = value; 149 } 150 151 public get requestId(): string { 152 return this._requestId; 153 } 154 155 public set needShowToast(value: boolean) { 156 this._needShowToast = value; 157 } 158 159 public get needShowToast(): boolean { 160 return this._needShowToast; 161 } 162 163 public set fileParse(value: FileParseType) { 164 this._fileParse = value; 165 } 166 167 public get fileParse(): FileParseType { 168 return this._fileParse; 169 } 170 171 private checkPermissionWithPluginId(): boolean { 172 if (this._pluginId === null || this._pluginId === undefined) { 173 return true; 174 } 175 176 if (this._callerAppId !== APPID_FOR_HOPE || this._callerBundleName !== BUNDLE_NAME_FOR_HOPE) { 177 HiLog.error(TAG, 'callerAppId or callerBundleName error.'); 178 return false; 179 } 180 181 this._isFromPlugin = true; 182 return true; 183 } 184 185 private CheckSuffixByUri(): boolean { 186 if (this._uri.length < Constants.DLP_FILE_SUFFIX.length) { 187 return false; 188 } 189 190 let fileSuffix: string = this._uri.substring(this._uri.length - Constants.DLP_FILE_SUFFIX.length); 191 let lowerFileSuffix: string = fileSuffix.toLowerCase(); 192 if (lowerFileSuffix === Constants.DLP_FILE_SUFFIX) { 193 return true; 194 } 195 196 return false; 197 } 198 199 public checkAndSetWantParams(): boolean { 200 this._sandboxBundleName = this._want.parameters?.[Constants.PARAMS_BUNDLE_NAME] as string; 201 if (isInvalidStr(this._sandboxBundleName)) { 202 HiLog.error(TAG, 'invalid bundleName in want.parameters'); 203 return false; 204 } 205 206 this._uri = this._want.uri as string; 207 if (isInvalidStr(this._uri) && !isValidPath(this._uri) && !this.CheckSuffixByUri()) { 208 HiLog.error(TAG, 'invalid uri in want.uri'); 209 return false; 210 } 211 212 let strArray: string[] = this._uri.split('/'); 213 this._fileName = strArray[strArray.length - 1]; 214 215 const tempRandom = Date.now() + String(Math.random()).substring(Constants.RAND_START, Constants.RAND_END); 216 this._requestId = this._fileName + tempRandom; 217 HiLog.info(TAG, `checkAndSetWantParams requestId ${this._requestId}`); 218 219 this._callerBundleName = this._want.parameters?.[Constants.PARAMS_CALLER_BUNDLE_NAME] as string; 220 if (isInvalidStr(this._callerBundleName)) { 221 HiLog.error(TAG, 'invalid callerBundleName'); 222 return false; 223 } 224 225 this._pluginId = 226 (this._want.parameters?.[Constants.PARAMS_DLP_CONNECTION_PLUGIN_ID] as DlpConnectionPluginIdObj)?.id; 227 this._callerAppId = this._want.parameters?.[Constants.PARAMS_CALLER_APP_IDENTIFIER] as string; 228 let checkPluginId: boolean = this.checkPermissionWithPluginId(); 229 if (!checkPluginId) { 230 HiLog.error(TAG, 'checkPermissionWithPluginId failed'); 231 return false; 232 } 233 234 this._sandboxAbilityName = this._want.parameters?.[Constants.PARAMS_ABILITY_NAME] as string; 235 if (isInvalidStr(this._sandboxAbilityName)) { 236 HiLog.error(TAG, 'invalid abilityName'); 237 return false; 238 } 239 240 this._sandboxModuleName = this._want.parameters?.[Constants.PARAMS_MODULE_NAME] as string; 241 return true; 242 } 243}