| 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/extensions/send_request_natives.h" | 5 #include "chrome/renderer/extensions/send_request_natives.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "content/public/renderer/v8_value_converter.h" |
| 8 #include "chrome/renderer/extensions/extension_request_sender.h" | 9 #include "chrome/renderer/extensions/extension_request_sender.h" |
| 9 | 10 |
| 11 using content::V8ValueConverter; |
| 12 |
| 10 namespace extensions { | 13 namespace extensions { |
| 11 | 14 |
| 12 SendRequestNatives::SendRequestNatives( | 15 SendRequestNatives::SendRequestNatives( |
| 13 ExtensionDispatcher* extension_dispatcher, | 16 ExtensionDispatcher* extension_dispatcher, |
| 14 ExtensionRequestSender* request_sender) | 17 ExtensionRequestSender* request_sender) |
| 15 : ChromeV8Extension(extension_dispatcher), | 18 : ChromeV8Extension(extension_dispatcher), |
| 16 request_sender_(request_sender) { | 19 request_sender_(request_sender) { |
| 17 RouteFunction("GetNextRequestId", | 20 RouteFunction("GetNextRequestId", |
| 18 base::Bind(&SendRequestNatives::GetNextRequestId, | 21 base::Bind(&SendRequestNatives::GetNextRequestId, |
| 19 base::Unretained(this))); | 22 base::Unretained(this))); |
| 20 RouteFunction("StartRequest", | 23 RouteFunction("StartRequest", |
| 21 base::Bind(&SendRequestNatives::StartRequest, | 24 base::Bind(&SendRequestNatives::StartRequest, |
| 22 base::Unretained(this))); | 25 base::Unretained(this))); |
| 23 } | 26 } |
| 24 | 27 |
| 25 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId( | 28 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId( |
| 26 const v8::Arguments& args) { | 29 const v8::Arguments& args) { |
| 27 static int next_request_id = 0; | 30 static int next_request_id = 0; |
| 28 return v8::Integer::New(next_request_id++); | 31 return v8::Integer::New(next_request_id++); |
| 29 } | 32 } |
| 30 | 33 |
| 31 // Starts an API request to the browser, with an optional callback. The | 34 // Starts an API request to the browser, with an optional callback. The |
| 32 // callback will be dispatched to EventBindings::HandleResponse. | 35 // callback will be dispatched to EventBindings::HandleResponse. |
| 33 v8::Handle<v8::Value> SendRequestNatives::StartRequest( | 36 v8::Handle<v8::Value> SendRequestNatives::StartRequest( |
| 34 const v8::Arguments& args) { | 37 const v8::Arguments& args) { |
| 35 std::string str_args = *v8::String::Utf8Value(args[1]); | |
| 36 scoped_ptr<Value> value_args(base::JSONReader::Read(str_args)); | |
| 37 | |
| 38 // Since we do the serialization in the v8 extension, we should always get | |
| 39 // valid JSON. | |
| 40 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { | |
| 41 NOTREACHED() << "Invalid JSON passed to StartRequest."; | |
| 42 return v8::Undefined(); | |
| 43 } | |
| 44 | |
| 45 std::string name = *v8::String::AsciiValue(args[0]); | 38 std::string name = *v8::String::AsciiValue(args[0]); |
| 46 int request_id = args[2]->Int32Value(); | 39 int request_id = args[2]->Int32Value(); |
| 47 bool has_callback = args[3]->BooleanValue(); | 40 bool has_callback = args[3]->BooleanValue(); |
| 48 bool for_io_thread = args[4]->BooleanValue(); | 41 bool for_io_thread = args[4]->BooleanValue(); |
| 49 | 42 |
| 43 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 44 scoped_ptr<Value> value_args( |
| 45 converter->FromV8Value(args[1], v8::Context::GetCurrent())); |
| 46 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { |
| 47 NOTREACHED() << "Unable to convert args passed to StartRequest"; |
| 48 return v8::Undefined(); |
| 49 } |
| 50 |
| 50 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread, | 51 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread, |
| 51 static_cast<ListValue*>(value_args.get())); | 52 static_cast<ListValue*>(value_args.get())); |
| 52 return v8::Undefined(); | 53 return v8::Undefined(); |
| 53 } | 54 } |
| 54 | 55 |
| 55 } // namespace extensions | 56 } // namespace extensions |
| OLD | NEW |