Home | History | Annotate | Download | only in omnibox
      1 // Copyright (c) 2012 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_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/strings/string16.h"
     14 #include "chrome/browser/autocomplete/autocomplete_match.h"
     15 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
     16 #include "chrome/browser/extensions/extension_function.h"
     17 #include "chrome/browser/extensions/extension_icon_manager.h"
     18 #include "chrome/common/extensions/api/omnibox.h"
     19 #include "content/public/browser/notification_observer.h"
     20 #include "content/public/browser/notification_registrar.h"
     21 #include "ui/base/window_open_disposition.h"
     22 
     23 class Profile;
     24 class TemplateURL;
     25 class TemplateURLService;
     26 
     27 namespace base {
     28 class ListValue;
     29 }
     30 
     31 namespace content {
     32 class WebContents;
     33 }
     34 
     35 namespace gfx {
     36 class Image;
     37 }
     38 
     39 namespace extensions {
     40 
     41 // Event router class for events related to the omnibox API.
     42 class ExtensionOmniboxEventRouter {
     43  public:
     44   // The user has just typed the omnibox keyword. This is sent exactly once in
     45   // a given input session, before any OnInputChanged events.
     46   static void OnInputStarted(
     47       Profile* profile, const std::string& extension_id);
     48 
     49   // The user has changed what is typed into the omnibox while in an extension
     50   // keyword session. Returns true if someone is listening to this event, and
     51   // thus we have some degree of confidence we'll get a response.
     52   static bool OnInputChanged(
     53       Profile* profile,
     54       const std::string& extension_id,
     55       const std::string& input, int suggest_id);
     56 
     57   // The user has accepted the omnibox input.
     58   static void OnInputEntered(
     59       content::WebContents* web_contents,
     60       const std::string& extension_id,
     61       const std::string& input,
     62       WindowOpenDisposition disposition);
     63 
     64   // The user has cleared the keyword, or closed the omnibox popup. This is
     65   // sent at most once in a give input session, after any OnInputChanged events.
     66   static void OnInputCancelled(
     67       Profile* profile, const std::string& extension_id);
     68 
     69  private:
     70   DISALLOW_COPY_AND_ASSIGN(ExtensionOmniboxEventRouter);
     71 };
     72 
     73 class OmniboxSendSuggestionsFunction : public SyncExtensionFunction {
     74  public:
     75   DECLARE_EXTENSION_FUNCTION("omnibox.sendSuggestions", OMNIBOX_SENDSUGGESTIONS)
     76 
     77  protected:
     78   virtual ~OmniboxSendSuggestionsFunction() {}
     79 
     80   // ExtensionFunction:
     81   virtual bool RunImpl() OVERRIDE;
     82 };
     83 
     84 class OmniboxAPI : public ProfileKeyedAPI,
     85                    public content::NotificationObserver {
     86  public:
     87   explicit OmniboxAPI(Profile* profile);
     88   virtual ~OmniboxAPI();
     89 
     90   // ProfileKeyedAPI implementation.
     91   static ProfileKeyedAPIFactory<OmniboxAPI>* GetFactoryInstance();
     92 
     93   // Convenience method to get the OmniboxAPI for a profile.
     94   static OmniboxAPI* Get(Profile* profile);
     95 
     96   // content::NotificationObserver implementation.
     97   virtual void Observe(int type,
     98                        const content::NotificationSource& source,
     99                        const content::NotificationDetails& details) OVERRIDE;
    100 
    101   // Returns the icon to display in the omnibox for the given extension.
    102   gfx::Image GetOmniboxIcon(const std::string& extension_id);
    103 
    104   // Returns the icon to display in the omnibox popup window for the given
    105   // extension.
    106   gfx::Image GetOmniboxPopupIcon(const std::string& extension_id);
    107 
    108  private:
    109   friend class ProfileKeyedAPIFactory<OmniboxAPI>;
    110 
    111   typedef std::set<const Extension*> PendingExtensions;
    112 
    113   // ProfileKeyedAPI implementation.
    114   static const char* service_name() {
    115     return "OmniboxAPI";
    116   }
    117   static const bool kServiceRedirectedInIncognito = true;
    118 
    119   Profile* profile_;
    120 
    121   TemplateURLService* url_service_;
    122 
    123   // List of extensions waiting for the TemplateURLService to Load to
    124   // have keywords registered.
    125   PendingExtensions pending_extensions_;
    126 
    127   content::NotificationRegistrar registrar_;
    128 
    129   // Keeps track of favicon-sized omnibox icons for extensions.
    130   ExtensionIconManager omnibox_icon_manager_;
    131   ExtensionIconManager omnibox_popup_icon_manager_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(OmniboxAPI);
    134 };
    135 
    136 template <>
    137 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies();
    138 
    139 class OmniboxSetDefaultSuggestionFunction : public SyncExtensionFunction {
    140  public:
    141   DECLARE_EXTENSION_FUNCTION("omnibox.setDefaultSuggestion",
    142                              OMNIBOX_SETDEFAULTSUGGESTION)
    143 
    144  protected:
    145   virtual ~OmniboxSetDefaultSuggestionFunction() {}
    146 
    147   // ExtensionFunction:
    148   virtual bool RunImpl() OVERRIDE;
    149 };
    150 
    151 // If the extension has set a custom default suggestion via
    152 // omnibox.setDefaultSuggestion, apply that to |match|. Otherwise, do nothing.
    153 void ApplyDefaultSuggestionForExtensionKeyword(
    154     Profile* profile,
    155     const TemplateURL* keyword,
    156     const string16& remaining_input,
    157     AutocompleteMatch* match);
    158 
    159 // This function converts style information populated by the JSON schema
    160 // // compiler into an ACMatchClassifications object.
    161 ACMatchClassifications StyleTypesToACMatchClassifications(
    162     const api::omnibox::SuggestResult &suggestion);
    163 
    164 }  // namespace extensions
    165 
    166 #endif  // CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_
    167