import { LSAClient } from '@lenovo-software/lsa-clients-common'; import { CurrentScreenRequestEventModel } from '@lenovo-software/lsa-clients-common/dist/models/eventModels'; import { extensionComm } from './extensionComm'; import { ScreenCapture, ScreenCaptureRequest, ScreenCaptureResponse, ScreenCaptureResponseFactory } from './screenCapture'; export class ScreenshotCaptureService extends ScreenCapture { private displayName = ''; private overlayUserName = false; private overlayDateTime = false; constructor() { // Name of the external event that is received from web helper extension for Active tab image. const externalExtensionEventName = 'screencap_response'; // Path of generic image that will be send in response in case of failure in screen capture. const genericImagePath = './images/thumbnail.png'; // Call the super constructor to configure and instantiate the super class super(externalExtensionEventName, genericImagePath); } /** * Processes screen capture event and send response to user for the same. * @param data */ public async processCurrentScreenRequest(data: CurrentScreenRequestEventModel): Promise { this.logger.logMessage('Received onCurrentScreenRequest = data: ' + JSON.stringify(data)); this.displayName = LSAClient.getInstance().storage.loadDisplayName(); this.overlayUserName = data.overlayUserName; this.overlayDateTime = data.overlayTimeDate; return this.getScreenCapture({ source: ScreenCapture.lastScreenSource }); } /** * Get full screen capture from the system. * * @param request {ScreenCaptureRequest} Screen capture request object * * @returns Promise * */ protected async captureFullScreen(request: ScreenCaptureRequest): Promise { return new Promise((resolve, reject) => { this.logger.logMessage('Getting fullscreen capture from system.'); // Sends a message to webview for getting full screen shot. The callback function captures // the received screenshot.In case a screenshot is not received or user has not granted // the screen permission the system will start the thumbnail capturing flow again. chrome.runtime.sendMessage( { message: 'GetFullScreenCapSync', params: { text: this.getOverlayMessage() } }, (imageData) => { // Checks if the imageData is present and imagedata.imageBase64 which // carry the screenshot in base64 is set. // In case the screenshot is not received the imageData.imageBase64 will // not have the screenshot base64 string. if (imageData && imageData.imageBase64 !== '') { this.logger.logMessage(`Screenshot received.`); resolve( ScreenCaptureResponseFactory.create( imageData.imageBase64, imageData.width, imageData.height ) ); } else { this.logger.logMessage(`Screenshot not received. Starting screenshot capture.`); // Restart the screenshot capturing provisioning. chrome.runtime.sendMessage({ message: 'StartThumbnailCapture', data: { source: 'Full Screen' } }); reject(); } } ); }); } /** * Get active tab screenshot capture. * This involves following: * 1. Send a message to web_helper_extension * 2. Wait for response form extension in case we receive the acknowledgement from extension * * @param request {ScreenCaptureRequest} Screeb capture request object * * @returns Promise */ protected async captureActiveTab(request: ScreenCaptureRequest): Promise { this.logger.logMessage('Getting active tab screen shot from web helper extension.'); await extensionComm.sendToWebHelper( { message: 'get_screencapture', overlayMessage: this.getOverlayMessage(), format: 'jpeg' }, () => {} ); } /** * Prepare screen overlay message to be printed on screen image. * * @returns string */ private getOverlayMessage(): string { let message = ''; if (this.overlayUserName && this.overlayDateTime) message = this.displayName + ' @ ' + this.logger.timeStampMS(false); else if (this.overlayUserName) { message = this.displayName; } else if (this.overlayDateTime) { message = this.logger.timeStampMS(false); } return message; } }