From 47190896d2972be83bd4d09754fe237941c99433 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 3 May 2022 06:36:23 -0400 Subject: [PATCH 1/4] webxdc: allow internal pages and open some known schemes externally --- src/org/thoughtcrime/securesms/WebViewActivity.java | 12 +++++++----- src/org/thoughtcrime/securesms/WebxdcActivity.java | 9 +++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/WebViewActivity.java b/src/org/thoughtcrime/securesms/WebViewActivity.java index f23f2d016..5a048103b 100644 --- a/src/org/thoughtcrime/securesms/WebViewActivity.java +++ b/src/org/thoughtcrime/securesms/WebViewActivity.java @@ -48,7 +48,8 @@ public class WebViewActivity extends PassphraseRequiredActionBarActivity webView = findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient() { // `shouldOverrideUrlLoading()` is called when the user clicks a URL, - // returning `true` means, the URL is passed to `loadUrl()`, `false` aborts loading. + // returning `true` causes the WebView to abort loading the URL, + // returning `false` causes the WebView to continue loading the URL as usual. // the method is not called for POST request nor for on-page-links. // // nb: from API 24, `shouldOverrideUrlLoading(String)` is deprecated and @@ -64,9 +65,8 @@ public class WebViewActivity extends PassphraseRequiredActionBarActivity case "https": case "mailto": case "openpgp4fpr": - openOnlineUrl(url); - // URL opened externally, returning `true` causes the WebView to abort loading - return true; + // open URL externally + return openOnlineUrl(url); } } // by returning `true`, we also abort loading other URLs in our WebView; @@ -271,12 +271,14 @@ public class WebViewActivity extends PassphraseRequiredActionBarActivity } } - protected void openOnlineUrl(String url) { + protected boolean openOnlineUrl(String url) { try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } catch (ActivityNotFoundException e) { Toast.makeText(this, R.string.no_browser_installed, Toast.LENGTH_LONG).show(); } + // returning `true` causes the WebView to abort loading + return true; } protected WebResourceResponse interceptRequest(String url) { diff --git a/src/org/thoughtcrime/securesms/WebxdcActivity.java b/src/org/thoughtcrime/securesms/WebxdcActivity.java index 239efcfb7..6774f654b 100644 --- a/src/org/thoughtcrime/securesms/WebxdcActivity.java +++ b/src/org/thoughtcrime/securesms/WebxdcActivity.java @@ -35,6 +35,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE private static final String TAG = WebxdcActivity.class.getSimpleName(); private DcContext dcContext; private DcMsg dcAppMsg; + private String baseURL; public static void openWebxdcActivity(Context context, DcMsg instance) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -105,8 +106,12 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE } @Override - protected void openOnlineUrl(String url) { - Toast.makeText(this, "Please embed needed resources.", Toast.LENGTH_LONG).show(); + protected boolean openOnlineUrl(String url) { + if (url.startsWith(baseURL +"/")) { + // internal page, continue loading in the WebView + return false; + } + return super.openOnlineUrl(url); } @Override From 35a535705bb42d41c50f1bc9f04c356f307a154f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asiel=20D=C3=ADaz=20Ben=C3=ADtez?= Date: Tue, 3 May 2022 06:53:16 -0400 Subject: [PATCH 2/4] Update src/org/thoughtcrime/securesms/WebxdcActivity.java Co-authored-by: bjoern --- src/org/thoughtcrime/securesms/WebxdcActivity.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/WebxdcActivity.java b/src/org/thoughtcrime/securesms/WebxdcActivity.java index 6774f654b..c27e36258 100644 --- a/src/org/thoughtcrime/securesms/WebxdcActivity.java +++ b/src/org/thoughtcrime/securesms/WebxdcActivity.java @@ -111,7 +111,8 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE // internal page, continue loading in the WebView return false; } - return super.openOnlineUrl(url); + Toast.makeText(this, "Please embed needed resources.", Toast.LENGTH_LONG).show(); + return true; // returning `true` causes the WebView to abort loading } @Override From 144a2df3c966a23b8a83f537dcdb89eba6d24851 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 3 May 2022 06:54:52 -0400 Subject: [PATCH 3/4] remove comment --- src/org/thoughtcrime/securesms/WebViewActivity.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/WebViewActivity.java b/src/org/thoughtcrime/securesms/WebViewActivity.java index 5a048103b..966e80b68 100644 --- a/src/org/thoughtcrime/securesms/WebViewActivity.java +++ b/src/org/thoughtcrime/securesms/WebViewActivity.java @@ -65,7 +65,6 @@ public class WebViewActivity extends PassphraseRequiredActionBarActivity case "https": case "mailto": case "openpgp4fpr": - // open URL externally return openOnlineUrl(url); } } From e24a72de615b8d49a4fb413692693921355ace35 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 3 May 2022 07:21:49 -0400 Subject: [PATCH 4/4] set baseURL --- src/org/thoughtcrime/securesms/WebxdcActivity.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/WebxdcActivity.java b/src/org/thoughtcrime/securesms/WebxdcActivity.java index c27e36258..59ec4c658 100644 --- a/src/org/thoughtcrime/securesms/WebxdcActivity.java +++ b/src/org/thoughtcrime/securesms/WebxdcActivity.java @@ -62,6 +62,11 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE this.dcContext = DcHelper.getContext(getApplicationContext()); this.dcAppMsg = this.dcContext.getMsg(appMessageId); + // `msg_id` in the subdomain makes sure, different apps using same files do not share the same cache entry + // (WebView may use a global cache shared across objects). + // (a random-id would also work, but would need maintenance and does not add benefits as we regard the file-part interceptRequest() only, + // also a random-id is not that useful for debugging) + this.baseURL = "https://acc" + dcContext.getAccountId() + "-msg" + appMessageId + ".localhost"; WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); @@ -75,11 +80,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE webSettings.setDomStorageEnabled(true); webView.addJavascriptInterface(new InternalJSApi(), "InternalJSApi"); - // `msg_id` in the subdomain makes sure, different apps using same files do not share the same cache entry - // (WebView may use a global cache shared across objects). - // (a random-id would also work, but would need maintenance and does not add benefits as we regard the file-part interceptRequest() only, - // also a random-id is not that useful for debugging) - webView.loadUrl("https://acc" + dcContext.getAccountId() + "-msg" + appMessageId + ".localhost/index.html"); + webView.loadUrl(this.baseURL + "/index.html"); Util.runOnAnyBackgroundThread(() -> { JSONObject info = this.dcAppMsg.getWebxdcInfo();