Commit 08acb899 authored by Tommy C. Li's avatar Tommy C. Li Committed by Commit Bot

Omnibox: Add Query in Omnibox Android to Native JNI bridge code

This adds the bridge code to allow Android Chrome to access the native
Query in Omnibox implementation that lives within the omnibox/
component.

Bug: 874592
Change-Id: I6d6721c648b9de453f4a17791bc91001ac104b1a
Reviewed-on: https://chromium-review.googlesource.com/1196162
Commit-Queue: Tommy Li <tommycli@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587810}
parent e2f136e4
// Copyright 2018 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.omnibox;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.security_state.ConnectionSecurityLevel;
/**
* Bridge to the native QueryInOmniboxAndroid.
*/
public class QueryInOmnibox {
private long mNativeQueryInOmniboxAndroid;
public QueryInOmnibox(Profile profile) {
assert profile != null;
assert profile.isNativeInitialized();
mNativeQueryInOmniboxAndroid = nativeInit(profile);
}
public void destroy() {
nativeDestroy(mNativeQueryInOmniboxAndroid);
mNativeQueryInOmniboxAndroid = 0;
}
/**
* Extracts query terms from the current URL if it's a SRP URL from the default search engine.
*
* @param securityLevel The {@link ConnectionSecurityLevel} of the tab.
* @param url The URL to extract search terms from.
* @return The extracted search terms. Returns null if the Omnibox should not display the
* search terms.
*/
public String getDisplaySearchTerms(@ConnectionSecurityLevel int securityLevel, String url) {
assert mNativeQueryInOmniboxAndroid != 0;
return nativeGetDisplaySearchTerms(mNativeQueryInOmniboxAndroid, securityLevel, url);
}
/**
* Sets a flag telling the model to ignore the security level in its check for whether to
* display search terms or not. This is useful for avoiding the flicker that occurs when loading
* a SRP URL before our SSL state updates.
*
* @param ignore Whether or not we should ignore the security level.
*/
public void setIgnoreSecurityLevelForSearchTerms(boolean ignore) {
assert mNativeQueryInOmniboxAndroid != 0;
nativeSetIgnoreSecurityLevel(mNativeQueryInOmniboxAndroid, ignore);
}
private native long nativeInit(Profile profile);
private native void nativeDestroy(long nativeQueryInOmniboxAndroid);
private native String nativeGetDisplaySearchTerms(
long nativeQueryInOmniboxAndroid, int securityLevel, String url);
private native void nativeSetIgnoreSecurityLevel(
long nativeQueryInOmniboxAndroid, boolean ignore);
}
......@@ -1004,6 +1004,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/omnibox/OmniboxSuggestionsList.java",
"java/src/org/chromium/chrome/browser/omnibox/OmniboxUrlEmphasizer.java",
"java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java",
"java/src/org/chromium/chrome/browser/omnibox/QueryInOmnibox.java",
"java/src/org/chromium/chrome/browser/omnibox/SpannableAutocompleteEditTextModel.java",
"java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java",
"java/src/org/chromium/chrome/browser/omnibox/SuggestionView.java",
......
......@@ -2232,6 +2232,8 @@ jumbo_split_static_library("browser") {
"android/omnibox/autocomplete_controller_android.h",
"android/omnibox/omnibox_prerender.cc",
"android/omnibox/omnibox_prerender.h",
"android/omnibox/query_in_omnibox_android.cc",
"android/omnibox/query_in_omnibox_android.h",
"android/oom_intervention/near_oom_monitor.cc",
"android/oom_intervention/near_oom_monitor.h",
"android/oom_intervention/oom_intervention_config.cc",
......@@ -4640,6 +4642,7 @@ if (is_android) {
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxPrerender.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxUrlEmphasizer.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/QueryInOmnibox.java",
"../android/java/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeader.java",
"../android/java/src/org/chromium/chrome/browser/page_info/CertificateChainHelper.java",
"../android/java/src/org/chromium/chrome/browser/page_info/CertificateViewer.java",
......
// Copyright 2018 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.
#include "chrome/browser/android/omnibox/query_in_omnibox_android.h"
#include "base/android/jni_string.h"
#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/omnibox/browser/query_in_omnibox.h"
#include "jni/QueryInOmnibox_jni.h"
using base::android::JavaParamRef;
QueryInOmniboxAndroid::QueryInOmniboxAndroid(Profile* profile)
: query_in_omnibox_(new QueryInOmnibox(
AutocompleteClassifierFactory::GetForProfile(profile),
TemplateURLServiceFactory::GetForProfile(profile))) {}
QueryInOmniboxAndroid::~QueryInOmniboxAndroid() {}
static jlong JNI_QueryInOmnibox_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jprofile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
if (!profile)
return 0;
QueryInOmniboxAndroid* native_bridge = new QueryInOmniboxAndroid(profile);
return reinterpret_cast<intptr_t>(native_bridge);
}
void QueryInOmniboxAndroid::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
delete this;
}
base::android::ScopedJavaLocalRef<jstring>
QueryInOmniboxAndroid::GetDisplaySearchTerms(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint j_security_level,
const JavaParamRef<jstring>& j_url) {
security_state::SecurityLevel security_level =
static_cast<security_state::SecurityLevel>(j_security_level);
GURL url = GURL(base::android::ConvertJavaStringToUTF16(env, j_url));
base::string16 search_terms;
bool should_display = query_in_omnibox_->GetDisplaySearchTerms(
security_level, url, &search_terms);
if (!should_display)
return nullptr;
return base::android::ConvertUTF16ToJavaString(env, search_terms);
}
void QueryInOmniboxAndroid::SetIgnoreSecurityLevel(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
bool ignore) {
query_in_omnibox_->set_ignore_security_level(ignore);
}
// Copyright 2018 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.
#ifndef CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_
#define CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_
#include <memory>
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
class QueryInOmnibox;
class Profile;
// The native part of the Java QueryInOmnibox class.
class QueryInOmniboxAndroid {
public:
explicit QueryInOmniboxAndroid(Profile* profile);
~QueryInOmniboxAndroid();
void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
// Methods that forward to QueryInOmnibox:
base::android::ScopedJavaLocalRef<jstring> GetDisplaySearchTerms(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
jint j_security_level,
const base::android::JavaParamRef<jstring>& j_url);
void SetIgnoreSecurityLevel(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
bool ignore);
private:
std::unique_ptr<QueryInOmnibox> query_in_omnibox_;
DISALLOW_COPY_AND_ASSIGN(QueryInOmniboxAndroid);
};
#endif // CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment