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