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