Skip to content

Commit 08acb89

Browse files
Tommy C. LiCommit Bot
authored andcommitted
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 <[email protected]> Reviewed-by: Ted Choc <[email protected]> Cr-Commit-Position: refs/heads/master@{#587810}
1 parent e2f136e commit 08acb89

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.chromium.chrome.browser.omnibox;
6+
7+
import org.chromium.chrome.browser.profiles.Profile;
8+
import org.chromium.components.security_state.ConnectionSecurityLevel;
9+
10+
/**
11+
* Bridge to the native QueryInOmniboxAndroid.
12+
*/
13+
public class QueryInOmnibox {
14+
private long mNativeQueryInOmniboxAndroid;
15+
16+
public QueryInOmnibox(Profile profile) {
17+
assert profile != null;
18+
assert profile.isNativeInitialized();
19+
mNativeQueryInOmniboxAndroid = nativeInit(profile);
20+
}
21+
22+
public void destroy() {
23+
nativeDestroy(mNativeQueryInOmniboxAndroid);
24+
mNativeQueryInOmniboxAndroid = 0;
25+
}
26+
27+
/**
28+
* Extracts query terms from the current URL if it's a SRP URL from the default search engine.
29+
*
30+
* @param securityLevel The {@link ConnectionSecurityLevel} of the tab.
31+
* @param url The URL to extract search terms from.
32+
* @return The extracted search terms. Returns null if the Omnibox should not display the
33+
* search terms.
34+
*/
35+
public String getDisplaySearchTerms(@ConnectionSecurityLevel int securityLevel, String url) {
36+
assert mNativeQueryInOmniboxAndroid != 0;
37+
return nativeGetDisplaySearchTerms(mNativeQueryInOmniboxAndroid, securityLevel, url);
38+
}
39+
40+
/**
41+
* Sets a flag telling the model to ignore the security level in its check for whether to
42+
* display search terms or not. This is useful for avoiding the flicker that occurs when loading
43+
* a SRP URL before our SSL state updates.
44+
*
45+
* @param ignore Whether or not we should ignore the security level.
46+
*/
47+
public void setIgnoreSecurityLevelForSearchTerms(boolean ignore) {
48+
assert mNativeQueryInOmniboxAndroid != 0;
49+
nativeSetIgnoreSecurityLevel(mNativeQueryInOmniboxAndroid, ignore);
50+
}
51+
52+
private native long nativeInit(Profile profile);
53+
private native void nativeDestroy(long nativeQueryInOmniboxAndroid);
54+
private native String nativeGetDisplaySearchTerms(
55+
long nativeQueryInOmniboxAndroid, int securityLevel, String url);
56+
private native void nativeSetIgnoreSecurityLevel(
57+
long nativeQueryInOmniboxAndroid, boolean ignore);
58+
}

chrome/android/java_sources.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ chrome_java_sources = [
10041004
"java/src/org/chromium/chrome/browser/omnibox/OmniboxSuggestionsList.java",
10051005
"java/src/org/chromium/chrome/browser/omnibox/OmniboxUrlEmphasizer.java",
10061006
"java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java",
1007+
"java/src/org/chromium/chrome/browser/omnibox/QueryInOmnibox.java",
10071008
"java/src/org/chromium/chrome/browser/omnibox/SpannableAutocompleteEditTextModel.java",
10081009
"java/src/org/chromium/chrome/browser/omnibox/SuggestionAnswer.java",
10091010
"java/src/org/chromium/chrome/browser/omnibox/SuggestionView.java",

chrome/browser/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,8 @@ jumbo_split_static_library("browser") {
22322232
"android/omnibox/autocomplete_controller_android.h",
22332233
"android/omnibox/omnibox_prerender.cc",
22342234
"android/omnibox/omnibox_prerender.h",
2235+
"android/omnibox/query_in_omnibox_android.cc",
2236+
"android/omnibox/query_in_omnibox_android.h",
22352237
"android/oom_intervention/near_oom_monitor.cc",
22362238
"android/oom_intervention/near_oom_monitor.h",
22372239
"android/oom_intervention/oom_intervention_config.cc",
@@ -4640,6 +4642,7 @@ if (is_android) {
46404642
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxPrerender.java",
46414643
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxUrlEmphasizer.java",
46424644
"../android/java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java",
4645+
"../android/java/src/org/chromium/chrome/browser/omnibox/QueryInOmnibox.java",
46434646
"../android/java/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeader.java",
46444647
"../android/java/src/org/chromium/chrome/browser/page_info/CertificateChainHelper.java",
46454648
"../android/java/src/org/chromium/chrome/browser/page_info/CertificateViewer.java",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "chrome/browser/android/omnibox/query_in_omnibox_android.h"
6+
7+
#include "base/android/jni_string.h"
8+
#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
9+
#include "chrome/browser/profiles/profile_android.h"
10+
#include "chrome/browser/search_engines/template_url_service_factory.h"
11+
#include "components/omnibox/browser/query_in_omnibox.h"
12+
#include "jni/QueryInOmnibox_jni.h"
13+
14+
using base::android::JavaParamRef;
15+
16+
QueryInOmniboxAndroid::QueryInOmniboxAndroid(Profile* profile)
17+
: query_in_omnibox_(new QueryInOmnibox(
18+
AutocompleteClassifierFactory::GetForProfile(profile),
19+
TemplateURLServiceFactory::GetForProfile(profile))) {}
20+
21+
QueryInOmniboxAndroid::~QueryInOmniboxAndroid() {}
22+
23+
static jlong JNI_QueryInOmnibox_Init(JNIEnv* env,
24+
const JavaParamRef<jobject>& obj,
25+
const JavaParamRef<jobject>& jprofile) {
26+
Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
27+
if (!profile)
28+
return 0;
29+
30+
QueryInOmniboxAndroid* native_bridge = new QueryInOmniboxAndroid(profile);
31+
return reinterpret_cast<intptr_t>(native_bridge);
32+
}
33+
34+
void QueryInOmniboxAndroid::Destroy(JNIEnv* env,
35+
const JavaParamRef<jobject>& obj) {
36+
delete this;
37+
}
38+
39+
base::android::ScopedJavaLocalRef<jstring>
40+
QueryInOmniboxAndroid::GetDisplaySearchTerms(
41+
JNIEnv* env,
42+
const JavaParamRef<jobject>& obj,
43+
jint j_security_level,
44+
const JavaParamRef<jstring>& j_url) {
45+
security_state::SecurityLevel security_level =
46+
static_cast<security_state::SecurityLevel>(j_security_level);
47+
GURL url = GURL(base::android::ConvertJavaStringToUTF16(env, j_url));
48+
49+
base::string16 search_terms;
50+
bool should_display = query_in_omnibox_->GetDisplaySearchTerms(
51+
security_level, url, &search_terms);
52+
53+
if (!should_display)
54+
return nullptr;
55+
56+
return base::android::ConvertUTF16ToJavaString(env, search_terms);
57+
}
58+
59+
void QueryInOmniboxAndroid::SetIgnoreSecurityLevel(
60+
JNIEnv* env,
61+
const JavaParamRef<jobject>& obj,
62+
bool ignore) {
63+
query_in_omnibox_->set_ignore_security_level(ignore);
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_
6+
#define CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_
7+
8+
#include <memory>
9+
10+
#include "base/android/scoped_java_ref.h"
11+
#include "base/macros.h"
12+
13+
class QueryInOmnibox;
14+
class Profile;
15+
16+
// The native part of the Java QueryInOmnibox class.
17+
class QueryInOmniboxAndroid {
18+
public:
19+
explicit QueryInOmniboxAndroid(Profile* profile);
20+
~QueryInOmniboxAndroid();
21+
22+
void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
23+
24+
// Methods that forward to QueryInOmnibox:
25+
base::android::ScopedJavaLocalRef<jstring> GetDisplaySearchTerms(
26+
JNIEnv* env,
27+
const base::android::JavaParamRef<jobject>& obj,
28+
jint j_security_level,
29+
const base::android::JavaParamRef<jstring>& j_url);
30+
void SetIgnoreSecurityLevel(JNIEnv* env,
31+
const base::android::JavaParamRef<jobject>& obj,
32+
bool ignore);
33+
34+
private:
35+
std::unique_ptr<QueryInOmnibox> query_in_omnibox_;
36+
37+
DISALLOW_COPY_AND_ASSIGN(QueryInOmniboxAndroid);
38+
};
39+
40+
#endif // CHROME_BROWSER_ANDROID_OMNIBOX_QUERY_IN_OMNIBOX_ANDROID_H_

0 commit comments

Comments
 (0)