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 // Starts an API request to the browser, with an optional callback. The |
| 32 // callback will be dispatched to EventBindings::HandleResponse. |
| 33 v8::Handle<v8::Value> SendRequestNatives::StartRequest( |
| 34 const v8::Arguments& args) { |
| 35 std::string str_args = *v8::String::Utf8Value(args[1]); |
| 36 base::JSONReader reader; |
| 37 scoped_ptr<Value> value_args; |
| 38 value_args.reset(reader.JsonToValue(str_args, false, false)); |
| 39 |
| 40 // Since we do the serialization in the v8 extension, we should always get |
| 41 // valid JSON. |
| 42 if (!value_args.get() || !value_args->IsType(Value::TYPE_LIST)) { |
| 43 NOTREACHED() << "Invalid JSON passed to StartRequest."; |
| 44 return v8::Undefined(); |
| 45 } |
| 46 |
| 47 std::string name = *v8::String::AsciiValue(args[0]); |
| 48 int request_id = args[2]->Int32Value(); |
| 49 bool has_callback = args[3]->BooleanValue(); |
| 50 bool for_io_thread = args[4]->BooleanValue(); |
| 51 |
| 52 request_sender_->StartRequest(name, request_id, has_callback, for_io_thread, |
| 53 static_cast<ListValue*>(value_args.get())); |
| 54 return v8::Undefined(); |
| 55 } |
| 56 |
| 57 } // namespace extensions |
OLD | NEW |