| 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/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 scoped_ptr<Event> event(new Event( | 169 scoped_ptr<Event> event(new Event( |
| 170 omnibox::OnInputCancelled::kEventName, | 170 omnibox::OnInputCancelled::kEventName, |
| 171 make_scoped_ptr(new base::ListValue()))); | 171 make_scoped_ptr(new base::ListValue()))); |
| 172 event->restrict_to_browser_context = profile; | 172 event->restrict_to_browser_context = profile; |
| 173 EventRouter::Get(profile) | 173 EventRouter::Get(profile) |
| 174 ->DispatchEventToExtension(extension_id, event.Pass()); | 174 ->DispatchEventToExtension(extension_id, event.Pass()); |
| 175 } | 175 } |
| 176 | 176 |
| 177 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) | 177 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) |
| 178 : profile_(Profile::FromBrowserContext(context)), | 178 : profile_(Profile::FromBrowserContext(context)), |
| 179 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)) { | 179 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
| 180 registrar_.Add(this, | 180 scoped_extension_registry_observer_(this) { |
| 181 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 181 scoped_extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 182 content::Source<Profile>(profile_)); | |
| 183 registrar_.Add(this, | |
| 184 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 185 content::Source<Profile>(profile_)); | |
| 186 if (url_service_) { | 182 if (url_service_) { |
| 187 template_url_sub_ = url_service_->RegisterOnLoadedCallback( | 183 template_url_sub_ = url_service_->RegisterOnLoadedCallback( |
| 188 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, | 184 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, |
| 189 base::Unretained(this))); | 185 base::Unretained(this))); |
| 190 } | 186 } |
| 191 | 187 |
| 192 // Use monochrome icons for Omnibox icons. | 188 // Use monochrome icons for Omnibox icons. |
| 193 omnibox_popup_icon_manager_.set_monochrome(true); | 189 omnibox_popup_icon_manager_.set_monochrome(true); |
| 194 omnibox_icon_manager_.set_monochrome(true); | 190 omnibox_icon_manager_.set_monochrome(true); |
| 195 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, | 191 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 209 // static | 205 // static |
| 210 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { | 206 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { |
| 211 return g_factory.Pointer(); | 207 return g_factory.Pointer(); |
| 212 } | 208 } |
| 213 | 209 |
| 214 // static | 210 // static |
| 215 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { | 211 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { |
| 216 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); | 212 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); |
| 217 } | 213 } |
| 218 | 214 |
| 219 void OmniboxAPI::Observe(int type, | 215 void OmniboxAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 220 const content::NotificationSource& source, | 216 const Extension* extension) { |
| 221 const content::NotificationDetails& details) { | 217 const std::string& keyword = OmniboxInfo::GetKeyword(extension); |
| 222 if (type == chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED) { | 218 if (!keyword.empty()) { |
| 223 const Extension* extension = | 219 // Load the omnibox icon so it will be ready to display in the URL bar. |
| 224 content::Details<const Extension>(details).ptr(); | 220 omnibox_popup_icon_manager_.LoadIcon(profile_, extension); |
| 225 const std::string& keyword = OmniboxInfo::GetKeyword(extension); | 221 omnibox_icon_manager_.LoadIcon(profile_, extension); |
| 226 if (!keyword.empty()) { | |
| 227 // Load the omnibox icon so it will be ready to display in the URL bar. | |
| 228 omnibox_popup_icon_manager_.LoadIcon(profile_, extension); | |
| 229 omnibox_icon_manager_.LoadIcon(profile_, extension); | |
| 230 | 222 |
| 231 if (url_service_) { | 223 if (url_service_) { |
| 232 url_service_->Load(); | 224 url_service_->Load(); |
| 233 if (url_service_->loaded()) { | 225 if (url_service_->loaded()) { |
| 234 url_service_->RegisterOmniboxKeyword(extension->id(), | 226 url_service_->RegisterOmniboxKeyword( |
| 235 extension->name(), | 227 extension->id(), extension->name(), keyword); |
| 236 keyword); | 228 } else { |
| 237 } else { | 229 pending_extensions_.insert(extension); |
| 238 pending_extensions_.insert(extension); | |
| 239 } | |
| 240 } | 230 } |
| 241 } | 231 } |
| 242 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | |
| 243 const Extension* extension = | |
| 244 content::Details<UnloadedExtensionInfo>(details)->extension; | |
| 245 if (!OmniboxInfo::GetKeyword(extension).empty()) { | |
| 246 if (url_service_) { | |
| 247 if (url_service_->loaded()) | |
| 248 url_service_->UnregisterOmniboxKeyword(extension->id()); | |
| 249 else | |
| 250 pending_extensions_.erase(extension); | |
| 251 } | |
| 252 } | |
| 253 } else { | |
| 254 NOTREACHED(); | |
| 255 } | 232 } |
| 256 } | 233 } |
| 257 | 234 |
| 235 void OmniboxAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 236 const Extension* extension, |
| 237 UnloadedExtensionInfo::Reason reason) { |
| 238 if (!OmniboxInfo::GetKeyword(extension).empty() && url_service_) { |
| 239 if (url_service_->loaded()) |
| 240 url_service_->UnregisterOmniboxKeyword(extension->id()); |
| 241 else |
| 242 pending_extensions_.erase(extension); |
| 243 } |
| 244 } |
| 245 |
| 258 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { | 246 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
| 259 return gfx::Image::CreateFrom1xBitmap( | 247 return gfx::Image::CreateFrom1xBitmap( |
| 260 omnibox_icon_manager_.GetIcon(extension_id)); | 248 omnibox_icon_manager_.GetIcon(extension_id)); |
| 261 } | 249 } |
| 262 | 250 |
| 263 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { | 251 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { |
| 264 return gfx::Image::CreateFrom1xBitmap( | 252 return gfx::Image::CreateFrom1xBitmap( |
| 265 omnibox_popup_icon_manager_.GetIcon(extension_id)); | 253 omnibox_popup_icon_manager_.GetIcon(extension_id)); |
| 266 } | 254 } |
| 267 | 255 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 for (size_t i = 0; i < description_styles.size(); ++i) { | 385 for (size_t i = 0; i < description_styles.size(); ++i) { |
| 398 if (description_styles[i].offset > placeholder) | 386 if (description_styles[i].offset > placeholder) |
| 399 description_styles[i].offset += replacement.length() - 2; | 387 description_styles[i].offset += replacement.length() - 2; |
| 400 } | 388 } |
| 401 } | 389 } |
| 402 | 390 |
| 403 match->contents.assign(description); | 391 match->contents.assign(description); |
| 404 } | 392 } |
| 405 | 393 |
| 406 } // namespace extensions | 394 } // namespace extensions |
| OLD | NEW |