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.
4
5 #include "extensions/renderer/send_request_natives.h"
6
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"
11
12 using content::V8ValueConverter;
13
14 namespace extensions {
15
SendRequestNatives(RequestSender * request_sender,ScriptContext * context)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)));
22 RouteFunction(
23 "StartRequest",
24 base::Bind(&SendRequestNatives::StartRequest, base::Unretained(this)));
25 RouteFunction(
26 "GetGlobal",
27 base::Bind(&SendRequestNatives::GetGlobal, base::Unretained(this)));
28 }
29
GetNextRequestId(const v8::FunctionCallbackInfo<v8::Value> & args)30 void SendRequestNatives::GetNextRequestId(
31 const v8::FunctionCallbackInfo<v8::Value>& args) {
32 args.GetReturnValue().Set(
33 static_cast<int32_t>(request_sender_->GetNextRequestId()));
34 }
35
36 // Starts an API request to the browser, with an optional callback. The
37 // callback will be dispatched to EventBindings::HandleResponse.
StartRequest(const v8::FunctionCallbackInfo<v8::Value> & args)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();
46
47 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
48
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);
52
53 if (!preserve_null_in_objects)
54 converter->SetStripNullFromObjects(true);
55
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";
60 return;
61 }
62
63 request_sender_->StartRequest(
64 context(),
65 name,
66 request_id,
67 has_callback,
68 for_io_thread,
69 static_cast<base::ListValue*>(value_args.get()));
70 }
71
GetGlobal(const v8::FunctionCallbackInfo<v8::Value> & args)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());
78 }
79
80 } // namespace extensions
81