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