Hi,
I've been working on refactoring the ImageData proxy to the "new" resource model, and I'm running in to a little trouble. In the "new" model, ResourceHosts are not reference-counted; the intent is that PpapiHost owns them in its
resource_hosts_ map. They get deleted when the plugin releases its last ref-count.
(CCing Brett since this is your baby; if you don't care, you can stop reading).
However, PepperGraphics2DHost currently holds a queue of PPB_ImageData_Impls via scoped_refptr. The plugin may e.g., do one or more calls to PaintImageData or ReplaceContents, and release the IamgeData resource prior to Flush()ing. So the problem is, the ImageData hosts have to live as long as the Graphics2D host needs them, even if the plugin is finished with them.
The straightforward way to solve this is to make ResourceHost inherit RefCounted and start owning them by scoped_refptr in PpapiHost. This is simple to implement, but the perils of RefCounted are well known.
I thought of another approach so that PpapiHost can still own all ResourceHosts. The idea is to allow 1 ResourceHost to "pin" another ResourceHost in memory. We'd make it a rule that any ResourceHost can only ever "pin" or be "pinned", but never both. This guarantees:
- No cycles
- The "graph" of lifetime dependencies is only 1 level deep.
The advantage is that lifetime is easier to underand:
- PpapiHost outlives all ResourceHosts.
- No cycles, no deep lifetime dependency graph.
- Only ResourceHosts may pin each other (with scoped_refptr, any body who gets a raw pointer to a Host can take a reference).
The big disadvantage is that the code is harder to understand, because it's a new concept with non-trivial new supporting code.
So what do you all think, which one are you more willing to help maintain? scoped_refptr or a new "Pin/Unpin" thing? Or do you have something better?