| 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/object_backed_native_handler.h" | 5 #include "extensions/renderer/object_backed_native_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
| 9 #include "extensions/renderer/console.h" | 9 #include "extensions/renderer/console.h" |
| 10 #include "extensions/renderer/module_system.h" | 10 #include "extensions/renderer/module_system.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) | 21 ObjectBackedNativeHandler::ObjectBackedNativeHandler(ScriptContext* context) |
| 22 : router_data_(context->isolate()), | 22 : router_data_(context->isolate()), |
| 23 context_(context), | 23 context_(context), |
| 24 object_template_(context->isolate(), | 24 object_template_(context->isolate(), |
| 25 v8::ObjectTemplate::New(context->isolate())) { | 25 v8::ObjectTemplate::New(context->isolate())) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 28 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
| 29 } | 29 } |
| 30 | 30 |
| 31 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 31 v8::Local<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
| 32 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) | 32 return v8::Local<v8::ObjectTemplate>::New(GetIsolate(), object_template_) |
| 33 ->NewInstance(); | 33 ->NewInstance(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // static | 36 // static |
| 37 void ObjectBackedNativeHandler::Router( | 37 void ObjectBackedNativeHandler::Router( |
| 38 const v8::FunctionCallbackInfo<v8::Value>& args) { | 38 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 39 v8::HandleScope handle_scope(args.GetIsolate()); | 39 v8::HandleScope handle_scope(args.GetIsolate()); |
| 40 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); | 40 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); |
| 41 | 41 |
| 42 v8::Handle<v8::Value> handler_function_value = | 42 v8::Local<v8::Value> handler_function_value = |
| 43 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); | 43 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); |
| 44 // See comment in header file for why we do this. | 44 // See comment in header file for why we do this. |
| 45 if (handler_function_value.IsEmpty() || | 45 if (handler_function_value.IsEmpty() || |
| 46 handler_function_value->IsUndefined()) { | 46 handler_function_value->IsUndefined()) { |
| 47 console::Error(args.GetIsolate()->GetCallingContext(), | 47 console::Error(args.GetIsolate()->GetCallingContext(), |
| 48 "Extension view no longer exists"); | 48 "Extension view no longer exists"); |
| 49 return; | 49 return; |
| 50 } | 50 } |
| 51 DCHECK(handler_function_value->IsExternal()); | 51 DCHECK(handler_function_value->IsExternal()); |
| 52 static_cast<HandlerFunction*>( | 52 static_cast<HandlerFunction*>( |
| 53 handler_function_value.As<v8::External>()->Value())->Run(args); | 53 handler_function_value.As<v8::External>()->Value())->Run(args); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void ObjectBackedNativeHandler::RouteFunction( | 56 void ObjectBackedNativeHandler::RouteFunction( |
| 57 const std::string& name, | 57 const std::string& name, |
| 58 const HandlerFunction& handler_function) { | 58 const HandlerFunction& handler_function) { |
| 59 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 59 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 60 v8::HandleScope handle_scope(isolate); | 60 v8::HandleScope handle_scope(isolate); |
| 61 v8::Context::Scope context_scope(context_->v8_context()); | 61 v8::Context::Scope context_scope(context_->v8_context()); |
| 62 | 62 |
| 63 v8::Local<v8::Object> data = v8::Object::New(isolate); | 63 v8::Local<v8::Object> data = v8::Object::New(isolate); |
| 64 data->Set( | 64 data->Set( |
| 65 v8::String::NewFromUtf8(isolate, kHandlerFunction), | 65 v8::String::NewFromUtf8(isolate, kHandlerFunction), |
| 66 v8::External::New(isolate, new HandlerFunction(handler_function))); | 66 v8::External::New(isolate, new HandlerFunction(handler_function))); |
| 67 v8::Handle<v8::FunctionTemplate> function_template = | 67 v8::Local<v8::FunctionTemplate> function_template = |
| 68 v8::FunctionTemplate::New(isolate, Router, data); | 68 v8::FunctionTemplate::New(isolate, Router, data); |
| 69 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) | 69 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) |
| 70 ->Set(isolate, name.c_str(), function_template); | 70 ->Set(isolate, name.c_str(), function_template); |
| 71 router_data_.Append(data); | 71 router_data_.Append(data); |
| 72 } | 72 } |
| 73 | 73 |
| 74 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { | 74 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { |
| 75 return context_->isolate(); | 75 return context_->isolate(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void ObjectBackedNativeHandler::Invalidate() { | 78 void ObjectBackedNativeHandler::Invalidate() { |
| 79 v8::Isolate* isolate = GetIsolate(); | 79 v8::Isolate* isolate = GetIsolate(); |
| 80 v8::HandleScope handle_scope(isolate); | 80 v8::HandleScope handle_scope(isolate); |
| 81 v8::Context::Scope context_scope(context_->v8_context()); | 81 v8::Context::Scope context_scope(context_->v8_context()); |
| 82 | 82 |
| 83 for (size_t i = 0; i < router_data_.Size(); i++) { | 83 for (size_t i = 0; i < router_data_.Size(); i++) { |
| 84 v8::Handle<v8::Object> data = router_data_.Get(i); | 84 v8::Local<v8::Object> data = router_data_.Get(i); |
| 85 v8::Handle<v8::Value> handler_function_value = | 85 v8::Local<v8::Value> handler_function_value = |
| 86 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 86 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 87 CHECK(!handler_function_value.IsEmpty()); | 87 CHECK(!handler_function_value.IsEmpty()); |
| 88 delete static_cast<HandlerFunction*>( | 88 delete static_cast<HandlerFunction*>( |
| 89 handler_function_value.As<v8::External>()->Value()); | 89 handler_function_value.As<v8::External>()->Value()); |
| 90 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 90 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 router_data_.Clear(); | 93 router_data_.Clear(); |
| 94 object_template_.Reset(); | 94 object_template_.Reset(); |
| 95 | 95 |
| 96 NativeHandler::Invalidate(); | 96 NativeHandler::Invalidate(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 } // namespace extensions | 99 } // namespace extensions |
| OLD | NEW |