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

Unified Diff: chrome/renderer/searchbox_extension.cc

Issue 6310003: Support specified JSON as argument to setSuggestions(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome
Patch Set: Address comments Created 9 years, 11 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/renderer/searchbox_extension.h ('k') | chrome/test/data/instant/search.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/renderer/searchbox_extension.cc
diff --git a/chrome/renderer/searchbox_extension.cc b/chrome/renderer/searchbox_extension.cc
index 180e92c0caa057a0bea4a572abb035b955ccf0f0..2e268640cceb4c6b70d682e850338c9673ac3bda 100644
--- a/chrome/renderer/searchbox_extension.cc
+++ b/chrome/renderer/searchbox_extension.cc
@@ -276,25 +276,62 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight(
return v8::Int32::New(render_view->searchbox().height);
}
+// Accepts a single argument in form:
+// {
+// suggestions: [
+// {
+// value: "..."
+// }
+// ]
+// }
// static
v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions(
const v8::Arguments& args) {
- if (!args.Length() || !args[0]->IsArray()) return v8::Undefined();
-
std::vector<std::string> suggestions;
- v8::Array* suggestions_arg = static_cast<v8::Array*>(*args[0]);
- uint32_t length = suggestions_arg->Length();
- for (uint32_t i = 0; i < length; i++) {
- std::string suggestion = *v8::String::Utf8Value(
- suggestions_arg->Get(v8::Integer::New(i))->ToString());
- if (!suggestion.length()) continue;
- suggestions.push_back(suggestion);
- }
- RenderView* render_view = GetRenderView();
- if (!render_view) return v8::Undefined();
+ if (args.Length() && args[0]->IsArray()) {
+ // For backwards compatibility, also accept an array of strings.
+ // TODO(tonyg): Remove this when it is confirmed to be unused.
+ v8::Array* suggestions_array = static_cast<v8::Array*>(*args[0]);
+ uint32_t length = suggestions_array->Length();
+ for (uint32_t i = 0; i < length; i++) {
+ std::string suggestion = *v8::String::Utf8Value(
+ suggestions_array->Get(v8::Integer::New(i))->ToString());
+ if (!suggestion.length()) continue;
+ suggestions.push_back(suggestion);
+ }
+ } else if (args.Length() && args[0]->IsObject()) {
+ // Standard version, object argument.
+ v8::Object* suggestion_json = static_cast<v8::Object*>(*args[0]);
+ v8::Local<v8::Value> suggestions_field =
+ suggestion_json->Get(v8::String::New("suggestions"));
+
+ if (suggestions_field->IsArray()) {
+ v8::Local<v8::Array> suggestions_array =
+ suggestions_field.As<v8::Array>();
+
+ uint32_t length = suggestions_array->Length();
+ for (uint32_t i = 0; i < length; i++) {
+ v8::Local<v8::Value> suggestion_value =
+ suggestions_array->Get(v8::Integer::New(i));
+ if (!suggestion_value->IsObject()) continue;
+
+ v8::Local<v8::Object> suggestion_object =
+ suggestion_value.As<v8::Object>();
+ v8::Local<v8::Value> suggestion_object_value =
+ suggestion_object->Get(v8::String::New("value"));
+ if (!suggestion_object_value->IsString()) continue;
+
+ std::string suggestion = *v8::String::Utf8Value(
+ suggestion_object_value->ToString());
+ if (!suggestion.length()) continue;
+ suggestions.push_back(suggestion);
+ }
+ }
+ }
- render_view->SetSuggestions(suggestions);
+ if (RenderView* render_view = GetRenderView())
+ render_view->SetSuggestions(suggestions);
return v8::Undefined();
}
« no previous file with comments | « chrome/renderer/searchbox_extension.h ('k') | chrome/test/data/instant/search.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698