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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_
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"
26 class TemplateURLService;
40 namespace extensions {
42 // Event router class for events related to the omnibox API.
43 class ExtensionOmniboxEventRouter {
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);
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(
55 const std::string& extension_id,
56 const std::string& input, int suggest_id);
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);
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);
71 DISALLOW_COPY_AND_ASSIGN(ExtensionOmniboxEventRouter);
74 class OmniboxSendSuggestionsFunction : public ChromeSyncExtensionFunction {
76 DECLARE_EXTENSION_FUNCTION("omnibox.sendSuggestions", OMNIBOX_SENDSUGGESTIONS)
79 virtual ~OmniboxSendSuggestionsFunction() {}
82 virtual bool RunImpl() OVERRIDE;
85 class OmniboxAPI : public ProfileKeyedAPI,
86 public content::NotificationObserver {
88 explicit OmniboxAPI(Profile* profile);
89 virtual ~OmniboxAPI();
91 // ProfileKeyedAPI implementation.
92 static ProfileKeyedAPIFactory<OmniboxAPI>* GetFactoryInstance();
94 // Convenience method to get the OmniboxAPI for a profile.
95 static OmniboxAPI* Get(Profile* profile);
97 // content::NotificationObserver implementation.
98 virtual void Observe(int type,
99 const content::NotificationSource& source,
100 const content::NotificationDetails& details) OVERRIDE;
102 // BrowserContextKeyedService implementation.
103 virtual void Shutdown() OVERRIDE;
105 // Returns the icon to display in the omnibox for the given extension.
106 gfx::Image GetOmniboxIcon(const std::string& extension_id);
108 // Returns the icon to display in the omnibox popup window for the given
110 gfx::Image GetOmniboxPopupIcon(const std::string& extension_id);
113 friend class ProfileKeyedAPIFactory<OmniboxAPI>;
115 typedef std::set<const Extension*> PendingExtensions;
117 void OnTemplateURLsLoaded();
119 // ProfileKeyedAPI implementation.
120 static const char* service_name() {
123 static const bool kServiceRedirectedInIncognito = true;
127 TemplateURLService* url_service_;
129 // List of extensions waiting for the TemplateURLService to Load to
130 // have keywords registered.
131 PendingExtensions pending_extensions_;
133 content::NotificationRegistrar registrar_;
135 // Keeps track of favicon-sized omnibox icons for extensions.
136 ExtensionIconManager omnibox_icon_manager_;
137 ExtensionIconManager omnibox_popup_icon_manager_;
139 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
141 DISALLOW_COPY_AND_ASSIGN(OmniboxAPI);
145 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies();
147 class OmniboxSetDefaultSuggestionFunction : public ChromeSyncExtensionFunction {
149 DECLARE_EXTENSION_FUNCTION("omnibox.setDefaultSuggestion",
150 OMNIBOX_SETDEFAULTSUGGESTION)
153 virtual ~OmniboxSetDefaultSuggestionFunction() {}
155 // ExtensionFunction:
156 virtual bool RunImpl() OVERRIDE;
159 // If the extension has set a custom default suggestion via
160 // omnibox.setDefaultSuggestion, apply that to |match|. Otherwise, do nothing.
161 void ApplyDefaultSuggestionForExtensionKeyword(
163 const TemplateURL* keyword,
164 const string16& remaining_input,
165 AutocompleteMatch* match);
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);
172 } // namespace extensions
174 #endif // CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_