fee9d8cde971fd59643253f4135dd9ccd1e8c4c7
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / shared / base-wrapped-object.h
1 #ifndef __DALI_V8PLUGIN_BASE_WRAPPED_OBJECT_H__
2 #define __DALI_V8PLUGIN_BASE_WRAPPED_OBJECT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <v8.h>
23
24 // INTERNAL INCLUDES
25 #include <interfaces/garbage-collector-interface.h>
26
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 class SignalManager;
35
36 /**
37  * @brief Used as a base class for all wrapped objects.
38  *
39  *  Dali JavaScript objects look like this
40  *
41  * \code
42  *  _______________________________________         ________________________________________
43  * | JavaScript Object                     |       |  C++ WrappedObject  (e.g. ImageWrapper)|
44  * |---------------------------------------|       |----------------------------------------|
45  * | Hidden internal fields                |       | Handle to a Dali::Image object         |
46  * | *Pointer to a BaseWrappedObject       | ----> |________________________________________|
47  * | Type of wrapped object (e.g. Image)   |
48  * -----------------------------------------
49  *
50  * \endcode
51  *
52  * Whenever we want to access functions / properties of that wrapped object, we unwrap it
53  * to get access to the Dali object.
54  *
55  * Each wrapped object registers with Dali garbage collector so they can be deleted
56  * when Dali shuts down
57  */
58 class BaseWrappedObject
59 {
60
61 public:
62
63   /*
64    * A type enum for the wrapped object
65    */
66   enum Type
67     {
68       UNKNOWN              = -1,
69       HANDLE               = 0 ,
70       HANDLE_SIGNAL,
71       CONNECTION,
72       ANIMATION,
73       PATH,
74       PATH_CONSTRAINT,
75       BUILDER,
76       STAGE,
77       FONT,
78       IMAGE,
79       IMAGE_ATTRIBUTES,
80       ACTOR,
81       ACTOR_PROPERTY,
82       RENDER_TASK,
83       RENDER_TASK_LIST,
84       TIMER,
85       TAP_GESTURE,
86       SHADER_EFFECT,
87       PAN_GESTURE,
88       LONGPRESS_GESTURE,
89       PINCH_GESTURE,
90       KEYBOARD_FOCUS_MANAGER,
91       PAN_GESTURE_DETECTOR,
92       ////////////////////////////////////////////////////////
93       PROPERTY_VALUE_START_RANGE,  // start of property values
94       VECTOR2,
95       VECTOR3,
96       VECTOR4,
97       MATRIX,
98       MATRIX3,
99       RECTANGLE,
100       ROTATION,
101       PROPERTY_VALUE_END_RANGE,  // end of property values
102       ///////////////////////////////////////////////////////
103     };
104
105   /*
106    * A type enum for the wrapped object internal field
107    */
108   enum Field
109     {
110       FIELD_POINTER   = 0,
111       FIELD_TYPE      = 1,
112       FIELD_COUNT     = 2 // increase if more fields are added
113     };
114
115    /**
116    * @brief virtual destructor
117    */
118   virtual ~BaseWrappedObject();
119
120   /**
121    * @return true if nothing else is referencing this object
122    */
123   bool IsReferenced();
124
125   /**
126    * @brief Called when the v8 garbage collector decides the JavaScript object (which contains the Dali wrapped object)
127    * is no longer used / reachable.
128    */
129   static void WeakCallback( const v8::WeakCallbackData<v8::Object,BaseWrappedObject >& data);
130
131   /**
132    * @brief create and set the internal fields of a JavaScript object.
133    * Currently has 2 internal fields, 1 is a pointer to dali wrapped object, 2nd is the type.
134    */
135   void SetJavascriptObject( v8::Isolate* isolate, v8::Local<v8::Object>& object );
136
137   /**
138    * @return true if the object is of a certain wrapped type (e.g. ImageActor )
139    */
140   static bool IsWrappedType( v8::Isolate* isolate, const v8::Local<v8::Object>& object,  BaseWrappedObject::Type type);
141
142   /**
143    * @return true if the object is a wrapped property value
144    */
145   static bool IsWrappedTypeAPropertyValue(const v8::Local<v8::Object>& object);
146
147   /**
148    * @brief Extracts the Dali wrapped object from the javascript object
149    */
150   static BaseWrappedObject* UnWrap( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
151
152   /**
153    * @return the wrapped type
154    */
155   Type GetType();
156
157   virtual SignalManager* GetSignalManager();
158
159 protected:
160
161   /**
162    * Constructor
163    * @param type wrapped type
164    * @param gc garbage collector interface
165    */
166   BaseWrappedObject( Type type, GarbageCollectorInterface& gc );
167
168 private:
169
170   BaseWrappedObject();
171
172   /**
173    * We store a  persistent handle to the JavaScript object and then set it to weak.
174    * This means we be notified when v8 decides it's no longer required and we can delete
175    * the associated Dali object
176    */
177   v8::Persistent<v8::Object > mWeakPersistentHandle;
178   Type mWrappedType;                                    ///< wrapped type
179   GarbageCollectorInterface& mGarbageCollector;         ///< Dali garbage collector
180 };
181
182 } // V8Plugin
183
184 } // Dali
185
186 #endif // header __DALI_V8PLUGIN_DALI_OBJECT_LIFETIME_MANAGER_H__