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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
6 #define CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
10 #include "base/android/jni_weak_ref.h"
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
15 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/metrics/proto/omnibox_event.pb.h"
18 #include "components/omnibox/autocomplete_input.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h"
23 class AutocompleteController;
24 struct AutocompleteMatch;
25 class AutocompleteResult;
29 // The native part of the Java AutocompleteController class.
30 class AutocompleteControllerAndroid : public AutocompleteControllerDelegate,
33 explicit AutocompleteControllerAndroid(Profile* profile);
35 // Methods that forward to AutocompleteController:
36 void Start(JNIEnv* env,
39 jstring j_desired_tld,
40 jstring j_current_url,
41 bool prevent_inline_autocomplete,
43 bool allow_exact_keyword_match,
44 bool best_match_only);
45 base::android::ScopedJavaLocalRef<jobject> Classify(JNIEnv* env,
48 void StartZeroSuggest(JNIEnv* env,
50 jstring j_omnibox_text,
51 jstring j_current_url,
52 jboolean is_query_in_omnibox,
53 jboolean focused_from_fakebox);
54 void Stop(JNIEnv* env, jobject obj, bool clear_result);
55 void ResetSession(JNIEnv* env, jobject obj);
56 void OnSuggestionSelected(JNIEnv* env,
59 jstring j_current_url,
60 jboolean is_query_in_omnibox,
61 jboolean focused_from_fakebox,
62 jlong elapsed_time_since_first_modified,
63 jobject j_web_contents);
64 void DeleteSuggestion(JNIEnv* env, jobject obj, int selected_index);
65 base::android::ScopedJavaLocalRef<jstring>
66 UpdateMatchDestinationURLWithQueryFormulationTime(
70 jlong elapsed_time_since_input_change);
72 base::android::ScopedJavaLocalRef<jobject> GetTopSynchronousMatch(
78 virtual void Shutdown() OVERRIDE;
80 class Factory : public BrowserContextKeyedServiceFactory {
82 static AutocompleteControllerAndroid* GetForProfile(Profile* profile,
86 static Factory* GetInstance();
89 virtual content::BrowserContext* GetBrowserContextToUse(
90 content::BrowserContext* context) const OVERRIDE;
93 friend struct DefaultSingletonTraits<Factory>;
98 // BrowserContextKeyedServiceFactory
99 virtual KeyedService* BuildServiceInstanceFor(
100 content::BrowserContext* profile) const OVERRIDE;
104 virtual ~AutocompleteControllerAndroid();
105 void InitJNI(JNIEnv* env, jobject obj);
107 // AutocompleteControllerDelegate implementation.
108 virtual void OnResultChanged(bool default_match_changed) OVERRIDE;
110 // Notifies the Java AutocompleteController that suggestions were received
111 // based on the text the user typed in last.
112 void NotifySuggestionsReceived(
113 const AutocompleteResult& autocomplete_result);
115 // Classifies the type of page we are on.
116 metrics::OmniboxEventProto::PageClassification ClassifyPage(
118 bool is_query_in_omnibox,
119 bool focused_from_fakebox) const;
121 base::android::ScopedJavaLocalRef<jobject> BuildOmniboxSuggestion(
122 JNIEnv* env, const AutocompleteMatch& match);
124 // Converts destination_url (which is in its canonical form or punycode) to a
125 // user-friendly URL by looking up accept languages of the current profile.
126 // e.g. http://xn--6q8b.kr/ --> 한.kr
127 base::string16 FormatURLUsingAcceptLanguages(GURL url);
129 // A helper method for fetching the top synchronous autocomplete result.
130 // The |prevent_inline_autocomplete| flag is passed to the AutocompleteInput
131 // object, see documentation there for its description.
132 base::android::ScopedJavaLocalRef<jobject> GetTopSynchronousResult(
136 bool prevent_inline_autocomplete);
138 scoped_ptr<AutocompleteController> autocomplete_controller_;
140 // Last input we sent to the autocomplete controller.
141 AutocompleteInput input_;
143 // Whether we're currently inside a call to Start() that's called
144 // from GetTopSynchronousResult().
145 bool inside_synchronous_start_;
147 JavaObjectWeakGlobalRef weak_java_autocomplete_controller_android_;
150 DISALLOW_COPY_AND_ASSIGN(AutocompleteControllerAndroid);
153 // Registers the LocationBar native method.
154 bool RegisterAutocompleteControllerAndroid(JNIEnv* env);
156 #endif // CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_