forked from microsoft/accessibility-insights-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard-shortcut-handler.ts
178 lines (149 loc) · 6.76 KB
/
keyboard-shortcut-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { TabContextManager } from 'background/tab-context-manager';
import { Tabs } from 'webextension-polyfill';
import { BrowserAdapter } from '../common/browser-adapters/browser-adapter';
import { CommandsAdapter } from '../common/browser-adapters/commands-adapter';
import { VisualizationConfigurationFactory } from '../common/configs/visualization-configuration-factory';
import { DisplayableStrings } from '../common/constants/displayable-strings';
import { ToggleTelemetryData } from '../common/extension-telemetry-events';
import { Logger } from '../common/logging/logger';
import { Messages } from '../common/messages';
import { NotificationCreator } from '../common/notification-creator';
import { TelemetryDataFactory } from '../common/telemetry-data-factory';
import {
ScanData,
VisualizationStoreData,
} from '../common/types/store-data/visualization-store-data';
import { VisualizationType } from '../common/types/visualization-type';
import { UrlValidator } from '../common/url-validator';
import { DictionaryStringTo } from '../types/common-types';
import { VisualizationTogglePayload } from './actions/action-payloads';
import { UserConfigurationStore } from './stores/global/user-configuration-store';
import { UsageLogger } from './usage-logger';
const VisualizationMessages = Messages.Visualizations;
export class KeyboardShortcutHandler {
private targetTabUrl: string;
private commandToVisualizationType: DictionaryStringTo<VisualizationType>;
constructor(
private tabContextManager: TabContextManager,
private browserAdapter: BrowserAdapter,
private urlValidator: UrlValidator,
private notificationCreator: NotificationCreator,
private visualizationConfigurationFactory: VisualizationConfigurationFactory,
private telemetryDataFactory: TelemetryDataFactory,
private userConfigurationStore: UserConfigurationStore,
private commandsAdapter: CommandsAdapter,
private logger: Logger,
private usageLogger: UsageLogger,
) {}
public initialize(): void {
this.commandToVisualizationType =
this.visualizationConfigurationFactory.getChromeCommandToVisualizationTypeMap();
this.commandsAdapter.addCommandListener(this.onCommand);
}
private onCommand = async (commandId: string): Promise<void> => {
try {
if (this.userConfigurationStore.getState().isFirstTime) {
// Avoid launching functionality until a user has decided whether to allow telemetry
return;
}
this.usageLogger.record();
const currentTab = await this.queryCurrentActiveTab();
if (!currentTab) {
return;
}
const tabId = currentTab.id;
const tabStores = this.tabContextManager.getTabContextStores(tabId);
this.targetTabUrl = currentTab.url;
if (!tabStores) {
return;
}
const hasAccess = await this.checkAccessUrl();
if (hasAccess === false) {
if (UrlValidator.isFileUrl(currentTab.url)) {
this.notificationCreator.createNotification(
DisplayableStrings.fileUrlDoesNotHaveAccess,
);
} else {
this.notificationCreator.createNotification(
DisplayableStrings.urlNotScannable.join('\n'),
);
}
return;
}
const state = tabStores.visualizationStore.getState();
if (state.scanning != null) {
// do not notify if we are already scanning
return;
}
const visualizationType = this.getVisualizationTypeFromCommand(commandId);
if (visualizationType != null) {
this.createEnableNotificationIfCurrentStateIsDisabled(visualizationType, state);
await this.invokeToggleAction(visualizationType, state, tabId);
}
} catch (err) {
this.logger.error('Error occurred at chrome command handler:', err);
}
};
private async checkAccessUrl(): Promise<boolean> {
return await this.urlValidator.isSupportedUrl(this.targetTabUrl);
}
private async queryCurrentActiveTab(): Promise<Tabs.Tab> {
const tabQueryParams: chrome.tabs.QueryInfo = { active: true, currentWindow: true };
const currentActiveTabs = await this.browserAdapter.tabsQuery(tabQueryParams);
if (currentActiveTabs && currentActiveTabs[0]) {
return currentActiveTabs[0];
}
return null;
}
private getVisualizationTypeFromCommand(commandId: string): VisualizationType {
return this.commandToVisualizationType[commandId];
}
private createEnableNotificationIfCurrentStateIsDisabled(
visualizationType: VisualizationType,
data: VisualizationStoreData,
): void {
if (!this.shouldNotifyOnEnable(visualizationType)) {
return;
}
const configuration =
this.visualizationConfigurationFactory.getConfiguration(visualizationType);
const scanData = configuration.getStoreData(data.tests);
if (!scanData.enabled) {
const displayableVisualizationData = configuration.displayableData;
if (displayableVisualizationData?.adHoc?.enableMessage) {
this.notificationCreator.createNotification(
displayableVisualizationData.adHoc.enableMessage,
);
}
}
}
private shouldNotifyOnEnable(visualizationType: VisualizationType): boolean {
return visualizationType !== VisualizationType.TabStops;
}
private async invokeToggleAction(
visualizationType: VisualizationType,
state: VisualizationStoreData,
tabId: number,
): Promise<void> {
const configuration =
this.visualizationConfigurationFactory.getConfiguration(visualizationType);
const scanData: ScanData = configuration.getStoreData(state.tests);
const action = VisualizationMessages.Common.Toggle;
const toEnabled = !scanData.enabled;
const telemetryInfo: ToggleTelemetryData =
this.telemetryDataFactory.forVisualizationToggleByCommand(toEnabled);
const payload: VisualizationTogglePayload = {
enabled: toEnabled,
telemetry: telemetryInfo,
test: visualizationType,
};
const response = this.tabContextManager.interpretMessageForTab(tabId, {
messageType: action,
payload,
tabId: tabId,
});
await response.result;
}
}