Chromium Code Reviews| Index: chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| diff --git a/chrome/browser/extensions/api/omnibox/omnibox_api.cc b/chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| index 153fc279841b7524da2687975c5f757fe4b1f3c0..648526dba8d6975d287f95b92bb0818f3bd91c50 100644 |
| --- a/chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| +++ b/chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| @@ -7,9 +7,12 @@ |
| #include "base/json/json_writer.h" |
| #include "base/lazy_instance.h" |
| #include "base/metrics/histogram.h" |
| +#include "base/string16.h" |
| #include "base/string_util.h" |
| +#include "base/supports_user_data.h" |
| #include "base/utf_string_conversions.h" |
| #include "base/values.h" |
| +#include "chrome/browser/extensions/api/omnibox/omnibox_api_factory.h" |
| #include "chrome/browser/extensions/event_router.h" |
| #include "chrome/browser/extensions/extension_prefs.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| @@ -18,8 +21,12 @@ |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/search_engines/template_url.h" |
| #include "chrome/common/chrome_notification_types.h" |
| -#include "chrome/common/extensions/extension_constants.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/extension_manifest_constants.h" |
| +#include "chrome/common/extensions/manifest_handler.h" |
| +#include "content/public/browser/notification_details.h" |
| #include "content/public/browser/notification_service.h" |
| +#include "ui/gfx/image/image.h" |
| namespace events { |
| const char kOnInputStarted[] = "omnibox.onInputStarted"; |
| @@ -32,6 +39,13 @@ namespace extensions { |
| namespace { |
| +// Manifest keys. |
| + |
| +// TODO(yoz): remove the constants from extension_manifest_constants, |
| +// which requires changing extension_l10n_util.cc. |
| +const char kOmnibox[] = "omnibox"; |
| +const char kKeyword[] = "keyword"; |
| + |
| const char kSuggestionContent[] = "content"; |
| const char kSuggestionDescription[] = "description"; |
| const char kSuggestionDescriptionStyles[] = "descriptionStyles"; |
| @@ -40,6 +54,54 @@ const char kDescriptionStylesType[] = "type"; |
| const char kDescriptionStylesOffset[] = "offset"; |
| const char kDescriptionStylesLength[] = "length"; |
| +#if defined(OS_LINUX) |
| +static const int kOmniboxIconPaddingLeft = 2; |
| +static const int kOmniboxIconPaddingRight = 2; |
| +#elif defined(OS_MACOSX) |
| +static const int kOmniboxIconPaddingLeft = 0; |
| +static const int kOmniboxIconPaddingRight = 2; |
| +#else |
| +static const int kOmniboxIconPaddingLeft = 0; |
| +static const int kOmniboxIconPaddingRight = 0; |
| +#endif |
| + |
| +struct OmniboxInfo : public base::SupportsUserData::Data { |
| + // The Omnibox keyword for an extension. |
| + std::string keyword; |
| +}; |
| + |
| +// Parses the "omnibox" manifest key. |
| +class OmniboxHandler : public ManifestHandler { |
| + public: |
| + OmniboxHandler(); |
| + virtual ~OmniboxHandler(); |
| + |
| + virtual bool Parse(const base::Value* value, |
| + Extension* extension, |
| + string16* error) OVERRIDE; |
| +}; |
| + |
| +OmniboxHandler::OmniboxHandler() { |
| +} |
| + |
| +OmniboxHandler::~OmniboxHandler() { |
| +} |
| + |
| +bool OmniboxHandler::Parse(const base::Value* value, |
| + Extension* extension, |
| + string16* error) { |
| + OmniboxInfo* info = new OmniboxInfo; |
| + const DictionaryValue* dict = NULL; |
| + if (!value->GetAsDictionary(&dict) || |
| + !dict->GetString(kKeyword, &info->keyword) || |
| + info->keyword.empty()) { |
| + *error = ASCIIToUTF16(extension_manifest_errors::kInvalidOmniboxKeyword); |
| + return false; |
|
Matt Perry
2012/12/07 01:22:59
leaking |info|. use scoped_ptr
Yoyo Zhou
2012/12/07 02:13:32
Done.
|
| + } |
| + extension->SetUserData(kOmnibox, info); |
| + return true; |
| +} |
| + |
| } // namespace |
| // static |
| @@ -106,6 +168,66 @@ void ExtensionOmniboxEventRouter::OnInputCancelled( |
| args.Pass(), profile, GURL()); |
| } |
| +OmniboxAPI::OmniboxAPI(Profile* profile) { |
| + RegisterManifestHandler(kOmnibox, |
| + new OmniboxHandler); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| + content::Source<Profile>(profile)); |
| + |
| + // Use monochrome icons for Omnibox icons. |
| + omnibox_popup_icon_manager_.set_monochrome(true); |
| + omnibox_icon_manager_.set_monochrome(true); |
| + omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
| + 0, kOmniboxIconPaddingRight)); |
| +} |
| + |
| +OmniboxAPI::~OmniboxAPI() { |
| +} |
| + |
| +void OmniboxAPI::Shutdown() { |
| +} |
| + |
| +// static |
| +OmniboxAPI* OmniboxAPI::Get(Profile* profile) { |
| + return OmniboxAPIFactory::GetForProfile(profile); |
| +} |
| + |
| +void OmniboxAPI::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED); |
| + const Extension* extension = content::Details<const Extension>(details).ptr(); |
| + if (!GetKeyword(extension).empty()) { |
| + // Load the omnibox icon so it will be ready to display in the URL bar. |
| + omnibox_popup_icon_manager_.LoadIcon(extension); |
| + omnibox_icon_manager_.LoadIcon(extension); |
| + } |
| +} |
| + |
| +gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
| + return gfx::Image(omnibox_icon_manager_.GetIcon(extension_id)); |
| +} |
| + |
| +gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { |
| + return gfx::Image(omnibox_popup_icon_manager_.GetIcon(extension_id)); |
| +} |
| + |
| +// static |
| +const std::string& OmniboxAPI::GetKeyword(const Extension* extension) { |
| + OmniboxInfo* info = static_cast<OmniboxInfo*>( |
| + extension->GetUserData(kOmnibox)); |
| + return info ? info->keyword : EmptyString(); |
| +} |
| + |
| +// static |
| +bool OmniboxAPI::IsVerboseInstallMessage(const Extension* extension) { |
| + return !GetKeyword(extension).empty() || |
| + extension->browser_action_info() || |
| + (extension->page_action_info() && |
| + (extension->page_action_command() || |
| + !extension->page_action_info()->default_icon.empty())); |
| +} |
| + |
| bool OmniboxSendSuggestionsFunction::RunImpl() { |
| ExtensionOmniboxSuggestions suggestions; |
| ListValue* suggestions_value; |