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_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/extensions/chrome_extension_function.h"
15 #include "chrome/browser/extensions/extension_icon_manager.h"
16 #include "chrome/common/extensions/api/omnibox.h"
17 #include "components/omnibox/browser/autocomplete_match.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "ui/base/window_open_disposition.h"
25 class TemplateURLService
;
40 namespace extensions
{
41 class ExtensionRegistry
;
43 // Event router class for events related to the omnibox API.
44 class ExtensionOmniboxEventRouter
{
46 // The user has just typed the omnibox keyword. This is sent exactly once in
47 // a given input session, before any OnInputChanged events.
48 static void OnInputStarted(
49 Profile
* profile
, const std::string
& extension_id
);
51 // The user has changed what is typed into the omnibox while in an extension
52 // keyword session. Returns true if someone is listening to this event, and
53 // thus we have some degree of confidence we'll get a response.
54 static bool OnInputChanged(
56 const std::string
& extension_id
,
57 const std::string
& input
, int suggest_id
);
59 // The user has accepted the omnibox input.
60 static void OnInputEntered(
61 content::WebContents
* web_contents
,
62 const std::string
& extension_id
,
63 const std::string
& input
,
64 WindowOpenDisposition disposition
);
66 // The user has cleared the keyword, or closed the omnibox popup. This is
67 // sent at most once in a give input session, after any OnInputChanged events.
68 static void OnInputCancelled(
69 Profile
* profile
, const std::string
& extension_id
);
72 DISALLOW_COPY_AND_ASSIGN(ExtensionOmniboxEventRouter
);
75 class OmniboxSendSuggestionsFunction
: public ChromeSyncExtensionFunction
{
77 DECLARE_EXTENSION_FUNCTION("omnibox.sendSuggestions", OMNIBOX_SENDSUGGESTIONS
)
80 ~OmniboxSendSuggestionsFunction() override
{}
83 bool RunSync() override
;
86 class OmniboxAPI
: public BrowserContextKeyedAPI
,
87 public ExtensionRegistryObserver
{
89 explicit OmniboxAPI(content::BrowserContext
* context
);
90 ~OmniboxAPI() override
;
92 // BrowserContextKeyedAPI implementation.
93 static BrowserContextKeyedAPIFactory
<OmniboxAPI
>* GetFactoryInstance();
95 // Convenience method to get the OmniboxAPI for a profile.
96 static OmniboxAPI
* Get(content::BrowserContext
* context
);
98 // KeyedService implementation.
99 void Shutdown() override
;
101 // Returns the icon to display in the omnibox for the given extension.
102 gfx::Image
GetOmniboxIcon(const std::string
& extension_id
);
104 // Returns the icon to display in the omnibox popup window for the given
106 gfx::Image
GetOmniboxPopupIcon(const std::string
& extension_id
);
109 friend class BrowserContextKeyedAPIFactory
<OmniboxAPI
>;
111 typedef std::set
<const Extension
*> PendingExtensions
;
113 void OnTemplateURLsLoaded();
115 // ExtensionRegistryObserver implementation.
116 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
117 const Extension
* extension
) override
;
118 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
119 const Extension
* extension
,
120 UnloadedExtensionInfo::Reason reason
) override
;
122 // BrowserContextKeyedAPI implementation.
123 static const char* service_name() {
126 static const bool kServiceRedirectedInIncognito
= true;
130 TemplateURLService
* url_service_
;
132 // List of extensions waiting for the TemplateURLService to Load to
133 // have keywords registered.
134 PendingExtensions pending_extensions_
;
136 // Listen to extension load, unloaded notifications.
137 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
138 extension_registry_observer_
;
140 // Keeps track of favicon-sized omnibox icons for extensions.
141 ExtensionIconManager omnibox_icon_manager_
;
142 ExtensionIconManager omnibox_popup_icon_manager_
;
144 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
146 DISALLOW_COPY_AND_ASSIGN(OmniboxAPI
);
150 void BrowserContextKeyedAPIFactory
<OmniboxAPI
>::DeclareFactoryDependencies();
152 class OmniboxSetDefaultSuggestionFunction
: public ChromeSyncExtensionFunction
{
154 DECLARE_EXTENSION_FUNCTION("omnibox.setDefaultSuggestion",
155 OMNIBOX_SETDEFAULTSUGGESTION
)
158 ~OmniboxSetDefaultSuggestionFunction() override
{}
160 // ExtensionFunction:
161 bool RunSync() override
;
164 // If the extension has set a custom default suggestion via
165 // omnibox.setDefaultSuggestion, apply that to |match|. Otherwise, do nothing.
166 void ApplyDefaultSuggestionForExtensionKeyword(
168 const TemplateURL
* keyword
,
169 const base::string16
& remaining_input
,
170 AutocompleteMatch
* match
);
172 // This function converts style information populated by the JSON schema
173 // // compiler into an ACMatchClassifications object.
174 ACMatchClassifications
StyleTypesToACMatchClassifications(
175 const api::omnibox::SuggestResult
&suggestion
);
177 } // namespace extensions
179 #endif // CHROME_BROWSER_EXTENSIONS_API_OMNIBOX_OMNIBOX_API_H_