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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 namespace { | 38 namespace { |
39 | 39 |
40 const char kSuggestionContent[] = "content"; | 40 const char kSuggestionContent[] = "content"; |
41 const char kCurrentTabDisposition[] = "currentTab"; | 41 const char kCurrentTabDisposition[] = "currentTab"; |
42 const char kForegroundTabDisposition[] = "newForegroundTab"; | 42 const char kForegroundTabDisposition[] = "newForegroundTab"; |
43 const char kBackgroundTabDisposition[] = "newBackgroundTab"; | 43 const char kBackgroundTabDisposition[] = "newBackgroundTab"; |
44 | 44 |
45 // Pref key for omnibox.setDefaultSuggestion. | 45 // Pref key for omnibox.setDefaultSuggestion. |
46 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; | 46 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; |
47 | 47 |
48 #if defined(OS_LINUX) | |
49 static const int kOmniboxIconPaddingLeft = 2; | |
50 static const int kOmniboxIconPaddingRight = 2; | |
51 #elif defined(OS_MACOSX) | |
52 static const int kOmniboxIconPaddingLeft = 0; | |
53 static const int kOmniboxIconPaddingRight = 2; | |
54 #else | |
55 static const int kOmniboxIconPaddingLeft = 0; | |
56 static const int kOmniboxIconPaddingRight = 0; | |
57 #endif | |
58 | |
59 std::unique_ptr<omnibox::SuggestResult> GetOmniboxDefaultSuggestion( | 48 std::unique_ptr<omnibox::SuggestResult> GetOmniboxDefaultSuggestion( |
60 Profile* profile, | 49 Profile* profile, |
61 const std::string& extension_id) { | 50 const std::string& extension_id) { |
62 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | 51 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
63 | 52 |
64 std::unique_ptr<omnibox::SuggestResult> suggestion; | 53 std::unique_ptr<omnibox::SuggestResult> suggestion; |
65 const base::DictionaryValue* dict = NULL; | 54 const base::DictionaryValue* dict = NULL; |
66 if (prefs && prefs->ReadPrefAsDictionary(extension_id, | 55 if (prefs && prefs->ReadPrefAsDictionary(extension_id, |
67 kOmniboxDefaultSuggestion, | 56 kOmniboxDefaultSuggestion, |
68 &dict)) { | 57 &dict)) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), | 177 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
189 extension_registry_observer_(this) { | 178 extension_registry_observer_(this) { |
190 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | 179 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
191 if (url_service_) { | 180 if (url_service_) { |
192 template_url_sub_ = url_service_->RegisterOnLoadedCallback( | 181 template_url_sub_ = url_service_->RegisterOnLoadedCallback( |
193 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, | 182 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, |
194 base::Unretained(this))); | 183 base::Unretained(this))); |
195 } | 184 } |
196 | 185 |
197 // Use monochrome icons for Omnibox icons. | 186 // Use monochrome icons for Omnibox icons. |
198 omnibox_popup_icon_manager_.set_monochrome(true); | |
199 omnibox_icon_manager_.set_monochrome(true); | 187 omnibox_icon_manager_.set_monochrome(true); |
200 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, | |
201 0, kOmniboxIconPaddingRight)); | |
202 } | 188 } |
203 | 189 |
204 void OmniboxAPI::Shutdown() { | 190 void OmniboxAPI::Shutdown() { |
205 template_url_sub_.reset(); | 191 template_url_sub_.reset(); |
206 } | 192 } |
207 | 193 |
208 OmniboxAPI::~OmniboxAPI() { | 194 OmniboxAPI::~OmniboxAPI() { |
209 } | 195 } |
210 | 196 |
211 static base::LazyInstance<BrowserContextKeyedAPIFactory<OmniboxAPI> > | 197 static base::LazyInstance<BrowserContextKeyedAPIFactory<OmniboxAPI> > |
212 g_factory = LAZY_INSTANCE_INITIALIZER; | 198 g_factory = LAZY_INSTANCE_INITIALIZER; |
213 | 199 |
214 // static | 200 // static |
215 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { | 201 BrowserContextKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { |
216 return g_factory.Pointer(); | 202 return g_factory.Pointer(); |
217 } | 203 } |
218 | 204 |
219 // static | 205 // static |
220 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { | 206 OmniboxAPI* OmniboxAPI::Get(content::BrowserContext* context) { |
221 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); | 207 return BrowserContextKeyedAPIFactory<OmniboxAPI>::Get(context); |
222 } | 208 } |
223 | 209 |
224 void OmniboxAPI::OnExtensionLoaded(content::BrowserContext* browser_context, | 210 void OmniboxAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
225 const Extension* extension) { | 211 const Extension* extension) { |
226 const std::string& keyword = OmniboxInfo::GetKeyword(extension); | 212 const std::string& keyword = OmniboxInfo::GetKeyword(extension); |
227 if (!keyword.empty()) { | 213 if (!keyword.empty()) { |
228 // Load the omnibox icon so it will be ready to display in the URL bar. | 214 // Load the omnibox icon so it will be ready to display in the URL bar. |
229 omnibox_popup_icon_manager_.LoadIcon(profile_, extension); | |
230 omnibox_icon_manager_.LoadIcon(profile_, extension); | 215 omnibox_icon_manager_.LoadIcon(profile_, extension); |
231 | 216 |
232 if (url_service_) { | 217 if (url_service_) { |
233 url_service_->Load(); | 218 url_service_->Load(); |
234 if (url_service_->loaded()) { | 219 if (url_service_->loaded()) { |
235 url_service_->RegisterOmniboxKeyword( | 220 url_service_->RegisterOmniboxKeyword( |
236 extension->id(), extension->name(), keyword, | 221 extension->id(), extension->name(), keyword, |
237 GetTemplateURLStringForExtension(extension->id())); | 222 GetTemplateURLStringForExtension(extension->id())); |
238 } else { | 223 } else { |
239 pending_extensions_.insert(extension); | 224 pending_extensions_.insert(extension); |
240 } | 225 } |
241 } | 226 } |
242 } | 227 } |
243 } | 228 } |
244 | 229 |
245 void OmniboxAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 230 void OmniboxAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
246 const Extension* extension, | 231 const Extension* extension, |
247 UnloadedExtensionInfo::Reason reason) { | 232 UnloadedExtensionInfo::Reason reason) { |
248 if (!OmniboxInfo::GetKeyword(extension).empty() && url_service_) { | 233 if (!OmniboxInfo::GetKeyword(extension).empty() && url_service_) { |
249 if (url_service_->loaded()) { | 234 if (url_service_->loaded()) { |
250 url_service_->RemoveExtensionControlledTURL( | 235 url_service_->RemoveExtensionControlledTURL( |
251 extension->id(), TemplateURL::OMNIBOX_API_EXTENSION); | 236 extension->id(), TemplateURL::OMNIBOX_API_EXTENSION); |
252 } else { | 237 } else { |
253 pending_extensions_.erase(extension); | 238 pending_extensions_.erase(extension); |
254 } | 239 } |
255 } | 240 } |
256 } | 241 } |
257 | 242 |
258 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { | 243 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
259 return gfx::Image::CreateFrom1xBitmap( | 244 return omnibox_icon_manager_.GetIcon(extension_id); |
260 omnibox_icon_manager_.GetIcon(extension_id)); | |
261 } | |
262 | |
263 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { | |
264 return gfx::Image::CreateFrom1xBitmap( | |
265 omnibox_popup_icon_manager_.GetIcon(extension_id)); | |
266 } | 245 } |
267 | 246 |
268 void OmniboxAPI::OnTemplateURLsLoaded() { | 247 void OmniboxAPI::OnTemplateURLsLoaded() { |
269 // Register keywords for pending extensions. | 248 // Register keywords for pending extensions. |
270 template_url_sub_.reset(); | 249 template_url_sub_.reset(); |
271 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); | 250 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); |
272 i != pending_extensions_.end(); ++i) { | 251 i != pending_extensions_.end(); ++i) { |
273 url_service_->RegisterOmniboxKeyword( | 252 url_service_->RegisterOmniboxKeyword( |
274 (*i)->id(), (*i)->name(), OmniboxInfo::GetKeyword(*i), | 253 (*i)->id(), (*i)->name(), OmniboxInfo::GetKeyword(*i), |
275 GetTemplateURLStringForExtension((*i)->id())); | 254 GetTemplateURLStringForExtension((*i)->id())); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 for (size_t i = 0; i < description_styles.size(); ++i) { | 374 for (size_t i = 0; i < description_styles.size(); ++i) { |
396 if (description_styles[i].offset > placeholder) | 375 if (description_styles[i].offset > placeholder) |
397 description_styles[i].offset += replacement.length() - 2; | 376 description_styles[i].offset += replacement.length() - 2; |
398 } | 377 } |
399 } | 378 } |
400 | 379 |
401 match->contents.assign(description); | 380 match->contents.assign(description); |
402 } | 381 } |
403 | 382 |
404 } // namespace extensions | 383 } // namespace extensions |
OLD | NEW |