| 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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 176 } |
| 177 | 177 |
| 178 OmniboxAPI::OmniboxAPI(Profile* profile) | 178 OmniboxAPI::OmniboxAPI(Profile* profile) |
| 179 : profile_(profile), | 179 : profile_(profile), |
| 180 url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { | 180 url_service_(TemplateURLServiceFactory::GetForProfile(profile)) { |
| 181 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 181 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 182 content::Source<Profile>(profile)); | 182 content::Source<Profile>(profile)); |
| 183 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 183 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 184 content::Source<Profile>(profile)); | 184 content::Source<Profile>(profile)); |
| 185 if (url_service_) { | 185 if (url_service_) { |
| 186 registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | 186 template_url_sub_ = url_service_->RegisterOnLoadedCallback( |
| 187 content::Source<TemplateURLService>(url_service_)); | 187 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, |
| 188 base::Unretained(this))); |
| 188 } | 189 } |
| 189 | 190 |
| 190 // Use monochrome icons for Omnibox icons. | 191 // Use monochrome icons for Omnibox icons. |
| 191 omnibox_popup_icon_manager_.set_monochrome(true); | 192 omnibox_popup_icon_manager_.set_monochrome(true); |
| 192 omnibox_icon_manager_.set_monochrome(true); | 193 omnibox_icon_manager_.set_monochrome(true); |
| 193 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, | 194 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft, |
| 194 0, kOmniboxIconPaddingRight)); | 195 0, kOmniboxIconPaddingRight)); |
| 195 } | 196 } |
| 196 | 197 |
| 198 void OmniboxAPI::Shutdown() { |
| 199 template_url_sub_.reset(); |
| 200 } |
| 201 |
| 197 OmniboxAPI::~OmniboxAPI() { | 202 OmniboxAPI::~OmniboxAPI() { |
| 198 } | 203 } |
| 199 | 204 |
| 200 static base::LazyInstance<ProfileKeyedAPIFactory<OmniboxAPI> > | 205 static base::LazyInstance<ProfileKeyedAPIFactory<OmniboxAPI> > |
| 201 g_factory = LAZY_INSTANCE_INITIALIZER; | 206 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 202 | 207 |
| 203 // static | 208 // static |
| 204 ProfileKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { | 209 ProfileKeyedAPIFactory<OmniboxAPI>* OmniboxAPI::GetFactoryInstance() { |
| 205 return &g_factory.Get(); | 210 return &g_factory.Get(); |
| 206 } | 211 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 content::Details<UnloadedExtensionInfo>(details)->extension; | 243 content::Details<UnloadedExtensionInfo>(details)->extension; |
| 239 if (!OmniboxInfo::GetKeyword(extension).empty()) { | 244 if (!OmniboxInfo::GetKeyword(extension).empty()) { |
| 240 if (url_service_) { | 245 if (url_service_) { |
| 241 if (url_service_->loaded()) | 246 if (url_service_->loaded()) |
| 242 url_service_->UnregisterExtensionKeyword(extension->id()); | 247 url_service_->UnregisterExtensionKeyword(extension->id()); |
| 243 else | 248 else |
| 244 pending_extensions_.erase(extension); | 249 pending_extensions_.erase(extension); |
| 245 } | 250 } |
| 246 } | 251 } |
| 247 } else { | 252 } else { |
| 248 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | 253 NOTREACHED(); |
| 249 // Load pending extensions. | |
| 250 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); | |
| 251 i != pending_extensions_.end(); ++i) { | |
| 252 url_service_->RegisterExtensionKeyword((*i)->id(), | |
| 253 (*i)->name(), | |
| 254 OmniboxInfo::GetKeyword(*i)); | |
| 255 } | |
| 256 pending_extensions_.clear(); | |
| 257 } | 254 } |
| 258 } | 255 } |
| 259 | 256 |
| 260 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { | 257 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) { |
| 261 return gfx::Image::CreateFrom1xBitmap( | 258 return gfx::Image::CreateFrom1xBitmap( |
| 262 omnibox_icon_manager_.GetIcon(extension_id)); | 259 omnibox_icon_manager_.GetIcon(extension_id)); |
| 263 } | 260 } |
| 264 | 261 |
| 265 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { | 262 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) { |
| 266 return gfx::Image::CreateFrom1xBitmap( | 263 return gfx::Image::CreateFrom1xBitmap( |
| 267 omnibox_popup_icon_manager_.GetIcon(extension_id)); | 264 omnibox_popup_icon_manager_.GetIcon(extension_id)); |
| 268 } | 265 } |
| 269 | 266 |
| 267 void OmniboxAPI::OnTemplateURLsLoaded() { |
| 268 // Register keywords for pending extensions. |
| 269 template_url_sub_.reset(); |
| 270 for (PendingExtensions::const_iterator i(pending_extensions_.begin()); |
| 271 i != pending_extensions_.end(); ++i) { |
| 272 url_service_->RegisterExtensionKeyword((*i)->id(), |
| 273 (*i)->name(), |
| 274 OmniboxInfo::GetKeyword(*i)); |
| 275 } |
| 276 pending_extensions_.clear(); |
| 277 } |
| 278 |
| 270 template <> | 279 template <> |
| 271 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies() { | 280 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies() { |
| 272 DependsOn(ExtensionSystemFactory::GetInstance()); | 281 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 273 DependsOn(TemplateURLServiceFactory::GetInstance()); | 282 DependsOn(TemplateURLServiceFactory::GetInstance()); |
| 274 } | 283 } |
| 275 | 284 |
| 276 bool OmniboxSendSuggestionsFunction::RunImpl() { | 285 bool OmniboxSendSuggestionsFunction::RunImpl() { |
| 277 scoped_ptr<SendSuggestions::Params> params( | 286 scoped_ptr<SendSuggestions::Params> params( |
| 278 SendSuggestions::Params::Create(*args_)); | 287 SendSuggestions::Params::Create(*args_)); |
| 279 EXTENSION_FUNCTION_VALIDATE(params); | 288 EXTENSION_FUNCTION_VALIDATE(params); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 for (size_t i = 0; i < description_styles.size(); ++i) { | 397 for (size_t i = 0; i < description_styles.size(); ++i) { |
| 389 if (description_styles[i].offset > placeholder) | 398 if (description_styles[i].offset > placeholder) |
| 390 description_styles[i].offset += replacement.length() - 2; | 399 description_styles[i].offset += replacement.length() - 2; |
| 391 } | 400 } |
| 392 } | 401 } |
| 393 | 402 |
| 394 match->contents.assign(description); | 403 match->contents.assign(description); |
| 395 } | 404 } |
| 396 | 405 |
| 397 } // namespace extensions | 406 } // namespace extensions |
| OLD | NEW |