1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/renderer/send_request_natives.h"
7 #include "base/json/json_reader.h"
8 #include "content/public/renderer/v8_value_converter.h"
9 #include "extensions/renderer/request_sender.h"
10 #include "extensions/renderer/script_context.h"
12 using content::V8ValueConverter
;
14 namespace extensions
{
16 SendRequestNatives::SendRequestNatives(RequestSender
* request_sender
,
17 ScriptContext
* context
)
18 : ObjectBackedNativeHandler(context
), request_sender_(request_sender
) {
19 RouteFunction("GetNextRequestId",
20 base::Bind(&SendRequestNatives::GetNextRequestId
,
21 base::Unretained(this)));
24 base::Bind(&SendRequestNatives::StartRequest
, base::Unretained(this)));
27 base::Bind(&SendRequestNatives::GetGlobal
, base::Unretained(this)));
30 void SendRequestNatives::GetNextRequestId(
31 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
32 args
.GetReturnValue().Set(
33 static_cast<int32_t>(request_sender_
->GetNextRequestId()));
36 // Starts an API request to the browser, with an optional callback. The
37 // callback will be dispatched to EventBindings::HandleResponse.
38 void SendRequestNatives::StartRequest(
39 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
40 CHECK_EQ(6, args
.Length());
41 std::string name
= *v8::String::Utf8Value(args
[0]);
42 int request_id
= args
[2]->Int32Value();
43 bool has_callback
= args
[3]->BooleanValue();
44 bool for_io_thread
= args
[4]->BooleanValue();
45 bool preserve_null_in_objects
= args
[5]->BooleanValue();
47 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
49 // See http://crbug.com/149880. The context menus APIs relies on this, but
50 // we shouldn't really be doing it (e.g. for the sake of the storage API).
51 converter
->SetFunctionAllowed(true);
53 if (!preserve_null_in_objects
)
54 converter
->SetStripNullFromObjects(true);
56 scoped_ptr
<base::Value
> value_args(
57 converter
->FromV8Value(args
[1], context()->v8_context()));
58 if (!value_args
.get() || !value_args
->IsType(base::Value::TYPE_LIST
)) {
59 NOTREACHED() << "Unable to convert args passed to StartRequest";
63 request_sender_
->StartRequest(
69 static_cast<base::ListValue
*>(value_args
.get()));
72 void SendRequestNatives::GetGlobal(
73 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
74 CHECK_EQ(1, args
.Length());
75 CHECK(args
[0]->IsObject());
76 args
.GetReturnValue().Set(
77 v8::Handle
<v8::Object
>::Cast(args
[0])->CreationContext()->Global());
80 } // namespace extensions