Chromium Code Reviews| Index: chrome/renderer/searchbox_extension.cc |
| diff --git a/chrome/renderer/searchbox_extension.cc b/chrome/renderer/searchbox_extension.cc |
| index 180e92c0caa057a0bea4a572abb035b955ccf0f0..2fde89c2880352691438c3e3c7b48ff660ae7091 100644 |
| --- a/chrome/renderer/searchbox_extension.cc |
| +++ b/chrome/renderer/searchbox_extension.cc |
| @@ -276,19 +276,61 @@ 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(); |
| + // Require at least one arg. |
|
sky
2011/01/13 22:37:46
Should we treat an empty array as no suggestions?
tonyg
2011/01/13 23:07:50
Seems reasonable. Done.
|
| + if (!args.Length()) 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); |
| + |
| + if (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[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::Array>(); |
|
sky
2011/01/13 22:37:46
How come you use As<Array> here?
tonyg
2011/01/13 23:07:50
Copy/paste error. Fixed.
|
| + 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; |
|
sky
2011/01/13 22:37:46
It would be nice to add LOGing to all fail cases.
tonyg
2011/01/13 23:07:50
Seems like noise to me. Is there a way to LOG only
|
| + suggestions.push_back(suggestion); |
| + } |
| + } |
| } |
| RenderView* render_view = GetRenderView(); |