Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/renderer/searchbox_extension.h" | 5 #include "chrome/renderer/searchbox_extension.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 } | 269 } |
| 270 | 270 |
| 271 // static | 271 // static |
| 272 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 272 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
| 273 const v8::Arguments& args) { | 273 const v8::Arguments& args) { |
| 274 RenderView* render_view = GetRenderView(); | 274 RenderView* render_view = GetRenderView(); |
| 275 if (!render_view) return v8::Undefined(); | 275 if (!render_view) return v8::Undefined(); |
| 276 return v8::Int32::New(render_view->searchbox().height); | 276 return v8::Int32::New(render_view->searchbox().height); |
| 277 } | 277 } |
| 278 | 278 |
| 279 // Accepts a single argument in form: | |
| 280 // { | |
| 281 // suggestions: [ | |
| 282 // { | |
| 283 // value: "..." | |
| 284 // } | |
| 285 // ] | |
| 286 // } | |
| 279 // static | 287 // static |
| 280 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( | 288 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
| 281 const v8::Arguments& args) { | 289 const v8::Arguments& args) { |
| 282 if (!args.Length() || !args[0]->IsArray()) return v8::Undefined(); | 290 // 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.
| |
| 291 if (!args.Length()) return v8::Undefined(); | |
| 283 | 292 |
| 284 std::vector<std::string> suggestions; | 293 std::vector<std::string> suggestions; |
| 285 v8::Array* suggestions_arg = static_cast<v8::Array*>(*args[0]); | 294 |
| 286 uint32_t length = suggestions_arg->Length(); | 295 if (args[0]->IsArray()) { |
| 287 for (uint32_t i = 0; i < length; i++) { | 296 // For backwards compatibility, also accept an array of strings. |
| 288 std::string suggestion = *v8::String::Utf8Value( | 297 // TODO(tonyg): Remove this when it is confirmed to be unused. |
| 289 suggestions_arg->Get(v8::Integer::New(i))->ToString()); | 298 v8::Array* suggestions_array = static_cast<v8::Array*>(*args[0]); |
| 290 if (!suggestion.length()) continue; | 299 uint32_t length = suggestions_array->Length(); |
| 291 suggestions.push_back(suggestion); | 300 for (uint32_t i = 0; i < length; i++) { |
| 301 std::string suggestion = *v8::String::Utf8Value( | |
| 302 suggestions_array->Get(v8::Integer::New(i))->ToString()); | |
| 303 if (!suggestion.length()) continue; | |
| 304 suggestions.push_back(suggestion); | |
| 305 } | |
| 306 } else if (args[0]->IsObject()) { | |
| 307 // Standard version, object argument. | |
| 308 v8::Object* suggestion_json = static_cast<v8::Object*>(*args[0]); | |
| 309 v8::Local<v8::Value> suggestions_field = | |
| 310 suggestion_json->Get(v8::String::New("suggestions")); | |
| 311 | |
| 312 if (suggestions_field->IsArray()) { | |
| 313 v8::Local<v8::Array> suggestions_array = | |
| 314 suggestions_field.As<v8::Array>(); | |
| 315 | |
| 316 uint32_t length = suggestions_array->Length(); | |
| 317 for (uint32_t i = 0; i < length; i++) { | |
| 318 v8::Local<v8::Value> suggestion_value = | |
| 319 suggestions_array->Get(v8::Integer::New(i)); | |
| 320 if (!suggestion_value->IsObject()) continue; | |
| 321 | |
| 322 v8::Local<v8::Object> suggestion_object = | |
| 323 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.
| |
| 324 v8::Local<v8::Value> suggestion_object_value = | |
| 325 suggestion_object->Get(v8::String::New("value")); | |
| 326 if (!suggestion_object_value->IsString()) continue; | |
| 327 | |
| 328 std::string suggestion = *v8::String::Utf8Value( | |
| 329 suggestion_object_value->ToString()); | |
| 330 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
| |
| 331 suggestions.push_back(suggestion); | |
| 332 } | |
| 333 } | |
| 292 } | 334 } |
| 293 | 335 |
| 294 RenderView* render_view = GetRenderView(); | 336 RenderView* render_view = GetRenderView(); |
| 295 if (!render_view) return v8::Undefined(); | 337 if (!render_view) return v8::Undefined(); |
| 296 | 338 |
| 297 render_view->SetSuggestions(suggestions); | 339 render_view->SetSuggestions(suggestions); |
| 298 return v8::Undefined(); | 340 return v8::Undefined(); |
| 299 } | 341 } |
| 300 | 342 |
| 301 // static | 343 // static |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 | 417 |
| 376 return supports_searchbox_api || supports_deprecated_api; | 418 return supports_searchbox_api || supports_deprecated_api; |
| 377 } | 419 } |
| 378 | 420 |
| 379 // static | 421 // static |
| 380 v8::Extension* SearchBoxExtension::Get() { | 422 v8::Extension* SearchBoxExtension::Get() { |
| 381 return new SearchBoxExtensionWrapper(); | 423 return new SearchBoxExtensionWrapper(); |
| 382 } | 424 } |
| 383 | 425 |
| 384 } // namespace extensions_v8 | 426 } // namespace extensions_v8 |
| OLD | NEW |