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