Chromium Code Reviews| 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" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | |
| 15 #include "chrome/browser/extensions/tab_helper.h" | 14 #include "chrome/browser/extensions/tab_helper.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
| 18 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/common/extensions/api/omnibox.h" | 19 #include "chrome/common/extensions/api/omnibox.h" |
| 21 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 20 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| 22 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 24 #include "extensions/browser/event_router.h" | 23 #include "extensions/browser/event_router.h" |
| 25 #include "extensions/browser/extension_prefs.h" | 24 #include "extensions/browser/extension_prefs.h" |
| 25 #include "extensions/browser/extension_prefs_factory.h" | |
| 26 #include "extensions/browser/extension_registry.h" | |
| 26 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
| 27 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 28 #include "ui/gfx/image/image.h" | 29 #include "ui/gfx/image/image.h" |
| 29 | 30 |
| 30 namespace extensions { | 31 namespace extensions { |
| 31 | 32 |
| 32 namespace omnibox = api::omnibox; | 33 namespace omnibox = api::omnibox; |
| 33 namespace SendSuggestions = omnibox::SendSuggestions; | 34 namespace SendSuggestions = omnibox::SendSuggestions; |
| 34 namespace SetDefaultSuggestion = omnibox::SetDefaultSuggestion; | 35 namespace SetDefaultSuggestion = omnibox::SetDefaultSuggestion; |
| 35 | 36 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 50 static const int kOmniboxIconPaddingLeft = 0; | 51 static const int kOmniboxIconPaddingLeft = 0; |
| 51 static const int kOmniboxIconPaddingRight = 2; | 52 static const int kOmniboxIconPaddingRight = 2; |
| 52 #else | 53 #else |
| 53 static const int kOmniboxIconPaddingLeft = 0; | 54 static const int kOmniboxIconPaddingLeft = 0; |
| 54 static const int kOmniboxIconPaddingRight = 0; | 55 static const int kOmniboxIconPaddingRight = 0; |
| 55 #endif | 56 #endif |
| 56 | 57 |
| 57 scoped_ptr<omnibox::SuggestResult> GetOmniboxDefaultSuggestion( | 58 scoped_ptr<omnibox::SuggestResult> GetOmniboxDefaultSuggestion( |
| 58 Profile* profile, | 59 Profile* profile, |
| 59 const std::string& extension_id) { | 60 const std::string& extension_id) { |
| 60 ExtensionPrefs* prefs = | 61 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
| 61 ExtensionSystem::Get(profile)->extension_service()->extension_prefs(); | |
| 62 | 62 |
| 63 scoped_ptr<omnibox::SuggestResult> suggestion; | 63 scoped_ptr<omnibox::SuggestResult> suggestion; |
| 64 const base::DictionaryValue* dict = NULL; | 64 const base::DictionaryValue* dict = NULL; |
| 65 if (prefs && prefs->ReadPrefAsDictionary(extension_id, | 65 if (prefs && prefs->ReadPrefAsDictionary(extension_id, |
| 66 kOmniboxDefaultSuggestion, | 66 kOmniboxDefaultSuggestion, |
| 67 &dict)) { | 67 &dict)) { |
| 68 suggestion.reset(new omnibox::SuggestResult); | 68 suggestion.reset(new omnibox::SuggestResult); |
| 69 omnibox::SuggestResult::Populate(*dict, suggestion.get()); | 69 omnibox::SuggestResult::Populate(*dict, suggestion.get()); |
| 70 } | 70 } |
| 71 return suggestion.Pass(); | 71 return suggestion.Pass(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Tries to set the omnibox default suggestion; returns true on success or | 74 // Tries to set the omnibox default suggestion; returns true on success or |
| 75 // false on failure. | 75 // false on failure. |
| 76 bool SetOmniboxDefaultSuggestion( | 76 bool SetOmniboxDefaultSuggestion( |
| 77 Profile* profile, | 77 Profile* profile, |
| 78 const std::string& extension_id, | 78 const std::string& extension_id, |
| 79 const omnibox::DefaultSuggestResult& suggestion) { | 79 const omnibox::DefaultSuggestResult& suggestion) { |
| 80 ExtensionPrefs* prefs = | 80 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
| 81 ExtensionSystem::Get(profile)->extension_service()->extension_prefs(); | |
| 82 if (!prefs) | 81 if (!prefs) |
| 83 return false; | 82 return false; |
| 84 | 83 |
| 85 scoped_ptr<base::DictionaryValue> dict = suggestion.ToValue(); | 84 scoped_ptr<base::DictionaryValue> dict = suggestion.ToValue(); |
| 86 // Add the content field so that the dictionary can be used to populate an | 85 // Add the content field so that the dictionary can be used to populate an |
| 87 // omnibox::SuggestResult. | 86 // omnibox::SuggestResult. |
| 88 dict->SetWithoutPathExpansion(kSuggestionContent, new base::StringValue("")); | 87 dict->SetWithoutPathExpansion(kSuggestionContent, new base::StringValue("")); |
| 89 prefs->UpdateExtensionPref(extension_id, | 88 prefs->UpdateExtensionPref(extension_id, |
| 90 kOmniboxDefaultSuggestion, | 89 kOmniboxDefaultSuggestion, |
| 91 dict.release()); | 90 dict.release()); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 103 make_scoped_ptr(new base::ListValue()))); | 102 make_scoped_ptr(new base::ListValue()))); |
| 104 event->restrict_to_browser_context = profile; | 103 event->restrict_to_browser_context = profile; |
| 105 ExtensionSystem::Get(profile)->event_router()-> | 104 ExtensionSystem::Get(profile)->event_router()-> |
| 106 DispatchEventToExtension(extension_id, event.Pass()); | 105 DispatchEventToExtension(extension_id, event.Pass()); |
| 107 } | 106 } |
| 108 | 107 |
| 109 // static | 108 // static |
| 110 bool ExtensionOmniboxEventRouter::OnInputChanged( | 109 bool ExtensionOmniboxEventRouter::OnInputChanged( |
| 111 Profile* profile, const std::string& extension_id, | 110 Profile* profile, const std::string& extension_id, |
| 112 const std::string& input, int suggest_id) { | 111 const std::string& input, int suggest_id) { |
| 113 if (!extensions::ExtensionSystem::Get(profile)->event_router()-> | 112 if (!ExtensionSystem::Get(profile)->event_router()-> |
| 114 ExtensionHasEventListener(extension_id, | 113 ExtensionHasEventListener(extension_id, |
| 115 omnibox::OnInputChanged::kEventName)) | 114 omnibox::OnInputChanged::kEventName)) |
| 116 return false; | 115 return false; |
| 117 | 116 |
| 118 scoped_ptr<base::ListValue> args(new base::ListValue()); | 117 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 119 args->Set(0, new base::StringValue(input)); | 118 args->Set(0, new base::StringValue(input)); |
| 120 args->Set(1, new base::FundamentalValue(suggest_id)); | 119 args->Set(1, new base::FundamentalValue(suggest_id)); |
| 121 | 120 |
| 122 scoped_ptr<Event> event(new Event(omnibox::OnInputChanged::kEventName, | 121 scoped_ptr<Event> event(new Event(omnibox::OnInputChanged::kEventName, |
| 123 args.Pass())); | 122 args.Pass())); |
| 124 event->restrict_to_browser_context = profile; | 123 event->restrict_to_browser_context = profile; |
| 125 ExtensionSystem::Get(profile)->event_router()-> | 124 ExtensionSystem::Get(profile)->event_router()-> |
| 126 DispatchEventToExtension(extension_id, event.Pass()); | 125 DispatchEventToExtension(extension_id, event.Pass()); |
| 127 return true; | 126 return true; |
| 128 } | 127 } |
| 129 | 128 |
| 130 // static | 129 // static |
| 131 void ExtensionOmniboxEventRouter::OnInputEntered( | 130 void ExtensionOmniboxEventRouter::OnInputEntered( |
| 132 content::WebContents* web_contents, | 131 content::WebContents* web_contents, |
| 133 const std::string& extension_id, | 132 const std::string& extension_id, |
| 134 const std::string& input, | 133 const std::string& input, |
| 135 WindowOpenDisposition disposition) { | 134 WindowOpenDisposition disposition) { |
| 136 Profile* profile = | 135 Profile* profile = |
| 137 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 136 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 138 | 137 |
| 139 const Extension* extension = | 138 const Extension* extension = ExtensionRegistry::Get(profile)-> |
| 140 ExtensionSystem::Get(profile)->extension_service()->extensions()-> | 139 enabled_extensions().GetByID(extension_id); |
| 141 GetByID(extension_id); | |
| 142 CHECK(extension); | 140 CHECK(extension); |
| 143 extensions::TabHelper::FromWebContents(web_contents)-> | 141 extensions::TabHelper::FromWebContents(web_contents)-> |
| 144 active_tab_permission_granter()->GrantIfRequested(extension); | 142 active_tab_permission_granter()->GrantIfRequested(extension); |
| 145 | 143 |
| 146 scoped_ptr<base::ListValue> args(new base::ListValue()); | 144 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 147 args->Set(0, new base::StringValue(input)); | 145 args->Set(0, new base::StringValue(input)); |
| 148 if (disposition == NEW_FOREGROUND_TAB) | 146 if (disposition == NEW_FOREGROUND_TAB) |
| 149 args->Set(1, new base::StringValue(kForegroundTabDisposition)); | 147 args->Set(1, new base::StringValue(kForegroundTabDisposition)); |
| 150 else if (disposition == NEW_BACKGROUND_TAB) | 148 else if (disposition == NEW_BACKGROUND_TAB) |
| 151 args->Set(1, new base::StringValue(kBackgroundTabDisposition)); | 149 args->Set(1, new base::StringValue(kBackgroundTabDisposition)); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 url_service_->RegisterOmniboxKeyword((*i)->id(), | 270 url_service_->RegisterOmniboxKeyword((*i)->id(), |
| 273 (*i)->name(), | 271 (*i)->name(), |
| 274 OmniboxInfo::GetKeyword(*i)); | 272 OmniboxInfo::GetKeyword(*i)); |
| 275 } | 273 } |
| 276 pending_extensions_.clear(); | 274 pending_extensions_.clear(); |
| 277 } | 275 } |
| 278 | 276 |
| 279 template <> | 277 template <> |
| 280 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies() { | 278 void ProfileKeyedAPIFactory<OmniboxAPI>::DeclareFactoryDependencies() { |
| 281 DependsOn(ExtensionSystemFactory::GetInstance()); | 279 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 280 DependsOn(ExtensionPrefsFactory::GetInstance()); | |
|
James Cook
2014/02/07 00:53:02
Good catch.
| |
| 282 DependsOn(TemplateURLServiceFactory::GetInstance()); | 281 DependsOn(TemplateURLServiceFactory::GetInstance()); |
| 283 } | 282 } |
| 284 | 283 |
| 285 bool OmniboxSendSuggestionsFunction::RunImpl() { | 284 bool OmniboxSendSuggestionsFunction::RunImpl() { |
| 286 scoped_ptr<SendSuggestions::Params> params( | 285 scoped_ptr<SendSuggestions::Params> params( |
| 287 SendSuggestions::Params::Create(*args_)); | 286 SendSuggestions::Params::Create(*args_)); |
| 288 EXTENSION_FUNCTION_VALIDATE(params); | 287 EXTENSION_FUNCTION_VALIDATE(params); |
| 289 | 288 |
| 290 content::NotificationService::current()->Notify( | 289 content::NotificationService::current()->Notify( |
| 291 chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, | 290 chrome::NOTIFICATION_EXTENSION_OMNIBOX_SUGGESTIONS_READY, |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 for (size_t i = 0; i < description_styles.size(); ++i) { | 394 for (size_t i = 0; i < description_styles.size(); ++i) { |
| 396 if (description_styles[i].offset > placeholder) | 395 if (description_styles[i].offset > placeholder) |
| 397 description_styles[i].offset += replacement.length() - 2; | 396 description_styles[i].offset += replacement.length() - 2; |
| 398 } | 397 } |
| 399 } | 398 } |
| 400 | 399 |
| 401 match->contents.assign(description); | 400 match->contents.assign(description); |
| 402 } | 401 } |
| 403 | 402 |
| 404 } // namespace extensions | 403 } // namespace extensions |
| OLD | NEW |