-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Create UriMatcher and various implementations - Handle allowed and disallowed URIs the same way, providing the same flexibility (e.g., requiring exact or partial match) for both - Extract URI matching logic from the main SecureWebView class, reducing its responsibilitites, also define disallowed URLs elsewhere - Add unit tests for URI matcher logic
- Loading branch information
Showing
25 changed files
with
772 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
library/src/main/java/dev/gerlot/securewebview/uri/AnyUriMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* Matches any URI. | ||
*/ | ||
public class AnyUriMatcher implements UriMatcher { | ||
|
||
/** | ||
* The singleton instance. | ||
*/ | ||
public static final AnyUriMatcher INSTANCE = new AnyUriMatcher(); | ||
|
||
private AnyUriMatcher() { | ||
// no need to construct separate instances | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return true; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
library/src/main/java/dev/gerlot/securewebview/uri/AuthorityAndPathMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.Objects; | ||
|
||
public class AuthorityAndPathMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
private final UriMatcher mAuthorityMatcher; | ||
|
||
public AuthorityAndPathMatcher(Uri uri) { | ||
this(uri, true); | ||
} | ||
|
||
public AuthorityAndPathMatcher(Uri uri, boolean includeWwwPrefix) { | ||
mUri = uri; | ||
mAuthorityMatcher = new AuthorityMatcher(uri, includeWwwPrefix); | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return mAuthorityMatcher.matches(uri) && Objects.equals(mUri.getPath(), uri.getPath()); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
library/src/main/java/dev/gerlot/securewebview/uri/AuthorityContainmentMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class AuthorityContainmentMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
public AuthorityContainmentMatcher(Uri uri) { | ||
mUri = uri; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return uri.getAuthority() != null && mUri.getAuthority() != null && uri.getAuthority().contains(mUri.getAuthority()); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
library/src/main/java/dev/gerlot/securewebview/uri/AuthorityMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class AuthorityMatcher implements UriMatcher { | ||
|
||
private final UriMatcher mMatchStrategy; | ||
|
||
public AuthorityMatcher(Uri uri) { | ||
this(uri, true); | ||
} | ||
|
||
public AuthorityMatcher(Uri uri, boolean includeWwwPrefix) { | ||
mMatchStrategy = includeWwwPrefix ? new AuthorityWithWwwMatcher(uri) : new AuthorityWithoutWwwMatcher(uri); | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return mMatchStrategy.matches(uri); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
library/src/main/java/dev/gerlot/securewebview/uri/AuthorityWithWwwMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.Objects; | ||
|
||
class AuthorityWithWwwMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
public AuthorityWithWwwMatcher(Uri uri) { | ||
mUri = uri; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return Objects.equals(mUri.getAuthority(), uri.getAuthority()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
library/src/main/java/dev/gerlot/securewebview/uri/AuthorityWithoutWwwMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.Objects; | ||
|
||
class AuthorityWithoutWwwMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
public AuthorityWithoutWwwMatcher(Uri uri) { | ||
mUri = uri; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return Objects.equals(trimAuthority(mUri.getAuthority()), trimAuthority(uri.getAuthority())); | ||
} | ||
|
||
private String trimAuthority(String authority) { | ||
return authority != null ? authority.replace("www.", "") : null; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
library/src/main/java/dev/gerlot/securewebview/uri/BeginningMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class BeginningMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
public BeginningMatcher(Uri uri) { | ||
mUri = uri; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return uri.toString().startsWith(mUri.toString()); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
library/src/main/java/dev/gerlot/securewebview/uri/ExactUriMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.gerlot.securewebview.uri; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class ExactUriMatcher implements UriMatcher { | ||
|
||
private final Uri mUri; | ||
|
||
public ExactUriMatcher(Uri uri) { | ||
mUri = uri; | ||
} | ||
|
||
@Override | ||
public boolean matches(@NonNull Uri uri) { | ||
return mUri.equals(uri); | ||
} | ||
|
||
} |
Oops, something went wrong.