Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: webkit/glue/cpp_bound_class.cc

Issue 15941006: Track NPObject ownership by the originating plugins' NPP identifier. [2/3] (Chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix CppBoundClass. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/glue/cpp_bound_class.h ('k') | webkit/plugins/npapi/webplugin_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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() : bound_to_frame_(false), 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
187 // Unregister ourselves if we were bound to a frame. 187 // TODO(wez): Remove once crrev.com/14019005 lands.
188 if (bound_to_frame_) 188 if (bound_to_frame_)
189 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_)); 189 WebBindings::unregisterObject(NPVARIANT_TO_OBJECT(self_variant_));
190
191 WebBindings::unregisterObjectOwner(npp_.get());
190 } 192 }
191 193
192 bool CppBoundClass::HasMethod(NPIdentifier ident) const { 194 bool CppBoundClass::HasMethod(NPIdentifier ident) const {
193 return (methods_.find(ident) != methods_.end()); 195 return (methods_.find(ident) != methods_.end());
194 } 196 }
195 197
196 bool CppBoundClass::HasProperty(NPIdentifier ident) const { 198 bool CppBoundClass::HasProperty(NPIdentifier ident) const {
197 return (properties_.find(ident) != properties_.end()); 199 return (properties_.find(ident) != properties_.end());
198 } 200 }
199 201
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 295 }
294 296
295 bool CppBoundClass::IsMethodRegistered(const std::string& name) const { 297 bool CppBoundClass::IsMethodRegistered(const std::string& name) const {
296 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str()); 298 NPIdentifier ident = WebBindings::getStringIdentifier(name.c_str());
297 MethodList::const_iterator callback = methods_.find(ident); 299 MethodList::const_iterator callback = methods_.find(ident);
298 return (callback != methods_.end()); 300 return (callback != methods_.end());
299 } 301 }
300 302
301 CppVariant* CppBoundClass::GetAsCppVariant() { 303 CppVariant* CppBoundClass::GetAsCppVariant() {
302 if (!self_variant_.isObject()) { 304 if (!self_variant_.isObject()) {
303 // Create an NPObject using our static NPClass. The first argument (a 305 // Create an NPObject using our static NPClass. The first argument has type
304 // plugin's instance handle) is passed through to the allocate function 306 // 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. 307 NPObject* np_obj = WebBindings::createObject(
306 NPObject* np_obj = WebBindings::createObject(0, &CppNPObject::np_class_); 308 npp_.get(), &CppNPObject::np_class_);
307 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj); 309 CppNPObject* obj = reinterpret_cast<CppNPObject*>(np_obj);
308 obj->bound_class = this; 310 obj->bound_class = this;
309 self_variant_.Set(np_obj); 311 self_variant_.Set(np_obj);
310 WebBindings::releaseObject(np_obj); // CppVariant takes the reference. 312 WebBindings::releaseObject(np_obj); // CppVariant takes the reference.
311 } 313 }
312 DCHECK(self_variant_.isObject()); 314 DCHECK(self_variant_.isObject());
313 return &self_variant_; 315 return &self_variant_;
314 } 316 }
315 317
316 void CppBoundClass::BindToJavascript(WebFrame* frame, 318 void CppBoundClass::BindToJavascript(WebFrame* frame,
317 const std::string& classname) { 319 const std::string& classname) {
318 // BindToWindowObject will take its own reference to the NPObject, and clean 320 // BindToWindowObject will take its own reference to the NPObject, and clean
319 // up after itself. It will also (indirectly) register the object with V8, 321 // 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. 322 // against an owner pointer we supply, so we must register that as an owner,
323 // and unregister when we teardown.
321 frame->bindToWindowObject(ASCIIToUTF16(classname), 324 frame->bindToWindowObject(ASCIIToUTF16(classname),
322 NPVARIANT_TO_OBJECT(*GetAsCppVariant())); 325 NPVARIANT_TO_OBJECT(*GetAsCppVariant()));
323 bound_to_frame_ = true; 326 bound_to_frame_ = true;
324 } 327 }
325 328
326 } // namespace webkit_glue 329 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/glue/cpp_bound_class.h ('k') | webkit/plugins/npapi/webplugin_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698