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

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: Style fixes 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
« no previous file with comments | « chrome/browser/android/omnibox/autocomplete_controller_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
380
381 if (gurl.SchemeIs(content::kChromeUIScheme) &&
382 gurl.host() == chrome::kChromeUINewTabHost) {
383 return AutocompleteInput::NTP;
384 }
385
386 if (url == chrome::kChromeUINativeNewTabURL) {
387 return focused_from_fakebox ?
388 AutocompleteInput::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS :
389 AutocompleteInput::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS;
390 }
391
392 if (url == content::kAboutBlankURL)
393 return AutocompleteInput::BLANK;
394
395 if (url == profile_->GetPrefs()->GetString(prefs::kHomePage))
396 return AutocompleteInput::HOME_PAGE;
397
398 if (is_query_in_omnibox)
399 return AutocompleteInput::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT;
400
401 bool is_search_url = TemplateURLServiceFactory::GetForProfile(profile_)->
402 IsSearchResultsPageFromDefaultSearchProvider(gurl);
403 if (is_search_url)
404 return AutocompleteInput::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT;
405
406 return AutocompleteInput::OTHER;
407 }
408
376 ScopedJavaLocalRef<jobject> 409 ScopedJavaLocalRef<jobject>
377 AutocompleteControllerAndroid::BuildOmniboxSuggestion( 410 AutocompleteControllerAndroid::BuildOmniboxSuggestion(
378 JNIEnv* env, 411 JNIEnv* env,
379 const AutocompleteMatch& match) { 412 const AutocompleteMatch& match) {
380 ScopedJavaLocalRef<jstring> contents = 413 ScopedJavaLocalRef<jstring> contents =
381 ConvertUTF16ToJavaString(env, match.contents); 414 ConvertUTF16ToJavaString(env, match.contents);
382 ScopedJavaLocalRef<jstring> description = 415 ScopedJavaLocalRef<jstring> description =
383 ConvertUTF16ToJavaString(env, match.description); 416 ConvertUTF16ToJavaString(env, match.description);
384 ScopedJavaLocalRef<jstring> answer_contents = 417 ScopedJavaLocalRef<jstring> answer_contents =
385 ConvertUTF16ToJavaString(env, match.answer_contents); 418 ConvertUTF16ToJavaString(env, match.answer_contents);
(...skipping 27 matching lines...) Expand all
413 if (profile_ == NULL) 446 if (profile_ == NULL)
414 return base::string16(); 447 return base::string16();
415 448
416 std::string languages( 449 std::string languages(
417 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); 450 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
418 451
419 return net::FormatUrl(url, languages, net::kFormatUrlOmitAll, 452 return net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
420 net::UnescapeRule::SPACES, NULL, NULL, NULL); 453 net::UnescapeRule::SPACES, NULL, NULL, NULL);
421 } 454 }
422 455
423 AutocompleteInput::PageClassification 456 ScopedJavaLocalRef<jobject>
424 AutocompleteControllerAndroid::ClassifyPage(const GURL& gurl, 457 AutocompleteControllerAndroid::GetTopSynchronousResult(
425 bool is_query_in_omnibox, 458 JNIEnv* env,
426 bool focused_from_fakebox) const { 459 jobject obj,
427 if (!gurl.is_valid()) 460 jstring j_text,
428 return AutocompleteInput::INVALID_SPEC; 461 bool prevent_inline_autocomplete) {
462 if (!autocomplete_controller_)
463 return ScopedJavaLocalRef<jobject>();
429 464
430 const std::string& url = gurl.spec(); 465 inside_synchronous_start_ = true;
466 Start(env,
467 obj,
468 j_text,
469 NULL,
470 NULL,
471 prevent_inline_autocomplete,
472 false,
473 false,
474 false);
475 inside_synchronous_start_ = false;
476 DCHECK(autocomplete_controller_->done());
477 const AutocompleteResult& result = autocomplete_controller_->result();
478 if (result.empty())
479 return ScopedJavaLocalRef<jobject>();
431 480
432 if (gurl.SchemeIs(content::kChromeUIScheme) && 481 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 } 482 }
455 483
456 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { 484 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) {
457 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); 485 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
458 if (!profile) 486 if (!profile)
459 return 0; 487 return 0;
460 488
461 AutocompleteControllerAndroid* native_bridge = 489 AutocompleteControllerAndroid* native_bridge =
462 AutocompleteControllerAndroid::Factory::GetForProfile(profile, env, obj); 490 AutocompleteControllerAndroid::Factory::GetForProfile(profile, env, obj);
463 return reinterpret_cast<intptr_t>(native_bridge); 491 return reinterpret_cast<intptr_t>(native_bridge);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 return; 527 return;
500 528
501 // ZeroSuggestPrefetcher deletes itself after it's done prefetching. 529 // ZeroSuggestPrefetcher deletes itself after it's done prefetching.
502 new ZeroSuggestPrefetcher(profile); 530 new ZeroSuggestPrefetcher(profile);
503 } 531 }
504 532
505 // Register native methods 533 // Register native methods
506 bool RegisterAutocompleteControllerAndroid(JNIEnv* env) { 534 bool RegisterAutocompleteControllerAndroid(JNIEnv* env) {
507 return RegisterNativesImpl(env); 535 return RegisterNativesImpl(env);
508 } 536 }
OLDNEW
« no previous file with comments | « chrome/browser/android/omnibox/autocomplete_controller_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698