| 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 "content/public/renderer/v8_value_converter.h" | |
| 9 #include "extensions/renderer/request_sender.h" | |
| 10 | |
| 11 using content::V8ValueConverter; | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 SendRequestNatives::SendRequestNatives(Dispatcher* dispatcher, | |
| 16 RequestSender* request_sender, | |
| 17 ChromeV8Context* context) | |
| 18 : ChromeV8Extension(dispatcher, context), | |
| 19 request_sender_(request_sender) { | |
| 20 RouteFunction("GetNextRequestId", | |
| 21 base::Bind(&SendRequestNatives::GetNextRequestId, | |
| 22 base::Unretained(this))); | |
| 23 RouteFunction("StartRequest", | |
| 24 base::Bind(&SendRequestNatives::StartRequest, | |
| 25 base::Unretained(this))); | |
| 26 RouteFunction("GetGlobal", | |
| 27 base::Bind(&SendRequestNatives::GetGlobal, | |
| 28 base::Unretained(this))); | |
| 29 } | |
| 30 | |
| 31 void SendRequestNatives::GetNextRequestId( | |
| 32 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 33 args.GetReturnValue().Set(static_cast<int32_t>( | |
| 34 request_sender_->GetNextRequestId())); | |
| 35 } | |
| 36 | |
| 37 // Starts an API request to the browser, with an optional callback. The | |
| 38 // callback will be dispatched to EventBindings::HandleResponse. | |
| 39 void SendRequestNatives::StartRequest( | |
| 40 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 41 CHECK_EQ(6, args.Length()); | |
| 42 std::string name = *v8::String::Utf8Value(args[0]); | |
| 43 int request_id = args[2]->Int32Value(); | |
| 44 bool has_callback = args[3]->BooleanValue(); | |
| 45 bool for_io_thread = args[4]->BooleanValue(); | |
| 46 bool preserve_null_in_objects = args[5]->BooleanValue(); | |
| 47 | |
| 48 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 49 | |
| 50 // See http://crbug.com/149880. The context menus APIs relies on this, but | |
| 51 // we shouldn't really be doing it (e.g. for the sake of the storage API). | |
| 52 converter->SetFunctionAllowed(true); | |
| 53 | |
| 54 if (!preserve_null_in_objects) | |
| 55 converter->SetStripNullFromObjects(true); | |
| 56 | |
| 57 scoped_ptr<base::Value> value_args( | |
| 58 converter->FromV8Value(args[1], context()->v8_context())); | |
| 59 if (!value_args.get() || !value_args->IsType(base::Value::TYPE_LIST)) { | |
| 60 NOTREACHED() << "Unable to convert args passed to StartRequest"; | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 request_sender_->StartRequest( | |
| 65 context(), name, request_id, has_callback, for_io_thread, | |
| 66 static_cast<base::ListValue*>(value_args.get())); | |
| 67 } | |
| 68 | |
| 69 void SendRequestNatives::GetGlobal( | |
| 70 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 71 CHECK_EQ(1, args.Length()); | |
| 72 CHECK(args[0]->IsObject()); | |
| 73 args.GetReturnValue().Set( | |
| 74 v8::Handle<v8::Object>::Cast(args[0])->CreationContext()->Global()); | |
| 75 } | |
| 76 | |
| 77 } // namespace extensions | |
| OLD | NEW |