-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathextensionWorkerBase.ts
664 lines (568 loc) · 26.2 KB
/
extensionWorkerBase.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import {BrowserUtils} from "../browserUtils";
import {ClientInfo} from "../clientInfo";
import {ClientType} from "../clientType";
import {ClipperUrls} from "../clipperUrls";
import {Constants} from "../constants";
import {Polyfills} from "../polyfills";
import {AuthType, UserInfo, UpdateReason} from "../userInfo";
import {Settings} from "../settings";
import {TooltipProps} from "../clipperUI/tooltipProps";
import {TooltipType} from "../clipperUI/tooltipType";
import {Communicator} from "../communicator/communicator";
import {MessageHandler} from "../communicator/messageHandler";
import {SmartValue} from "../communicator/smartValue";
import {ClipperCachedHttp} from "../http/clipperCachedHttp";
import {LocalizationHelper} from "../localization/localizationHelper";
import * as Log from "../logging/log";
import {LogHelpers} from "../logging/logHelpers";
import {SessionLogger} from "../logging/sessionLogger";
import {ClipperData} from "../storage/clipperData";
import {ClipperStorageKeys} from "../storage/clipperStorageKeys";
import {ChangeLog} from "../versioning/changeLog";
import {AuthenticationHelper} from "./authenticationHelper";
import {ExtensionBase} from "./extensionBase";
import {InvokeInfo} from "./invokeInfo";
import {InvokeSource} from "./invokeSource";
import {InvokeMode, InvokeOptions} from "./invokeOptions";
/**
* The abstract base class for all of the extension workers
*/
export abstract class ExtensionWorkerBase<TTab, TTabIdentifier> {
private onUnloading: () => void;
private loggerId: string;
private clipperFunnelAlreadyLogged = false;
private keepAlive: number;
protected consoleOutputEnabledFlagProcessed: Promise<void>;
protected tab: TTab;
protected tabId: TTabIdentifier;
protected auth: AuthenticationHelper;
protected clipperData: ClipperData;
protected uiCommunicator: Communicator;
protected pageNavUiCommunicator: Communicator;
protected debugLoggingInjectCommunicator: Communicator;
protected injectCommunicator: Communicator;
protected pageNavInjectCommunicator: Communicator;
protected logger: SessionLogger;
protected clientInfo: SmartValue<ClientInfo>;
protected sessionId: SmartValue<string>;
constructor(clientInfo: SmartValue<ClientInfo>, auth: AuthenticationHelper, clipperData: ClipperData, uiMessageHandlerThunk: () => MessageHandler, injectMessageHandlerThunk: () => MessageHandler) {
Polyfills.init();
this.onUnloading = () => { };
this.uiCommunicator = new Communicator(uiMessageHandlerThunk(), Constants.CommunicationChannels.extensionAndUi);
this.pageNavUiCommunicator = new Communicator(uiMessageHandlerThunk(), Constants.CommunicationChannels.extensionAndPageNavUi);
this.debugLoggingInjectCommunicator = new Communicator(injectMessageHandlerThunk(), Constants.CommunicationChannels.debugLoggingInjectedAndExtension);
this.injectCommunicator = new Communicator(injectMessageHandlerThunk(), Constants.CommunicationChannels.injectedAndExtension);
this.pageNavInjectCommunicator = new Communicator(injectMessageHandlerThunk(), Constants.CommunicationChannels.pageNavInjectedAndExtension);
this.sessionId = new SmartValue<string>();
this.clipperData = clipperData;
this.auth = auth;
this.clientInfo = clientInfo;
this.consoleOutputEnabledFlagProcessed = LogHelpers.isConsoleOutputEnabled().then((isConsoleOutputEnabled) => {
this.logger = LogManager.createExtLogger(this.sessionId, isConsoleOutputEnabled ? this.debugLoggingInjectCommunicator : undefined);
this.logger.logSessionStart();
this.clipperData.setLogger(this.logger);
this.initializeCommunicators();
this.initializeContextProperties();
});
}
private initializeContextProperties() {
let clientInfo = this.clientInfo.get();
this.logger.setContextProperty(Log.Context.Custom.AppInfoId, Settings.getSetting("App_Id"));
this.logger.setContextProperty(Log.Context.Custom.ExtensionLifecycleId, ExtensionBase.getExtensionId());
this.logger.setContextProperty(Log.Context.Custom.UserInfoId, undefined);
this.logger.setContextProperty(Log.Context.Custom.AuthType, "None");
this.logger.setContextProperty(Log.Context.Custom.AppInfoVersion, clientInfo.clipperVersion);
this.logger.setContextProperty(Log.Context.Custom.DeviceInfoId, clientInfo.clipperId);
this.logger.setContextProperty(Log.Context.Custom.ClipperType, ClientType[clientInfo.clipperType]);
// Sometimes the worker is created really early (e.g., pageNav, inline extension), so we need to wait
// for flighting info to be returned before we set the context property
if (!clientInfo.flightingInfo) {
let clientInfoSetCb = ((newClientInfo) => {
if (newClientInfo.flightingInfo) {
this.clientInfo.unsubscribe(clientInfoSetCb);
this.logger.setContextProperty(Log.Context.Custom.FlightInfo, newClientInfo.flightingInfo.join(","));
}
}).bind(this);
this.clientInfo.subscribe(clientInfoSetCb, { callOnSubscribe: false });
} else {
this.logger.setContextProperty(Log.Context.Custom.FlightInfo, clientInfo.flightingInfo.join(","));
}
}
private setKeepAlive() {
if (!!this.keepAlive) {
clearInterval(this.keepAlive);
}
this.keepAlive = setInterval(chrome.runtime.getPlatformInfo, 25 * 1000);
// Ensure to clear the interval after 10 minutes if it hasn't been cleared already
setTimeout(() => {
if (!!this.keepAlive) {
clearInterval(this.keepAlive);
this.keepAlive = undefined;
/**
* If we reach this point, it means that the keep alive interval was not cleared within 10 minutes
* which is an indication that the clipper ux is not being used. We will now allow the service worker
* to become inactive and notifiy the user to refresh the page.
*/
this.injectCommunicator.callRemoteFunction(Constants.FunctionKeys.showRefreshClipperMessage, {
param: "Communicator " + Constants.CommunicationChannels.injectedAndExtension + " caught an error: " +
"Clipper UX was not used for 10 minutes."
});
}
}, 10 * 60 * 1000);
}
/**
* Get the unique id associated with this worker's tab. The type is any type that allows us to distinguish
* between tabs, and is dependent on the browser itself.
*/
public getUniqueId(): TTabIdentifier {
return this.tabId;
}
/**
* Get the url associated with this worker's tab
*/
public abstract getUrl(): string;
/**
* Launches the sign in window, rejecting with an error object if something went wrong on the server during
* authentication. Otherwise, it resolves with true if the redirect endpoint was hit as a result of a successful
* sign in attempt, and false if it was not hit (e.g., user manually closed the popup)
*/
protected abstract doSignInAction(authType: AuthType): Promise<boolean>;
/**
* Signs the user out
*/
protected abstract doSignOutAction(authType: AuthType);
/**
* Notify the UI to invoke the clipper. Resolve with true if it was thought to be successfully
* injected; otherwise resolves with false. Also performs logging.
*/
protected abstract invokeClipperBrowserSpecific(): Promise<boolean>;
/**
* Notify the UI to invoke the frontend script that handles logging to the conosle. Resolve with
* true if it was thought to be successfully injected; otherwise resolves with false.
*/
protected abstract invokeDebugLoggingBrowserSpecific(): Promise<boolean>;
/**
* Notify the UI to invoke the What's New tooltip. Resolve with true if it was thought to be successfully
* injected; otherwise resolves with false.
*/
protected abstract invokeWhatsNewTooltipBrowserSpecific(newVersions: ChangeLog.Update[]): Promise<boolean>;
/**
* Notify the UI to invoke a specific tooltip. Resolve with true if successfully injected, and false otherwise;
*/
protected abstract invokeTooltipBrowserSpecific(tooltipType: TooltipType): Promise<boolean>;
/**
* Returns true if the user has allowed our extension to access file:/// links. Edge does not have a function to
* check this as of 10/3/2016
*/
protected abstract isAllowedFileSchemeAccessBrowserSpecific(callback: (isAllowed: boolean) => void): void;
/**
* Gets the visible tab's screenshot as an image url
*/
protected abstract takeTabScreenshot(): Promise<string>;
/**
* Closes all active frames and notifies the UI to invoke the clipper.
*/
public closeAllFramesAndInvokeClipper(invokeInfo: InvokeInfo, options: InvokeOptions) {
this.pageNavInjectCommunicator.callRemoteFunction(Constants.FunctionKeys.closePageNavTooltip);
this.invokeClipper(invokeInfo, options);
}
public getLogger() {
return this.logger;
}
/**
* Skeleton method that notifies the UI to invoke the Clipper. Also performs logging.
*/
public invokeClipper(invokeInfo: InvokeInfo, options: InvokeOptions) {
this.setKeepAlive();
// For safety, we enforce that the object we send is never undefined.
let invokeOptionsToSend: InvokeOptions = {
invokeDataForMode: options ? options.invokeDataForMode : undefined,
invokeMode: options ? options.invokeMode : InvokeMode.Default
};
this.sendInvokeOptionsToInject(invokeOptionsToSend);
this.isAllowedFileSchemeAccessBrowserSpecific((isAllowed) => {
if (!isAllowed) {
this.uiCommunicator.callRemoteFunction(Constants.FunctionKeys.extensionNotAllowedToAccessLocalFiles);
}
});
Promise.all([this.consoleOutputEnabledFlagProcessed, this.invokeClipperBrowserSpecific()]).then(([v, wasInvoked]) => {
if (wasInvoked && !this.clipperFunnelAlreadyLogged) {
this.logger.logUserFunnel(Log.Funnel.Label.Invoke);
this.clipperFunnelAlreadyLogged = true;
}
this.logClipperInvoke(invokeInfo, invokeOptionsToSend);
});
}
/**
* Notify the UI to invoke the What's New experience. Resolve with true if it was thought to be successfully
* injected; otherwise resolves with false.
*/
public invokeWhatsNewTooltip(newVersions: ChangeLog.Update[]): Promise<boolean> {
let invokeWhatsNewEvent = new Log.Event.PromiseEvent(Log.Event.Label.InvokeWhatsNew);
return this.registerLocalizedStringsForPageNav().then((successful) => {
if (successful) {
this.registerWhatsNewCommunicatorFunctions(newVersions);
return this.invokeWhatsNewTooltipBrowserSpecific(newVersions).then((wasInvoked) => {
if (!wasInvoked) {
invokeWhatsNewEvent.setStatus(Log.Status.Failed);
invokeWhatsNewEvent.setFailureInfo({ error: "invoking the What's New experience failed" });
}
this.logger.logEvent(invokeWhatsNewEvent);
return Promise.resolve(wasInvoked);
});
} else {
invokeWhatsNewEvent.setStatus(Log.Status.Failed);
invokeWhatsNewEvent.setFailureInfo({ error: "getLocalizedStringsForBrowser returned undefined/null" });
this.logger.logEvent(invokeWhatsNewEvent);
return Promise.resolve(false);
}
});
}
public invokeTooltip(tooltipType: TooltipType): Promise<boolean> {
let tooltipInvokeEvent = new Log.Event.PromiseEvent(Log.Event.Label.InvokeTooltip);
tooltipInvokeEvent.setCustomProperty(Log.PropertyName.Custom.TooltipType, TooltipType[tooltipType]);
return this.registerLocalizedStringsForPageNav().then((successful) => {
if (successful) {
this.registerTooltipCommunicatorFunctions(tooltipType);
return this.invokeTooltipBrowserSpecific(tooltipType).then((wasInvoked) => {
this.logger.logEvent(tooltipInvokeEvent);
return Promise.resolve(wasInvoked);
});
} else {
tooltipInvokeEvent.setStatus(Log.Status.Failed);
tooltipInvokeEvent.setFailureInfo({ error: "getLocalizedStringsForBrowser returned undefined/null" });
this.logger.logEvent(tooltipInvokeEvent);
return Promise.resolve(false);
}
});
}
/**
* Sets the hook method that will be called when this worker object goes away.
*/
public setOnUnloading(callback: () => void) {
this.onUnloading = callback;
}
/**
* Clean up anything related to the worker before it stops being used (aka the tab or window was closed)
*/
public destroy() {
this.logger.logSessionEnd(Log.Session.EndTrigger.Unload);
}
/**
* Returns the current version of localized strings used in the UI.
*/
protected getLocalizedStrings(locale: string, callback?: Function) {
this.logger.setContextProperty(Log.Context.Custom.BrowserLanguage, locale);
this.clipperData.getValue(ClipperStorageKeys.locale).then((storedLocale) => {
let localeInStorageIsDifferent = !storedLocale || storedLocale !== locale;
let getLocaleEvent = new Log.Event.BaseEvent(Log.Event.Label.GetLocale);
getLocaleEvent.setCustomProperty(Log.PropertyName.Custom.StoredLocaleDifferentThanRequested, localeInStorageIsDifferent);
this.logger.logEvent(getLocaleEvent);
let fetchStringDataFunction = () => { return LocalizationHelper.makeLocStringsFetchRequest(locale); };
let updateInterval = localeInStorageIsDifferent ? 0 : ClipperCachedHttp.getDefaultExpiry();
let getLocalizedStringsEvent = new Log.Event.PromiseEvent(Log.Event.Label.GetLocalizedStrings);
getLocalizedStringsEvent.setCustomProperty(Log.PropertyName.Custom.ForceRetrieveFreshLocStrings, localeInStorageIsDifferent);
this.clipperData.getFreshValue(ClipperStorageKeys.locStrings, fetchStringDataFunction, updateInterval).then((response) => {
this.clipperData.setValue(ClipperStorageKeys.locale, locale);
if (callback) {
callback(response ? response.data : undefined);
}
}, (error: OneNoteApi.GenericError) => {
getLocalizedStringsEvent.setStatus(Log.Status.Failed);
getLocalizedStringsEvent.setFailureInfo(error);
// Still proceed, as we have backup strings on the client
if (callback) {
callback(undefined);
}
}).then(() => {
this.logger.logEvent(getLocalizedStringsEvent);
});
});
}
protected getLocalizedStringsForBrowser(callback: Function) {
this.clipperData.getValue(ClipperStorageKeys.displayLanguageOverride).then((localeOverride) => {
// navigator.userLanguage is only available in IE, and Typescript will not recognize this property
let locale = localeOverride || navigator.language || (<any>navigator).userLanguage;
this.getLocalizedStrings(locale, callback);
});
}
protected getUserSessionIdQueryParamValue(): string {
let usidQueryParamValue = this.logger.getUserSessionId();
return usidQueryParamValue ? usidQueryParamValue : this.clientInfo.get().clipperId;
}
protected async invokeDebugLoggingIfEnabled(): Promise<boolean> {
if (await LogHelpers.isConsoleOutputEnabled()) {
return this.invokeDebugLoggingBrowserSpecific();
}
return Promise.resolve(false);
}
protected launchPopupAndWaitForClose(url: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
let signInWindow: Window = BrowserUtils.openPopupWindow(url);
let errorObject;
let popupMessageHandler = (event: MessageEvent) => {
if (event.source === signInWindow) {
let dataAsJson: any;
try {
dataAsJson = JSON.parse(event.data);
} catch (e) {
this.logger.logJsonParseUnexpected(event.data);
}
if (dataAsJson && (dataAsJson[Constants.Urls.QueryParams.error] || dataAsJson[Constants.Urls.QueryParams.errorDescription])) {
errorObject = {
correlationId: dataAsJson[Constants.Urls.QueryParams.correlationId],
error: dataAsJson[Constants.Urls.QueryParams.error],
errorDescription: dataAsJson[Constants.Urls.QueryParams.errorDescription]
};
}
}
};
window.addEventListener("message", popupMessageHandler);
let timer = setInterval(() => {
if (!signInWindow || signInWindow.closed) {
clearInterval(timer);
window.removeEventListener("message", popupMessageHandler);
// We always resolve with true in the non-error case as we can't reliably detect redirects
// on non-IE bookmarklets
errorObject ? reject(errorObject) : resolve(true);
}
}, 100);
});
}
protected logClipperInvoke(invokeInfo: InvokeInfo, options: InvokeOptions) {
let invokeClipperEvent = new Log.Event.BaseEvent(Log.Event.Label.InvokeClipper);
invokeClipperEvent.setCustomProperty(Log.PropertyName.Custom.InvokeSource, InvokeSource[invokeInfo.invokeSource]);
invokeClipperEvent.setCustomProperty(Log.PropertyName.Custom.InvokeMode, InvokeMode[options.invokeMode]);
this.logger.logEvent(invokeClipperEvent);
}
/**
* Registers the tooltip type that needs to appear in the Page Nav experience, as well as any props it needs
*/
protected registerTooltipToRenderInPageNav(tooltipType: TooltipType, tooltipProps?: any) {
this.pageNavUiCommunicator.registerFunction(Constants.FunctionKeys.getTooltipToRenderInPageNav, () => {
return Promise.resolve(tooltipType);
});
this.pageNavUiCommunicator.registerFunction(Constants.FunctionKeys.getPageNavTooltipProps, () => {
return Promise.resolve(tooltipProps);
});
}
/**
* Register communicator functions specific to the What's New experience
*/
protected registerWhatsNewCommunicatorFunctions(newVersions: ChangeLog.Update[]) {
this.registerTooltipToRenderInPageNav(TooltipType.WhatsNew, {
updates: newVersions
} as TooltipProps.WhatsNew);
}
protected registerTooltipCommunicatorFunctions(tooltipType: TooltipType) {
this.registerTooltipToRenderInPageNav(tooltipType);
}
protected sendInvokeOptionsToInject(options: InvokeOptions) {
this.injectCommunicator.callRemoteFunction(Constants.FunctionKeys.setInvokeOptions, {
param: options
});
}
protected setUpNoOpTrackers(url: string) {
// No-op tracker for communication with inject
let injectNoOpTrackerTimeout = Log.ErrorUtils.setNoOpTrackerRequestTimeout({
label: Log.NoOp.Label.InitializeCommunicator,
channel: Constants.CommunicationChannels.injectedAndExtension,
clientInfo: this.clientInfo,
url: url
}, true);
this.injectCommunicator.callRemoteFunction(Constants.FunctionKeys.noOpTracker, {
param: new Date().getTime(),
callback: () => {
clearTimeout(injectNoOpTrackerTimeout);
}
});
// No-op tracker for communication with the UI
let uiNoOpTrackerTimeout = Log.ErrorUtils.setNoOpTrackerRequestTimeout({
label: Log.NoOp.Label.InitializeCommunicator,
channel: Constants.CommunicationChannels.extensionAndUi,
clientInfo: this.clientInfo,
url: url
}, true);
this.uiCommunicator.callRemoteFunction(Constants.FunctionKeys.noOpTracker, {
param: new Date().getTime(),
callback: () => {
clearTimeout(uiNoOpTrackerTimeout);
}
});
}
/**
* Signs the user out on in the frontend. TODO: this was implemented as an Edge workaround, and
* should be removed when they fix their iframes not properly loading in the background.
*/
private doSignOutActionInFrontEnd(authType: AuthType) {
let usidQueryParamValue = this.getUserSessionIdQueryParamValue();
let signOutUrl = ClipperUrls.generateSignOutUrl(this.clientInfo.get().clipperId, usidQueryParamValue, AuthType[authType]);
this.uiCommunicator.callRemoteFunction(Constants.FunctionKeys.createHiddenIFrame, {
param: signOutUrl
});
}
private initializeCommunicators() {
this.initializeDebugLoggingCommunicators();
this.initializeClipperCommunicators();
this.initializePageNavCommunicators();
}
private initializeClipperCommunicators() {
this.initializeClipperUiCommunicator();
this.initializeClipperInjectCommunicator();
}
private initializeClipperUiCommunicator() {
this.uiCommunicator.broadcastAcrossCommunicator(this.auth.user, Constants.SmartValueKeys.user);
this.uiCommunicator.broadcastAcrossCommunicator(this.clientInfo, Constants.SmartValueKeys.clientInfo);
this.uiCommunicator.broadcastAcrossCommunicator(this.sessionId, Constants.SmartValueKeys.sessionId);
this.uiCommunicator.registerFunction(Constants.FunctionKeys.keepAlive, () => {
/**
* This function is currently not being called from anywhere, but it is being registered
* so that it may be used in the future if needed.
*/
this.setKeepAlive();
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.clearKeepAlive, () => {
if (!!this.keepAlive) {
clearInterval(this.keepAlive);
this.keepAlive = undefined;
}
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.clipperStrings, () => {
return new Promise<string>((resolve) => {
this.getLocalizedStringsForBrowser((dataResult: string) => {
resolve(dataResult);
});
});
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.getStorageValue, (key: string) => {
return new Promise<string>((resolve) => {
let value = this.clipperData.getValue(key);
resolve(value);
});
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.getMultipleStorageValues, (keys: string[]) => {
return new Promise<{ [key: string]: string }>(async(resolve) => {
let values: { [key: string]: string } = {};
for (let key of keys) {
values[key] = await this.clipperData.getValue(key);
}
resolve(values);
});
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.setStorageValue, (keyValuePair: { key: string, value: string }) => {
this.clipperData.setValue(keyValuePair.key, keyValuePair.value);
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.getInitialUser, () => {
return this.auth.updateUserInfoData(this.clientInfo.get().clipperId, UpdateReason.InitialRetrieval);
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.signInUser, (authType: AuthType) => {
return this.doSignInAction(authType).then((redirectOccurred) => {
// Recently, a change in sign-in flow broke our redirect detection, so now we give the benefit of the doubt
// and always attempt to update userInfo regardless
return this.auth.updateUserInfoData(this.clientInfo.get().clipperId, UpdateReason.SignInAttempt).then((updatedUser: UserInfo) => {
// While redirect detection is somewhat unreliable, it's still sometimes correct. So we try and
// detect this case only after we try get the latest userInfo
if ((!updatedUser || !updatedUser.user) && !redirectOccurred) {
let userInfoToSet: UserInfo = { updateReason: UpdateReason.SignInCancel };
this.auth.user.set(userInfoToSet);
return Promise.resolve(userInfoToSet);
}
return Promise.resolve(updatedUser);
});
}).catch((errorObject) => {
// Set the user info object to undefined as a result of an attempted sign in
this.auth.user.set({ updateReason: UpdateReason.SignInAttempt });
// Right now we're adding the update reason to the errorObject as well so that it is preserved in the callback.
// The right thing to do is revise the way we use callbacks in the communicator and instead use Promises so that
// we are able to return distinct objects.
errorObject.updateReason = UpdateReason.SignInAttempt;
return Promise.reject(errorObject);
});
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.signOutUser, (authType: AuthType) => {
if (this.clientInfo.get().clipperType === ClientType.EdgeExtension) {
this.doSignOutActionInFrontEnd(authType);
} else {
this.doSignOutAction(authType);
}
this.auth.user.set({ updateReason: UpdateReason.SignOutAction });
this.clipperData.setValue(ClipperStorageKeys.userInformation, undefined);
this.clipperData.setValue(ClipperStorageKeys.currentSelectedSection, undefined);
this.clipperData.setValue(ClipperStorageKeys.cachedNotebooks, undefined);
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.telemetry, (data: Log.LogDataPackage) => {
Log.parseAndLogDataPackage(data, this.logger);
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.ensureFreshUserBeforeClip, () => {
return this.auth.updateUserInfoData(this.clientInfo.get().clipperId, UpdateReason.TokenRefreshForPendingClip);
});
this.uiCommunicator.registerFunction(Constants.FunctionKeys.takeTabScreenshot, () => {
return this.takeTabScreenshot();
});
this.uiCommunicator.setErrorHandler((e: Error) => {
Log.ErrorUtils.handleCommunicatorError(Constants.CommunicationChannels.extensionAndUi, e, this.clientInfo);
});
}
private initializeClipperInjectCommunicator() {
this.injectCommunicator.broadcastAcrossCommunicator(this.clientInfo, Constants.SmartValueKeys.clientInfo);
this.injectCommunicator.registerFunction(Constants.FunctionKeys.unloadHandler, () => {
this.tearDownCommunicators();
this.onUnloading();
});
this.injectCommunicator.registerFunction(Constants.FunctionKeys.setStorageValue, (keyValuePair: { key: string, value: string }) => {
this.clipperData.setValue(keyValuePair.key, keyValuePair.value);
});
this.injectCommunicator.setErrorHandler((e: Error) => {
Log.ErrorUtils.handleCommunicatorError(Constants.CommunicationChannels.injectedAndExtension, e, this.clientInfo);
});
}
private initializeDebugLoggingCommunicators() {
this.debugLoggingInjectCommunicator.registerFunction(Constants.FunctionKeys.unloadHandler, () => {
this.tearDownCommunicators();
this.onUnloading();
});
}
private initializePageNavCommunicators() {
this.initializePageNavUiCommunicator();
this.initializePageNavInjectCommunicator();
}
private initializePageNavUiCommunicator() {
this.pageNavUiCommunicator.registerFunction(Constants.FunctionKeys.telemetry, (data: Log.LogDataPackage) => {
Log.parseAndLogDataPackage(data, this.logger);
});
this.pageNavUiCommunicator.registerFunction(Constants.FunctionKeys.invokeClipperFromPageNav, (invokeSource: InvokeSource) => {
this.closeAllFramesAndInvokeClipper({ invokeSource: invokeSource }, { invokeMode: InvokeMode.Default });
});
}
private initializePageNavInjectCommunicator() {
this.pageNavInjectCommunicator.registerFunction(Constants.FunctionKeys.telemetry, (data: Log.LogDataPackage) => {
Log.parseAndLogDataPackage(data, this.logger);
});
this.pageNavInjectCommunicator.registerFunction(Constants.FunctionKeys.unloadHandler, () => {
this.tearDownCommunicators();
this.onUnloading();
});
}
/**
* Fetches fresh localized strings and prepares a remote function for the Page Nav UI to fetch them.
* Resolves with true if successful; false otherwise.
*/
private registerLocalizedStringsForPageNav(): Promise<boolean> {
return new Promise<boolean>((resolve) => {
this.getLocalizedStringsForBrowser((localizedStrings) => {
if (localizedStrings) {
this.pageNavUiCommunicator.registerFunction(Constants.FunctionKeys.clipperStringsFrontLoaded, () => {
return Promise.resolve(localizedStrings);
});
}
resolve(!!localizedStrings);
});
});
}
private tearDownCommunicators() {
this.uiCommunicator.tearDown();
this.pageNavUiCommunicator.tearDown();
this.injectCommunicator.tearDown();
this.pageNavInjectCommunicator.tearDown();
}
}