Chromium Code Reviews| Index: components/web_restrictions/browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java |
| diff --git a/components/web_restrictions/browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java b/components/web_restrictions/browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java |
| index 6e2c2221cbfee304d61df001f18451709249dca5..5b481f205be7a3acca9669b2941e8d5d9eca38ac 100644 |
| --- a/components/web_restrictions/browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java |
| +++ b/components/web_restrictions/browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java |
| @@ -12,7 +12,6 @@ import android.content.pm.ProviderInfo; |
| import android.database.AbstractCursor; |
| import android.database.Cursor; |
| import android.net.Uri; |
| -import android.util.Pair; |
| import java.util.regex.Matcher; |
| import java.util.regex.Pattern; |
| @@ -35,9 +34,57 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| private UriMatcher mContentUriMatcher; |
| private Uri mContentUri; |
| + /** |
| + * Structure for returning result including the custom error data. |
| + */ |
| + public static class WebRestrictionsResult { |
| + private int mNumErrorInts; |
|
Bernhard Bauer
2016/03/31 10:09:07
These could be final. Or you could derive the valu
aberent
2016/03/31 11:53:58
Done.
|
| + private int mNumErrorStrings; |
| + private final boolean mShouldProceed; |
| + private final int mErrorInts[]; |
| + private final String mErrorStrings[]; |
| + |
| + public WebRestrictionsResult(boolean shouldProceed, int numErrorInts, int numErrorStrings) { |
|
Bernhard Bauer
2016/03/31 10:09:07
You could assert that |numErrorInts| and |numError
aberent
2016/03/31 11:53:58
Done.
|
| + mShouldProceed = shouldProceed; |
| + mNumErrorInts = numErrorInts; |
| + mNumErrorStrings = numErrorStrings; |
| + mErrorInts = numErrorInts == 0 ? null : new int[numErrorInts]; |
| + mErrorStrings = numErrorStrings == 0 ? null : new String[numErrorStrings]; |
| + } |
| + |
| + public int getErrorInt(int i) { |
| + return mErrorInts[i]; |
| + } |
| + |
| + public void setErrorInt(int i, int value) { |
| + mErrorInts[i] = value; |
| + } |
| + |
| + public String getErrorString(int i) { |
| + return mErrorStrings[i]; |
| + } |
| + |
| + public void setErrorString(int i, String value) { |
| + mErrorStrings[i] = value; |
| + } |
| + |
| + public boolean shouldProceed() { |
| + return mShouldProceed; |
| + } |
| + |
| + public int errorIntCount() { |
| + return mNumErrorInts; |
| + } |
| + |
| + public int errorStringCount() { |
| + return mNumErrorStrings; |
| + } |
| + } |
| + |
| protected WebRestrictionsContentProvider() { |
| // Pattern to extract the URL from the selection. |
| - // Matches patterns of the form "url = '<url>'" with arbitrary spacing around the "=" etc. |
| + // Matches patterns of the form "url = '<url>'" with arbitrary spacing |
| + // around the "=" etc. |
| mSelectionPattern = Pattern.compile("\\s*url\\s*=\\s*'([^']*)'"); |
| } |
| @@ -66,7 +113,8 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| Matcher matcher = mSelectionPattern.matcher(selection); |
| if (!matcher.find()) return null; |
| final String url = matcher.group(1); |
| - final Pair<Boolean, String> result = shouldProceed(url); |
| + final WebRestrictionsResult result = shouldProceed(url); |
| + if (result == null) return null; |
| return new AbstractCursor() { |
| @@ -77,12 +125,26 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| @Override |
| public String[] getColumnNames() { |
| - return new String[] {"Should Proceed", "Error message"}; |
| + String errorNames[] = getErrorColumnNames(); |
| + String names[] = new String[errorNames.length + 1]; |
| + names[0] = "Should Proceed"; |
| + for (int i = 0; i < errorNames.length; i++) { |
| + names[i + 1] = errorNames[i]; |
| + } |
| + return names; |
| } |
| @Override |
| public String getString(int column) { |
| - if (column == 1) return result.second; |
| + // The column order is: |
| + // result, |
| + // integer error parameters, |
| + // string error parameters |
| + // so offset the string error parameters by the number of integer parameters + 1 |
| + int errorStringNumber = column - result.errorIntCount() - 1; |
| + if (errorStringNumber >= 0 && errorStringNumber < result.errorStringCount()) { |
| + return result.getErrorString(errorStringNumber); |
| + } |
| return null; |
| } |
| @@ -93,7 +155,16 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| @Override |
| public int getInt(int column) { |
| - if (column == 0) return result.first ? PROCEED : BLOCKED; |
| + if (column == 0) return result.shouldProceed() ? PROCEED : BLOCKED; |
| + // The column order is: |
| + // result, |
| + // integer error parameters, |
| + // string error parameters |
| + // so offset the integer error parameters by 1 |
| + int errorIntNumber = column - 1; |
| + if (errorIntNumber < result.errorIntCount()) { |
| + return result.getErrorInt(errorIntNumber); |
| + } |
| return 0; |
| } |
| @@ -116,6 +187,15 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| public boolean isNull(int column) { |
| return false; |
| } |
| + |
| + @Override |
| + public int getType(int column) { |
| + if (column < result.errorIntCount() + 1) return FIELD_TYPE_INTEGER; |
| + if (column < result.errorIntCount() + result.errorStringCount() + 1) { |
| + return FIELD_TYPE_STRING; |
| + } |
| + return FIELD_TYPE_NULL; |
| + } |
| }; |
| } |
| @@ -157,7 +237,7 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| * proceed, false otherwise. error message is only meaningful if result is false, a null |
| * error message means use application default. |
| */ |
| - protected abstract Pair<Boolean, String> shouldProceed(final String url); |
| + protected abstract WebRestrictionsResult shouldProceed(final String url); |
| /** |
| * @return whether the content provider allows insertions. |
| @@ -165,6 +245,12 @@ public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| protected abstract boolean canInsert(); |
| /** |
| + * @return the names of the custom error columns, integer valued columns must proceed string |
| + * valued columns. |
| + */ |
| + protected abstract String[] getErrorColumnNames(); |
| + |
| + /** |
| * Start a request that a URL should be permitted |
| * |
| * @param url the URL that is wanted. |