Skip to content

Commit 71e27f7

Browse files
committed
Address feedback from PR
1 parent e34851a commit 71e27f7

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ class BrowserTabFragment :
12671267
}
12681268

12691269
private fun onPageStarted() {
1270-
lifecycleScope.launch(dispatchers.main()) {
1270+
lifecycleScope.launch {
12711271
webViewCompatTestHelper.onPageStarted(webView)
12721272
}
12731273
}
@@ -3401,7 +3401,7 @@ class BrowserTabFragment :
34013401
val script = if (!webViewCompatUsesBlobDownloadsMessageListener) {
34023402
blobDownloadScript()
34033403
} else {
3404-
blobDownloadScriptForWebViewCompatTest()
3404+
webViewCompatTestHelper.blobDownloadScriptForWebViewCompatTest()
34053405
}
34063406
WebViewCompat.addDocumentStartJavaScript(webView, script, setOf("*"))
34073407

@@ -3454,61 +3454,6 @@ class BrowserTabFragment :
34543454
}
34553455
}
34563456

3457-
private fun blobDownloadScriptForWebViewCompatTest(): String {
3458-
val script =
3459-
"""
3460-
(function() {
3461-
3462-
const urlToBlobCollection = {};
3463-
3464-
const original_createObjectURL = URL.createObjectURL;
3465-
3466-
URL.createObjectURL = function () {
3467-
const blob = arguments[0];
3468-
const url = original_createObjectURL.call(this, ...arguments);
3469-
if (blob instanceof Blob) {
3470-
urlToBlobCollection[url] = blob;
3471-
}
3472-
return url;
3473-
}
3474-
3475-
function blobToBase64DataUrl(blob) {
3476-
return new Promise((resolve, reject) => {
3477-
const reader = new FileReader();
3478-
reader.onloadend = function() {
3479-
resolve(reader.result);
3480-
}
3481-
reader.onerror = function() {
3482-
reject(new Error('Failed to read Blob object'));
3483-
}
3484-
reader.readAsDataURL(blob);
3485-
});
3486-
}
3487-
3488-
const pingMessage = 'Ping:' + window.location.href;
3489-
window.ddgBlobDownloadObj.postMessage(pingMessage);
3490-
console.log('Sent ping message for blob downloads: ' + pingMessage);
3491-
3492-
window.ddgBlobDownloadObj.addEventListener('message', function(event) {
3493-
if (event.data.startsWith('blob:')) {
3494-
console.log(event.data);
3495-
const blob = urlToBlobCollection[event.data];
3496-
if (blob) {
3497-
blobToBase64DataUrl(blob).then((dataUrl) => {
3498-
console.log('Sending data URL back to native ' + dataUrl);
3499-
window.ddgBlobDownloadObj.postMessage(dataUrl);
3500-
});
3501-
} else {
3502-
console.log('No Blob found for URL: ' + event.data);
3503-
}
3504-
}
3505-
});
3506-
})();
3507-
""".trimIndent()
3508-
3509-
return script
3510-
}
3511-
35123457
private fun blobDownloadScript(): String {
35133458
val script =
35143459
"""

app/src/main/java/com/duckduckgo/app/browser/WebViewCompatTestHelper.kt

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ interface WebViewCompatTestHelper {
5353

5454
suspend fun onPageStarted(webView: DuckDuckGoWebView?)
5555
suspend fun onBrowserMenuButtonPressed(webView: DuckDuckGoWebView?)
56+
57+
fun blobDownloadScriptForWebViewCompatTest(): String
5658
}
5759

5860
@ContributesBinding(FragmentScope::class)
@@ -134,13 +136,6 @@ class RealWebViewCompatTestHelper @Inject constructor(
134136
}
135137
}
136138

137-
@SuppressLint("PostMessageUsage", "RequiresFeature")
138-
private suspend fun postMessage(string: String) {
139-
withContext(dispatchers.main()) {
140-
proxy?.postMessage(string)
141-
}
142-
}
143-
144139
@SuppressLint("PostMessageUsage", "RequiresFeature")
145140
override suspend fun handleWebViewCompatMessage(
146141
message: WebMessageCompat,
@@ -178,7 +173,7 @@ class RealWebViewCompatTestHelper @Inject constructor(
178173
val messageData = "PageStarted"
179174

180175
if (config.sendMessagesUsingReplyProxy) {
181-
postMessage(messageData)
176+
proxy?.postMessage(messageData)
182177
} else {
183178
webView?.url?.let {
184179
WebViewCompat.postWebMessage(
@@ -191,7 +186,7 @@ class RealWebViewCompatTestHelper @Inject constructor(
191186
}
192187
}
193188

194-
@SuppressLint("RequiresFeature")
189+
@SuppressLint("RequiresFeature", "PostMessageUsage")
195190
override suspend fun onBrowserMenuButtonPressed(webView: DuckDuckGoWebView?) {
196191
withContext(dispatchers.main()) {
197192
val config = getWebViewCompatConfig()
@@ -201,7 +196,7 @@ class RealWebViewCompatTestHelper @Inject constructor(
201196
val messageData = "ContextMenuOpened"
202197

203198
if (config.sendMessagesUsingReplyProxy) {
204-
postMessage(messageData)
199+
proxy?.postMessage(messageData)
205200
} else {
206201
webView?.url?.let {
207202
WebViewCompat.postWebMessage(
@@ -213,4 +208,59 @@ class RealWebViewCompatTestHelper @Inject constructor(
213208
}
214209
}
215210
}
211+
212+
override fun blobDownloadScriptForWebViewCompatTest(): String {
213+
val script =
214+
"""
215+
(function() {
216+
217+
const urlToBlobCollection = {};
218+
219+
const original_createObjectURL = URL.createObjectURL;
220+
221+
URL.createObjectURL = function () {
222+
const blob = arguments[0];
223+
const url = original_createObjectURL.call(this, ...arguments);
224+
if (blob instanceof Blob) {
225+
urlToBlobCollection[url] = blob;
226+
}
227+
return url;
228+
}
229+
230+
function blobToBase64DataUrl(blob) {
231+
return new Promise((resolve, reject) => {
232+
const reader = new FileReader();
233+
reader.onloadend = function() {
234+
resolve(reader.result);
235+
}
236+
reader.onerror = function() {
237+
reject(new Error('Failed to read Blob object'));
238+
}
239+
reader.readAsDataURL(blob);
240+
});
241+
}
242+
243+
const pingMessage = 'Ping:' + window.location.href;
244+
window.ddgBlobDownloadObj.postMessage(pingMessage);
245+
console.log('Sent ping message for blob downloads: ' + pingMessage);
246+
247+
window.ddgBlobDownloadObj.addEventListener('message', function(event) {
248+
if (event.data.startsWith('blob:')) {
249+
console.log(event.data);
250+
const blob = urlToBlobCollection[event.data];
251+
if (blob) {
252+
blobToBase64DataUrl(blob).then((dataUrl) => {
253+
console.log('Sending data URL back to native ' + dataUrl);
254+
window.ddgBlobDownloadObj.postMessage(dataUrl);
255+
});
256+
} else {
257+
console.log('No Blob found for URL: ' + event.data);
258+
}
259+
}
260+
});
261+
})();
262+
""".trimIndent()
263+
264+
return script
265+
}
216266
}

0 commit comments

Comments
 (0)