]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java
Finish tabbed browsing. https://redmine.stoutner.com/issues/22
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / GetHostIpAddresses.java
1 /*
2  * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.asynctasks;
21
22 import android.app.Activity;
23 import android.os.AsyncTask;
24
25 import androidx.fragment.app.FragmentManager;
26
27 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
28 import com.stoutner.privacybrowser.views.NestedScrollWebView;
29
30 import java.lang.ref.WeakReference;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33
34 // This must run asynchronously because it involves a network request.  `String` declares the parameters.  `Void` does not declare progress units.  `String` contains the results.
35 public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
36     // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
37     private final WeakReference<Activity> activityWeakReference;
38     private final WeakReference<FragmentManager> fragmentManagerWeakReference;
39     private final WeakReference<NestedScrollWebView> nestedScrollWebViewWeakReference;
40
41     public GetHostIpAddresses(Activity activity, FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
42         // Populate the weak references.
43         activityWeakReference = new WeakReference<>(activity);
44         fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
45         nestedScrollWebViewWeakReference = new WeakReference<>(nestedScrollWebView);
46     }
47
48     @Override
49     protected String doInBackground(String... domainName) {
50         // Get a handles for the weak references.
51         Activity activity = activityWeakReference.get();
52         FragmentManager fragmentManager = fragmentManagerWeakReference.get();
53         NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
54
55         // Abort if the activity or its components are gone.
56         if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
57             // Return an empty spannable string builder.
58             return "";
59         }
60
61         // Initialize an IP address string builder.
62         StringBuilder ipAddresses = new StringBuilder();
63
64         // Get an array with the IP addresses for the host.
65         try {
66             // Get an array with all the IP addresses for the domain.
67             InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
68
69             // Add each IP address to the string builder.
70             for (InetAddress inetAddress : inetAddressesArray) {
71                 if (ipAddresses.length() == 0) {  // This is the first IP address.
72                     // Add the IP address to the string builder.
73                     ipAddresses.append(inetAddress.getHostAddress());
74                 } else {  // This is not the first IP address.
75                     // Add a line break to the string builder first.
76                     ipAddresses.append("\n");
77
78                     // Add the IP address to the string builder.
79                     ipAddresses.append(inetAddress.getHostAddress());
80                 }
81             }
82         } catch (UnknownHostException exception) {
83             // Do nothing.
84         }
85
86         // Return the string.
87         return ipAddresses.toString();
88     }
89
90     // `onPostExecute()` operates on the UI thread.
91     @Override
92     protected void onPostExecute(String ipAddresses) {
93         // Get a handle for the activity and the nested scroll WebView.
94         Activity activity = activityWeakReference.get();
95         FragmentManager fragmentManager = fragmentManagerWeakReference.get();
96         NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
97
98         // Abort if the activity or its components are gone.
99         if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
100             return;
101         }
102
103         // Store the IP addresses.
104         nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
105
106         // Checked for pinned mismatches if the WebView is not loading a URL, pinned information is not ignored, and there is pinned information.
107         if ((nestedScrollWebView.getProgress() == 100) && !nestedScrollWebView.ignorePinnedDomainInformation() && (nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.hasPinnedIpAddresses())) {
108             CheckPinnedMismatchHelper.checkPinnedMismatch(fragmentManager, nestedScrollWebView);
109         }
110     }
111 }