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 // This file contains definitions for CppBoundClass | 5 // This file contains definitions for CppBoundClass |
| 6 | 6 |
| 7 // Here's the control flow of a JS method getting forwarded to a class. | 7 // Here's the control flow of a JS method getting forwarded to a class. |
| 8 // - Something calls our NPObject with a function like "Invoke". | 8 // - Something calls our NPObject with a function like "Invoke". |
| 9 // - CppNPObject's static invoke() function forwards it to its attached | 9 // - CppNPObject's static invoke() function forwards it to its attached |
| 10 // CppBoundClass's Invoke() method. | 10 // CppBoundClass's Invoke() method. |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 return obj->bound_class->GetProperty(ident, result); | 170 return obj->bound_class->GetProperty(ident, result); |
| 171 } | 171 } |
| 172 | 172 |
| 173 /* static */ bool CppNPObject::setProperty(NPObject* np_obj, | 173 /* static */ bool CppNPObject::setProperty(NPObject* np_obj, |
| 174 NPIdentifier ident, | 174 NPIdentifier ident, |
| 175 const NPVariant* value) { | 175 const NPVariant* value) { |
| 176 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); | 176 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); |
| 177 return obj->bound_class->SetProperty(ident, value); | 177 return obj->bound_class->SetProperty(ident, value); |
| 178 } | 178 } |
| 179 | 179 |
| 180 CppBoundClass::CppBoundClass() | 180 CppBoundClass::CppBoundClass() : npp_(new NPP_t) { |
| 181 : bound_to_frame_(false) { | 181 WebBindings::registerObjectOwner(npp_.get()); |
| 182 } | 182 } |
| 183 | 183 |
| 184 CppBoundClass::~CppBoundClass() { | 184 CppBoundClass::~CppBoundClass() { |
| 185 STLDeleteValues(&properties_); | 185 STLDeleteValues(&properties_); |
| 186 | 186 WebBindings::unregisterObjectOwner(npp_.get()); |
| 187 // Unregister ourselves if we were bound to a frame. | |
| 188 if (bound_to_frame_) | |
| 189 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_)); | |
| 190 } | 187 } |
| 191 | 188 |
| 192 bool CppBoundClass::HasMethod(NPIdentifier ident) const { | 189 bool CppBoundClass::HasMethod(NPIdentifier ident) const { |
| 193 return (methods_.find(ident) != methods_.end()); | 190 return (methods_.find(ident) != methods_.end()); |
| 194 } | 191 } |
| 195 | 192 |
| 196 bool CppBoundClass::HasProperty(NPIdentifier ident) const { | 193 bool CppBoundClass::HasProperty(NPIdentifier ident) const { |
| 197 return (properties_.find(ident) != properties_.end()); | 194 return (properties_.find(ident) != properties_.end()); |
| 198 } | 195 } |
| 199 | 196 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 } | 290 } |
| 294 | 291 |
| 295 bool CppBoundClass::IsMethodRegistered(const std::string& name) const { | 292 bool CppBoundClass::IsMethodRegistered(const std::string& name) const { |
| 296 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); | 293 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); |
| 297 MethodList::const_iterator callback = methods_.find(ident); | 294 MethodList::const_iterator callback = methods_.find(ident); |
| 298 return (callback != methods_.end()); | 295 return (callback != methods_.end()); |
| 299 } | 296 } |
| 300 | 297 |
| 301 CppVariant* CppBoundClass::GetAsCppVariant() { | 298 CppVariant* CppBoundClass::GetAsCppVariant() { |
| 302 if (!self_variant_.isObject()) { | 299 if (!self_variant_.isObject()) { |
| 303 // Create an NPObject using our static NPClass. The first argument (a | 300 // Create an NPObject using our static NPClass. The first argument has type |
| 304 // plugin's instance handle) is passed through to the allocate function | 301 // NPP, but is only used to track object ownership, so passing this is fine. |
| 305 // directly, and we don't use it, so it's ok to be 0. | 302 NPObject* np_obj = WebBindings::createObject( |
| 306 NPObject* np_obj = WebBindings::createObject(0, &CppNPObject::np_class_); | 303 reinterpret_cast<NPP>(this), &CppNPObject::np_class_); |
|
jamesr
2013/05/21 18:20:21
why isn't this returning npp_.get() ?
Wez
2013/05/22 06:55:25
Done.
| |
| 307 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); | 304 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); |
| 308 obj->bound_class = this; | 305 obj->bound_class = this; |
| 309 self_variant_.Set(np_obj); | 306 self_variant_.Set(np_obj); |
| 310 WebBindings::releaseObject(np_obj); // CppVariant takes the reference. | 307 WebBindings::releaseObject(np_obj); // CppVariant takes the reference. |
| 311 } | 308 } |
| 312 DCHECK(self_variant_.isObject()); | 309 DCHECK(self_variant_.isObject()); |
| 313 return &self_variant_; | 310 return &self_variant_; |
| 314 } | 311 } |
| 315 | 312 |
| 316 void CppBoundClass::BindToJavascript(WebFrame* frame, | 313 void CppBoundClass::BindToJavascript(WebFrame* frame, |
| 317 const std::string& classname) { | 314 const std::string& classname) { |
| 318 // BindToWindowObject will take its own reference to the NPObject, and clean | 315 // BindToWindowObject will take its own reference to the NPObject, and clean |
| 319 // up after itself. It will also (indirectly) register the object with V8, | 316 // up after itself. It will also (indirectly) register the object with V8, |
| 320 // so we must remember this so we can unregister it when we're destroyed. | 317 // against an owner pointer we supply, so we must register that as an owner, |
| 318 // and unregister when we teardown. | |
| 321 frame->bindToWindowObject(ASCIIToUTF16(classname), | 319 frame->bindToWindowObject(ASCIIToUTF16(classname), |
| 322 NPVARIANT_TO_OBJECT(*GetAsCppVariant())); | 320 NPVARIANT_TO_OBJECT(*GetAsCppVariant()), |
| 323 bound_to_frame_ = true; | 321 npp_.get()); |
| 324 } | 322 } |
| 325 | 323 |
| 326 } // namespace webkit_glue | 324 } // namespace webkit_glue |
| OLD | NEW |