Home | History | Annotate | Download | only in omnibox
      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 "chrome/browser/autocomplete/autocomplete_input.h"
     16 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
     17 #include "components/keyed_service/core/keyed_service.h"
     18 #include "components/metrics/proto/omnibox_event.pb.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> UpdateMatchDestinationURL(
     66       JNIEnv* env,
     67       jobject obj,
     68       jint selected_index,
     69       jlong elapsed_time_since_input_change);
     70 
     71   base::android::ScopedJavaLocalRef<jobject> GetTopSynchronousMatch(
     72       JNIEnv* env,
     73       jobject obj,
     74       jstring query);
     75 
     76   // KeyedService:
     77   virtual void Shutdown() OVERRIDE;
     78 
     79   class Factory : public BrowserContextKeyedServiceFactory {
     80    public:
     81     static AutocompleteControllerAndroid* GetForProfile(Profile* profile,
     82                                              JNIEnv* env,
     83                                              jobject obj);
     84 
     85     static Factory* GetInstance();
     86 
     87    protected:
     88     virtual content::BrowserContext* GetBrowserContextToUse(
     89         content::BrowserContext* context) const OVERRIDE;
     90 
     91    private:
     92     friend struct DefaultSingletonTraits<Factory>;
     93 
     94     Factory();
     95     virtual ~Factory();
     96 
     97     // BrowserContextKeyedServiceFactory
     98     virtual KeyedService* BuildServiceInstanceFor(
     99         content::BrowserContext* profile) const OVERRIDE;
    100   };
    101 
    102  private:
    103   virtual ~AutocompleteControllerAndroid();
    104   void InitJNI(JNIEnv* env, jobject obj);
    105 
    106   // AutocompleteControllerDelegate implementation.
    107   virtual void OnResultChanged(bool default_match_changed) OVERRIDE;
    108 
    109   // Notifies the Java AutocompleteController that suggestions were received
    110   // based on the text the user typed in last.
    111   void NotifySuggestionsReceived(
    112       const AutocompleteResult& autocomplete_result);
    113 
    114   // Classifies the type of page we are on.
    115   metrics::OmniboxEventProto::PageClassification ClassifyPage(
    116       const GURL& gurl,
    117       bool is_query_in_omnibox,
    118       bool focused_from_fakebox) const;
    119 
    120   base::android::ScopedJavaLocalRef<jobject> BuildOmniboxSuggestion(
    121       JNIEnv* env, const AutocompleteMatch& match);
    122 
    123   // Converts destination_url (which is in its canonical form or punycode) to a
    124   // user-friendly URL by looking up accept languages of the current profile.
    125   // e.g. http://xn--6q8b.kr/ --> .kr
    126   base::string16 FormatURLUsingAcceptLanguages(GURL url);
    127 
    128   // A helper method for fetching the top synchronous autocomplete result.
    129   // The |prevent_inline_autocomplete| flag is passed to the AutocompleteInput
    130   // object, see documentation there for its description.
    131   base::android::ScopedJavaLocalRef<jobject> GetTopSynchronousResult(
    132       JNIEnv* env,
    133       jobject obj,
    134       jstring j_text,
    135       bool prevent_inline_autocomplete);
    136 
    137   scoped_ptr<AutocompleteController> autocomplete_controller_;
    138 
    139   // Last input we sent to the autocomplete controller.
    140   AutocompleteInput input_;
    141 
    142   // Whether we're currently inside a call to Start() that's called
    143   // from GetTopSynchronousResult().
    144   bool inside_synchronous_start_;
    145 
    146   JavaObjectWeakGlobalRef weak_java_autocomplete_controller_android_;
    147   Profile* profile_;
    148 
    149   DISALLOW_COPY_AND_ASSIGN(AutocompleteControllerAndroid);
    150 };
    151 
    152 // Registers the LocationBar native method.
    153 bool RegisterAutocompleteControllerAndroid(JNIEnv* env);
    154 
    155 #endif  // CHROME_BROWSER_ANDROID_OMNIBOX_AUTOCOMPLETE_CONTROLLER_ANDROID_H_
    156