| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" | 5 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string16.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/omnibox/omnibox_api_factory.h" |
| 13 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
| 14 #include "chrome/browser/extensions/extension_prefs.h" | 15 #include "chrome/browser/extensions/extension_prefs.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" | 17 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/extensions/tab_helper.h" | 18 #include "chrome/browser/extensions/tab_helper.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url.h" | 20 #include "chrome/browser/search_engines/template_url.h" |
| 21 #include "chrome/browser/search_engines/template_url_service.h" |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | 24 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| 25 #include "chrome/common/extensions/extension.h" |
| 26 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 27 #include "chrome/common/extensions/manifest_handler.h" |
| 28 #include "content/public/browser/notification_details.h" |
| 22 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 30 #include "ui/gfx/image/image.h" |
| 23 | 31 |
| 24 namespace events { | 32 namespace events { |
| 25 const char kOnInputStarted[] = "omnibox.onInputStarted"; | 33 const char kOnInputStarted[] = "omnibox.onInputStarted"; |
| 26 const char kOnInputChanged[] = "omnibox.onInputChanged"; | 34 const char kOnInputChanged[] = "omnibox.onInputChanged"; |
| 27 const char kOnInputEntered[] = "omnibox.onInputEntered"; | 35 const char kOnInputEntered[] = "omnibox.onInputEntered"; |
| 28 const char kOnInputCancelled[] = "omnibox.onInputCancelled"; | 36 const char kOnInputCancelled[] = "omnibox.onInputCancelled"; |
| 29 } // namespace events | 37 } // namespace events |
| 30 | 38 |
| 31 namespace extensions { | 39 namespace extensions { |
| 32 | 40 |
| 33 namespace { | 41 namespace { |
| 34 | 42 |
| 35 const char kSuggestionContent[] = "content"; | 43 const char kSuggestionContent[] = "content"; |
| 36 const char kSuggestionDescription[] = "description"; | 44 const char kSuggestionDescription[] = "description"; |
| 37 const char kSuggestionDescriptionStyles[] = "descriptionStyles"; | 45 const char kSuggestionDescriptionStyles[] = "descriptionStyles"; |
| 38 const char kSuggestionDescriptionStylesRaw[] = "descriptionStylesRaw"; | 46 const char kSuggestionDescriptionStylesRaw[] = "descriptionStylesRaw"; |
| 39 const char kDescriptionStylesType[] = "type"; | 47 const char kDescriptionStylesType[] = "type"; |
| 40 const char kDescriptionStylesOffset[] = "offset"; | 48 const char kDescriptionStylesOffset[] = "offset"; |
| 41 const char kDescriptionStylesLength[] = "length"; | 49 const char kDescriptionStylesLength[] = "length"; |
| 42 | 50 |
| 51 #if defined(OS_LINUX) |
| 52 static const int kOmniboxIconPaddingLeft = 2; |
| 53 static const int kOmniboxIconPaddingRight = 2; |
| 54 #elif defined(OS_MACOSX) |
| 55 static const int kOmniboxIconPaddingLeft = 0; |
| 56 static const int kOmniboxIconPaddingRight = 2; |
| 57 #else |
| 58 static const int kOmniboxIconPaddingLeft = 0; |
| 59 static const int kOmniboxIconPaddingRight = 0; |
| 60 #endif |
| 61 |
| 43 } // namespace | 62 } // namespace |
| 44 | 63 |
| 45 // static | 64 // static |
| 46 void ExtensionOmniboxEventRouter::OnInputStarted( | 65 void ExtensionOmniboxEventRouter::OnInputStarted( |
| 47 Profile* profile, const std::string& extension_id) { | 66 Profile* profile, const std::string& extension_id) { |
| 48 scoped_ptr<Event> event(new Event( | 67 scoped_ptr<Event> event(new Event( |
| 49 events::kOnInputStarted, make_scoped_ptr(new ListValue()))); | 68 events::kOnInputStarted, make_scoped_ptr(new ListValue()))); |
| 50 event->restrict_to_profile = profile; | 69 event->restrict_to_profile = profile; |
| 51 ExtensionSystem::Get(profile)->event_router()-> | 70 ExtensionSystem::Get(profile)->event_router()-> |
| 52 DispatchEventToExtension(extension_id, event.Pass()); | 71 DispatchEventToExtension(extension_id, event.Pass()); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // static | 122 // static |
| 104 void ExtensionOmniboxEventRouter::OnInputCancelled( | 123 void ExtensionOmniboxEventRouter::OnInputCancelled( |
| 105 Profile* profile, const std::string& extension_id) { | 124 Profile* profile, const std::string& extension_id) { |
| 106 scoped_ptr<Event> event(new Event( | 125 scoped_ptr<Event> event(new Event( |
| 107 events::kOnInputCancelled, make_scoped_ptr(new ListValue()))); | 126 events::kOnInputCancelled, make_scoped_ptr(new ListValue()))); |
| 108 event->restrict_to_profile = profile; | 127 event->restrict_to_profile = profile; |
| 109 ExtensionSystem::Get(profile)->event_router()-> | 128 ExtensionSystem::Get(profile)->event_router()-> |
| 110 DispatchEventToExtension(extension_id, event.Pass()); | 129 DispatchEventToExtension(extension_id, event.Pass()); |
| 111 } | 130 } |
| 112 | 131 |
| 132 OmniboxAPI::OmniboxAPI(Profile* profile) |
| 133 : url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { |
| 134 ManifestHandler::Register(extension_manifest_keys::kOmnibox, |
| 135 new OmniboxHandler); |
| 136 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 137 content::Source<Profile>(profile)); |
| 138 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 139 content::Source<Profile>(profile)); |
| 140 if (url_service_) { |
| 141 registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
| 142 content::Source<TemplateURLService>(url_service_)); |
| 143 } |
| 144 |
| 145 // Use monochrome icons for Omnibox icons. |
| 146 omnibox_popup_icon_manager_.set_monochrome(true); |
| 147 omnibox_icon_manager_.set_monochrome(true); |
| 148 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
| 149 0, kOmniboxIconPaddingRight)); |
| 150 } |
| 151 |
| 152 OmniboxAPI::~OmniboxAPI() { |
| 153 } |
| 154 |
| 155 void OmniboxAPI::Shutdown() { |
| 156 } |
| 157 |
| 158 // static |
| 159 OmniboxAPI* OmniboxAPI::Get(Profile* profile) { |
| 160 return OmniboxAPIFactory::GetForProfile(profile); |
| 161 } |
| 162 |
| 163 void OmniboxAPI::Observe(int type, |
| 164 const content::NotificationSource& source, |
| 165 const content::NotificationDetails& details) { |
| 166 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { |
| 167 const Extension* extension = |
| 168 content::Details<const Extension>(details).ptr(); |
| 169 const std::string& keyword = OmniboxInfo::GetKeyword(extension); |
| 170 if (!keyword.empty()) { |
| 171 // Load the omnibox icon so it will be ready to display in the URL bar. |
| 172 omnibox_popup_icon_manager_.LoadIcon(extension); |
| 173 omnibox_icon_manager_.LoadIcon(extension); |
| 174 |
| 175 if (url_service_) { |
| 176 url_service_->Load(); |
| 177 if (url_service_->loaded()) { |
| 178 url_service_->RegisterExtensionKeyword(extension->id(), |
| 179 extension->name(), |
| 180 keyword); |
| 181 } else { |
| 182 pending_extensions_.insert(extension); |
| 183 } |
| 184 } |
| 185 } |
| 186 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| 187 const Extension* extension = |
| 188 content::Details<const UnloadedExtensionInfo>(details)->extension; |
| 189 if (!OmniboxInfo::GetKeyword(extension).empty()) { |
| 190 if (url_service_) { |
| 191 if (url_service_->loaded()) |
| 192 url_service_->UnregisterExtensionKeyword(extension->id()); |
| 193 else |
| 194 pending_extensions_.erase(extension); |
| 195 } |
| 196 } |
| 197 } else { |
| 198 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); |
| 199 // Load pending extensions. |
| 200 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); |
| 201 i != pending_extensions_.end(); ++i) { |
| 202 url_service_->RegisterExtensionKeyword((*i)->id(), |
| 203 (*i)->name(), |
| 204 OmniboxInfo::GetKeyword(*i)); |
| 205 } |
| 206 pending_extensions_.clear(); |
| 207 } |
| 208 } |
| 209 |
| 210 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
| 211 return gfx::Image(omnibox_icon_manager_.GetIcon(extension_id)); |
| 212 } |
| 213 |
| 214 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { |
| 215 return gfx::Image(omnibox_popup_icon_manager_.GetIcon(extension_id)); |
| 216 } |
| 217 |
| 113 bool OmniboxSendSuggestionsFunction::RunImpl() { | 218 bool OmniboxSendSuggestionsFunction::RunImpl() { |
| 114 ExtensionOmniboxSuggestions suggestions; | 219 ExtensionOmniboxSuggestions suggestions; |
| 115 ListValue* suggestions_value; | 220 ListValue* suggestions_value; |
| 116 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id)); | 221 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id)); |
| 117 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value)); | 222 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value)); |
| 118 | 223 |
| 119 suggestions.suggestions.resize(suggestions_value->GetSize()); | 224 suggestions.suggestions.resize(suggestions_value->GetSize()); |
| 120 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) { | 225 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) { |
| 121 ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i]; | 226 ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i]; |
| 122 DictionaryValue* suggestion_value; | 227 DictionaryValue* suggestion_value; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 for (size_t i = 0; i < description_styles.size(); ++i) { | 409 for (size_t i = 0; i < description_styles.size(); ++i) { |
| 305 if (description_styles[i].offset > placeholder) | 410 if (description_styles[i].offset > placeholder) |
| 306 description_styles[i].offset += replacement.length() - 2; | 411 description_styles[i].offset += replacement.length() - 2; |
| 307 } | 412 } |
| 308 } | 413 } |
| 309 | 414 |
| 310 match->contents.assign(description); | 415 match->contents.assign(description); |
| 311 } | 416 } |
| 312 | 417 |
| 313 } // namespace extensions | 418 } // namespace extensions |
| OLD | NEW |