Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / ppapi / shared_impl / resource.cc
blob4fb9a47be90954b9c2767c8e047ba68b54d0de06
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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/resource.h"
7 #include "base/logging.h"
8 #include "ppapi/shared_impl/resource_tracker.h"
9 #include "ppapi/shared_impl/ppapi_globals.h"
11 namespace ppapi {
13 Resource::Resource(ResourceObjectType type, PP_Instance instance)
14 : host_resource_(HostResource::MakeInstanceOnly(instance)) {
15 // The instance should be valid (nonzero).
16 DCHECK(instance);
18 pp_resource_ = PpapiGlobals::Get()->GetResourceTracker()->AddResource(this);
19 if (type == OBJECT_IS_IMPL) {
20 // For the in-process case, the host resource and resource are the same.
22 // Note that we need to have set the instance above (in the initializer
23 // list) since AddResource needs our instance() getter to work, and that
24 // goes through the host resource. When we get the "real" resource ID,
25 // we re-set the host_resource.
26 host_resource_.SetHostResource(instance, pp_resource_);
30 Resource::Resource(ResourceObjectType type, const HostResource& host_resource)
31 : host_resource_(host_resource) {
32 pp_resource_ = PpapiGlobals::Get()->GetResourceTracker()->AddResource(this);
33 if (type == OBJECT_IS_IMPL) {
34 // When using this constructor for the implementation, the resource ID
35 // should not have been passed in.
36 DCHECK(host_resource_.host_resource() == 0);
38 // See previous constructor.
39 host_resource_.SetHostResource(host_resource.instance(), pp_resource_);
43 Resource::Resource(Untracked) {
44 pp_resource_ = PpapiGlobals::Get()->GetResourceTracker()->AddResource(this);
47 Resource::~Resource() {
48 RemoveFromResourceTracker();
51 PP_Resource Resource::GetReference() {
52 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(pp_resource());
53 return pp_resource();
56 void Resource::NotifyLastPluginRefWasDeleted() {
57 // Notify subclasses.
58 LastPluginRefWasDeleted();
61 void Resource::NotifyInstanceWasDeleted() {
62 // Hold a reference, because InstanceWasDeleted() may cause us to be
63 // destroyed.
64 scoped_refptr<Resource> keep_alive(this);
66 // Notify subclasses.
67 InstanceWasDeleted();
69 host_resource_ = HostResource();
72 void Resource::OnReplyReceived(const proxy::ResourceMessageReplyParams& params,
73 const IPC::Message& msg) {
74 NOTREACHED();
77 void Resource::Log(PP_LogLevel level, const std::string& message) {
78 printf("Log:%s\n", message.c_str());
79 PpapiGlobals::Get()->LogWithSource(pp_instance(), level, std::string(),
80 message);
83 void Resource::RemoveFromResourceTracker() {
84 PpapiGlobals::Get()->GetResourceTracker()->RemoveResource(this);
87 #define DEFINE_TYPE_GETTER(RESOURCE) \
88 thunk::RESOURCE* Resource::As##RESOURCE() { return NULL; }
89 FOR_ALL_PPAPI_RESOURCE_APIS(DEFINE_TYPE_GETTER)
90 #undef DEFINE_TYPE_GETTER
92 } // namespace ppapi