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