| 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 "content/public/renderer/v8_value_converter.h" |
| 9 #include "chrome/renderer/extensions/extension_request_sender.h" | 9 #include "chrome/renderer/extensions/extension_request_sender.h" |
| 10 | 10 |
| 11 using content::V8ValueConverter; | 11 using content::V8ValueConverter; |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 | 14 |
| 15 SendRequestNatives::SendRequestNatives( | 15 SendRequestNatives::SendRequestNatives( |
| 16 ExtensionDispatcher* extension_dispatcher, | 16 ExtensionDispatcher* extension_dispatcher, |
| 17 ExtensionRequestSender* request_sender) | 17 ExtensionRequestSender* request_sender) |
| 18 : ChromeV8Extension(extension_dispatcher), | 18 : ChromeV8Extension(extension_dispatcher), |
| 19 request_sender_(request_sender) { | 19 request_sender_(request_sender) { |
| 20 RouteFunction("GetNextRequestId", | |
| 21 base::Bind(&SendRequestNatives::GetNextRequestId, | |
| 22 base::Unretained(this))); | |
| 23 RouteFunction("StartRequest", | 20 RouteFunction("StartRequest", |
| 24 base::Bind(&SendRequestNatives::StartRequest, | 21 base::Bind(&SendRequestNatives::StartRequest, |
| 25 base::Unretained(this))); | 22 base::Unretained(this))); |
| 26 } | 23 } |
| 27 | 24 |
| 28 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId( | |
| 29 const v8::Arguments& args) { | |
| 30 static int next_request_id = 0; | |
| 31 return v8::Integer::New(next_request_id++); | |
| 32 } | |
| 33 | |
| 34 // Starts an API request to the browser, with an optional callback. The | 25 // Starts an API request to the browser, with an optional callback. The |
| 35 // callback will be dispatched to EventBindings::HandleResponse. | 26 // callback will be dispatched to EventBindings::HandleResponse. |
| 36 v8::Handle<v8::Value> SendRequestNatives::StartRequest( | 27 v8::Handle<v8::Value> SendRequestNatives::StartRequest( |
| 37 const v8::Arguments& args) { | 28 const v8::Arguments& args) { |
| 38 std::string name = *v8::String::AsciiValue(args[0]); | 29 std::string name = *v8::String::AsciiValue(args[0]); |
| 39 int request_id = args[2]->Int32Value(); | 30 // args[1] is the request args object. |
| 40 bool has_callback = args[3]->BooleanValue(); | 31 bool has_callback = args[2]->BooleanValue(); |
| 41 bool for_io_thread = args[4]->BooleanValue(); | 32 bool for_io_thread = args[3]->BooleanValue(); |
| 42 | 33 |
| 43 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 34 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 44 | 35 |
| 45 // Make "undefined" the same as "null" for optional arguments, but for objects | 36 // Make "undefined" the same as "null" for optional arguments, but for objects |
| 46 // strip nulls (like {foo: null}, and therefore {foo: undefined} as well) to | 37 // strip nulls (like {foo: null}, and therefore {foo: undefined} as well) to |
| 47 // make it easier for extension APIs to check for optional arguments. | 38 // make it easier for extension APIs to check for optional arguments. |
| 48 converter->SetUndefinedAllowed(true); | 39 converter->SetUndefinedAllowed(true); |
| 49 converter->SetStripNullFromObjects(true); | 40 converter->SetStripNullFromObjects(true); |
| 50 | 41 |
| 51 scoped_ptr<Value> value_args( | 42 scoped_ptr<Value> value_args( |
| 52 converter->FromV8Value(args[1], v8::Context::GetCurrent())); | 43 converter->FromV8Value(args[1], v8::Context::GetCurrent())); |
| 53 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { | 44 CHECK(value_args.get()); |
| 54 NOTREACHED() << "Unable to convert args passed to StartRequest"; | 45 ListValue* api_args = NULL; |
| 55 return v8::Undefined(); | 46 CHECK(value_args->GetAsList(&api_args)); |
| 56 } | |
| 57 | 47 |
| 58 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread, | 48 return v8::Integer::New(request_sender_->StartRequest( |
| 59 static_cast<ListValue*>(value_args.get())); | 49 name, has_callback, for_io_thread, api_args)); |
| 60 return v8::Undefined(); | |
| 61 } | 50 } |
| 62 | 51 |
| 63 } // namespace extensions | 52 } // namespace extensions |
| OLD | NEW |