| 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 // TODO(dcarney): Remove this when UnsafePersistent is removed. | 5 // TODO(dcarney): Remove this when UnsafePersistent is removed. |
| 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | 6 #define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR |
| 7 | 7 |
| 8 #include "chrome/renderer/extensions/object_backed_native_handler.h" | 8 #include "chrome/renderer/extensions/object_backed_native_handler.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 void ObjectBackedNativeHandler::Router( | 39 void ObjectBackedNativeHandler::Router( |
| 40 const v8::FunctionCallbackInfo<v8::Value>& args) { | 40 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 41 v8::HandleScope handle_scope(args.GetIsolate()); | 41 v8::HandleScope handle_scope(args.GetIsolate()); |
| 42 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); | 42 v8::Handle<v8::Object> data = args.Data().As<v8::Object>(); |
| 43 | 43 |
| 44 v8::Handle<v8::Value> handler_function_value = | 44 v8::Handle<v8::Value> handler_function_value = |
| 45 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); | 45 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); |
| 46 // See comment in header file for why we do this. | 46 // See comment in header file for why we do this. |
| 47 if (handler_function_value.IsEmpty() || | 47 if (handler_function_value.IsEmpty() || |
| 48 handler_function_value->IsUndefined()) { | 48 handler_function_value->IsUndefined()) { |
| 49 console::Error(v8::Context::GetCalling(), | 49 console::Error(args.GetIsolate()->GetCallingContext(), |
| 50 "Extension view no longer exists"); | 50 "Extension view no longer exists"); |
| 51 return; | 51 return; |
| 52 } | 52 } |
| 53 DCHECK(handler_function_value->IsExternal()); | 53 DCHECK(handler_function_value->IsExternal()); |
| 54 static_cast<HandlerFunction*>( | 54 static_cast<HandlerFunction*>( |
| 55 handler_function_value.As<v8::External>()->Value())->Run(args); | 55 handler_function_value.As<v8::External>()->Value())->Run(args); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void ObjectBackedNativeHandler::RouteFunction( | 58 void ObjectBackedNativeHandler::RouteFunction( |
| 59 const std::string& name, | 59 const std::string& name, |
| 60 const HandlerFunction& handler_function) { | 60 const HandlerFunction& handler_function) { |
| 61 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 61 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 62 v8::HandleScope handle_scope(isolate); | 62 v8::HandleScope handle_scope(isolate); |
| 63 v8::Context::Scope context_scope(context_->v8_context()); | 63 v8::Context::Scope context_scope(context_->v8_context()); |
| 64 | 64 |
| 65 v8::Persistent<v8::Object> data(isolate, v8::Object::New()); | 65 v8::Persistent<v8::Object> data(isolate, v8::Object::New()); |
| 66 v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data); | 66 v8::Local<v8::Object> local_data = v8::Local<v8::Object>::New(isolate, data); |
| 67 local_data->Set( | 67 local_data->Set( |
| 68 v8::String::NewFromUtf8(isolate, kHandlerFunction), | 68 v8::String::NewFromUtf8(isolate, kHandlerFunction), |
| 69 v8::External::New(isolate, new HandlerFunction(handler_function))); | 69 v8::External::New(isolate, new HandlerFunction(handler_function))); |
| 70 v8::Handle<v8::FunctionTemplate> function_template = | 70 v8::Handle<v8::FunctionTemplate> function_template = |
| 71 v8::FunctionTemplate::New(Router, local_data); | 71 v8::FunctionTemplate::New(Router, local_data); |
| 72 object_template_.NewHandle(isolate)->Set(name.c_str(), function_template); | 72 object_template_.NewHandle(isolate) |
| 73 ->Set(isolate, name.c_str(), function_template); |
| 73 router_data_.push_back(UnsafePersistent<v8::Object>(&data)); | 74 router_data_.push_back(UnsafePersistent<v8::Object>(&data)); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void ObjectBackedNativeHandler::Invalidate() { | 77 void ObjectBackedNativeHandler::Invalidate() { |
| 77 if (!is_valid()) | 78 if (!is_valid()) |
| 78 return; | 79 return; |
| 79 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 80 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 80 v8::HandleScope handle_scope(isolate); | 81 v8::HandleScope handle_scope(isolate); |
| 81 v8::Context::Scope context_scope(context_->v8_context()); | 82 v8::Context::Scope context_scope(context_->v8_context()); |
| 82 | 83 |
| 83 for (RouterData::iterator it = router_data_.begin(); | 84 for (RouterData::iterator it = router_data_.begin(); |
| 84 it != router_data_.end(); ++it) { | 85 it != router_data_.end(); ++it) { |
| 85 v8::Handle<v8::Object> data = it->newLocal(isolate); | 86 v8::Handle<v8::Object> data = it->newLocal(isolate); |
| 86 v8::Handle<v8::Value> handler_function_value = | 87 v8::Handle<v8::Value> handler_function_value = |
| 87 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 88 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 88 CHECK(!handler_function_value.IsEmpty()); | 89 CHECK(!handler_function_value.IsEmpty()); |
| 89 delete static_cast<HandlerFunction*>( | 90 delete static_cast<HandlerFunction*>( |
| 90 handler_function_value.As<v8::External>()->Value()); | 91 handler_function_value.As<v8::External>()->Value()); |
| 91 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 92 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 92 it->dispose(); | 93 it->dispose(); |
| 93 } | 94 } |
| 94 object_template_.reset(); | 95 object_template_.reset(); |
| 95 context_ = NULL; | 96 context_ = NULL; |
| 96 NativeHandler::Invalidate(); | 97 NativeHandler::Invalidate(); |
| 97 } | 98 } |
| 98 | 99 |
| 99 } // namespace extensions | 100 } // namespace extensions |
| OLD | NEW |