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. 4 5 #ifndef LIBRARIES_NACL_IO_PEPPER_INTERFACE_H_ 6 #define LIBRARIES_NACL_IO_PEPPER_INTERFACE_H_ 7 8 #include <ppapi/c/pp_bool.h> 9 #include <ppapi/c/pp_completion_callback.h> 10 #include <ppapi/c/pp_errors.h> 11 #include <ppapi/c/pp_file_info.h> 12 #include <ppapi/c/pp_instance.h> 13 #include <ppapi/c/pp_resource.h> 14 #include <ppapi/c/pp_var.h> 15 #include <ppapi/c/ppb_console.h> 16 #include <ppapi/c/ppb_core.h> 17 #include <ppapi/c/ppb_file_io.h> 18 #include <ppapi/c/ppb_file_ref.h> 19 #include <ppapi/c/ppb_file_system.h> 20 #include <ppapi/c/ppb_host_resolver.h> 21 #include <ppapi/c/ppb_messaging.h> 22 #include <ppapi/c/ppb_messaging.h> 23 #include <ppapi/c/ppb_net_address.h> 24 #include <ppapi/c/ppb_tcp_socket.h> 25 #include <ppapi/c/ppb_url_loader.h> 26 #include <ppapi/c/ppb_url_request_info.h> 27 #include <ppapi/c/ppb_url_response_info.h> 28 #include <ppapi/c/ppb_udp_socket.h> 29 #include <ppapi/c/ppb_var.h> 30 #include <ppapi/c/ppb_var_array.h> 31 #include <ppapi/c/ppb_var_array_buffer.h> 32 #include <ppapi/c/ppb_var_dictionary.h> 33 34 #include <sdk_util/macros.h> 35 36 namespace nacl_io { 37 38 // This class is the base interface for Pepper used by nacl_io. 39 // 40 // We use #include and macro magic to simplify adding new interfaces. The 41 // resulting PepperInterface basically looks like this: 42 // 43 // class PepperInterface { 44 // public: 45 // virtual ~PepperInterface() {} 46 // virtual PP_Instance GetInstance() = 0; 47 // ... 48 // 49 // // Interface getters. 50 // ConsoleInterface* GetConsoleInterface() = 0; 51 // CoreInterface* GetCoreInterface() = 0; 52 // FileIoInterface* GetFileIoInterface() = 0; 53 // ... etc. 54 // }; 55 // 56 // 57 // Note: To add a new interface: 58 // 59 // 1. Using one of the other interfaces as a template, add your interface to 60 // all_interfaces.h. 61 // 2. Add the necessary pepper header to the top of this file. 62 // 3. Compile and cross your fingers! 63 64 // Forward declare interface classes. 65 #include "nacl_io/pepper/undef_macros.h" 66 #include "nacl_io/pepper/define_empty_macros.h" 67 #undef BEGIN_INTERFACE 68 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \ 69 class BaseClass; 70 #include "nacl_io/pepper/all_interfaces.h" 71 72 int PPErrorToErrno(int32_t err); 73 74 class PepperInterface { 75 public: 76 virtual ~PepperInterface() {} 77 virtual PP_Instance GetInstance() = 0; 78 79 // Convenience functions. These forward to 80 // GetCoreInterface()->{AddRef,Release}Resource. 81 void AddRefResource(PP_Resource resource); 82 void ReleaseResource(PP_Resource resource); 83 84 // Interface getters. 85 // 86 // These macros expand to definitions like: 87 // 88 // CoreInterface* GetCoreInterface() = 0; 89 // 90 #include "nacl_io/pepper/undef_macros.h" 91 #include "nacl_io/pepper/define_empty_macros.h" 92 #undef BEGIN_INTERFACE 93 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \ 94 virtual BaseClass* Get##BaseClass() = 0; 95 #include "nacl_io/pepper/all_interfaces.h" 96 }; 97 98 // Interface class definitions. 99 // 100 // Each class will be defined with all pure virtual methods, e.g: 101 // 102 // class CoreInterface { 103 // public: 104 // virtual ~CoreInterface() {} 105 // virtual void AddRefResource() = 0; 106 // virtual void ReleaseResource() = 0; 107 // virtual PP_Bool IsMainThread() = 0; 108 // }; 109 // 110 #include "nacl_io/pepper/undef_macros.h" 111 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \ 112 class BaseClass { \ 113 public: \ 114 virtual ~BaseClass() {} 115 #define END_INTERFACE(BaseClass, PPInterface) \ 116 }; 117 #define METHOD0(Class, ReturnType, MethodName) \ 118 virtual ReturnType MethodName() = 0; 119 #define METHOD1(Class, ReturnType, MethodName, Type0) \ 120 virtual ReturnType MethodName(Type0) = 0; 121 #define METHOD2(Class, ReturnType, MethodName, Type0, Type1) \ 122 virtual ReturnType MethodName(Type0, Type1) = 0; 123 #define METHOD3(Class, ReturnType, MethodName, Type0, Type1, Type2) \ 124 virtual ReturnType MethodName(Type0, Type1, Type2) = 0; 125 #define METHOD4(Class, ReturnType, MethodName, Type0, Type1, Type2, Type3) \ 126 virtual ReturnType MethodName(Type0, Type1, Type2, Type3) = 0; 127 #define METHOD5(Class, ReturnType, MethodName, Type0, Type1, Type2, Type3, \ 128 Type4) \ 129 virtual ReturnType MethodName(Type0, Type1, Type2, Type3, Type4) = 0; 130 #include "nacl_io/pepper/all_interfaces.h" 131 132 133 class ScopedResource { 134 public: 135 // Does not AddRef. 136 explicit ScopedResource(PepperInterface* ppapi); 137 ScopedResource(PepperInterface* ppapi, PP_Resource resource); 138 ~ScopedResource(); 139 140 PP_Resource pp_resource() const { return resource_; } 141 142 // Set a new resource, releasing the old one. Does not AddRef the new 143 // resource. 144 void Reset(PP_Resource resource); 145 146 // Return the resource without decrementing its refcount. 147 PP_Resource Release(); 148 149 private: 150 PepperInterface* ppapi_; 151 PP_Resource resource_; 152 153 DISALLOW_COPY_AND_ASSIGN(ScopedResource); 154 }; 155 156 class ScopedVar { 157 public: 158 // Does not AddRef. 159 explicit ScopedVar(PepperInterface* ppapi); 160 ScopedVar(PepperInterface* ppapi, PP_Var var); 161 ~ScopedVar(); 162 163 PP_Var pp_var() const { return var_; } 164 165 // Set a new var, releasing the old one. Does not AddRef the new 166 // resource. 167 void Reset(PP_Var resource); 168 169 // Return the var without decrementing its refcount. 170 PP_Var Release(); 171 172 private: 173 PepperInterface* ppapi_; 174 PP_Var var_; 175 176 DISALLOW_COPY_AND_ASSIGN(ScopedVar); 177 }; 178 179 } // namespace nacl_io 180 181 #endif // LIBRARIES_NACL_IO_PEPPER_INTERFACE_H_ 182