• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
6 #define CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
7 
8 #include <string>
9 
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"
22 
23 class AutocompleteController;
24 struct AutocompleteMatch;
25 class AutocompleteResult;
26 class Profile;
27 class Tab;
28 
29 // The native part of the Java AutocompleteController class.
30 class AutocompleteControllerAndroid : public AutocompleteControllerDelegate,
31                                       public KeyedService {
32  public:
33   explicit AutocompleteControllerAndroid(Profile* profile);
34 
35   // Methods that forward to AutocompleteController:
36   void Start(JNIEnv* env,
37              jobject obj,
38              jstring j_text,
39              jstring j_desired_tld,
40              jstring j_current_url,
41              bool prevent_inline_autocomplete,
42              bool prefer_keyword,
43              bool allow_exact_keyword_match,
44              bool best_match_only);
45   base::android::ScopedJavaLocalRef<jobject> Classify(JNIEnv* env,
46                                                       jobject obj,
47                                                       jstring j_text);
48   void StartZeroSuggest(JNIEnv* env,
49                         jobject obj,
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,
57                             jobject obj,
58                             jint selected_index,
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(
67           JNIEnv* env,
68           jobject obj,
69           jint selected_index,
70           jlong elapsed_time_since_input_change);
71 
72   base::android::ScopedJavaLocalRef<jobject> GetTopSynchronousMatch(
73       JNIEnv* env,
74       jobject obj,
75       jstring query);
76 
77   // KeyedService:
78   virtual void Shutdown() OVERRIDE;
79 
80   class Factory : public BrowserContextKeyedServiceFactory {
81    public:
82     static AutocompleteControllerAndroid* GetForProfile(Profile* profile,
83                                              JNIEnv* env,
84                                              jobject obj);
85 
86     static Factory* GetInstance();
87 
88    protected:
89     virtual content::BrowserContext* GetBrowserContextToUse(
90         content::BrowserContext* context) const OVERRIDE;
91 
92    private:
93     friend struct DefaultSingletonTraits<Factory>;
94 
95     Factory();
96     virtual ~Factory();
97 
98     // BrowserContextKeyedServiceFactory
99     virtual KeyedService* BuildServiceInstanceFor(
100         content::BrowserContext* profile) const OVERRIDE;
101   };
102 
103  private:
104   virtual ~AutocompleteControllerAndroid();
105   void InitJNI(JNIEnv* env, jobject obj);
106 
107   // AutocompleteControllerDelegate implementation.
108   virtual void OnResultChanged(bool default_match_changed) OVERRIDE;
109 
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);
114 
115   // Classifies the type of page we are on.
116   metrics::OmniboxEventProto::PageClassification ClassifyPage(
117       const GURL& gurl,
118       bool is_query_in_omnibox,
119       bool focused_from_fakebox) const;
120 
121   base::android::ScopedJavaLocalRef<jobject> BuildOmniboxSuggestion(
122       JNIEnv* env, const AutocompleteMatch& match);
123 
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);
128 
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(
133       JNIEnv* env,
134       jobject obj,
135       jstring j_text,
136       bool prevent_inline_autocomplete);
137 
138   scoped_ptr<AutocompleteController> autocomplete_controller_;
139 
140   // Last input we sent to the autocomplete controller.
141   AutocompleteInput input_;
142 
143   // Whether we're currently inside a call to Start() that's called
144   // from GetTopSynchronousResult().
145   bool inside_synchronous_start_;
146 
147   JavaObjectWeakGlobalRef weak_java_autocomplete_controller_android_;
148   Profile* profile_;
149 
150   DISALLOW_COPY_AND_ASSIGN(AutocompleteControllerAndroid);
151 };
152 
153 // Registers the LocationBar native method.
154 bool RegisterAutocompleteControllerAndroid(JNIEnv* env);
155 
156 #endif  // CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
157