Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: chrome/browser/android/omnibox/autocomplete_controller_android.cc

Issue 319813002: Add a method to retrieve the top synchronous suggestion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix docs Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/omnibox/autocomplete_controller_android.h" 5 #include "chrome/browser/android/omnibox/autocomplete_controller_android.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // Nothing to do here, the results have been cached. 108 // Nothing to do here, the results have been cached.
109 // We don't want to trigger deletion here because this is being called by the 109 // We don't want to trigger deletion here because this is being called by the
110 // AutocompleteController object. 110 // AutocompleteController object.
111 } 111 }
112 112
113 } // namespace 113 } // namespace
114 114
115 AutocompleteControllerAndroid::AutocompleteControllerAndroid(Profile* profile) 115 AutocompleteControllerAndroid::AutocompleteControllerAndroid(Profile* profile)
116 : autocomplete_controller_(new AutocompleteController( 116 : autocomplete_controller_(new AutocompleteController(
117 profile, this, kAndroidAutocompleteProviders)), 117 profile, this, kAndroidAutocompleteProviders)),
118 inside_classify_(false), 118 inside_synchronous_start_(false),
119 profile_(profile) { 119 profile_(profile) {
120 } 120 }
121 121
122 void AutocompleteControllerAndroid::Start(JNIEnv* env, 122 void AutocompleteControllerAndroid::Start(JNIEnv* env,
123 jobject obj, 123 jobject obj,
124 jstring j_text, 124 jstring j_text,
125 jstring j_desired_tld, 125 jstring j_desired_tld,
126 jstring j_current_url, 126 jstring j_current_url,
127 bool prevent_inline_autocomplete, 127 bool prevent_inline_autocomplete,
128 bool prefer_keyword, 128 bool prefer_keyword,
(...skipping 20 matching lines...) Expand all
149 prefer_keyword, 149 prefer_keyword,
150 allow_exact_keyword_match, 150 allow_exact_keyword_match,
151 want_asynchronous_matches); 151 want_asynchronous_matches);
152 autocomplete_controller_->Start(input_); 152 autocomplete_controller_->Start(input_);
153 } 153 }
154 154
155 ScopedJavaLocalRef<jobject> AutocompleteControllerAndroid::Classify( 155 ScopedJavaLocalRef<jobject> AutocompleteControllerAndroid::Classify(
156 JNIEnv* env, 156 JNIEnv* env,
157 jobject obj, 157 jobject obj,
158 jstring j_text) { 158 jstring j_text) {
159 if (!autocomplete_controller_) 159 return GetTopSynchronousResult(env, obj, j_text, true);
160 return ScopedJavaLocalRef<jobject>();
161
162 inside_classify_ = true;
163 Start(env, obj, j_text, NULL, NULL, true, false, false, false);
164 inside_classify_ = false;
165 DCHECK(autocomplete_controller_->done());
166 const AutocompleteResult& result = autocomplete_controller_->result();
167 if (result.empty())
168 return ScopedJavaLocalRef<jobject>();
169
170 return BuildOmniboxSuggestion(env, *result.begin());
171 } 160 }
172 161
173 void AutocompleteControllerAndroid::StartZeroSuggest( 162 void AutocompleteControllerAndroid::StartZeroSuggest(
174 JNIEnv* env, 163 JNIEnv* env,
175 jobject obj, 164 jobject obj,
176 jstring j_omnibox_text, 165 jstring j_omnibox_text,
177 jstring j_current_url, 166 jstring j_current_url,
178 jboolean is_query_in_omnibox, 167 jboolean is_query_in_omnibox,
179 jboolean focused_from_fakebox) { 168 jboolean focused_from_fakebox) {
180 if (!autocomplete_controller_) 169 if (!autocomplete_controller_)
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 return ScopedJavaLocalRef<jstring>(); 256 return ScopedJavaLocalRef<jstring>();
268 257
269 AutocompleteMatch match( 258 AutocompleteMatch match(
270 autocomplete_controller_->result().match_at(selected_index)); 259 autocomplete_controller_->result().match_at(selected_index));
271 autocomplete_controller_->UpdateMatchDestinationURL( 260 autocomplete_controller_->UpdateMatchDestinationURL(
272 base::TimeDelta::FromMilliseconds(elapsed_time_since_input_change), 261 base::TimeDelta::FromMilliseconds(elapsed_time_since_input_change),
273 &match); 262 &match);
274 return ConvertUTF8ToJavaString(env, match.destination_url.spec()); 263 return ConvertUTF8ToJavaString(env, match.destination_url.spec());
275 } 264 }
276 265
266 ScopedJavaLocalRef<jobject>
267 AutocompleteControllerAndroid::GetTopSynchronousMatch(JNIEnv* env,
268 jobject obj,
269 jstring query) {
270 return GetTopSynchronousResult(env, obj, query, false);
271 }
272
277 void AutocompleteControllerAndroid::Shutdown() { 273 void AutocompleteControllerAndroid::Shutdown() {
278 autocomplete_controller_.reset(); 274 autocomplete_controller_.reset();
279 275
280 JNIEnv* env = AttachCurrentThread(); 276 JNIEnv* env = AttachCurrentThread();
281 ScopedJavaLocalRef<jobject> java_bridge = 277 ScopedJavaLocalRef<jobject> java_bridge =
282 weak_java_autocomplete_controller_android_.get(env); 278 weak_java_autocomplete_controller_android_.get(env);
283 if (java_bridge.obj()) 279 if (java_bridge.obj())
284 Java_AutocompleteController_notifyNativeDestroyed(env, java_bridge.obj()); 280 Java_AutocompleteController_notifyNativeDestroyed(env, java_bridge.obj());
285 281
286 weak_java_autocomplete_controller_android_.reset(); 282 weak_java_autocomplete_controller_android_.reset();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 AutocompleteControllerAndroid::~AutocompleteControllerAndroid() { 322 AutocompleteControllerAndroid::~AutocompleteControllerAndroid() {
327 } 323 }
328 324
329 void AutocompleteControllerAndroid::InitJNI(JNIEnv* env, jobject obj) { 325 void AutocompleteControllerAndroid::InitJNI(JNIEnv* env, jobject obj) {
330 weak_java_autocomplete_controller_android_ = 326 weak_java_autocomplete_controller_android_ =
331 JavaObjectWeakGlobalRef(env, obj); 327 JavaObjectWeakGlobalRef(env, obj);
332 } 328 }
333 329
334 void AutocompleteControllerAndroid::OnResultChanged( 330 void AutocompleteControllerAndroid::OnResultChanged(
335 bool default_match_changed) { 331 bool default_match_changed) {
336 if (autocomplete_controller_.get() != NULL && !inside_classify_) 332 if (autocomplete_controller_.get() != NULL && !inside_synchronous_start_)
337 NotifySuggestionsReceived(autocomplete_controller_->result()); 333 NotifySuggestionsReceived(autocomplete_controller_->result());
338 } 334 }
339 335
340 void AutocompleteControllerAndroid::NotifySuggestionsReceived( 336 void AutocompleteControllerAndroid::NotifySuggestionsReceived(
341 const AutocompleteResult& autocomplete_result) { 337 const AutocompleteResult& autocomplete_result) {
342 JNIEnv* env = AttachCurrentThread(); 338 JNIEnv* env = AttachCurrentThread();
343 ScopedJavaLocalRef<jobject> java_bridge = 339 ScopedJavaLocalRef<jobject> java_bridge =
344 weak_java_autocomplete_controller_android_.get(env); 340 weak_java_autocomplete_controller_android_.get(env);
345 if (!java_bridge.obj()) 341 if (!java_bridge.obj())
346 return; 342 return;
(...skipping 19 matching lines...) Expand all
366 ConvertUTF16ToJavaString(env, inline_autocomplete_text); 362 ConvertUTF16ToJavaString(env, inline_autocomplete_text);
367 jlong j_autocomplete_result = 363 jlong j_autocomplete_result =
368 reinterpret_cast<intptr_t>(&(autocomplete_result)); 364 reinterpret_cast<intptr_t>(&(autocomplete_result));
369 Java_AutocompleteController_onSuggestionsReceived(env, 365 Java_AutocompleteController_onSuggestionsReceived(env,
370 java_bridge.obj(), 366 java_bridge.obj(),
371 suggestion_list_obj.obj(), 367 suggestion_list_obj.obj(),
372 inline_text.obj(), 368 inline_text.obj(),
373 j_autocomplete_result); 369 j_autocomplete_result);
374 } 370 }
375 371
372 AutocompleteInput::PageClassification
373 AutocompleteControllerAndroid::ClassifyPage(const GURL& gurl,
374 bool is_query_in_omnibox,
375 bool focused_from_fakebox) const {
376 if (!gurl.is_valid())
377 return AutocompleteInput::INVALID_SPEC;
378
379 const std::string& url = gurl.spec();
David Trainor- moved to gerrit 2014/06/06 01:20:39 url(gurl.spec()) if it works w/ reference?
Maria 2014/06/06 01:27:55 That's not necessary with reference. Since there's
380
381 if (gurl.SchemeIs(content::kChromeUIScheme) &&
David Trainor- moved to gerrit 2014/06/06 01:20:39 new line after each if block or no new lines imo (
Maria 2014/06/06 01:27:55 Done.
382 gurl.host() == chrome::kChromeUINewTabHost) {
383 return AutocompleteInput::NTP;
384 }
385 if (url == chrome::kChromeUINativeNewTabURL) {
386 return focused_from_fakebox ?
387 AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS :
388 AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
389 }
390 if (url == content::kAboutBlankURL)
391 return AutocompleteInput::BLANK;
392
393 if (url == profile_->GetPrefs()->GetString(prefs::kHomePage))
394 return AutocompleteInput::HOME_PAGE;
395 if (is_query_in_omnibox)
396 return AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
397
398 bool is_search_url = TemplateURLServiceFactory::GetForProfile(profile_)->
399 IsSearchResultsPageFromDefaultSearchProvider(gurl);
400 if (is_search_url)
401 return AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
402 return AutocompleteInput::OTHER;
403 }
404
376 ScopedJavaLocalRef<jobject> 405 ScopedJavaLocalRef<jobject>
377 AutocompleteControllerAndroid::BuildOmniboxSuggestion( 406 AutocompleteControllerAndroid::BuildOmniboxSuggestion(
378 JNIEnv* env, 407 JNIEnv* env,
379 const AutocompleteMatch& match) { 408 const AutocompleteMatch& match) {
380 ScopedJavaLocalRef<jstring> contents = 409 ScopedJavaLocalRef<jstring> contents =
381 ConvertUTF16ToJavaString(env, match.contents); 410 ConvertUTF16ToJavaString(env, match.contents);
382 ScopedJavaLocalRef<jstring> description = 411 ScopedJavaLocalRef<jstring> description =
383 ConvertUTF16ToJavaString(env, match.description); 412 ConvertUTF16ToJavaString(env, match.description);
384 ScopedJavaLocalRef<jstring> answer_contents = 413 ScopedJavaLocalRef<jstring> answer_contents =
385 ConvertUTF16ToJavaString(env, match.answer_contents); 414 ConvertUTF16ToJavaString(env, match.answer_contents);
(...skipping 27 matching lines...) Expand all
413 if (profile_ == NULL) 442 if (profile_ == NULL)
414 return base::string16(); 443 return base::string16();
415 444
416 std::string languages( 445 std::string languages(
417 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); 446 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
418 447
419 return net::FormatUrl(url, languages, net::kFormatUrlOmitAll, 448 return net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
420 net::UnescapeRule::SPACES, NULL, NULL, NULL); 449 net::UnescapeRule::SPACES, NULL, NULL, NULL);
421 } 450 }
422 451
423 AutocompleteInput::PageClassification 452 ScopedJavaLocalRef<jobject>
424 AutocompleteControllerAndroid::ClassifyPage(const GURL& gurl, 453 AutocompleteControllerAndroid::GetTopSynchronousResult(
425 bool is_query_in_omnibox, 454 JNIEnv* env,
426 bool focused_from_fakebox) const { 455 jobject obj,
427 if (!gurl.is_valid()) 456 jstring j_text,
428 return AutocompleteInput::INVALID_SPEC; 457 bool prevent_inline_autocomplete) {
458 if (!autocomplete_controller_)
459 return ScopedJavaLocalRef<jobject>();
429 460
430 const std::string& url = gurl.spec(); 461 inside_synchronous_start_ = true;
462 Start(env, obj, j_text, NULL, NULL, prevent_inline_autocomplete, false, false,
David Trainor- moved to gerrit 2014/06/06 01:20:39 each param on it's own line if they don't fit on a
Maria 2014/06/06 01:27:55 Done. GRRR. Have I mentioned to anyone lately how
463 false);
464 inside_synchronous_start_ = false;
465 DCHECK(autocomplete_controller_->done());
466 const AutocompleteResult& result = autocomplete_controller_->result();
467 if (result.empty())
468 return ScopedJavaLocalRef<jobject>();
431 469
432 if (gurl.SchemeIs(content::kChromeUIScheme) && 470 return BuildOmniboxSuggestion(env, *result.begin());
433 gurl.host() == chrome::kChromeUINewTabHost) {
434 return AutocompleteInput::NTP;
435 }
436 if (url == chrome::kChromeUINativeNewTabURL) {
437 return focused_from_fakebox ?
438 AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS :
439 AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
440 }
441 if (url == content::kAboutBlankURL)
442 return AutocompleteInput::BLANK;
443
444 if (url == profile_->GetPrefs()->GetString(prefs::kHomePage))
445 return AutocompleteInput::HOME_PAGE;
446 if (is_query_in_omnibox)
447 return AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
448
449 bool is_search_url = TemplateURLServiceFactory::GetForProfile(profile_)->
450 IsSearchResultsPageFromDefaultSearchProvider(gurl);
451 if (is_search_url)
452 return AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
453 return AutocompleteInput::OTHER;
454 } 471 }
455 472
456 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { 473 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) {
457 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); 474 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
458 if (!profile) 475 if (!profile)
459 return 0; 476 return 0;
460 477
461 AutocompleteControllerAndroid* native_bridge = 478 AutocompleteControllerAndroid* native_bridge =
462 AutocompleteControllerAndroid::Factory::GetForProfile(profile, env, obj); 479 AutocompleteControllerAndroid::Factory::GetForProfile(profile, env, obj);
463 return reinterpret_cast<intptr_t>(native_bridge); 480 return reinterpret_cast<intptr_t>(native_bridge);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 return; 516 return;
500 517
501 // ZeroSuggestPrefetcher deletes itself after it's done prefetching. 518 // ZeroSuggestPrefetcher deletes itself after it's done prefetching.
502 new ZeroSuggestPrefetcher(profile); 519 new ZeroSuggestPrefetcher(profile);
503 } 520 }
504 521
505 // Register native methods 522 // Register native methods
506 bool RegisterAutocompleteControllerAndroid(JNIEnv* env) { 523 bool RegisterAutocompleteControllerAndroid(JNIEnv* env) {
507 return RegisterNativesImpl(env); 524 return RegisterNativesImpl(env);
508 } 525 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698