Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / pepper_interface.cc
blob545c1a1862061d825a9dd286f3af8b9747639548
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 "nacl_io/pepper_interface.h"
6 #include <errno.h>
7 #include <ppapi/c/pp_errors.h>
9 #include "nacl_io/log.h"
11 namespace nacl_io {
13 void PepperInterface::AddRefResource(PP_Resource resource) {
14 GetCoreInterface()->AddRefResource(resource);
17 void PepperInterface::ReleaseResource(PP_Resource resource) {
18 GetCoreInterface()->ReleaseResource(resource);
21 ScopedResource::ScopedResource(PepperInterface* ppapi)
22 : ppapi_(ppapi), resource_(0) {
25 ScopedResource::ScopedResource(PepperInterface* ppapi, PP_Resource resource)
26 : ppapi_(ppapi), resource_(resource) {
29 ScopedResource::~ScopedResource() {
30 if (resource_)
31 ppapi_->ReleaseResource(resource_);
34 void ScopedResource::Reset(PP_Resource resource) {
35 if (resource_)
36 ppapi_->ReleaseResource(resource_);
38 resource_ = resource;
41 PP_Resource ScopedResource::Release() {
42 PP_Resource result = resource_;
43 resource_ = 0;
44 return result;
47 ScopedVar::ScopedVar(PepperInterface* ppapi)
48 : ppapi_(ppapi), var_(PP_MakeUndefined()) {}
50 ScopedVar::ScopedVar(PepperInterface* ppapi, PP_Var var)
51 : ppapi_(ppapi), var_(var) {}
53 ScopedVar::~ScopedVar() {
54 ppapi_->GetVarInterface()->Release(var_);
57 void ScopedVar::Reset(PP_Var var) {
58 ppapi_->GetVarInterface()->Release(var_);
59 var_ = var;
62 PP_Var ScopedVar::Release() {
63 PP_Var result = var_;
64 var_ = PP_MakeUndefined();
65 return result;
68 int PPErrorToErrno(int32_t err) {
69 // If not an error, then just return it.
70 if (err >= PP_OK)
71 return err;
73 switch (err) {
74 case PP_OK_COMPLETIONPENDING: return 0;
75 case PP_ERROR_FAILED: return EPERM;
76 case PP_ERROR_ABORTED: return EPERM;
77 case PP_ERROR_BADARGUMENT: return EINVAL;
78 case PP_ERROR_BADRESOURCE: return EBADF;
79 case PP_ERROR_NOINTERFACE: return ENOSYS;
80 case PP_ERROR_NOACCESS: return EACCES;
81 case PP_ERROR_NOMEMORY: return ENOMEM;
82 case PP_ERROR_NOSPACE: return ENOSPC;
83 case PP_ERROR_NOQUOTA: return ENOSPC;
84 case PP_ERROR_INPROGRESS: return EBUSY;
85 case PP_ERROR_NOTSUPPORTED: return ENOSYS;
86 case PP_ERROR_BLOCKS_MAIN_THREAD: return EPERM;
87 case PP_ERROR_FILENOTFOUND: return ENOENT;
88 case PP_ERROR_FILEEXISTS: return EEXIST;
89 case PP_ERROR_FILETOOBIG: return EFBIG;
90 case PP_ERROR_FILECHANGED: return EINVAL;
91 case PP_ERROR_TIMEDOUT: return EBUSY;
92 case PP_ERROR_USERCANCEL: return EPERM;
93 case PP_ERROR_NO_USER_GESTURE: return EPERM;
94 case PP_ERROR_CONTEXT_LOST: return EPERM;
95 case PP_ERROR_NO_MESSAGE_LOOP: return EPERM;
96 case PP_ERROR_WRONG_THREAD: return EPERM;
97 case PP_ERROR_CONNECTION_ABORTED: return ECONNABORTED;
98 case PP_ERROR_CONNECTION_REFUSED: return ECONNREFUSED;
99 case PP_ERROR_CONNECTION_FAILED: return ECONNREFUSED;
100 case PP_ERROR_CONNECTION_TIMEDOUT: return ETIMEDOUT;
101 case PP_ERROR_ADDRESS_UNREACHABLE: return ENETUNREACH;
102 case PP_ERROR_ADDRESS_IN_USE: return EADDRINUSE;
105 return EINVAL;
108 #if !defined(NDEBUG)
110 int PPErrorToErrnoLog(int32_t err, const char* file, int line) {
111 if (err >= PP_OK)
112 return err;
114 #define PP_ERRORS(V) \
115 V(PP_OK) \
116 V(PP_OK_COMPLETIONPENDING) \
117 V(PP_ERROR_FAILED) \
118 V(PP_ERROR_ABORTED) \
119 V(PP_ERROR_BADARGUMENT) \
120 V(PP_ERROR_BADRESOURCE) \
121 V(PP_ERROR_NOINTERFACE) \
122 V(PP_ERROR_NOACCESS) \
123 V(PP_ERROR_NOMEMORY) \
124 V(PP_ERROR_NOSPACE) \
125 V(PP_ERROR_NOQUOTA) \
126 V(PP_ERROR_INPROGRESS) \
127 V(PP_ERROR_NOTSUPPORTED) \
128 V(PP_ERROR_BLOCKS_MAIN_THREAD) \
129 V(PP_ERROR_MALFORMED_INPUT) \
130 V(PP_ERROR_RESOURCE_FAILED) \
131 V(PP_ERROR_FILENOTFOUND) \
132 V(PP_ERROR_FILEEXISTS) \
133 V(PP_ERROR_FILETOOBIG) \
134 V(PP_ERROR_FILECHANGED) \
135 V(PP_ERROR_NOTAFILE) \
136 V(PP_ERROR_TIMEDOUT) \
137 V(PP_ERROR_USERCANCEL) \
138 V(PP_ERROR_NO_USER_GESTURE) \
139 V(PP_ERROR_CONTEXT_LOST) \
140 V(PP_ERROR_NO_MESSAGE_LOOP) \
141 V(PP_ERROR_WRONG_THREAD) \
142 V(PP_ERROR_WOULD_BLOCK_THREAD) \
143 V(PP_ERROR_CONNECTION_CLOSED) \
144 V(PP_ERROR_CONNECTION_RESET) \
145 V(PP_ERROR_CONNECTION_REFUSED) \
146 V(PP_ERROR_CONNECTION_ABORTED) \
147 V(PP_ERROR_CONNECTION_FAILED) \
148 V(PP_ERROR_CONNECTION_TIMEDOUT) \
149 V(PP_ERROR_ADDRESS_INVALID) \
150 V(PP_ERROR_ADDRESS_UNREACHABLE) \
151 V(PP_ERROR_ADDRESS_IN_USE) \
152 V(PP_ERROR_MESSAGE_TOO_BIG) \
153 V(PP_ERROR_NAME_NOT_RESOLVED)
155 #define ERROR_STRING_PAIR(x) {x, #x},
157 const struct {
158 int err;
159 const char* name;
160 } kErrorStringPair[] = {
161 PP_ERRORS(ERROR_STRING_PAIR)
164 #undef ERROR_STRING_PAIR
165 #undef PP_ERRORS
167 const char* err_string = "Unknown PPError value";
168 for (size_t i = 0; i < sizeof(kErrorStringPair) / sizeof(kErrorStringPair[0]);
169 ++i) {
170 if (err == kErrorStringPair[i].err) {
171 err_string = kErrorStringPair[i].name;
175 nacl_io_log(LOG_PREFIX "%s:%d: Got PPError %d = %s\n", file, line, err,
176 err_string);
178 return PPErrorToErrno(err);
181 #endif
183 } // namespace nacl_io