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
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/send_request_natives.h"
7 #include "base/json/json_reader.h"
8 #include "content/public/renderer/v8_value_converter.h"
9 #include "chrome/renderer/extensions/request_sender.h"
11 using content::V8ValueConverter
;
13 namespace extensions
{
15 SendRequestNatives::SendRequestNatives(Dispatcher
* dispatcher
,
16 RequestSender
* request_sender
,
17 ChromeV8Context
* context
)
18 : ChromeV8Extension(dispatcher
, context
->v8_context()),
19 request_sender_(request_sender
),
21 RouteFunction("GetNextRequestId",
22 base::Bind(&SendRequestNatives::GetNextRequestId
,
23 base::Unretained(this)));
24 RouteFunction("StartRequest",
25 base::Bind(&SendRequestNatives::StartRequest
,
26 base::Unretained(this)));
27 RouteFunction("GetGlobal",
28 base::Bind(&SendRequestNatives::GetGlobal
,
29 base::Unretained(this)));
32 v8::Handle
<v8::Value
> SendRequestNatives::GetNextRequestId(
33 const v8::Arguments
& args
) {
34 return v8::Integer::New(request_sender_
->GetNextRequestId());
37 // Starts an API request to the browser, with an optional callback. The
38 // callback will be dispatched to EventBindings::HandleResponse.
39 v8::Handle
<v8::Value
> SendRequestNatives::StartRequest(
40 const v8::Arguments
& args
) {
41 std::string name
= *v8::String::AsciiValue(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
<Value
> value_args(converter
->FromV8Value(args
[1], v8_context()));
57 if (!value_args
.get() || !value_args
->IsType(Value::TYPE_LIST
)) {
58 NOTREACHED() << "Unable to convert args passed to StartRequest";
59 return v8::Undefined();
62 request_sender_
->StartRequest(
63 context_
, name
, request_id
, has_callback
, for_io_thread
,
64 static_cast<ListValue
*>(value_args
.get()));
65 return v8::Undefined();
68 v8::Handle
<v8::Value
> SendRequestNatives::GetGlobal(const v8::Arguments
& args
) {
69 CHECK_EQ(1, args
.Length());
70 CHECK(args
[0]->IsObject());
71 return v8::Handle
<v8::Object
>::Cast(args
[0])->CreationContext()->Global();
74 } // namespace extensions