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 std::vector<std::string> suggestions; |
283 | 291 |
284 std::vector<std::string> suggestions; | 292 if (args.Length() && args[0]->IsArray()) { |
285 v8::Array* suggestions_arg = static_cast<v8::Array*>(*args[0]); | 293 // For backwards compatibility, also accept an array of strings. |
286 uint32_t length = suggestions_arg->Length(); | 294 // TODO(tonyg): Remove this when it is confirmed to be unused. |
287 for (uint32_t i = 0; i < length; i++) { | 295 v8::Array* suggestions_array = static_cast<v8::Array*>(*args[0]); |
288 std::string suggestion = *v8::String::Utf8Value( | 296 uint32_t length = suggestions_array->Length(); |
289 suggestions_arg->Get(v8::Integer::New(i))->ToString()); | 297 for (uint32_t i = 0; i < length; i++) { |
290 if (!suggestion.length()) continue; | 298 std::string suggestion = *v8::String::Utf8Value( |
291 suggestions.push_back(suggestion); | 299 suggestions_array->Get(v8::Integer::New(i))->ToString()); |
| 300 if (!suggestion.length()) continue; |
| 301 suggestions.push_back(suggestion); |
| 302 } |
| 303 } else if (args.Length() && args[0]->IsObject()) { |
| 304 // Standard version, object argument. |
| 305 v8::Object* suggestion_json = static_cast<v8::Object*>(*args[0]); |
| 306 v8::Local<v8::Value> suggestions_field = |
| 307 suggestion_json->Get(v8::String::New("suggestions")); |
| 308 |
| 309 if (suggestions_field->IsArray()) { |
| 310 v8::Local<v8::Array> suggestions_array = |
| 311 suggestions_field.As<v8::Array>(); |
| 312 |
| 313 uint32_t length = suggestions_array->Length(); |
| 314 for (uint32_t i = 0; i < length; i++) { |
| 315 v8::Local<v8::Value> suggestion_value = |
| 316 suggestions_array->Get(v8::Integer::New(i)); |
| 317 if (!suggestion_value->IsObject()) continue; |
| 318 |
| 319 v8::Local<v8::Object> suggestion_object = |
| 320 suggestion_value.As<v8::Object>(); |
| 321 v8::Local<v8::Value> suggestion_object_value = |
| 322 suggestion_object->Get(v8::String::New("value")); |
| 323 if (!suggestion_object_value->IsString()) continue; |
| 324 |
| 325 std::string suggestion = *v8::String::Utf8Value( |
| 326 suggestion_object_value->ToString()); |
| 327 if (!suggestion.length()) continue; |
| 328 suggestions.push_back(suggestion); |
| 329 } |
| 330 } |
292 } | 331 } |
293 | 332 |
294 RenderView* render_view = GetRenderView(); | 333 if (RenderView* render_view = GetRenderView()) |
295 if (!render_view) return v8::Undefined(); | 334 render_view->SetSuggestions(suggestions); |
296 | |
297 render_view->SetSuggestions(suggestions); | |
298 return v8::Undefined(); | 335 return v8::Undefined(); |
299 } | 336 } |
300 | 337 |
301 // static | 338 // static |
302 bool Dispatch(WebFrame* frame, const std::string& event_name) { | 339 bool Dispatch(WebFrame* frame, const std::string& event_name) { |
303 DCHECK(frame) << "Dispatch requires frame"; | 340 DCHECK(frame) << "Dispatch requires frame"; |
304 if (!frame) return false; | 341 if (!frame) return false; |
305 | 342 |
306 v8::HandleScope handle_scope; | 343 v8::HandleScope handle_scope; |
307 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 344 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 412 |
376 return supports_searchbox_api || supports_deprecated_api; | 413 return supports_searchbox_api || supports_deprecated_api; |
377 } | 414 } |
378 | 415 |
379 // static | 416 // static |
380 v8::Extension* SearchBoxExtension::Get() { | 417 v8::Extension* SearchBoxExtension::Get() { |
381 return new SearchBoxExtensionWrapper(); | 418 return new SearchBoxExtensionWrapper(); |
382 } | 419 } |
383 | 420 |
384 } // namespace extensions_v8 | 421 } // namespace extensions_v8 |
OLD | NEW |