| 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 23 matching lines...) Expand all Loading... |
| 34 ->NewInstance(); | 34 ->NewInstance(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // static | 37 // static |
| 38 void ObjectBackedNativeHandler::Router( | 38 void ObjectBackedNativeHandler::Router( |
| 39 const v8::FunctionCallbackInfo<v8::Value>& args) { | 39 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 40 v8::HandleScope handle_scope(args.GetIsolate()); | 40 v8::HandleScope handle_scope(args.GetIsolate()); |
| 41 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); | 41 v8::Local<v8::Object> data = args.Data().As<v8::Object>(); |
| 42 | 42 |
| 43 v8::Local<v8::Value> handler_function_value = | 43 v8::Local<v8::Value> handler_function_value = |
| 44 data->Get(v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); | 44 data->GetHiddenValue( |
| 45 v8::String::NewFromUtf8(args.GetIsolate(), kHandlerFunction)); |
| 45 // See comment in header file for why we do this. | 46 // See comment in header file for why we do this. |
| 46 if (handler_function_value.IsEmpty() || | 47 if (handler_function_value.IsEmpty() || |
| 47 handler_function_value->IsUndefined()) { | 48 handler_function_value->IsUndefined()) { |
| 48 ScriptContext* script_context = ScriptContextSet::GetContextByV8Context( | 49 ScriptContext* script_context = ScriptContextSet::GetContextByV8Context( |
| 49 args.GetIsolate()->GetCurrentContext()); | 50 args.GetIsolate()->GetCurrentContext()); |
| 50 console::Error(script_context ? script_context->GetRenderFrame() : nullptr, | 51 console::Error(script_context ? script_context->GetRenderFrame() : nullptr, |
| 51 "Extension view no longer exists"); | 52 "Extension view no longer exists"); |
| 52 return; | 53 return; |
| 53 } | 54 } |
| 54 DCHECK(handler_function_value->IsExternal()); | 55 // This CHECK is *important*. Otherwise, we'll go around happily executing |
| 56 // something random. See crbug.com/548273. |
| 57 CHECK(handler_function_value->IsExternal()); |
| 55 static_cast<HandlerFunction*>( | 58 static_cast<HandlerFunction*>( |
| 56 handler_function_value.As<v8::External>()->Value())->Run(args); | 59 handler_function_value.As<v8::External>()->Value())->Run(args); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void ObjectBackedNativeHandler::RouteFunction( | 62 void ObjectBackedNativeHandler::RouteFunction( |
| 60 const std::string& name, | 63 const std::string& name, |
| 61 const HandlerFunction& handler_function) { | 64 const HandlerFunction& handler_function) { |
| 62 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 65 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 63 v8::HandleScope handle_scope(isolate); | 66 v8::HandleScope handle_scope(isolate); |
| 64 v8::Context::Scope context_scope(context_->v8_context()); | 67 v8::Context::Scope context_scope(context_->v8_context()); |
| 65 | 68 |
| 66 v8::Local<v8::Object> data = v8::Object::New(isolate); | 69 v8::Local<v8::Object> data = v8::Object::New(isolate); |
| 67 data->Set( | 70 data->SetHiddenValue( |
| 68 v8::String::NewFromUtf8(isolate, kHandlerFunction), | 71 v8::String::NewFromUtf8(isolate, kHandlerFunction), |
| 69 v8::External::New(isolate, new HandlerFunction(handler_function))); | 72 v8::External::New(isolate, new HandlerFunction(handler_function))); |
| 70 v8::Local<v8::FunctionTemplate> function_template = | 73 v8::Local<v8::FunctionTemplate> function_template = |
| 71 v8::FunctionTemplate::New(isolate, Router, data); | 74 v8::FunctionTemplate::New(isolate, Router, data); |
| 72 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) | 75 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_) |
| 73 ->Set(isolate, name.c_str(), function_template); | 76 ->Set(isolate, name.c_str(), function_template); |
| 74 router_data_.Append(data); | 77 router_data_.Append(data); |
| 75 } | 78 } |
| 76 | 79 |
| 77 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { | 80 v8::Isolate* ObjectBackedNativeHandler::GetIsolate() const { |
| 78 return context_->isolate(); | 81 return context_->isolate(); |
| 79 } | 82 } |
| 80 | 83 |
| 81 void ObjectBackedNativeHandler::Invalidate() { | 84 void ObjectBackedNativeHandler::Invalidate() { |
| 82 v8::Isolate* isolate = GetIsolate(); | 85 v8::Isolate* isolate = GetIsolate(); |
| 83 v8::HandleScope handle_scope(isolate); | 86 v8::HandleScope handle_scope(isolate); |
| 84 v8::Context::Scope context_scope(context_->v8_context()); | 87 v8::Context::Scope context_scope(context_->v8_context()); |
| 85 | 88 |
| 86 for (size_t i = 0; i < router_data_.Size(); i++) { | 89 for (size_t i = 0; i < router_data_.Size(); i++) { |
| 87 v8::Local<v8::Object> data = router_data_.Get(i); | 90 v8::Local<v8::Object> data = router_data_.Get(i); |
| 88 v8::Local<v8::Value> handler_function_value = | 91 v8::Local<v8::Value> handler_function_value = |
| 89 data->Get(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 92 data->GetHiddenValue( |
| 93 v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 90 CHECK(!handler_function_value.IsEmpty()); | 94 CHECK(!handler_function_value.IsEmpty()); |
| 91 delete static_cast<HandlerFunction*>( | 95 delete static_cast<HandlerFunction*>( |
| 92 handler_function_value.As<v8::External>()->Value()); | 96 handler_function_value.As<v8::External>()->Value()); |
| 93 data->Delete(v8::String::NewFromUtf8(isolate, kHandlerFunction)); | 97 data->DeleteHiddenValue(v8::String::NewFromUtf8(isolate, kHandlerFunction)); |
| 94 } | 98 } |
| 95 | 99 |
| 96 router_data_.Clear(); | 100 router_data_.Clear(); |
| 97 object_template_.Reset(); | 101 object_template_.Reset(); |
| 98 | 102 |
| 99 NativeHandler::Invalidate(); | 103 NativeHandler::Invalidate(); |
| 100 } | 104 } |
| 101 | 105 |
| 102 } // namespace extensions | 106 } // namespace extensions |
| OLD | NEW |