Merge "JavaScript binding for ItemView" into devel/master
[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_CONSTRAINER,
75       LINEAR_CONSTRAINER,
76       BUILDER,
77       STAGE,
78       FONT,
79       IMAGE,
80       IMAGE_ATTRIBUTES,
81       ACTOR,
82       ACTOR_PROPERTY,
83       ITEMVIEW,
84       ITEMFACTORY,
85       RENDER_TASK,
86       RENDER_TASK_LIST,
87       TIMER,
88       SHADER,
89       SAMPLER,
90       MATERIAL,
91       GEOMETRY,
92       RENDERER,
93       PROPERTY_BUFFER,
94       TAP_GESTURE,
95       SHADER_EFFECT,
96       PAN_GESTURE,
97       LONGPRESS_GESTURE,
98       PINCH_GESTURE,
99       KEYBOARD_FOCUS_MANAGER,
100       PAN_GESTURE_DETECTOR,
101       ////////////////////////////////////////////////////////
102       PROPERTY_VALUE_START_RANGE,  // start of property values
103       VECTOR2,
104       VECTOR3,
105       VECTOR4,
106       MATRIX,
107       MATRIX3,
108       RECTANGLE,
109       ROTATION,
110       PROPERTY_VALUE_END_RANGE,  // end of property values
111       ///////////////////////////////////////////////////////
112     };
113
114   /*
115    * A type enum for the wrapped object internal field
116    */
117   enum Field
118     {
119       FIELD_POINTER   = 0,
120       FIELD_TYPE      = 1,
121       FIELD_COUNT     = 2 // increase if more fields are added
122     };
123
124    /**
125    * @brief virtual destructor
126    */
127   virtual ~BaseWrappedObject();
128
129   /**
130    * @return true if nothing else is referencing this object
131    */
132   bool IsReferenced();
133
134   /**
135    * @brief Called when the v8 garbage collector decides the JavaScript object (which contains the Dali wrapped object)
136    * is no longer used / reachable.
137    */
138   static void WeakCallback( const v8::WeakCallbackData<v8::Object,BaseWrappedObject >& data);
139
140   /**
141    * @brief create and set the internal fields of a JavaScript object.
142    * Currently has 2 internal fields, 1 is a pointer to dali wrapped object, 2nd is the type.
143    */
144   void SetJavascriptObject( v8::Isolate* isolate, v8::Local<v8::Object>& object );
145
146   /**
147    * @return true if the object is of a certain wrapped type (e.g. Animation )
148    */
149   static bool IsWrappedType( v8::Isolate* isolate, const v8::Local<v8::Object>& object,  BaseWrappedObject::Type type);
150
151   /**
152    * @return true if the object is a wrapped property value
153    */
154   static bool IsWrappedTypeAPropertyValue(const v8::Local<v8::Object>& object);
155
156   /**
157    * @brief Extracts the Dali wrapped object from the javascript object
158    */
159   static BaseWrappedObject* UnWrap( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
160
161   /**
162    * @return the wrapped type
163    */
164   Type GetType();
165
166   virtual SignalManager* GetSignalManager();
167
168 protected:
169
170   /**
171    * Constructor
172    * @param type wrapped type
173    * @param gc garbage collector interface
174    */
175   BaseWrappedObject( Type type, GarbageCollectorInterface& gc );
176
177 private:
178
179   BaseWrappedObject();
180
181   /**
182    * We store a  persistent handle to the JavaScript object and then set it to weak.
183    * This means we be notified when v8 decides it's no longer required and we can delete
184    * the associated Dali object
185    */
186   v8::Persistent<v8::Object > mWeakPersistentHandle;
187   Type mWrappedType;                                    ///< wrapped type
188   GarbageCollectorInterface& mGarbageCollector;         ///< Dali garbage collector
189 };
190
191 } // V8Plugin
192
193 } // Dali
194
195 #endif // header __DALI_V8PLUGIN_DALI_OBJECT_LIFETIME_MANAGER_H__