| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/send_request_natives.h" | 5 #include "extensions/renderer/send_request_natives.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/timer/elapsed_timer.h" |
| 10 #include "content/public/child/v8_value_converter.h" | 12 #include "content/public/child/v8_value_converter.h" |
| 11 #include "extensions/renderer/request_sender.h" | 13 #include "extensions/renderer/request_sender.h" |
| 12 #include "extensions/renderer/script_context.h" | 14 #include "extensions/renderer/script_context.h" |
| 13 | 15 |
| 14 using content::V8ValueConverter; | 16 using content::V8ValueConverter; |
| 15 | 17 |
| 16 namespace extensions { | 18 namespace extensions { |
| 17 | 19 |
| 18 SendRequestNatives::SendRequestNatives(RequestSender* request_sender, | 20 SendRequestNatives::SendRequestNatives(RequestSender* request_sender, |
| 19 ScriptContext* context) | 21 ScriptContext* context) |
| 20 : ObjectBackedNativeHandler(context), request_sender_(request_sender) { | 22 : ObjectBackedNativeHandler(context), request_sender_(request_sender) { |
| 21 RouteFunction( | 23 RouteFunction( |
| 22 "StartRequest", | 24 "StartRequest", |
| 23 base::Bind(&SendRequestNatives::StartRequest, base::Unretained(this))); | 25 base::Bind(&SendRequestNatives::StartRequest, base::Unretained(this))); |
| 24 RouteFunction( | 26 RouteFunction( |
| 25 "GetGlobal", | 27 "GetGlobal", |
| 26 base::Bind(&SendRequestNatives::GetGlobal, base::Unretained(this))); | 28 base::Bind(&SendRequestNatives::GetGlobal, base::Unretained(this))); |
| 27 } | 29 } |
| 28 | 30 |
| 29 // Starts an API request to the browser, with an optional callback. The | 31 // Starts an API request to the browser, with an optional callback. The |
| 30 // callback will be dispatched to EventBindings::HandleResponse. | 32 // callback will be dispatched to EventBindings::HandleResponse. |
| 31 void SendRequestNatives::StartRequest( | 33 void SendRequestNatives::StartRequest( |
| 32 const v8::FunctionCallbackInfo<v8::Value>& args) { | 34 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 35 base::ElapsedTimer timer; |
| 33 CHECK_EQ(5, args.Length()); | 36 CHECK_EQ(5, args.Length()); |
| 34 std::string name = *v8::String::Utf8Value(args[0]); | 37 std::string name = *v8::String::Utf8Value(args[0]); |
| 35 bool has_callback = args[2]->BooleanValue(); | 38 bool has_callback = args[2]->BooleanValue(); |
| 36 bool for_io_thread = args[3]->BooleanValue(); | 39 bool for_io_thread = args[3]->BooleanValue(); |
| 37 bool preserve_null_in_objects = args[4]->BooleanValue(); | 40 bool preserve_null_in_objects = args[4]->BooleanValue(); |
| 38 | 41 |
| 39 int request_id = request_sender_->GetNextRequestId(); | 42 int request_id = request_sender_->GetNextRequestId(); |
| 40 args.GetReturnValue().Set(static_cast<int32_t>(request_id)); | 43 args.GetReturnValue().Set(static_cast<int32_t>(request_id)); |
| 41 | 44 |
| 42 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 45 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 43 | 46 |
| 44 // See http://crbug.com/149880. The context menus APIs relies on this, but | 47 // See http://crbug.com/149880. The context menus APIs relies on this, but |
| 45 // we shouldn't really be doing it (e.g. for the sake of the storage API). | 48 // we shouldn't really be doing it (e.g. for the sake of the storage API). |
| 46 converter->SetFunctionAllowed(true); | 49 converter->SetFunctionAllowed(true); |
| 47 | 50 |
| 48 if (!preserve_null_in_objects) | 51 if (!preserve_null_in_objects) |
| 49 converter->SetStripNullFromObjects(true); | 52 converter->SetStripNullFromObjects(true); |
| 50 | 53 |
| 51 std::unique_ptr<base::Value> value_args( | 54 std::unique_ptr<base::Value> value_args( |
| 52 converter->FromV8Value(args[1], context()->v8_context())); | 55 converter->FromV8Value(args[1], context()->v8_context())); |
| 53 if (!value_args.get() || !value_args->IsType(base::Value::TYPE_LIST)) { | 56 if (!value_args.get() || !value_args->IsType(base::Value::TYPE_LIST)) { |
| 54 NOTREACHED() << "Unable to convert args passed to StartRequest"; | 57 NOTREACHED() << "Unable to convert args passed to StartRequest"; |
| 55 return; | 58 return; |
| 56 } | 59 } |
| 57 | 60 |
| 58 request_sender_->StartRequest( | 61 if (request_sender_->StartRequest( |
| 59 context(), | 62 context(), |
| 60 name, | 63 name, |
| 61 request_id, | 64 request_id, |
| 62 has_callback, | 65 has_callback, |
| 63 for_io_thread, | 66 for_io_thread, |
| 64 static_cast<base::ListValue*>(value_args.get())); | 67 static_cast<base::ListValue*>(value_args.get()))) { |
| 68 // TODO(devlin): Would it be useful to partition this data based on |
| 69 // extension function once we have a suitable baseline? crbug.com/608561. |
| 70 UMA_HISTOGRAM_TIMES("Extensions.Functions.StartRequestElapsedTime", |
| 71 timer.Elapsed()); |
| 72 } |
| 65 } | 73 } |
| 66 | 74 |
| 67 void SendRequestNatives::GetGlobal( | 75 void SendRequestNatives::GetGlobal( |
| 68 const v8::FunctionCallbackInfo<v8::Value>& args) { | 76 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 69 CHECK_EQ(1, args.Length()); | 77 CHECK_EQ(1, args.Length()); |
| 70 CHECK(args[0]->IsObject()); | 78 CHECK(args[0]->IsObject()); |
| 71 v8::Local<v8::Context> v8_context = | 79 v8::Local<v8::Context> v8_context = |
| 72 v8::Local<v8::Object>::Cast(args[0])->CreationContext(); | 80 v8::Local<v8::Object>::Cast(args[0])->CreationContext(); |
| 73 if (ContextCanAccessObject(context()->v8_context(), v8_context->Global(), | 81 if (ContextCanAccessObject(context()->v8_context(), v8_context->Global(), |
| 74 false)) { | 82 false)) { |
| 75 args.GetReturnValue().Set(v8_context->Global()); | 83 args.GetReturnValue().Set(v8_context->Global()); |
| 76 } | 84 } |
| 77 } | 85 } |
| 78 | 86 |
| 79 } // namespace extensions | 87 } // namespace extensions |
| OLD | NEW |