• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #include <sdk_util/macros.h>
32 
33 namespace nacl_io {
34 
35 // This class is the base interface for Pepper used by nacl_io.
36 //
37 // We use #include and macro magic to simplify adding new interfaces. The
38 // resulting PepperInterface basically looks like this:
39 //
40 // class PepperInterface {
41 //  public:
42 //   virtual ~PepperInterface() {}
43 //   virtual PP_Instance GetInstance() = 0;
44 //   ...
45 //
46 //   // Interface getters.
47 //   ConsoleInterface* GetConsoleInterface() = 0;
48 //   CoreInterface* GetCoreInterface() = 0;
49 //   FileIoInterface* GetFileIoInterface() = 0;
50 //   ... etc.
51 // };
52 //
53 //
54 // Note: To add a new interface:
55 //
56 // 1. Using one of the other interfaces as a template, add your interface to
57 //    all_interfaces.h.
58 // 2. Add the necessary pepper header to the top of this file.
59 // 3. Compile and cross your fingers!
60 
61 
62 // Forward declare interface classes.
63 #include "nacl_io/pepper/undef_macros.h"
64 #include "nacl_io/pepper/define_empty_macros.h"
65 #undef BEGIN_INTERFACE
66 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \
67     class BaseClass;
68 #include "nacl_io/pepper/all_interfaces.h"
69 
70 int PPErrorToErrno(int32_t err);
71 
72 class PepperInterface {
73  public:
~PepperInterface()74   virtual ~PepperInterface() {}
75   virtual PP_Instance GetInstance() = 0;
76 
77   // Convenience functions. These forward to
78   // GetCoreInterface()->{AddRef,Release}Resource.
79   void AddRefResource(PP_Resource resource);
80   void ReleaseResource(PP_Resource resource);
81 
82 // Interface getters.
83 //
84 // These macros expand to definitions like:
85 //
86 //   CoreInterface* GetCoreInterface() = 0;
87 //
88 #include "nacl_io/pepper/undef_macros.h"
89 #include "nacl_io/pepper/define_empty_macros.h"
90 #undef BEGIN_INTERFACE
91 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \
92     virtual BaseClass* Get##BaseClass() = 0;
93 #include "nacl_io/pepper/all_interfaces.h"
94 };
95 
96 // Interface class definitions.
97 //
98 // Each class will be defined with all pure virtual methods, e.g:
99 //
100 //   class CoreInterface {
101 //    public:
102 //     virtual ~CoreInterface() {}
103 //     virtual void AddRefResource() = 0;
104 //     virtual void ReleaseResource() = 0;
105 //     virtual PP_Bool IsMainThread() = 0;
106 //   };
107 //
108 #include "nacl_io/pepper/undef_macros.h"
109 #define BEGIN_INTERFACE(BaseClass, PPInterface, InterfaceString) \
110     class BaseClass { \
111      public: \
112       virtual ~BaseClass() {}
113 #define END_INTERFACE(BaseClass, PPInterface) \
114     };
115 #define METHOD0(Class, ReturnType, MethodName) \
116     virtual ReturnType MethodName() = 0;
117 #define METHOD1(Class, ReturnType, MethodName, Type0) \
118     virtual ReturnType MethodName(Type0) = 0;
119 #define METHOD2(Class, ReturnType, MethodName, Type0, Type1) \
120     virtual ReturnType MethodName(Type0, Type1) = 0;
121 #define METHOD3(Class, ReturnType, MethodName, Type0, Type1, Type2) \
122     virtual ReturnType MethodName(Type0, Type1, Type2) = 0;
123 #define METHOD4(Class, ReturnType, MethodName, Type0, Type1, Type2, Type3) \
124     virtual ReturnType MethodName(Type0, Type1, Type2, Type3) = 0;
125 #define METHOD5(Class, ReturnType, MethodName, Type0, Type1, Type2, Type3, \
126                 Type4) \
127     virtual ReturnType MethodName(Type0, Type1, Type2, Type3, Type4) = 0;
128 #include "nacl_io/pepper/all_interfaces.h"
129 
130 
131 class ScopedResource {
132  public:
133   // Does not AddRef.
134   explicit ScopedResource(PepperInterface* ppapi);
135   ScopedResource(PepperInterface* ppapi, PP_Resource resource);
136   ~ScopedResource();
137 
pp_resource()138   PP_Resource pp_resource() const { return resource_; }
139 
140   // Set a new resource, releasing the old one. Does not AddRef the new
141   // resource.
142   void Reset(PP_Resource resource);
143 
144   // Return the resource without decrementing its refcount.
145   PP_Resource Release();
146 
147  private:
148   PepperInterface* ppapi_;
149   PP_Resource resource_;
150 
151   DISALLOW_COPY_AND_ASSIGN(ScopedResource);
152 };
153 
154 }  // namespace nacl_io
155 
156 #endif  // LIBRARIES_NACL_IO_PEPPER_INTERFACE_H_
157