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 1b4b2e0561fff7052af4d4c7e8e4160ea228e8bb..7a05f40f03edf536f2c6dff553f85e990e825aaa 100644 |
| --- a/chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| +++ b/chrome/browser/extensions/api/omnibox/omnibox_api.cc |
| @@ -7,9 +7,10 @@ |
| #include "base/json/json_writer.h" |
| #include "base/lazy_instance.h" |
| #include "base/metrics/histogram.h" |
| -#include "base/string_util.h" |
| +#include "base/string16.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" |
| @@ -17,9 +18,16 @@ |
| #include "chrome/browser/extensions/tab_helper.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/search_engines/template_url.h" |
| +#include "chrome/browser/search_engines/template_url_service.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| #include "chrome/common/chrome_notification_types.h" |
| -#include "chrome/common/extensions/extension_constants.h" |
| +#include "chrome/common/extensions/api/omnibox/omnibox_handler.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"; |
| @@ -40,6 +48,17 @@ 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 |
| + |
| } // namespace |
| // static |
| @@ -110,6 +129,98 @@ void ExtensionOmniboxEventRouter::OnInputCancelled( |
| DispatchEventToExtension(extension_id, event.Pass()); |
| } |
| +OmniboxAPI::OmniboxAPI(Profile* profile) |
| + : url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { |
| + ManifestHandler::Register(extension_manifest_keys::kOmnibox, |
| + new OmniboxHandler); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| + content::Source<Profile>(profile)); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| + content::Source<Profile>(profile)); |
| + if (url_service_) { |
| + registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
| + content::Source<TemplateURLService>(url_service_)); |
| + } |
| + |
| + // 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) { |
| + if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { |
| + const Extension* extension = |
| + content::Details<const Extension>(details).ptr(); |
| + const std::string& keyword = OmniboxInfo::GetKeyword(extension); |
| + if (!keyword.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); |
| + |
| + if (url_service_) { |
| + url_service_->Load(); |
| + if (url_service_->loaded()) { |
| + url_service_->RegisterExtensionKeyword(extension->id(), |
| + extension->name(), |
| + keyword); |
| + } else { |
| + pending_extensions_.push_back(extension); |
| + } |
| + } |
| + } |
| + } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| + const Extension* extension = |
| + content::Details<const UnloadedExtensionInfo>(details)->extension; |
| + if (!OmniboxInfo::GetKeyword(extension).empty()) { |
| + if (url_service_) { |
| + if (url_service_->loaded()) { |
| + url_service_->UnregisterExtensionKeyword(extension->id()); |
| + } else { |
| + PendingExtensions::iterator i = std::find( |
| + pending_extensions_.begin(), |
| + pending_extensions_.end(), |
| + extension); |
| + if (i != pending_extensions_.end()) |
| + pending_extensions_.erase(i); |
|
Matt Perry
2012/12/08 00:46:26
pendings_extensions_.erase(extension) should work
Yoyo Zhou
2012/12/08 00:58:18
It doesn't work for std::list. But it should have
|
| + } |
| + } |
| + } |
| + } else { |
| + DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); |
| + // Load pending extensions. |
| + for (PendingExtensions::const_iterator i(pending_extensions_.begin()); |
| + i != pending_extensions_.end(); ++i) { |
| + url_service_->RegisterExtensionKeyword((*i)->id(), |
| + (*i)->name(), |
| + OmniboxInfo::GetKeyword(*i)); |
| + } |
| + pending_extensions_.clear(); |
| + } |
| +} |
| + |
| +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)); |
| +} |
| + |
| bool OmniboxSendSuggestionsFunction::RunImpl() { |
| ExtensionOmniboxSuggestions suggestions; |
| ListValue* suggestions_value; |