Index: chrome/android/java/src/org/chromium/chrome/browser/search_engines/TemplateUrlService.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/search_engines/TemplateUrlService.java b/chrome/android/java/src/org/chromium/chrome/browser/search_engines/TemplateUrlService.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..43b0edceb6841dbdd3a72aba2c3361933fd93c76 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/search_engines/TemplateUrlService.java |
@@ -0,0 +1,118 @@ |
+// Copyright (c) 2013 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.search_engines; |
+ |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.ThreadUtils; |
+ |
+import java.util.ArrayList; |
+import java.util.HashMap; |
+import java.util.List; |
+ |
+/** |
+ * Android wrapper of the TemplateUrlService which provides access from the Java |
+ * layer. |
+ * |
+ * See chrome/browser/search_engines/template_url_service.h for more details. |
+ */ |
+public class TemplateUrlService { |
+ |
+ /** |
+ * This listener will be notified when template url service is done laoding. |
+ */ |
+ public interface LoadListener { |
+ public abstract void onTemplateUrlServiceLoaded(); |
+ } |
+ |
+ private static TemplateUrlService sService; |
nyquist
2013/02/20 07:33:09
Nit: Insert newline
Yaron
2013/02/21 21:39:15
Done.
|
+ public static TemplateUrlService getInstance() { |
+ ThreadUtils.assertOnUiThread(); |
nyquist
2013/02/20 07:33:09
Maybe add to the class-level documentation that th
Yaron
2013/02/21 21:39:15
I decided to keep this simple and require all acce
|
+ if (sService == null) { |
+ sService = new TemplateUrlService(); |
+ } |
+ return sService; |
+ } |
+ |
+ private TemplateUrlService() { |
+ // Note that this technically leaks the native object, however, TemlateUrlService |
+ // is a singleton that lives forever and there's no clean shutdown of Chrome on Android |
+ mNativeTemplateUrlServiceAndroid = nativeInit(); |
+ } |
+ |
+ public boolean isLoaded() { |
+ return nativeIsLoaded(mNativeTemplateUrlServiceAndroid); |
+ } |
+ |
+ public void load() { |
+ nativeLoad(mNativeTemplateUrlServiceAndroid); |
+ } |
+ |
+ /** |
+ * Get the hash map of the localized search engines. |
+ */ |
+ public HashMap<String,String>[] getLocalizedSearchEngines() { |
+ return nativeGetLocalizedSearchEngines(mNativeTemplateUrlServiceAndroid); |
+ } |
+ |
+ private final int mNativeTemplateUrlServiceAndroid; |
nyquist
2013/02/20 07:33:09
Move final member fields above constructor
Yaron
2013/02/21 21:39:15
Done.
|
+ private final List<LoadListener> mLoadListeners = new ArrayList<LoadListener>(); |
+ |
+ // Keys for localized search engines. |
+ // MUST match static strings defined in template_url_service_android.cc |
+ public static final String SEARCH_ENGINE_ID = "searchEngineId"; |
nyquist
2013/02/20 07:33:09
Move these up to the top of the class, before any
Yaron
2013/02/21 21:39:15
Done.
|
+ public static final String SEARCH_ENGINE_URL = "searchEngineUrl"; |
+ public static final String SEARCH_ENGINE_KEYWORD = "searchEngineKeyword"; |
+ public static final String SEARCH_ENGINE_SHORT_NAME = "searchEngineShortName"; |
+ |
+ /** |
+ * Called from native when template URL service is done loading. |
+ */ |
+ @CalledByNative |
+ private void templateUrlServiceLoaded() { |
+ synchronized (mLoadListeners) { |
bulach
2013/02/20 14:41:25
so the listeners will be potentially called on a d
Yaron
2013/02/21 21:39:15
Changed to only ui thread.
|
+ for (LoadListener listener : mLoadListeners) { |
+ listener.onTemplateUrlServiceLoaded(); |
+ } |
+ } |
+ } |
+ |
+ public int getSearchEngine() { |
+ return nativeGetSearchEngine(mNativeTemplateUrlServiceAndroid); |
+ } |
+ |
+ public void setSearchEngine(int selectedIndex) { |
+ nativeSetSearchEngine(mNativeTemplateUrlServiceAndroid, selectedIndex); |
+ } |
+ |
+ /** |
+ * Registers a listener for the TEMPLATE_URL_SERVICE_LOADED notification. |
+ */ |
+ public void registerLoadListener(LoadListener listener) { |
+ synchronized (mLoadListeners) { |
+ assert !mLoadListeners.contains(listener); |
+ mLoadListeners.add(listener); |
+ } |
+ } |
+ |
+ /** |
+ * Unregisters a listener for the TEMPLATE_URL_SERVICE_LOADED notification. |
+ */ |
+ public void unregisterLoadListener(LoadListener listener) { |
+ synchronized (mLoadListeners) { |
+ assert (mLoadListeners.size() > 0); |
+ assert (mLoadListeners.contains(listener)); |
+ mLoadListeners.remove(listener); |
+ } |
+ } |
+ |
+ private native int nativeInit(); |
+ private native void nativeLoad(int nativeTemplateUrlServiceAndroid); |
+ private native boolean nativeIsLoaded(int nativeTemplateUrlServiceAndroid); |
+ private native HashMap<String,String>[] nativeGetLocalizedSearchEngines( |
+ int nativeTemplateUrlServiceAndroid); |
+ private native void nativeSetSearchEngine(int nativeTemplateUrlServiceAndroid, |
+ int selectedIndex); |
+ private native int nativeGetSearchEngine(int nativeTemplateUrlServiceAndroid); |
+} |