import { WebViewWindowController } from './webViewWindowController'; import { ThumbnailImage, ThumbnailRequest, ThumbnailSize, ThumbnailSource } from '@lenovo-software/lsa-clients-common/dist/models/thumbnail'; import { extensionComm } from './extensionComm'; import { ScreenCapture, ScreenCaptureRequest, ScreenCaptureResponse, ScreenCaptureResponseFactory, ScreenSource } from './screenCapture'; import { IThumbnailInstrumentation } from '@lenovo-software/lsa-clients-common'; export class ThumbnailInstrumentation extends ScreenCapture implements IThumbnailInstrumentation { constructor(private webViewWindowController: WebViewWindowController) { // Name of the external event that is received from web helper extension for Active tab image. const externalExtensionEventName = 'thumbnail_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); } /** * Handle telemetry request for thumbnail. * * @param request Thumbnail request * * @returns Promise */ public async getThumbnail(request: ThumbnailRequest): Promise { const width = request.size ? request.size.width : 480; const height = request.size ? request.size.height : 240; let source; if (request.source === ThumbnailSource.FullScreen) { source = ScreenSource.FullScreen; } else { source = ScreenSource.ActiveTab; } const response: ScreenCaptureResponse = await this.getScreenCapture({ width: width, height: height, source: source }); const thumbnailImage: ThumbnailImage = new ThumbnailImage(); thumbnailImage.type = 'base64'; thumbnailImage.source = request.source; thumbnailImage.size = new ThumbnailSize(); thumbnailImage.size.width = response.width === undefined ? 0 : response.width; thumbnailImage.size.height = response.height === undefined ? 0 : response.height; thumbnailImage.image = response.imageBase64; return thumbnailImage; } public isWaitingForFullScreenPermission(): boolean { return this.webViewWindowController.isStudentWaitingForFullScreenPermission(); } /** * Get full screen thumbnail from the system. * This involves following * 1. StartThumbnailCapture is not started * 2. Request user permission to capture screen * 3. Generate thumbnail for specific size as passed in request * * @param request {ScreenCaptureRequest} Screen capture request object * * @returns Promise */ protected async captureFullScreen(request: ScreenCaptureRequest): Promise { return new Promise((resolve, reject) => { this.logger.logMessage('Getting full screen thumbnail from system.'); // Sends a message to webview for generating the thumbnail. The callback function captures // the received thumbnail.In case a thumbnail is not received or user has not granted // the screen permission the system will start the thumbnail capturing flow again. chrome.runtime.sendMessage( { message: 'GetFullScreenThumbnailSync', params: { width: request.width, height: request.height, format: 'jpeg' } }, (imageData) => { // Checks if the imageData is present and imagedata.buffer which // carry the thumbnail in base64 is set. // In case the thumbnail is not received the imageData.buffer will // not have the thumbnail base64 string. if (imageData && imageData.buffer) { this.logger.logMessage(`Full screen thumbnail received.`); resolve( ScreenCaptureResponseFactory.create(imageData.buffer, imageData.width, imageData.height) ); } else { this.logger.logMessage(`Thumbnail not received. Starting thumbnail capture.`); // Restart the thumbnail capturing provisioning. chrome.runtime.sendMessage({ message: 'StartThumbnailCapture', data: { source: 'Full Screen' } }); reject(); } } ); }); } /** * Get active tab thumbnail from chrome extension. * 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} Screen capture request object * * @returns Promise */ protected async captureActiveTab(request: ScreenCaptureRequest): Promise { this.logger.logMessage('Getting active tab thumbnail from web helper extension.'); await extensionComm.sendToWebHelper( { message: 'get_screenthumbnail', width: request.width, height: request.height, forat: 'jpeg' }, () => {} ); } }