| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "chrome/browser/extensions/tab_helper.h" | 14 #include "chrome/browser/extensions/tab_helper.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/common/extensions/api/omnibox.h" | 17 #include "chrome/common/extensions/api/omnibox.h" |
| 17 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 18 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | 60 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
| 60 | 61 |
| 61 scoped_ptr<omnibox::SuggestResult> suggestion; | 62 scoped_ptr<omnibox::SuggestResult> suggestion; |
| 62 const base::DictionaryValue* dict = NULL; | 63 const base::DictionaryValue* dict = NULL; |
| 63 if (prefs && prefs->ReadPrefAsDictionary(extension_id, | 64 if (prefs && prefs->ReadPrefAsDictionary(extension_id, |
| 64 kOmniboxDefaultSuggestion, | 65 kOmniboxDefaultSuggestion, |
| 65 &dict)) { | 66 &dict)) { |
| 66 suggestion.reset(new omnibox::SuggestResult); | 67 suggestion.reset(new omnibox::SuggestResult); |
| 67 omnibox::SuggestResult::Populate(*dict, suggestion.get()); | 68 omnibox::SuggestResult::Populate(*dict, suggestion.get()); |
| 68 } | 69 } |
| 69 return suggestion.Pass(); | 70 return suggestion; |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Tries to set the omnibox default suggestion; returns true on success or | 73 // Tries to set the omnibox default suggestion; returns true on success or |
| 73 // false on failure. | 74 // false on failure. |
| 74 bool SetOmniboxDefaultSuggestion( | 75 bool SetOmniboxDefaultSuggestion( |
| 75 Profile* profile, | 76 Profile* profile, |
| 76 const std::string& extension_id, | 77 const std::string& extension_id, |
| 77 const omnibox::DefaultSuggestResult& suggestion) { | 78 const omnibox::DefaultSuggestResult& suggestion) { |
| 78 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | 79 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); |
| 79 if (!prefs) | 80 if (!prefs) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 100 } // namespace | 101 } // namespace |
| 101 | 102 |
| 102 // static | 103 // static |
| 103 void ExtensionOmniboxEventRouter::OnInputStarted( | 104 void ExtensionOmniboxEventRouter::OnInputStarted( |
| 104 Profile* profile, const std::string& extension_id) { | 105 Profile* profile, const std::string& extension_id) { |
| 105 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_STARTED, | 106 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_STARTED, |
| 106 omnibox::OnInputStarted::kEventName, | 107 omnibox::OnInputStarted::kEventName, |
| 107 make_scoped_ptr(new base::ListValue()))); | 108 make_scoped_ptr(new base::ListValue()))); |
| 108 event->restrict_to_browser_context = profile; | 109 event->restrict_to_browser_context = profile; |
| 109 EventRouter::Get(profile) | 110 EventRouter::Get(profile) |
| 110 ->DispatchEventToExtension(extension_id, event.Pass()); | 111 ->DispatchEventToExtension(extension_id, std::move(event)); |
| 111 } | 112 } |
| 112 | 113 |
| 113 // static | 114 // static |
| 114 bool ExtensionOmniboxEventRouter::OnInputChanged( | 115 bool ExtensionOmniboxEventRouter::OnInputChanged( |
| 115 Profile* profile, const std::string& extension_id, | 116 Profile* profile, const std::string& extension_id, |
| 116 const std::string& input, int suggest_id) { | 117 const std::string& input, int suggest_id) { |
| 117 EventRouter* event_router = EventRouter::Get(profile); | 118 EventRouter* event_router = EventRouter::Get(profile); |
| 118 if (!event_router->ExtensionHasEventListener( | 119 if (!event_router->ExtensionHasEventListener( |
| 119 extension_id, omnibox::OnInputChanged::kEventName)) | 120 extension_id, omnibox::OnInputChanged::kEventName)) |
| 120 return false; | 121 return false; |
| 121 | 122 |
| 122 scoped_ptr<base::ListValue> args(new base::ListValue()); | 123 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 123 args->Set(0, new base::StringValue(input)); | 124 args->Set(0, new base::StringValue(input)); |
| 124 args->Set(1, new base::FundamentalValue(suggest_id)); | 125 args->Set(1, new base::FundamentalValue(suggest_id)); |
| 125 | 126 |
| 126 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_CHANGED, | 127 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_CHANGED, |
| 127 omnibox::OnInputChanged::kEventName, | 128 omnibox::OnInputChanged::kEventName, |
| 128 args.Pass())); | 129 std::move(args))); |
| 129 event->restrict_to_browser_context = profile; | 130 event->restrict_to_browser_context = profile; |
| 130 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 131 event_router->DispatchEventToExtension(extension_id, std::move(event)); |
| 131 return true; | 132 return true; |
| 132 } | 133 } |
| 133 | 134 |
| 134 // static | 135 // static |
| 135 void ExtensionOmniboxEventRouter::OnInputEntered( | 136 void ExtensionOmniboxEventRouter::OnInputEntered( |
| 136 content::WebContents* web_contents, | 137 content::WebContents* web_contents, |
| 137 const std::string& extension_id, | 138 const std::string& extension_id, |
| 138 const std::string& input, | 139 const std::string& input, |
| 139 WindowOpenDisposition disposition) { | 140 WindowOpenDisposition disposition) { |
| 140 Profile* profile = | 141 Profile* profile = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 151 args->Set(0, new base::StringValue(input)); | 152 args->Set(0, new base::StringValue(input)); |
| 152 if (disposition == NEW_FOREGROUND_TAB) | 153 if (disposition == NEW_FOREGROUND_TAB) |
| 153 args->Set(1, new base::StringValue(kForegroundTabDisposition)); | 154 args->Set(1, new base::StringValue(kForegroundTabDisposition)); |
| 154 else if (disposition == NEW_BACKGROUND_TAB) | 155 else if (disposition == NEW_BACKGROUND_TAB) |
| 155 args->Set(1, new base::StringValue(kBackgroundTabDisposition)); | 156 args->Set(1, new base::StringValue(kBackgroundTabDisposition)); |
| 156 else | 157 else |
| 157 args->Set(1, new base::StringValue(kCurrentTabDisposition)); | 158 args->Set(1, new base::StringValue(kCurrentTabDisposition)); |
| 158 | 159 |
| 159 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_ENTERED, | 160 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_ENTERED, |
| 160 omnibox::OnInputEntered::kEventName, | 161 omnibox::OnInputEntered::kEventName, |
| 161 args.Pass())); | 162 std::move(args))); |
| 162 event->restrict_to_browser_context = profile; | 163 event->restrict_to_browser_context = profile; |
| 163 EventRouter::Get(profile) | 164 EventRouter::Get(profile) |
| 164 ->DispatchEventToExtension(extension_id, event.Pass()); | 165 ->DispatchEventToExtension(extension_id, std::move(event)); |
| 165 | 166 |
| 166 content::NotificationService::current()->Notify( | 167 content::NotificationService::current()->Notify( |
| 167 extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, | 168 extensions::NOTIFICATION_EXTENSION_OMNIBOX_INPUT_ENTERED, |
| 168 content::Source<Profile>(profile), | 169 content::Source<Profile>(profile), |
| 169 content::NotificationService::NoDetails()); | 170 content::NotificationService::NoDetails()); |
| 170 } | 171 } |
| 171 | 172 |
| 172 // static | 173 // static |
| 173 void ExtensionOmniboxEventRouter::OnInputCancelled( | 174 void ExtensionOmniboxEventRouter::OnInputCancelled( |
| 174 Profile* profile, const std::string& extension_id) { | 175 Profile* profile, const std::string& extension_id) { |
| 175 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_CANCELLED, | 176 scoped_ptr<Event> event(new Event(events::OMNIBOX_ON_INPUT_CANCELLED, |
| 176 omnibox::OnInputCancelled::kEventName, | 177 omnibox::OnInputCancelled::kEventName, |
| 177 make_scoped_ptr(new base::ListValue()))); | 178 make_scoped_ptr(new base::ListValue()))); |
| 178 event->restrict_to_browser_context = profile; | 179 event->restrict_to_browser_context = profile; |
| 179 EventRouter::Get(profile) | 180 EventRouter::Get(profile) |
| 180 ->DispatchEventToExtension(extension_id, event.Pass()); | 181 ->DispatchEventToExtension(extension_id, std::move(event)); |
| 181 } | 182 } |
| 182 | 183 |
| 183 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) | 184 OmniboxAPI::OmniboxAPI(content::BrowserContext* context) |
| 184 : profile_(Profile::FromBrowserContext(context)), | 185 : profile_(Profile::FromBrowserContext(context)), |
| 185 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), | 186 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
| 186 extension_registry_observer_(this) { | 187 extension_registry_observer_(this) { |
| 187 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | 188 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 188 if (url_service_) { | 189 if (url_service_) { |
| 189 template_url_sub_ = url_service_->RegisterOnLoadedCallback( | 190 template_url_sub_ = url_service_->RegisterOnLoadedCallback( |
| 190 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, | 191 base::Bind(&OmniboxAPI::OnTemplateURLsLoaded, |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 for (size_t i = 0; i < description_styles.size(); ++i) { | 395 for (size_t i = 0; i < description_styles.size(); ++i) { |
| 395 if (description_styles[i].offset > placeholder) | 396 if (description_styles[i].offset > placeholder) |
| 396 description_styles[i].offset += replacement.length() - 2; | 397 description_styles[i].offset += replacement.length() - 2; |
| 397 } | 398 } |
| 398 } | 399 } |
| 399 | 400 |
| 400 match->contents.assign(description); | 401 match->contents.assign(description); |
| 401 } | 402 } |
| 402 | 403 |
| 403 } // namespace extensions | 404 } // namespace extensions |
| OLD | NEW |