| OLD | NEW |
| 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/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/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/string16.h" |
| 12 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 14 #include "chrome/renderer/searchbox.h" | 15 #include "chrome/renderer/searchbox.h" |
| 15 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
| 16 #include "grit/renderer_resources.h" | 17 #include "grit/renderer_resources.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "v8/include/v8.h" | 22 #include "v8/include/v8.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 210 } |
| 210 | 211 |
| 211 // static | 212 // static |
| 212 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetValue( | 213 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetValue( |
| 213 const v8::Arguments& args) { | 214 const v8::Arguments& args) { |
| 214 content::RenderView* render_view = GetRenderView(); | 215 content::RenderView* render_view = GetRenderView(); |
| 215 if (!render_view) return v8::Undefined(); | 216 if (!render_view) return v8::Undefined(); |
| 216 return v8::String::New( | 217 return v8::String::New( |
| 217 reinterpret_cast<const uint16_t*>( | 218 reinterpret_cast<const uint16_t*>( |
| 218 SearchBox::Get(render_view)->value().c_str()), | 219 SearchBox::Get(render_view)->value().c_str()), |
| 219 SearchBox::Get(render_view)->value().length()); | 220 SearchBox::Get(render_view)->value().length()); |
| 220 } | 221 } |
| 221 | 222 |
| 222 // static | 223 // static |
| 223 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetVerbatim( | 224 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetVerbatim( |
| 224 const v8::Arguments& args) { | 225 const v8::Arguments& args) { |
| 225 content::RenderView* render_view = GetRenderView(); | 226 content::RenderView* render_view = GetRenderView(); |
| 226 if (!render_view) return v8::Undefined(); | 227 if (!render_view) return v8::Undefined(); |
| 227 return v8::Boolean::New(SearchBox::Get(render_view)->verbatim()); | 228 return v8::Boolean::New(SearchBox::Get(render_view)->verbatim()); |
| 228 } | 229 } |
| 229 | 230 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // { | 280 // { |
| 280 // suggestions: [ | 281 // suggestions: [ |
| 281 // { | 282 // { |
| 282 // value: "..." | 283 // value: "..." |
| 283 // } | 284 // } |
| 284 // ] | 285 // ] |
| 285 // } | 286 // } |
| 286 // static | 287 // static |
| 287 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( | 288 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
| 288 const v8::Arguments& args) { | 289 const v8::Arguments& args) { |
| 289 std::vector<std::string> suggestions; | 290 std::vector<string16> suggestions; |
| 290 InstantCompleteBehavior behavior = INSTANT_COMPLETE_NOW; | 291 InstantCompleteBehavior behavior = INSTANT_COMPLETE_NOW; |
| 291 | 292 |
| 292 if (args.Length() && args[0]->IsArray()) { | 293 if (args.Length() && args[0]->IsArray()) { |
| 293 // For backwards compatibility, also accept an array of strings. | 294 // For backwards compatibility, also accept an array of strings. |
| 294 // TODO(tonyg): Remove this when it is confirmed to be unused. | 295 // TODO(tonyg): Remove this when it is confirmed to be unused. |
| 295 v8::Local<v8::Array> suggestions_array = | 296 v8::Local<v8::Array> suggestions_array = |
| 296 v8::Local<v8::Array>::Cast(args[0]); | 297 v8::Local<v8::Array>::Cast(args[0]); |
| 297 uint32_t length = suggestions_array->Length(); | 298 uint32_t length = suggestions_array->Length(); |
| 298 for (uint32_t i = 0; i < length; i++) { | 299 for (uint32_t i = 0; i < length; i++) { |
| 299 std::string suggestion = *v8::String::Utf8Value( | 300 string16 suggestion(reinterpret_cast<char16*>(*v8::String::Value( |
| 300 suggestions_array->Get(v8::Integer::New(i))->ToString()); | 301 suggestions_array->Get(v8::Integer::New(i))->ToString()))); |
| 301 if (!suggestion.length()) continue; | 302 if (!suggestion.length()) continue; |
| 302 suggestions.push_back(suggestion); | 303 suggestions.push_back(suggestion); |
| 303 } | 304 } |
| 304 } else if (args.Length() && args[0]->IsObject()) { | 305 } else if (args.Length() && args[0]->IsObject()) { |
| 305 // Standard version, object argument. | 306 // Standard version, object argument. |
| 306 v8::Local<v8::Object> suggestion_json = | 307 v8::Local<v8::Object> suggestion_json = |
| 307 v8::Local<v8::Object>::Cast(args[0]); | 308 v8::Local<v8::Object>::Cast(args[0]); |
| 308 v8::Local<v8::Value> suggestions_field = | 309 v8::Local<v8::Value> suggestions_field = |
| 309 suggestion_json->Get(v8::String::New("suggestions")); | 310 suggestion_json->Get(v8::String::New("suggestions")); |
| 310 | 311 |
| 311 if (suggestions_field->IsArray()) { | 312 if (suggestions_field->IsArray()) { |
| 312 v8::Local<v8::Array> suggestions_array = | 313 v8::Local<v8::Array> suggestions_array = |
| 313 suggestions_field.As<v8::Array>(); | 314 suggestions_field.As<v8::Array>(); |
| 314 | 315 |
| 315 uint32_t length = suggestions_array->Length(); | 316 uint32_t length = suggestions_array->Length(); |
| 316 for (uint32_t i = 0; i < length; i++) { | 317 for (uint32_t i = 0; i < length; i++) { |
| 317 v8::Local<v8::Value> suggestion_value = | 318 v8::Local<v8::Value> suggestion_value = |
| 318 suggestions_array->Get(v8::Integer::New(i)); | 319 suggestions_array->Get(v8::Integer::New(i)); |
| 319 if (!suggestion_value->IsObject()) continue; | 320 if (!suggestion_value->IsObject()) continue; |
| 320 | 321 |
| 321 v8::Local<v8::Object> suggestion_object = | 322 v8::Local<v8::Object> suggestion_object = |
| 322 suggestion_value.As<v8::Object>(); | 323 suggestion_value.As<v8::Object>(); |
| 323 v8::Local<v8::Value> suggestion_object_value = | 324 v8::Local<v8::Value> suggestion_object_value = |
| 324 suggestion_object->Get(v8::String::New("value")); | 325 suggestion_object->Get(v8::String::New("value")); |
| 325 if (!suggestion_object_value->IsString()) continue; | 326 if (!suggestion_object_value->IsString()) continue; |
| 326 | 327 |
| 327 std::string suggestion = *v8::String::Utf8Value( | 328 string16 suggestion(reinterpret_cast<char16*>(*v8::String::Value( |
| 328 suggestion_object_value->ToString()); | 329 suggestion_object_value->ToString()))); |
| 329 if (!suggestion.length()) continue; | 330 if (!suggestion.length()) continue; |
| 330 suggestions.push_back(suggestion); | 331 suggestions.push_back(suggestion); |
| 331 } | 332 } |
| 332 } | 333 } |
| 333 if (suggestion_json->Has(v8::String::New("complete_behavior"))) { | 334 if (suggestion_json->Has(v8::String::New("complete_behavior"))) { |
| 334 v8::Local<v8::Value> complete_value = | 335 v8::Local<v8::Value> complete_value = |
| 335 suggestion_json->Get(v8::String::New("complete_behavior")); | 336 suggestion_json->Get(v8::String::New("complete_behavior")); |
| 336 if (complete_value->IsString()) { | 337 if (complete_value->IsString()) { |
| 337 if (complete_value->Equals(v8::String::New("never"))) | 338 if (complete_value->Equals(v8::String::New("never"))) |
| 338 behavior = INSTANT_COMPLETE_NEVER; | 339 behavior = INSTANT_COMPLETE_NEVER; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 409 |
| 409 // static | 410 // static |
| 410 v8::Extension* SearchBoxExtension::Get() { | 411 v8::Extension* SearchBoxExtension::Get() { |
| 411 const base::StringPiece code = | 412 const base::StringPiece code = |
| 412 ResourceBundle::GetSharedInstance().GetRawDataResource( | 413 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 413 IDR_SEARCHBOX_API, ui::SCALE_FACTOR_NONE); | 414 IDR_SEARCHBOX_API, ui::SCALE_FACTOR_NONE); |
| 414 return new SearchBoxExtensionWrapper(code); | 415 return new SearchBoxExtensionWrapper(code); |
| 415 } | 416 } |
| 416 | 417 |
| 417 } // namespace extensions_v8 | 418 } // namespace extensions_v8 |
| OLD | NEW |