Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bdaa1aaf authored by Mathew Inwood's avatar Mathew Inwood
Browse files

SearchBox API to determine if it's supported by the current page.

Change-Id: I0119243ed0e19e237c1f51de887af5c954f96693
parent 2c742c2a
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -119,6 +119,7 @@ class CallbackProxy extends Handler {
    private static final int NOTIFY_SEARCHBOX_LISTENERS          = 139;
    private static final int AUTO_LOGIN                          = 140;
    private static final int CLIENT_CERT_REQUEST                 = 141;
    private static final int SEARCHBOX_IS_SUPPORTED_CALLBACK     = 142;

    // Message triggered by the client to resume execution
    private static final int NOTIFY                              = 200;
@@ -796,13 +797,14 @@ class CallbackProxy extends Handler {
                    mWebChromeClient.setInstallableWebApp();
                }
                break;
            case NOTIFY_SEARCHBOX_LISTENERS:
            case NOTIFY_SEARCHBOX_LISTENERS: {
                SearchBoxImpl searchBox = (SearchBoxImpl) mWebView.getSearchBox();

                @SuppressWarnings("unchecked")
                List<String> suggestions = (List<String>) msg.obj;
                searchBox.handleSuggestions(msg.getData().getString("query"), suggestions);
                break;
            }
            case AUTO_LOGIN: {
                if (mWebViewClient != null) {
                    String realm = msg.getData().getString("realm");
@@ -813,6 +815,12 @@ class CallbackProxy extends Handler {
                }
                break;
            }
            case SEARCHBOX_IS_SUPPORTED_CALLBACK: {
                SearchBoxImpl searchBox = (SearchBoxImpl) mWebView.getSearchBox();
                Boolean supported = (Boolean) msg.obj;
                searchBox.handleIsSupportedCallback(supported);
                break;
            }
        }
    }

@@ -1627,4 +1635,10 @@ class CallbackProxy extends Handler {

        sendMessage(msg);
    }

    void onIsSupportedCallback(boolean isSupported) {
        Message msg = obtainMessage(SEARCHBOX_IS_SUPPORTED_CALLBACK);
        msg.obj = new Boolean(isSupported);
        sendMessage(msg);
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -82,6 +82,11 @@ public interface SearchBox {
    void addSearchBoxListener(SearchBoxListener l);
    void removeSearchBoxListener(SearchBoxListener l);

    /**
     * Indicates if the searchbox API is supported in the current page.
     */
    void isSupported(IsSupportedCallback callback);

    /**
     * Listeners (if any) will be called on the thread that created the
     * webview.
@@ -89,4 +94,8 @@ public interface SearchBox {
    interface SearchBoxListener {
        void onSuggestionsReceived(String query, List<String> suggestions);
    }

    interface IsSupportedCallback {
        void searchBoxIsSupported(boolean supported);
    }
}
+29 −0
Original line number Diff line number Diff line
@@ -92,9 +92,19 @@ final class SearchBoxImpl implements SearchBox {
            = "if (window.chrome && window.chrome.searchBox &&"
            + "  window.chrome.searchBox.on%1$s) { window.chrome.searchBox.on%1$s(); }";

    private static final String IS_SUPPORTED_SCRIPT
            = "if (window.searchBoxJavaBridge_) {"
            + "  if (window.chrome && window.chrome.searchBox && "
            + "  window.chrome.searchBox.onsubmit) {"
            + "    window.searchBoxJavaBridge_.isSupportedCallback(true);"
            + "  } else {"
            + "    window.searchBoxJavaBridge_.isSupportedCallback(false);"
            + "  }}";

    private final List<SearchBoxListener> mListeners;
    private final WebViewCore mWebViewCore;
    private final CallbackProxy mCallbackProxy;
    private IsSupportedCallback mSupportedCallback;

    SearchBoxImpl(WebViewCore webViewCore, CallbackProxy callbackProxy) {
        mListeners = new ArrayList<SearchBoxListener>();
@@ -173,6 +183,25 @@ final class SearchBoxImpl implements SearchBox {
        }
    }

    @Override
    public void isSupported(IsSupportedCallback callback) {
        mSupportedCallback = callback;
        dispatchJs(IS_SUPPORTED_SCRIPT);
    }

    // Called by Javascript through the Java bridge.
    public void isSupportedCallback(boolean isSupported) {
        mCallbackProxy.onIsSupportedCallback(isSupported);
    }

    public void handleIsSupportedCallback(boolean isSupported) {
        IsSupportedCallback callback = mSupportedCallback;
        mSupportedCallback = null;
        if (callback != null) {
            callback.searchBoxIsSupported(isSupported);
        }
    }

    // This is used as a hackish alternative to javascript escaping.
    // There appears to be no such functionality in the core framework.
    private String jsonSerialize(String query) {