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() : npp_(new NPP_t) { | 180 CppBoundClass::CppBoundClass() |
181 WebBindings::registerObjectOwner(npp_.get()); | 181 : bound_to_frame_(false) { |
182 } | 182 } |
183 | 183 |
184 CppBoundClass::~CppBoundClass() { | 184 CppBoundClass::~CppBoundClass() { |
185 STLDeleteValues(&properties_); | 185 STLDeleteValues(&properties_); |
186 WebBindings::unregisterObjectOwner(npp_.get()); | 186 |
| 187 // Unregister ourselves if we were bound to a frame. |
| 188 if (bound_to_frame_) |
| 189 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_)); |
187 } | 190 } |
188 | 191 |
189 bool CppBoundClass::HasMethod(NPIdentifier ident) const { | 192 bool CppBoundClass::HasMethod(NPIdentifier ident) const { |
190 return (methods_.find(ident) != methods_.end()); | 193 return (methods_.find(ident) != methods_.end()); |
191 } | 194 } |
192 | 195 |
193 bool CppBoundClass::HasProperty(NPIdentifier ident) const { | 196 bool CppBoundClass::HasProperty(NPIdentifier ident) const { |
194 return (properties_.find(ident) != properties_.end()); | 197 return (properties_.find(ident) != properties_.end()); |
195 } | 198 } |
196 | 199 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 } | 293 } |
291 | 294 |
292 bool CppBoundClass::IsMethodRegistered(const std::string& name) const { | 295 bool CppBoundClass::IsMethodRegistered(const std::string& name) const { |
293 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); | 296 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); |
294 MethodList::const_iterator callback = methods_.find(ident); | 297 MethodList::const_iterator callback = methods_.find(ident); |
295 return (callback != methods_.end()); | 298 return (callback != methods_.end()); |
296 } | 299 } |
297 | 300 |
298 CppVariant* CppBoundClass::GetAsCppVariant() { | 301 CppVariant* CppBoundClass::GetAsCppVariant() { |
299 if (!self_variant_.isObject()) { | 302 if (!self_variant_.isObject()) { |
300 // Create an NPObject using our static NPClass. The first argument has type | 303 // Create an NPObject using our static NPClass. The first argument (a |
301 // NPP, but is only used to track object ownership, so passing this is fine. | 304 // plugin's instance handle) is passed through to the allocate function |
302 NPObject* np_obj = WebBindings::createObject( | 305 // directly, and we don't use it, so it's ok to be 0. |
303 npp_.get(), &CppNPObject::np_class_); | 306 NPObject* np_obj = WebBindings::createObject(0, &CppNPObject::np_class_); |
304 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); | 307 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); |
305 obj->bound_class = this; | 308 obj->bound_class = this; |
306 self_variant_.Set(np_obj); | 309 self_variant_.Set(np_obj); |
307 WebBindings::releaseObject(np_obj); // CppVariant takes the reference. | 310 WebBindings::releaseObject(np_obj); // CppVariant takes the reference. |
308 } | 311 } |
309 DCHECK(self_variant_.isObject()); | 312 DCHECK(self_variant_.isObject()); |
310 return &self_variant_; | 313 return &self_variant_; |
311 } | 314 } |
312 | 315 |
313 void CppBoundClass::BindToJavascript(WebFrame* frame, | 316 void CppBoundClass::BindToJavascript(WebFrame* frame, |
314 const std::string& classname) { | 317 const std::string& classname) { |
315 // BindToWindowObject will take its own reference to the NPObject, and clean | 318 // BindToWindowObject will take its own reference to the NPObject, and clean |
316 // up after itself. It will also (indirectly) register the object with V8, | 319 // up after itself. It will also (indirectly) register the object with V8, |
317 // against an owner pointer we supply, so we must register that as an owner, | 320 // so we must remember this so we can unregister it when we're destroyed. |
318 // and unregister when we teardown. | |
319 frame->bindToWindowObject(ASCIIToUTF16(classname), | 321 frame->bindToWindowObject(ASCIIToUTF16(classname), |
320 NPVARIANT_TO_OBJECT(*GetAsCppVariant())); | 322 NPVARIANT_TO_OBJECT(*GetAsCppVariant())); |
| 323 bound_to_frame_ = true; |
321 } | 324 } |
322 | 325 |
323 } // namespace webkit_glue | 326 } // namespace webkit_glue |
OLD | NEW |