-
Notifications
You must be signed in to change notification settings - Fork 6
/
ios-communication.js
131 lines (123 loc) · 4.65 KB
/
ios-communication.js
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
/**
*
* iOS shares the majority of functionality from {@link "macOS integration"}, with the exception
* of the webkit handlers listed below.
*
* **Incoming data**
*
* Please see the links under the heading 'macOS -> JavaScript Interface' from {@link "macOS integration"}
*
* Examples from the macOS integration:
* - {@link "macOS integration".onChangeProtectionStatus}
* - {@link "macOS integration".onChangeRequestData}
* - {@link "macOS integration".onChangeLocale}
*
* **Outgoing messages**
*
* Although iOS uses the outgoing messages from the {@link "macOS integration"} - there are some that are iOS-only,
* those are listed below under `"Webkit Message Handlers"`
*
* @module iOS integration
* @category integrations
*/
import invariant from 'tiny-invariant';
import {
CheckBrokenSiteReportHandledMessage,
setupColorScheme,
ShowAlertForMissingDescription,
ShowNativeFeedback,
TelemetrySpanMsg,
} from './common.js';
import { backgroundMessage, getBackgroundTabData, fetch as macosFetch, setupShared } from './macos-communication.js';
/**
* iOS specific setup
*/
export function setup() {
const setColorScheme = setupColorScheme();
window.onChangeTheme = function (themeName) {
setColorScheme(themeName);
};
window.history.replaceState({}, '', window.location.href);
setupShared();
}
/**
* On iOS, the breakage report form is handled natively - so all the dashboard needs
* to do in this situation is ping the correct message to the backend.
*
* @category Webkit Message Handlers
* @param {{}} args - An empty object to keep the `webkit` message handlers happy
* @example
* ```js
* window.webkit.messageHandlers.privacyDashboardShowReportBrokenSite.postMessage(args)
* ```
*/
export function privacyDashboardShowReportBrokenSite(args) {
invariant(window.webkit?.messageHandlers, 'webkit.messageHandlers required');
window.webkit.messageHandlers.privacyDashboardShowReportBrokenSite.postMessage(args);
}
/**
* On iOS, the breakage report form is handled natively - so all the dashboard needs
* to do in this situation is ping the correct message to the backend.
*
* @category Webkit Message Handlers
* @param {{}} args - An empty object to keep the `webkit` message handlers happy
* @example
* ```js
* window.webkit.messageHandlers.privacyDashboardShowAlertForMissingDescription.postMessage(args)
* ```
*/
export function privacyDashboardShowAlertForMissingDescription(args) {
invariant(window.webkit?.messageHandlers, 'webkit.messageHandlers required');
window.webkit.messageHandlers.privacyDashboardShowAlertForMissingDescription.postMessage(args);
}
/**
* On iOS, the breakage report form is handled natively - so all the dashboard needs
* to do in this situation is ping the correct message to the backend.
*
* @category Webkit Message Handlers
* @param {{}} args - An empty object to keep the `webkit` message handlers happy
* @example
* ```js
* window.webkit.messageHandlers.privacyDashboardShowNativeFeedback.postMessage(args)
* ```
*/
export function privacyDashboardShowNativeFeedback(args) {
invariant(window.webkit?.messageHandlers, 'webkit.messageHandlers required');
window.webkit.messageHandlers.privacyDashboardShowNativeFeedback.postMessage(args);
}
/**
* @category Webkit Message Handlers
* @param {import('../../../schema/__generated__/schema.types.js').TelemetrySpan} args - An empty object to keep the `webkit` message handlers happy
* @example
* ```js
* window.webkit.messageHandlers.privacyDashboardTelemetrySpan.postMessage(args)
* ```
*/
export function privacyDashboardTelemetrySpan(args) {
invariant(window.webkit?.messageHandlers, 'webkit.messageHandlers required');
window.webkit.messageHandlers.privacyDashboardTelemetrySpan.postMessage(args);
}
/**
* @category Internal API
* @type {import("./common.js").fetcher}
*/
async function fetch(message) {
if (message instanceof CheckBrokenSiteReportHandledMessage) {
privacyDashboardShowReportBrokenSite({});
return false; // Return true to prevent HTML form from showing
}
if (message instanceof ShowAlertForMissingDescription) {
privacyDashboardShowAlertForMissingDescription({});
return false; // Return true to prevent HTML form from showing
}
if (message instanceof ShowNativeFeedback) {
privacyDashboardShowNativeFeedback({});
return false; // Return true to prevent HTML form from showing
}
if (message instanceof TelemetrySpanMsg) {
privacyDashboardTelemetrySpan(message);
return false; // Return true to prevent HTML form from showing
}
return macosFetch(message);
}
export { backgroundMessage, getBackgroundTabData, fetch };