OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/renderer/extensions/send_request_natives.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "chrome/renderer/extensions/extension_request_sender.h" | |
9 | |
10 namespace extensions { | |
11 | |
12 SendRequestNatives::SendRequestNatives( | |
13 ExtensionDispatcher* extension_dispatcher, | |
14 ExtensionRequestSender* request_sender) | |
15 : ChromeV8Extension(extension_dispatcher), | |
16 request_sender_(request_sender) { | |
17 RouteFunction("GetNextRequestId", | |
18 base::Bind(&SendRequestNatives::GetNextRequestId, | |
19 base::Unretained(this))); | |
20 RouteFunction("StartRequest", | |
21 base::Bind(&SendRequestNatives::StartRequest, | |
22 base::Unretained(this))); | |
23 } | |
24 | |
25 v8::Handle<v8::Value> SendRequestNatives::GetNextRequestId( | |
26 const v8::Arguments& args) { | |
27 static int next_request_id = 0; | |
28 return v8::Integer::New(next_request_id++); | |
29 } | |
30 | |
31 v8::Handle<v8::Value> SendRequestNatives::StartRequestCommon( | |
not at google - send to devlin
2012/03/30 03:23:18
method seems redundant now, can just include it as
koz (OOO until 15th September)
2012/04/03 00:15:27
Done.
| |
32 const v8::Arguments& args, ListValue* value_args) { | |
33 std::string name = *v8::String::AsciiValue(args[0]); | |
34 int request_id = args[2]->Int32Value(); | |
35 bool has_callback = args[3]->BooleanValue(); | |
36 bool for_io_thread = args[4]->BooleanValue(); | |
37 | |
38 request_sender_->StartRequest(name, | |
39 request_id, | |
40 has_callback, | |
41 for_io_thread, | |
42 value_args); | |
43 return v8::Undefined(); | |
44 } | |
45 | |
46 // Starts an API request to the browser, with an optional callback. The | |
47 // callback will be dispatched to EventBindings::HandleResponse. | |
48 v8::Handle<v8::Value> SendRequestNatives::StartRequest( | |
49 const v8::Arguments& args) { | |
50 std::string str_args = *v8::String::Utf8Value(args[1]); | |
51 base::JSONReader reader; | |
52 scoped_ptr<Value> value_args; | |
53 value_args.reset(reader.JsonToValue(str_args, false, false)); | |
54 | |
55 // Since we do the serialization in the v8 extension, we should always get | |
56 // valid JSON. | |
57 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { | |
58 NOTREACHED() << "Invalid JSON passed to StartRequest."; | |
59 return v8::Undefined(); | |
60 } | |
61 | |
62 return StartRequestCommon(args, static_cast<ListValue*>(value_args.get())); | |
63 } | |
64 | |
65 } // namespace extensions | |
OLD | NEW |