• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ppapi/shared_impl/scoped_pp_resource.h"
6 
7 #include "ppapi/shared_impl/ppapi_globals.h"
8 #include "ppapi/shared_impl/resource.h"
9 #include "ppapi/shared_impl/resource_tracker.h"
10 
11 namespace ppapi {
12 
ScopedPPResource()13 ScopedPPResource::ScopedPPResource() : id_(0) {}
14 
ScopedPPResource(PP_Resource resource)15 ScopedPPResource::ScopedPPResource(PP_Resource resource) : id_(resource) {
16   CallAddRef();
17 }
18 
ScopedPPResource(const PassRef &,PP_Resource resource)19 ScopedPPResource::ScopedPPResource(const PassRef&, PP_Resource resource)
20     : id_(resource) {}
21 
ScopedPPResource(Resource * resource)22 ScopedPPResource::ScopedPPResource(Resource* resource)
23     : id_(resource ? resource->GetReference() : 0) {
24   // GetReference AddRef's for us.
25 }
26 
ScopedPPResource(const ScopedPPResource & other)27 ScopedPPResource::ScopedPPResource(const ScopedPPResource& other)
28     : id_(other.id_) {
29   CallAddRef();
30 }
31 
~ScopedPPResource()32 ScopedPPResource::~ScopedPPResource() { CallRelease(); }
33 
operator =(PP_Resource resource)34 ScopedPPResource& ScopedPPResource::operator=(PP_Resource resource) {
35   if (id_ == resource)
36     return *this;  // Be careful about self-assignment.
37   CallRelease();
38   id_ = resource;
39   CallAddRef();
40   return *this;
41 }
42 
operator =(const ScopedPPResource & resource)43 ScopedPPResource& ScopedPPResource::operator=(
44     const ScopedPPResource& resource) {
45   if (id_ == resource.id_)
46     return *this;  // Be careful about self-assignment.
47   CallRelease();
48   id_ = resource.id_;
49   CallAddRef();
50   return *this;
51 }
52 
Release()53 PP_Resource ScopedPPResource::Release() {
54   // We do NOT call CallRelease, because we want to pass our reference to the
55   // caller.
56 
57   PP_Resource ret = id_;
58   id_ = 0;
59   return ret;
60 }
61 
CallAddRef()62 void ScopedPPResource::CallAddRef() {
63   if (id_)
64     PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(id_);
65 }
66 
CallRelease()67 void ScopedPPResource::CallRelease() {
68   if (id_)
69     PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(id_);
70 }
71 
72 }  // namespace ppapi
73