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" | |
8 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
9 #include "base/metrics/histogram.h" | |
10 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
11 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | |
13 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/extensions/tab_helper.h" | 11 #include "chrome/browser/extensions/tab_helper.h" |
15 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
17 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
19 #include "chrome/common/extensions/api/omnibox.h" | 16 #include "chrome/common/extensions/api/omnibox.h" |
20 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 17 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
21 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
22 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
23 #include "extensions/browser/event_router.h" | 20 #include "extensions/browser/event_router.h" |
24 #include "extensions/browser/extension_prefs.h" | 21 #include "extensions/browser/extension_prefs.h" |
25 #include "extensions/browser/extension_prefs_factory.h" | 22 #include "extensions/browser/extension_prefs_factory.h" |
26 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
27 #include "extensions/browser/extension_system_provider.h" | |
28 #include "extensions/browser/extensions_browser_client.h" | |
29 #include "extensions/common/extension.h" | |
30 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
31 | 25 |
32 namespace extensions { | 26 namespace extensions { |
33 | 27 |
34 namespace omnibox = api::omnibox; | 28 namespace omnibox = api::omnibox; |
35 namespace SendSuggestions = omnibox::SendSuggestions; | 29 namespace SendSuggestions = omnibox::SendSuggestions; |
36 namespace SetDefaultSuggestion = omnibox::SetDefaultSuggestion; | 30 namespace SetDefaultSuggestion = omnibox::SetDefaultSuggestion; |
37 | 31 |
38 namespace { | 32 namespace { |
39 | 33 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 scoped_ptr<Event> event(new Event( | 163 scoped_ptr<Event> event(new Event( |
170 omnibox::OnInputCancelled::kEventName, | 164 omnibox::OnInputCancelled::kEventName, |
171 make_scoped_ptr(new base::ListValue()))); | 165 make_scoped_ptr(new base::ListValue()))); |
172 event->restrict_to_browser_context = profile; | 166 event->restrict_to_browser_context = profile; |
173 EventRouter::Get(profile) | 167 EventRouter::Get(profile) |
174 ->DispatchEventToExtension(extension_id, event.Pass()); | 168 ->DispatchEventToExtension(extension_id, event.Pass()); |
175 } | 169 } |
176 | 170 |
177 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) | 171 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) |
178 : profile_(Profile::FromBrowserContext(context)), | 172 : profile_(Profile::FromBrowserContext(context)), |
179 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)) { | 173 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
180 registrar_.Add(this, | 174 extension_registry_observer_(this) { |
181 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 175 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_) { | 176 if (url_service_) { |
187 template_url_sub_ = url_service_->RegisterOnLoadedCallback( | 177 template_url_sub_ = url_service_->RegisterOnLoadedCallback( |
188 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, | 178 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, |
189 base::Unretained(this))); | 179 base::Unretained(this))); |
190 } | 180 } |
191 | 181 |
192 // Use monochrome icons for Omnibox icons. | 182 // Use monochrome icons for Omnibox icons. |
193 omnibox_popup_icon_manager_.set_monochrome(true); | 183 omnibox_popup_icon_manager_.set_monochrome(true); |
194 omnibox_icon_manager_.set_monochrome(true); | 184 omnibox_icon_manager_.set_monochrome(true); |
195 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, | 185 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
(...skipping 13 matching lines...) Expand all Loading... |
209 // static | 199 // static |
210 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { | 200 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { |
211 return g_factory.Pointer(); | 201 return g_factory.Pointer(); |
212 } | 202 } |
213 | 203 |
214 // static | 204 // static |
215 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { | 205 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { |
216 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); | 206 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); |
217 } | 207 } |
218 | 208 |
219 void OmniboxAPI::Observe(int type, | 209 void OmniboxAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
220 const content::NotificationSource& source, | 210 const Extension* extension) { |
221 const content::NotificationDetails& details) { | 211 const std::string& keyword = OmniboxInfo::GetKeyword(extension); |
222 if (type == chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED) { | 212 if (!keyword.empty()) { |
223 const Extension* extension = | 213 // Load the omnibox icon so it will be ready to display in the URL bar. |
224 content::Details<const Extension>(details).ptr(); | 214 omnibox_popup_icon_manager_.LoadIcon(profile_, extension); |
225 const std::string& keyword = OmniboxInfo::GetKeyword(extension); | 215 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 | 216 |
231 if (url_service_) { | 217 if (url_service_) { |
232 url_service_->Load(); | 218 url_service_->Load(); |
233 if (url_service_->loaded()) { | 219 if (url_service_->loaded()) { |
234 url_service_->RegisterOmniboxKeyword(extension->id(), | 220 url_service_->RegisterOmniboxKeyword( |
235 extension->name(), | 221 extension->id(), extension->name(), keyword); |
236 keyword); | 222 } else { |
237 } else { | 223 pending_extensions_.insert(extension); |
238 pending_extensions_.insert(extension); | |
239 } | |
240 } | 224 } |
241 } | 225 } |
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 } | 226 } |
256 } | 227 } |
257 | 228 |
| 229 void OmniboxAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 230 const Extension* extension, |
| 231 UnloadedExtensionInfo::Reason reason) { |
| 232 if (!OmniboxInfo::GetKeyword(extension).empty() && url_service_) { |
| 233 if (url_service_->loaded()) |
| 234 url_service_->UnregisterOmniboxKeyword(extension->id()); |
| 235 else |
| 236 pending_extensions_.erase(extension); |
| 237 } |
| 238 } |
| 239 |
258 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { | 240 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
259 return gfx::Image::CreateFrom1xBitmap( | 241 return gfx::Image::CreateFrom1xBitmap( |
260 omnibox_icon_manager_.GetIcon(extension_id)); | 242 omnibox_icon_manager_.GetIcon(extension_id)); |
261 } | 243 } |
262 | 244 |
263 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { | 245 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { |
264 return gfx::Image::CreateFrom1xBitmap( | 246 return gfx::Image::CreateFrom1xBitmap( |
265 omnibox_popup_icon_manager_.GetIcon(extension_id)); | 247 omnibox_popup_icon_manager_.GetIcon(extension_id)); |
266 } | 248 } |
267 | 249 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 for (size_t i = 0; i < description_styles.size(); ++i) { | 379 for (size_t i = 0; i < description_styles.size(); ++i) { |
398 if (description_styles[i].offset > placeholder) | 380 if (description_styles[i].offset > placeholder) |
399 description_styles[i].offset += replacement.length() - 2; | 381 description_styles[i].offset += replacement.length() - 2; |
400 } | 382 } |
401 } | 383 } |
402 | 384 |
403 match->contents.assign(description); | 385 match->contents.assign(description); |
404 } | 386 } |
405 | 387 |
406 } // namespace extensions | 388 } // namespace extensions |
OLD | NEW |