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 "extensions/renderer/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 "extensions/renderer/request_sender.h" | 9 #include "extensions/renderer/request_sender.h" |
| 10 #include "extensions/renderer/script_context.h" |
10 | 11 |
11 using content::V8ValueConverter; | 12 using content::V8ValueConverter; |
12 | 13 |
13 namespace extensions { | 14 namespace extensions { |
14 | 15 |
15 SendRequestNatives::SendRequestNatives(Dispatcher* dispatcher, | 16 SendRequestNatives::SendRequestNatives(RequestSender* request_sender, |
16 RequestSender* request_sender, | 17 ScriptContext* context) |
17 ChromeV8Context* context) | 18 : ObjectBackedNativeHandler(context), request_sender_(request_sender) { |
18 : ChromeV8Extension(dispatcher, context), | |
19 request_sender_(request_sender) { | |
20 RouteFunction("GetNextRequestId", | 19 RouteFunction("GetNextRequestId", |
21 base::Bind(&SendRequestNatives::GetNextRequestId, | 20 base::Bind(&SendRequestNatives::GetNextRequestId, |
22 base::Unretained(this))); | 21 base::Unretained(this))); |
23 RouteFunction("StartRequest", | 22 RouteFunction( |
24 base::Bind(&SendRequestNatives::StartRequest, | 23 "StartRequest", |
25 base::Unretained(this))); | 24 base::Bind(&SendRequestNatives::StartRequest, base::Unretained(this))); |
26 RouteFunction("GetGlobal", | 25 RouteFunction( |
27 base::Bind(&SendRequestNatives::GetGlobal, | 26 "GetGlobal", |
28 base::Unretained(this))); | 27 base::Bind(&SendRequestNatives::GetGlobal, base::Unretained(this))); |
29 } | 28 } |
30 | 29 |
31 void SendRequestNatives::GetNextRequestId( | 30 void SendRequestNatives::GetNextRequestId( |
32 const v8::FunctionCallbackInfo<v8::Value>& args) { | 31 const v8::FunctionCallbackInfo<v8::Value>& args) { |
33 args.GetReturnValue().Set(static_cast<int32_t>( | 32 args.GetReturnValue().Set( |
34 request_sender_->GetNextRequestId())); | 33 static_cast<int32_t>(request_sender_->GetNextRequestId())); |
35 } | 34 } |
36 | 35 |
37 // Starts an API request to the browser, with an optional callback. The | 36 // Starts an API request to the browser, with an optional callback. The |
38 // callback will be dispatched to EventBindings::HandleResponse. | 37 // callback will be dispatched to EventBindings::HandleResponse. |
39 void SendRequestNatives::StartRequest( | 38 void SendRequestNatives::StartRequest( |
40 const v8::FunctionCallbackInfo<v8::Value>& args) { | 39 const v8::FunctionCallbackInfo<v8::Value>& args) { |
41 CHECK_EQ(6, args.Length()); | 40 CHECK_EQ(6, args.Length()); |
42 std::string name = *v8::String::Utf8Value(args[0]); | 41 std::string name = *v8::String::Utf8Value(args[0]); |
43 int request_id = args[2]->Int32Value(); | 42 int request_id = args[2]->Int32Value(); |
44 bool has_callback = args[3]->BooleanValue(); | 43 bool has_callback = args[3]->BooleanValue(); |
(...skipping 10 matching lines...) Expand all Loading... |
55 converter->SetStripNullFromObjects(true); | 54 converter->SetStripNullFromObjects(true); |
56 | 55 |
57 scoped_ptr<base::Value> value_args( | 56 scoped_ptr<base::Value> value_args( |
58 converter->FromV8Value(args[1], context()->v8_context())); | 57 converter->FromV8Value(args[1], context()->v8_context())); |
59 if (!value_args.get() || !value_args->IsType(base::Value::TYPE_LIST)) { | 58 if (!value_args.get() || !value_args->IsType(base::Value::TYPE_LIST)) { |
60 NOTREACHED() << "Unable to convert args passed to StartRequest"; | 59 NOTREACHED() << "Unable to convert args passed to StartRequest"; |
61 return; | 60 return; |
62 } | 61 } |
63 | 62 |
64 request_sender_->StartRequest( | 63 request_sender_->StartRequest( |
65 context(), name, request_id, has_callback, for_io_thread, | 64 context(), |
| 65 name, |
| 66 request_id, |
| 67 has_callback, |
| 68 for_io_thread, |
66 static_cast<base::ListValue*>(value_args.get())); | 69 static_cast<base::ListValue*>(value_args.get())); |
67 } | 70 } |
68 | 71 |
69 void SendRequestNatives::GetGlobal( | 72 void SendRequestNatives::GetGlobal( |
70 const v8::FunctionCallbackInfo<v8::Value>& args) { | 73 const v8::FunctionCallbackInfo<v8::Value>& args) { |
71 CHECK_EQ(1, args.Length()); | 74 CHECK_EQ(1, args.Length()); |
72 CHECK(args[0]->IsObject()); | 75 CHECK(args[0]->IsObject()); |
73 args.GetReturnValue().Set( | 76 args.GetReturnValue().Set( |
74 v8::Handle<v8::Object>::Cast(args[0])->CreationContext()->Global()); | 77 v8::Handle<v8::Object>::Cast(args[0])->CreationContext()->Global()); |
75 } | 78 } |
76 | 79 |
77 } // namespace extensions | 80 } // namespace extensions |
OLD | NEW |