1/* 2 * Copyright (c) 2023-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 16import Vector from '@ohos.util.Vector'; 17import { Log, PrinterCapability } from '@ohos/common'; 18import CheckEmptyUtils from '@ohos/common'; 19import type { PrintServiceAdapter } from '../PrintServiceAdapter'; 20import type DiscoveredPrinter from '../discovery/DiscoveredPrinter'; 21import type { Backend } from '../ipp/Backend'; 22import type uri from '@ohos.uri'; 23 24const TAG = 'CapabilitiesCache'; 25 26export class CapabilitiesCache { 27 private readonly mPrintServiceAdapter: PrintServiceAdapter; 28 private readonly mBackend: Backend; 29 private readonly requestVector: Vector<Request> = new Vector<Request>(); 30 private currentRequest: Request; 31 32 constructor(printServiceAdapter: PrintServiceAdapter) { 33 this.mPrintServiceAdapter = printServiceAdapter; 34 this.mBackend = this.mPrintServiceAdapter.backend; 35 } 36 37 /** 38 * request printer caps 39 * 40 * @param printer 41 * @param onLocalPrinterCapabilities 42 */ 43 public async requestCapabilities(printer: DiscoveredPrinter, onLocalPrinterCapabilities: OnLocalPrinterCapabilities): Promise<void> { 44 if (CheckEmptyUtils.isEmpty<DiscoveredPrinter>(printer)) { 45 Log.error(TAG, 'printer is empty'); 46 return; 47 } 48 if (CheckEmptyUtils.isEmpty<uri.URI>(printer.getUri())) { 49 Log.error(TAG, 'uri is empty'); 50 return; 51 } 52 let uri = printer.getUri(); 53 Log.debug(TAG, 'caps not in cache, request from printer'); 54 let request = new Request(this.mBackend, uri, printer.getDeviceName(), onLocalPrinterCapabilities); 55 this.requestVector.add(request); 56 this.startNextRequest(); 57 } 58 59 private startNextRequest(): void { 60 Log.debug(TAG, 'startNextRequest enter'); 61 if (this.requestVector.length === 0) { 62 Log.error(TAG, 'no active request in requestVector'); 63 return; 64 } 65 if (this.currentRequest !== undefined) { 66 Log.error(TAG, 'a active request is running, requestVector len: ' + this.requestVector.length); 67 return; 68 } 69 this.currentRequest = this.requestVector.removeByIndex(0); 70 if (CheckEmptyUtils.isEmpty<Request>(this.currentRequest)) { 71 Log.error(TAG, 'currentRequest is empty'); 72 return; 73 } 74 this.currentRequest.start(() => { 75 Log.info(TAG, 'Previous request complete, start next request'); 76 this.currentRequest = undefined; 77 this.startNextRequest(); 78 }); 79 } 80 81 public close(): void { 82 Log.info(TAG, 'close'); 83 } 84} 85 86class Request { 87 constructor(private backend: Backend, private uri: uri.URI, private printerName: string, private capsCallback: OnLocalPrinterCapabilities) { 88 this.backend = backend; 89 this.uri = uri; 90 this.printerName = printerName; 91 this.capsCallback = capsCallback; 92 } 93 94 public start(requestCallback: () => void): void { 95 this.backend.getCapabilities(this.uri, this.printerName, this.capsCallback, requestCallback); 96 } 97} 98 99 100/** 101 * Callback for receiving capabilities 102 */ 103export interface OnLocalPrinterCapabilities { 104 105 /** 106 * Called when capabilities are retrieved 107 * 108 * @param capabilities PrinterCapability 109 */ 110 onCapabilities(capabilities: PrinterCapability): void; 111 112}