Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: chrome/browser/extensions/api/omnibox/omnibox_api.cc

Issue 11446034: SupportsUserData and manifest handlers for Extension; use them for the Omnibox API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/string16.h"
10 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/supports_user_data.h"
11 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
12 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/extensions/api/omnibox/omnibox_api_factory.h"
13 #include "chrome/browser/extensions/event_router.h" 16 #include "chrome/browser/extensions/event_router.h"
14 #include "chrome/browser/extensions/extension_prefs.h" 17 #include "chrome/browser/extensions/extension_prefs.h"
15 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h" 19 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/browser/extensions/tab_helper.h" 20 #include "chrome/browser/extensions/tab_helper.h"
18 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url.h" 22 #include "chrome/browser/search_engines/template_url.h"
20 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/common/extensions/extension_constants.h" 24 #include "chrome/common/extensions/extension.h"
25 #include "chrome/common/extensions/extension_manifest_constants.h"
26 #include "chrome/common/extensions/manifest_handler.h"
27 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
29 #include "ui/gfx/image/image.h"
23 30
24 namespace events { 31 namespace events {
25 const char kOnInputStarted[] = "omnibox.onInputStarted"; 32 const char kOnInputStarted[] = "omnibox.onInputStarted";
26 const char kOnInputChanged[] = "omnibox.onInputChanged"; 33 const char kOnInputChanged[] = "omnibox.onInputChanged";
27 const char kOnInputEntered[] = "omnibox.onInputEntered"; 34 const char kOnInputEntered[] = "omnibox.onInputEntered";
28 const char kOnInputCancelled[] = "omnibox.onInputCancelled"; 35 const char kOnInputCancelled[] = "omnibox.onInputCancelled";
29 } // namespace events 36 } // namespace events
30 37
31 namespace extensions { 38 namespace extensions {
32 39
33 namespace { 40 namespace {
34 41
42 // Manifest keys.
43
44 // TODO(yoz): remove the constants from extension_manifest_constants,
45 // which requires changing extension_l10n_util.cc.
46 const char kOmnibox[] = "omnibox";
47 const char kKeyword[] = "keyword";
48
35 const char kSuggestionContent[] = "content"; 49 const char kSuggestionContent[] = "content";
36 const char kSuggestionDescription[] = "description"; 50 const char kSuggestionDescription[] = "description";
37 const char kSuggestionDescriptionStyles[] = "descriptionStyles"; 51 const char kSuggestionDescriptionStyles[] = "descriptionStyles";
38 const char kSuggestionDescriptionStylesRaw[] = "descriptionStylesRaw"; 52 const char kSuggestionDescriptionStylesRaw[] = "descriptionStylesRaw";
39 const char kDescriptionStylesType[] = "type"; 53 const char kDescriptionStylesType[] = "type";
40 const char kDescriptionStylesOffset[] = "offset"; 54 const char kDescriptionStylesOffset[] = "offset";
41 const char kDescriptionStylesLength[] = "length"; 55 const char kDescriptionStylesLength[] = "length";
42 56
57 #if defined(OS_LINUX)
58 static const int kOmniboxIconPaddingLeft = 2;
59 static const int kOmniboxIconPaddingRight = 2;
60 #elif defined(OS_MACOSX)
61 static const int kOmniboxIconPaddingLeft = 0;
62 static const int kOmniboxIconPaddingRight = 2;
63 #else
64 static const int kOmniboxIconPaddingLeft = 0;
65 static const int kOmniboxIconPaddingRight = 0;
66 #endif
67
68 struct OmniboxInfo : public base::SupportsUserData::Data {
69 // The Omnibox keyword for an extension.
70 std::string keyword;
71 };
72
73 // Parses the "omnibox" manifest key.
74 class OmniboxHandler : public ManifestHandler {
75 public:
76 OmniboxHandler();
77 virtual ~OmniboxHandler();
78
79 virtual bool Parse(const base::Value* value,
80 Extension* extension,
81 string16* error) OVERRIDE;
82 };
83
84 OmniboxHandler::OmniboxHandler() {
85 }
86
87 OmniboxHandler::~OmniboxHandler() {
88 }
89
90 bool OmniboxHandler::Parse(const base::Value* value,
91 Extension* extension,
92 string16* error) {
93 OmniboxInfo* info = new OmniboxInfo;
94 const DictionaryValue* dict = NULL;
95 if (!value->GetAsDictionary(&dict) ||
96 !dict->GetString(kKeyword, &info->keyword) ||
97 info->keyword.empty()) {
98 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidOmniboxKeyword);
99 return false;
Matt Perry 2012/12/07 01:22:59 leaking |info|. use scoped_ptr
Yoyo Zhou 2012/12/07 02:13:32 Done.
100 }
101 extension->SetUserData(kOmnibox, info);
102 return true;
103 }
104
43 } // namespace 105 } // namespace
44 106
45 // static 107 // static
46 void ExtensionOmniboxEventRouter::OnInputStarted( 108 void ExtensionOmniboxEventRouter::OnInputStarted(
47 Profile* profile, const std::string& extension_id) { 109 Profile* profile, const std::string& extension_id) {
48 scoped_ptr<ListValue> event_args(new ListValue()); 110 scoped_ptr<ListValue> event_args(new ListValue());
49 extensions::ExtensionSystem::Get(profile)->event_router()-> 111 extensions::ExtensionSystem::Get(profile)->event_router()->
50 DispatchEventToExtension(extension_id, events::kOnInputStarted, 112 DispatchEventToExtension(extension_id, events::kOnInputStarted,
51 event_args.Pass(), profile, GURL()); 113 event_args.Pass(), profile, GURL());
52 } 114 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 161
100 // static 162 // static
101 void ExtensionOmniboxEventRouter::OnInputCancelled( 163 void ExtensionOmniboxEventRouter::OnInputCancelled(
102 Profile* profile, const std::string& extension_id) { 164 Profile* profile, const std::string& extension_id) {
103 scoped_ptr<ListValue> args(new ListValue()); 165 scoped_ptr<ListValue> args(new ListValue());
104 extensions::ExtensionSystem::Get(profile)->event_router()-> 166 extensions::ExtensionSystem::Get(profile)->event_router()->
105 DispatchEventToExtension(extension_id, events::kOnInputCancelled, 167 DispatchEventToExtension(extension_id, events::kOnInputCancelled,
106 args.Pass(), profile, GURL()); 168 args.Pass(), profile, GURL());
107 } 169 }
108 170
171 OmniboxAPI::OmniboxAPI(Profile* profile) {
172 RegisterManifestHandler(kOmnibox,
173 new OmniboxHandler);
174 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
175 content::Source<Profile>(profile));
176
177 // Use monochrome icons for Omnibox icons.
178 omnibox_popup_icon_manager_.set_monochrome(true);
179 omnibox_icon_manager_.set_monochrome(true);
180 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
181 0, kOmniboxIconPaddingRight));
182 }
183
184 OmniboxAPI::~OmniboxAPI() {
185 }
186
187 void OmniboxAPI::Shutdown() {
188 }
189
190 // static
191 OmniboxAPI* OmniboxAPI::Get(Profile* profile) {
192 return OmniboxAPIFactory::GetForProfile(profile);
193 }
194
195 void OmniboxAPI::Observe(int type,
196 const content::NotificationSource& source,
197 const content::NotificationDetails& details) {
198 DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED);
199 const Extension* extension = content::Details<const Extension>(details).ptr();
200 if (!GetKeyword(extension).empty()) {
201 // Load the omnibox icon so it will be ready to display in the URL bar.
202 omnibox_popup_icon_manager_.LoadIcon(extension);
203 omnibox_icon_manager_.LoadIcon(extension);
204 }
205 }
206
207 gfx::Image OmniboxAPI::GetOmniboxIcon(const std::string& extension_id) {
208 return gfx::Image(omnibox_icon_manager_.GetIcon(extension_id));
209 }
210
211 gfx::Image OmniboxAPI::GetOmniboxPopupIcon(const std::string& extension_id) {
212 return gfx::Image(omnibox_popup_icon_manager_.GetIcon(extension_id));
213 }
214
215 // static
216 const std::string& OmniboxAPI::GetKeyword(const Extension* extension) {
217 OmniboxInfo* info = static_cast<OmniboxInfo*>(
218 extension->GetUserData(kOmnibox));
219 return info ? info->keyword : EmptyString();
220 }
221
222 // static
223 bool OmniboxAPI::IsVerboseInstallMessage(const Extension* extension) {
224 return !GetKeyword(extension).empty() ||
225 extension->browser_action_info() ||
226 (extension->page_action_info() &&
227 (extension->page_action_command() ||
228 !extension->page_action_info()->default_icon.empty()));
229 }
230
109 bool OmniboxSendSuggestionsFunction::RunImpl() { 231 bool OmniboxSendSuggestionsFunction::RunImpl() {
110 ExtensionOmniboxSuggestions suggestions; 232 ExtensionOmniboxSuggestions suggestions;
111 ListValue* suggestions_value; 233 ListValue* suggestions_value;
112 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id)); 234 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &suggestions.request_id));
113 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value)); 235 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &suggestions_value));
114 236
115 suggestions.suggestions.resize(suggestions_value->GetSize()); 237 suggestions.suggestions.resize(suggestions_value->GetSize());
116 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) { 238 for (size_t i = 0; i < suggestions_value->GetSize(); ++i) {
117 ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i]; 239 ExtensionOmniboxSuggestion& suggestion = suggestions.suggestions[i];
118 DictionaryValue* suggestion_value; 240 DictionaryValue* suggestion_value;
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 for (size_t i = 0; i < description_styles.size(); ++i) { 422 for (size_t i = 0; i < description_styles.size(); ++i) {
301 if (description_styles[i].offset > placeholder) 423 if (description_styles[i].offset > placeholder)
302 description_styles[i].offset += replacement.length() - 2; 424 description_styles[i].offset += replacement.length() - 2;
303 } 425 }
304 } 426 }
305 427
306 match->contents.assign(description); 428 match->contents.assign(description);
307 } 429 }
308 430
309 } // namespace extensions 431 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698