Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / CommonsJavaScript / JSCallbackManager.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        JSCallbackManager.h
18  * @author      Lukasz Marek (l.marel@samsung.com)
19  * @version     0.1
20  */
21
22 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_CALLBACK_MANAGER_H_
23 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_CALLBACK_MANAGER_H_
24
25
26 #include <dpl/shared_ptr.h>
27 #include <Commons/IEvent.h>
28 #include <JavaScriptCore/JavaScript.h>
29
30 namespace WrtDeviceApis {
31 namespace CommonsJavaScript {
32 class JSCallbackManager;
33 typedef DPL::SharedPtr<JSCallbackManager> JSCallbackManagerPtr;
34
35 class JSCallbackManager : public WrtDeviceApis::Commons::IEventPrivateData
36 {
37   private:
38     JSCallbackManager()
39     {}
40
41     /**
42      *  Used to create JSCallbackManagerPtr object by factory method
43      * createObject
44      */
45     JSCallbackManager(JSContextRef context,
46                       JSObjectRef onSuccess,
47                       JSObjectRef onError);
48
49   public:
50
51     //JSCallbackManager( JSContextRef context, JSValueRef onSuccess, JSValueRef
52     // onError );
53
54     /**
55      * Create JSCallbackManagerPtr object witch checking callback parameters
56      * if callbacks are NULL it was programmer intention to not use callback and
57      * no exception will be rised
58      * if callbacks are some object but not a function it will rise exception
59      * @param[in] context - global context
60      * @param[in] onSuccess - success callback (may be NULL if not needed)
61      * @param[in] onError   - error callback (may be NULL if not needed)
62      * @param[in] acceptJSNullAsOnSuccess - converts JSNull on onSuccess to NULL
63      * when true
64      * @param[in] acceptJSNullAsOnError   - converts JSNull on onError to NULL
65      * when true
66      * @throw Commons::InvalidArgumentException - if callback is not null and is
67      * not a js function
68      */
69     static JSCallbackManagerPtr createObject(
70         JSContextRef context,
71         JSValueRef onSuccess = NULL,
72         JSValueRef onError = NULL,
73         bool acceptJSNullAsOnSuccess = false,
74         bool acceptJSNullAsOnError = false);
75
76     virtual ~JSCallbackManager();
77
78     void setContext(JSContextRef context);
79
80     JSContextRef getContext() const
81     {
82         return m_context;
83     }
84
85     void setOnSuccess(JSValueRef onSuccess);
86     JSValueRef getOnSuccess() const;
87     void setOnError(JSValueRef onError);
88     JSValueRef getOnError() const;
89     void setObject(JSObjectRef object);
90     JSObjectRef getObject() const;
91
92     void callOnSuccess();
93     void callOnSuccess(JSValueRef obj);
94     void callOnSuccess(JSValueRef obj[], int paramCount);
95
96     void callOnError();
97     void callOnError(JSValueRef obj);
98     void callOnError(JSValueRef obj[], int paramCount);
99
100   private:
101     void makeCallback(JSContextRef context,
102                       JSObjectRef object,
103                       JSObjectRef callback,
104                       JSValueRef argv[],
105                       unsigned argc);
106
107     JSObjectRef m_onSuccess;
108     JSObjectRef m_onError;
109     JSContextRef m_context;
110     JSObjectRef m_object;
111 };
112
113 bool isCallback(JSContextRef ctx,
114                 JSValueRef value,
115                 JSValueRef* exception);
116 }
117 }
118
119 #endif // WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_CALLBACK_MANAGER_H_