Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/safe_browsing/SafeBrowsingApiBridge.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/safe_browsing/SafeBrowsingApiBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/safe_browsing/SafeBrowsingApiBridge.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..128d65f41619c7cd9a4c8313d9872cc3712c8453 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/safe_browsing/SafeBrowsingApiBridge.java |
| @@ -0,0 +1,68 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.safe_browsing; |
| + |
| +import android.content.Context; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +/** |
| + * Helper for calling GMSCore Safe Browsing API from native code. |
| + */ |
| +@JNINamespace("safe_browsing") |
| +public class SafeBrowsingApiBridge { |
|
nyquist
2016/04/29 20:01:34
I thinks this amazing class definition deserves th
Yaron
2016/05/02 13:25:07
Done.
|
| + private static final String TAG = "SafeBrowsingApi"; |
| + |
| + private static Class<? extends SafeBrowsingApiHandler> sHandler; |
| + |
| + private SafeBrowsingApiBridge() { |
| + // Util class, do not instantiate. |
| + } |
| + |
| + /** |
| + * Set the class-file for the implementation of SafeBrowsingApiHandler to use when the safe |
| + * browsing api is invoked. |
| + * @param handler |
|
nyquist
2016/04/29 20:01:34
Nit: This @param isn't really helpful.
Yaron
2016/05/02 13:25:07
Done.
|
| + */ |
| + public static void setSafeBrowingHandlerType(Class<? extends SafeBrowsingApiHandler> handler) { |
| + sHandler = handler; |
| + } |
| + |
| + /** |
| + * Create a SafeBrowsingApiHandler obj and initialize its client, if supported. |
| + * Should be called on IO thread. |
| + * |
| + * @return the handler if it's usable, or null if the API is not supported. |
| + */ |
| + @CalledByNative |
| + private static SafeBrowsingApiHandler create(Context context) { |
| + SafeBrowsingApiHandler handler; |
| + try { |
| + handler = sHandler.newInstance(); |
| + } catch (NullPointerException | InstantiationException | IllegalAccessException e) { |
| + Log.e(TAG, "Failed to init handler: " + e.getMessage()); |
| + return null; |
| + } |
| + boolean initSuccesssful = handler.init(context, new SafeBrowsingApiHandler.Observer() { |
| + @Override |
| + public void onUrlCheckDone(long callbackId, int resultStatus, String metadata) { |
| + nativeOnUrlCheckDone(callbackId, resultStatus, metadata); |
| + } |
| + }); |
| + return initSuccesssful ? handler : null; |
| + } |
| + |
| + @CalledByNative |
| + private static void startUriLookup(SafeBrowsingApiHandler handler, long callbackId, |
| + String uri, int[] threatsOfInterest) { |
| + handler.startUriLookup(callbackId, uri, threatsOfInterest); |
| + Log.d(TAG, "Done starting request"); |
| + } |
| + |
| + private static native void nativeOnUrlCheckDone( |
| + long callbackId, int resultStatus, String metadata); |
| +} |