Ocean
JSOcean.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_INTERACTION_JS_OCEAN_H
9 #define META_OCEAN_INTERACTION_JS_OCEAN_H
10 
14 
16 #include "ocean/base/Singleton.h"
17 
18 #include "ocean/io/FileConfig.h"
19 
20 namespace Ocean
21 {
22 
23 namespace Interaction
24 {
25 
26 namespace JavaScript
27 {
28 
29 /**
30  * This class implements the JavaScript Ocean objevct.
31  * @ingroup interactionjs
32  */
33 class OCEAN_INTERACTION_JS_EXPORT JSOcean : public JSBase
34 {
35  friend class JSLibrary;
36 
37  public:
38 
39  /**
40  * This class holds a global ocean object.
41  */
42  class OceanContainer : public Singleton<OceanContainer>
43  {
44  friend class Singleton<OceanContainer>;
45 
46  private:
47 
48  /**
49  * Definition of a pair combining a JSContext object with a function name.
50  */
51  typedef std::pair<std::weak_ptr<JSContext>, std::string> ContextFunctionPair;
52 
53  /**
54  * Definition of a vector holding ContextFunctionPair objects.
55  */
56  typedef std::vector<ContextFunctionPair> ContextFunctionPairs;
57 
58  public:
59 
60  /**
61  * Destructs the global ocean object.
62  */
63  virtual ~OceanContainer();
64 
65  /**
66  * Adds a new content added event function.
67  * @param function The function to be added
68  */
69  void addContentAddedEventFunction(const v8::Local<v8::Function>& function);
70 
71  /**
72  * Adds a new content removed event function.
73  * @param function The function to be added
74  */
75  void addContentRemovedEventFunction(const v8::Local<v8::Function>& function);
76 
77  /**
78  * Removes a new content added event function.
79  * @param function The function to be removed
80  */
81  void removeContentAddedEventFunction(const v8::Local<v8::Function>& function);
82 
83  /**
84  * Removes a new content removed event function.
85  * @param function The function to be removed
86  */
87  void removeContentRemovedEventFunction(const v8::Local<v8::Function>& function);
88 
89  /**
90  * Releases all event callback functions.
91  */
92  void release();
93 
94  private:
95 
96  /**
97  * On content event function.
98  * @param eventId Id of the event
99  * @param value Event value
100  * @param state Event state
101  */
102  void onContentEvent(const ApplicationInterface::EventId eventId, const bool value, const bool state);
103 
104  /**
105  * Creates JavaScript parameters of one given values.
106  * @param eventId Id of the event
107  * @param state State value
108  */
109  static std::vector<v8::Handle<v8::Value>> parameter(const ApplicationInterface::EventId eventId, const bool state);
110 
111  private:
112 
113  /// Functions for content added events.
115 
116  /// Functions for content removed events.
118 
119  /// File config object.
121 
122  /// Lock object.
124  };
125 
126  public:
127 
128  /**
129  * Returns the JavaScript name of this object.
130  * @return The object's JavaScript name
131  */
132  static inline const char* objectName();
133 
134  /**
135  * Returns the object template for this object.
136  * @return The object template
137  */
138  static inline v8::Local<v8::ObjectTemplate> objectTemplate();
139 
140  /**
141  * Releases the function and object template for this object.
142  */
143  static void release();
144 
145  protected:
146 
147  /**
148  * Creates the function object template for this object.
149  */
150  static void createObjectTemplate();
151 
152  /**
153  * Callback function for 'debug()' function.
154  * @param info The function callback info
155  */
156  static void functionDebug(const v8::FunctionCallbackInfo<v8::Value>& info);
157 
158  /**
159  * Callback function for 'info()' function.
160  * @param info The function callback info
161  */
162  static void functionInfo(const v8::FunctionCallbackInfo<v8::Value>& info);
163 
164  /**
165  * Callback function for 'warning()' function.
166  * @param info The function callback info
167  */
168  static void functionWarning(const v8::FunctionCallbackInfo<v8::Value>& info);
169 
170  /**
171  * Callback function for 'error()' function.
172  * @param info The function callback info
173  */
174  static void functionError(const v8::FunctionCallbackInfo<v8::Value>& info);
175 
176  /**
177  * Callback function for 'platformType()' function.
178  * @param info The function callback info
179  */
180  static void functionPlatformType(const v8::FunctionCallbackInfo<v8::Value>& info);
181 
182  /**
183  * Callback function for 'architectureType()' function.
184  * @param info The function callback info
185  */
186  static void functionArchitectureType(const v8::FunctionCallbackInfo<v8::Value>& info);
187 
188  /**
189  * Callback function for 'releaseType()' function.
190  * @param info The function callback info
191  */
192  static void functionReleaseType(const v8::FunctionCallbackInfo<v8::Value>& info);
193 
194  /**
195  * Callback function for 'compilerVersion()' function.
196  * @param info The function callback info
197  */
198  static void functionCompilerVersion(const v8::FunctionCallbackInfo<v8::Value>& info);
199 
200  /**
201  * Callback function for 'addContent()' function.
202  * @param info The function callback info
203  */
204  static void functionAddContent(const v8::FunctionCallbackInfo<v8::Value>& info);
205 
206  /**
207  * Callback function for 'removeContent()' function.
208  * @param info The function callback info
209  */
210  static void functionRemoveContent(const v8::FunctionCallbackInfo<v8::Value>& info);
211 
212  /**
213  * Callback function for 'setContentAddedEventFunction()' function.
214  * @param info The function callback info
215  */
216  static void functionSetContentAddedEventFunction(const v8::FunctionCallbackInfo<v8::Value>& info);
217 
218  /**
219  * Callback function for 'setContentRemovedEventFunction()' function.
220  * @param info The function callback info
221  */
222  static void functionSetContentRemovedEventFunction(const v8::FunctionCallbackInfo<v8::Value>& info);
223 
224  /**
225  * Callback function for 'timestamp()' function.
226  * @param info The function callback info
227  */
228  static void functionTimestamp(const v8::FunctionCallbackInfo<v8::Value>& info);
229 
230  /**
231  * Pushes information into a message object.
232  * @param messageObject The message object to which the information will be pushed
233  * @param info The function callback info
234  */
235  template <bool tActive>
236  static void pushMessage(MessageObject<tActive>& messageObject, const v8::FunctionCallbackInfo<v8::Value>& info);
237 
238  /**
239  * Resolves the filenames from a given java script argument.
240  * @param info The function callback info
241  * @return Resolved filenames
242  */
243  static ApplicationInterface::StringVector resolveFilenames(const v8::FunctionCallbackInfo<v8::Value>& info);
244 
245  protected:
246 
247  /// Object template for the Vector2 object.
248  static v8::Persistent<v8::ObjectTemplate> objectTemplate_;
249 };
250 
251 inline const char* JSOcean::objectName()
252 {
253  return "ocean";
254 }
255 
256 inline v8::Local<v8::ObjectTemplate> JSOcean::objectTemplate()
257 {
258  if (objectTemplate_.IsEmpty())
259  {
261  }
262 
263  ocean_assert(objectTemplate_.IsEmpty() == false);
264  return objectTemplate_.Get(v8::Isolate::GetCurrent());
265 }
266 
267 inline void JSOcean::release()
268 {
269  ocean_assert(!objectTemplate_.IsEmpty());
270  objectTemplate_.Reset();
271 }
272 
273 template <bool tActive>
274 void JSOcean::pushMessage(MessageObject<tActive>& messageObject, const v8::FunctionCallbackInfo<v8::Value>& info)
275 {
276  ocean_assert(info.Length() > 0);
277 
278  std::string message;
279 
280  for (int n = 0; n < info.Length(); n++)
281  {
282  message += toAString(info[n]->ToDetailString(JSContext::currentContext()));
283  }
284 
285  messageObject << message;
286 }
287 
288 }
289 
290 }
291 
292 }
293 
294 #endif // META_OCEAN_INTERACTION_JS_OCEAN_H
unsigned int EventId
Definition of an event id.
Definition: ApplicationInterface.h:37
std::vector< std::string > StringVector
Definition of a vector holding strings.
Definition: ApplicationInterface.h:52
This class implements an application or module configuration toolkit using files as input or output.
Definition: FileConfig.h:29
This class implements the base class for all JavaScript wrappers.
Definition: JSBase.h:34
static std::string toAString(const v8::Local< v8::String > &value)
Returns the std string of a given JavaScript string object.
static const v8::Local< v8::Context > & currentContext()
Returns the current JavaScript context object.
Definition: JSContext.h:337
This class implements the java script interaction library object.
Definition: JSLibrary.h:32
This class holds a global ocean object.
Definition: JSOcean.h:43
void addContentAddedEventFunction(const v8::Local< v8::Function > &function)
Adds a new content added event function.
Lock lock_
Lock object.
Definition: JSOcean.h:123
std::pair< std::weak_ptr< JSContext >, std::string > ContextFunctionPair
Definition of a pair combining a JSContext object with a function name.
Definition: JSOcean.h:51
std::vector< ContextFunctionPair > ContextFunctionPairs
Definition of a vector holding ContextFunctionPair objects.
Definition: JSOcean.h:56
void release()
Releases all event callback functions.
void addContentRemovedEventFunction(const v8::Local< v8::Function > &function)
Adds a new content removed event function.
virtual ~OceanContainer()
Destructs the global ocean object.
static std::vector< v8::Handle< v8::Value > > parameter(const ApplicationInterface::EventId eventId, const bool state)
Creates JavaScript parameters of one given values.
IO::FileConfig containerFileConfig
File config object.
Definition: JSOcean.h:120
ContextFunctionPairs contentRemovedEventFunctions_
Functions for content removed events.
Definition: JSOcean.h:117
void onContentEvent(const ApplicationInterface::EventId eventId, const bool value, const bool state)
On content event function.
ContextFunctionPairs contentAddedEventFunctions_
Functions for content added events.
Definition: JSOcean.h:114
void removeContentRemovedEventFunction(const v8::Local< v8::Function > &function)
Removes a new content removed event function.
void removeContentAddedEventFunction(const v8::Local< v8::Function > &function)
Removes a new content added event function.
This class implements the JavaScript Ocean objevct.
Definition: JSOcean.h:34
static void functionRemoveContent(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'removeContent()' function.
static ApplicationInterface::StringVector resolveFilenames(const v8::FunctionCallbackInfo< v8::Value > &info)
Resolves the filenames from a given java script argument.
static void functionCompilerVersion(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'compilerVersion()' function.
static void pushMessage(MessageObject< tActive > &messageObject, const v8::FunctionCallbackInfo< v8::Value > &info)
Pushes information into a message object.
Definition: JSOcean.h:274
static void functionError(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'error()' function.
static void functionWarning(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'warning()' function.
static void functionSetContentAddedEventFunction(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'setContentAddedEventFunction()' function.
static const char * objectName()
Returns the JavaScript name of this object.
Definition: JSOcean.h:251
static void functionTimestamp(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'timestamp()' function.
static v8::Local< v8::ObjectTemplate > objectTemplate()
Returns the object template for this object.
Definition: JSOcean.h:256
static void functionInfo(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'info()' function.
static void createObjectTemplate()
Creates the function object template for this object.
static v8::Persistent< v8::ObjectTemplate > objectTemplate_
Object template for the Vector2 object.
Definition: JSOcean.h:248
static void release()
Releases the function and object template for this object.
Definition: JSOcean.h:267
static void functionPlatformType(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'platformType()' function.
static void functionSetContentRemovedEventFunction(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'setContentRemovedEventFunction()' function.
static void functionAddContent(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'addContent()' function.
static void functionReleaseType(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'releaseType()' function.
static void functionDebug(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'debug()' function.
static void functionArchitectureType(const v8::FunctionCallbackInfo< v8::Value > &info)
Callback function for 'architectureType()' function.
This class implements a recursive lock object.
Definition: Lock.h:31
Messenger object, one object for each message.
Definition: Messenger.h:427
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
void release(T *object)
This functions allows to release a DirectShow object if it does exist.
Definition: DSObject.h:266
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15