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/child/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
) {
21 base::Bind(&SendRequestNatives::StartRequest
, base::Unretained(this)));
24 base::Bind(&SendRequestNatives::GetGlobal
, base::Unretained(this)));
27 // Starts an API request to the browser, with an optional callback. The
28 // callback will be dispatched to EventBindings::HandleResponse.
29 void SendRequestNatives::StartRequest(
30 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
31 CHECK_EQ(5, args
.Length());
32 std::string name
= *v8::String::Utf8Value(args
[0]);
33 bool has_callback
= args
[2]->BooleanValue();
34 bool for_io_thread
= args
[3]->BooleanValue();
35 bool preserve_null_in_objects
= args
[4]->BooleanValue();
37 int request_id
= request_sender_
->GetNextRequestId();
38 args
.GetReturnValue().Set(static_cast<int32_t>(request_id
));
40 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
42 // See http://crbug.com/149880. The context menus APIs relies on this, but
43 // we shouldn't really be doing it (e.g. for the sake of the storage API).
44 converter
->SetFunctionAllowed(true);
46 if (!preserve_null_in_objects
)
47 converter
->SetStripNullFromObjects(true);
49 scoped_ptr
<base::Value
> value_args(
50 converter
->FromV8Value(args
[1], context()->v8_context()));
51 if (!value_args
.get() || !value_args
->IsType(base::Value::TYPE_LIST
)) {
52 NOTREACHED() << "Unable to convert args passed to StartRequest";
56 request_sender_
->StartRequest(
62 static_cast<base::ListValue
*>(value_args
.get()));
65 void SendRequestNatives::GetGlobal(
66 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
67 CHECK_EQ(1, args
.Length());
68 CHECK(args
[0]->IsObject());
69 args
.GetReturnValue().Set(
70 v8::Local
<v8::Object
>::Cast(args
[0])->CreationContext()->Global());
73 } // namespace extensions