Chromium Code Reviews| 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/object_backed_native_handler.h" | 5 #include "chrome/renderer/extensions/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 "chrome/renderer/extensions/chrome_v8_context.h" | |
| 9 #include "chrome/renderer/extensions/console.h" | 10 #include "chrome/renderer/extensions/console.h" |
| 10 #include "chrome/renderer/extensions/module_system.h" | 11 #include "chrome/renderer/extensions/module_system.h" |
| 11 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 // Key for the base::Bound routed function. | 17 // Key for the base::Bound routed function. |
| 17 const char* kHandlerFunction = "handler_function"; | 18 const char* kHandlerFunction = "handler_function"; |
| 18 } // namespace | 19 } // namespace |
| 19 | 20 |
| 20 ObjectBackedNativeHandler::ObjectBackedNativeHandler( | 21 ObjectBackedNativeHandler::ObjectBackedNativeHandler( |
| 21 v8::Handle<v8::Context> context) | 22 ChromeV8Context* context) |
| 22 : v8_context_(context), | 23 : context_(context), |
| 23 object_template_(v8::ObjectTemplate::New()) { | 24 object_template_(v8::ObjectTemplate::New()) { |
| 24 } | 25 } |
| 25 | 26 |
| 26 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { | 27 ObjectBackedNativeHandler::~ObjectBackedNativeHandler() { |
| 27 Invalidate(); | 28 Invalidate(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { | 31 v8::Handle<v8::Object> ObjectBackedNativeHandler::NewInstance() { |
| 31 return object_template_->NewInstance(); | 32 return object_template_->NewInstance(); |
| 32 } | 33 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 48 } | 49 } |
| 49 DCHECK(handler_function_value->IsExternal()); | 50 DCHECK(handler_function_value->IsExternal()); |
| 50 return handle_scope.Close(static_cast<HandlerFunction*>( | 51 return handle_scope.Close(static_cast<HandlerFunction*>( |
| 51 handler_function_value.As<v8::External>()->Value())->Run(args)); | 52 handler_function_value.As<v8::External>()->Value())->Run(args)); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void ObjectBackedNativeHandler::RouteFunction( | 55 void ObjectBackedNativeHandler::RouteFunction( |
| 55 const std::string& name, | 56 const std::string& name, |
| 56 const HandlerFunction& handler_function) { | 57 const HandlerFunction& handler_function) { |
| 57 v8::HandleScope handle_scope; | 58 v8::HandleScope handle_scope; |
| 58 v8::Context::Scope context_scope(v8_context_.get()); | 59 v8::Context::Scope context_scope(context_->v8_context()); |
| 59 | 60 |
| 60 v8::Persistent<v8::Object> data(v8_context_->GetIsolate(), v8::Object::New()); | 61 v8::Persistent<v8::Object> data(context_->v8_context()->GetIsolate(), |
| 62 v8::Object::New()); | |
| 61 data->Set(v8::String::New(kHandlerFunction), | 63 data->Set(v8::String::New(kHandlerFunction), |
| 62 v8::External::New(new HandlerFunction(handler_function))); | 64 v8::External::New(new HandlerFunction(handler_function))); |
| 63 router_data_.push_back(data); | 65 router_data_.push_back(data); |
| 64 v8::Handle<v8::FunctionTemplate> function_template = | 66 v8::Handle<v8::FunctionTemplate> function_template = |
| 65 v8::FunctionTemplate::New(Router, data); | 67 v8::FunctionTemplate::New(Router, data); |
| 66 object_template_->Set(name.c_str(), function_template); | 68 object_template_->Set(name.c_str(), function_template); |
| 67 } | 69 } |
| 68 | 70 |
| 71 v8::Handle<v8::Context> ObjectBackedNativeHandler::v8_context() { | |
| 72 return context_->v8_context(); | |
| 73 } | |
| 74 | |
| 69 void ObjectBackedNativeHandler::Invalidate() { | 75 void ObjectBackedNativeHandler::Invalidate() { |
| 70 if (!is_valid()) | 76 if (!is_valid()) |
| 71 return; | 77 return; |
| 72 v8::HandleScope handle_scope; | 78 v8::HandleScope handle_scope; |
| 73 v8::Context::Scope context_scope(v8_context_.get()); | 79 v8::Context::Scope context_scope(context_->v8_context()); |
| 74 | 80 |
| 75 for (RouterData::iterator it = router_data_.begin(); | 81 for (RouterData::iterator it = router_data_.begin(); |
| 76 it != router_data_.end(); ++it) { | 82 it != router_data_.end(); ++it) { |
| 77 v8::Persistent<v8::Object> data = *it; | 83 v8::Persistent<v8::Object> data = *it; |
| 78 v8::Handle<v8::Value> handler_function_value = | 84 v8::Handle<v8::Value> handler_function_value = |
| 79 data->Get(v8::String::New(kHandlerFunction)); | 85 data->Get(v8::String::New(kHandlerFunction)); |
| 80 CHECK(!handler_function_value.IsEmpty()); | 86 CHECK(!handler_function_value.IsEmpty()); |
| 81 delete static_cast<HandlerFunction*>( | 87 delete static_cast<HandlerFunction*>( |
| 82 handler_function_value.As<v8::External>()->Value()); | 88 handler_function_value.As<v8::External>()->Value()); |
| 83 data->Delete(v8::String::New(kHandlerFunction)); | 89 data->Delete(v8::String::New(kHandlerFunction)); |
| 84 data.Dispose(v8_context_->GetIsolate()); | 90 data.Dispose(context_->v8_context()->GetIsolate()); |
| 85 } | 91 } |
| 86 object_template_.reset(); | 92 object_template_.reset(); |
| 87 v8_context_.reset(); | |
| 88 NativeHandler::Invalidate(); | 93 NativeHandler::Invalidate(); |
| 94 context_ = NULL; | |
|
not at google - send to devlin
2013/05/29 15:55:13
can this be moved back above the call to NativeHan
marja
2013/05/29 16:56:33
Done.
| |
| 89 } | 95 } |
| 90 | 96 |
| 91 } // extensions | 97 } // extensions |
| OLD | NEW |