]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java
Update the URL in the copyright header. https://redmine.stoutner.com/issues/796
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / GetHostIpAddresses.java
index 1e004d13ba4dc4dbca8f04971686c14d623503a1..c3e30c619b9adeeb1446dea380b61b8ca1cff244 100644 (file)
@@ -1,20 +1,20 @@
 /*
- * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
  *
- * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+ * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
- * Privacy Browser is free software: you can redistribute it and/or modify
+ * Privacy Browser Android is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * Privacy Browser is distributed in the hope that it will be useful,
+ * Privacy Browser Android is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
+ * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package com.stoutner.privacybrowser.asynctasks;
@@ -22,7 +22,10 @@ package com.stoutner.privacybrowser.asynctasks;
 import android.app.Activity;
 import android.os.AsyncTask;
 
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import androidx.fragment.app.FragmentManager;
+
+import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
+import com.stoutner.privacybrowser.views.NestedScrollWebView;
 
 import java.lang.ref.WeakReference;
 import java.net.InetAddress;
@@ -32,39 +35,25 @@ import java.net.UnknownHostException;
 public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
     // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
     private final WeakReference<Activity> activityWeakReference;
-    private final int domainSettingsDatabaseId;
+    private final WeakReference<FragmentManager> fragmentManagerWeakReference;
+    private final WeakReference<NestedScrollWebView> nestedScrollWebViewWeakReference;
 
-    public GetHostIpAddresses(Activity activity, int domainSettingsDatabaseId) {
-        // Populate the weak activity reference.
+    public GetHostIpAddresses(Activity activity, FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
+        // Populate the weak references.
         activityWeakReference = new WeakReference<>(activity);
-
-        // Populate the domain settings database ID.
-        this.domainSettingsDatabaseId = domainSettingsDatabaseId;
-    }
-
-    // `onPreExecute()` operates on the UI thread.
-    @Override
-    protected void onPreExecute() {
-        // Get a handle for the activity.
-        Activity activity = activityWeakReference.get();
-
-        // Abort if the activity is gone.
-        if ((activity == null) || activity.isFinishing()) {
-            return;
-        }
-
-        // Set the getting IP addresses tracker.
-        MainWebViewActivity.gettingIpAddresses = true;
+        fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
+        nestedScrollWebViewWeakReference = new WeakReference<>(nestedScrollWebView);
     }
 
-
     @Override
     protected String doInBackground(String... domainName) {
-        // Get a handle for the activity.
+        // Get a handles for the weak references.
         Activity activity = activityWeakReference.get();
+        FragmentManager fragmentManager = fragmentManagerWeakReference.get();
+        NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
 
-        // Abort if the activity is gone.
-        if ((activity == null) || activity.isFinishing()) {
+        // Abort if the activity or its components are gone.
+        if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
             // Return an empty spannable string builder.
             return "";
         }
@@ -79,16 +68,13 @@ public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
 
             // Add each IP address to the string builder.
             for (InetAddress inetAddress : inetAddressesArray) {
-                if (ipAddresses.length() == 0) {  // This is the first IP address.
-                    // Add the IP address to the string builder.
-                    ipAddresses.append(inetAddress.getHostAddress());
-                } else {  // This is not the first IP address.
-                    // Add a line break to the string builder first.
+                // Add a line break to the string builder if this is not the first IP address.
+                if (ipAddresses.length() > 0) {
                     ipAddresses.append("\n");
-
-                    // Add the IP address to the string builder.
-                    ipAddresses.append(inetAddress.getHostAddress());
                 }
+
+                // Add the IP address to the string builder.
+                ipAddresses.append(inetAddress.getHostAddress());
             }
         } catch (UnknownHostException exception) {
             // Do nothing.
@@ -101,22 +87,22 @@ public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
     // `onPostExecute()` operates on the UI thread.
     @Override
     protected void onPostExecute(String ipAddresses) {
-        // Get a handle for the activity.
+        // Get a handle for the activity and the nested scroll WebView.
         Activity activity = activityWeakReference.get();
+        FragmentManager fragmentManager = fragmentManagerWeakReference.get();
+        NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
 
-        // Abort if the activity is gone.
-        if ((activity == null) || activity.isFinishing()) {
+        // Abort if the activity or its components are gone.
+        if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
             return;
         }
 
         // Store the IP addresses.
-        MainWebViewActivity.currentHostIpAddresses = ipAddresses;
+        nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
 
-        if (!MainWebViewActivity.urlIsLoading) {
-            MainWebViewActivity.checkPinnedMismatch(domainSettingsDatabaseId);
+        // Checked for pinned mismatches if there is pinned information and it is not ignored.
+        if ((nestedScrollWebView.hasPinnedSslCertificate() || !nestedScrollWebView.getPinnedIpAddresses().equals("")) && !nestedScrollWebView.getIgnorePinnedDomainInformation()) {
+            CheckPinnedMismatchHelper.checkPinnedMismatch(activity, fragmentManager, nestedScrollWebView);
         }
-
-        // Reset the getting IP addresses tracker.
-        MainWebViewActivity.gettingIpAddresses = false;
     }
 }
\ No newline at end of file