Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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 #include <dpl/shared_ptr.h>
26 #include <Commons/IEvent.h>
27 #include <JavaScriptCore/JavaScript.h>
28
29 namespace WrtDeviceApis {
30 namespace CommonsJavaScript {
31 class JSCallbackManager;
32 typedef DPL::SharedPtr<JSCallbackManager> JSCallbackManagerPtr;
33
34 class JSCallbackManager : public WrtDeviceApis::Commons::IEventPrivateData
35 {
36   private:
37     JSCallbackManager()
38     {}
39
40     /**
41      *  Used to create JSCallbackManagerPtr object by factory method
42      * createObject
43      */
44     JSCallbackManager(JSContextRef context,
45                       JSObjectRef onSuccess,
46                       JSObjectRef onError);
47
48   public:
49
50     //JSCallbackManager( JSContextRef context, JSValueRef onSuccess, JSValueRef
51     // onError );
52
53     /**
54      * Create JSCallbackManagerPtr object witch checking callback parameters
55      * if callbacks are NULL it was programmer intention to not use callback and
56      * no exception will be rised
57      * if callbacks are some object but not a function it will rise exception
58      * @param[in] context - global context
59      * @param[in] onSuccess - success callback (may be NULL if not needed)
60      * @param[in] onError   - error callback (may be NULL if not needed)
61      * @param[in] acceptJSNullAsOnSuccess - converts JSNull on onSuccess to NULL
62      * when true
63      * @param[in] acceptJSNullAsOnError   - converts JSNull on onError to NULL
64      * when true
65      * @throw Commons::InvalidArgumentException - if callback is not null and is
66      * not a js function
67      */
68     static JSCallbackManagerPtr createObject(
69         JSContextRef context,
70         JSValueRef onSuccess = NULL,
71         JSValueRef onError = NULL,
72         bool acceptJSNullAsOnSuccess = false,
73         bool acceptJSNullAsOnError = false);
74
75     virtual ~JSCallbackManager();
76
77     void setContext(JSContextRef context);
78
79     JSContextRef getContext() const
80     {
81         return m_context;
82     }
83
84     void setOnSuccess(JSValueRef onSuccess);
85     JSValueRef getOnSuccess() const;
86     void setOnError(JSValueRef onError);
87     JSValueRef getOnError() const;
88     void setObject(JSObjectRef object);
89     JSObjectRef getObject() const;
90
91     void callOnSuccess();
92     void callOnSuccess(JSValueRef obj);
93     void callOnSuccess(JSValueRef obj[], int paramCount);
94
95     void callOnError();
96     void callOnError(JSValueRef obj);
97     void callOnError(JSValueRef obj[], int paramCount);
98
99   private:
100     void makeCallback(JSContextRef context,
101                       JSObjectRef object,
102                       JSObjectRef callback,
103                       JSValueRef argv[],
104                       unsigned argc);
105
106     JSObjectRef m_onSuccess;
107     JSObjectRef m_onError;
108     JSContextRef m_context;
109     JSObjectRef m_object;
110 };
111
112 bool isCallback(JSContextRef ctx,
113                 JSValueRef value,
114                 JSValueRef* exception);
115 }
116 }
117
118 #endif // WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_CALLBACK_MANAGER_H_