Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ppapi / shared_impl / scoped_pp_resource.cc
blobb842b56d8754d1c9f6891f856c522c5ad4023f79
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.
5 #include "ppapi/shared_impl/scoped_pp_resource.h"
7 #include "ppapi/shared_impl/ppapi_globals.h"
8 #include "ppapi/shared_impl/resource.h"
9 #include "ppapi/shared_impl/resource_tracker.h"
11 namespace ppapi {
13 ScopedPPResource::ScopedPPResource() : id_(0) {}
15 ScopedPPResource::ScopedPPResource(PP_Resource resource) : id_(resource) {
16 CallAddRef();
19 ScopedPPResource::ScopedPPResource(const PassRef&, PP_Resource resource)
20 : id_(resource) {}
22 ScopedPPResource::ScopedPPResource(Resource* resource)
23 : id_(resource ? resource->GetReference() : 0) {
24 // GetReference AddRef's for us.
27 ScopedPPResource::ScopedPPResource(const ScopedPPResource& other)
28 : id_(other.id_) {
29 CallAddRef();
32 ScopedPPResource::~ScopedPPResource() { CallRelease(); }
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;
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;
53 PP_Resource ScopedPPResource::Release() {
54 // We do NOT call CallRelease, because we want to pass our reference to the
55 // caller.
57 PP_Resource ret = id_;
58 id_ = 0;
59 return ret;
62 void ScopedPPResource::CallAddRef() {
63 if (id_)
64 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(id_);
67 void ScopedPPResource::CallRelease() {
68 if (id_)
69 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(id_);
72 } // namespace ppapi