2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "Converter.h"
19 #include <Commons/Exception.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/ScopedJSStringRef.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include "CallHistoryDefine.h"
25 #include "JSCallHistoryEntry.h"
26 #include "JSRemoteParty.h"
29 using namespace WrtDeviceApis;
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
34 namespace CallHistory {
36 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
38 static bool init = initializeAllowedProperties();
42 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& jsValue)
44 return toVectorOfT_(jsValue, &Converter::toULong);
47 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& jsValue)
49 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
50 ThrowMsg(Commons::ConversionException, "Undefined history entry");
53 if (!JSIsArrayValue(m_context, jsValue)) {
54 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
58 CallHistoryEntryList result;
59 JSObjectRef objArg = toJSObjectRef(jsValue);
60 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
61 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
62 result.push_back(toCallHistoryEntryProperties(element));
65 } catch (Commons::ConversionException& ex) {
66 ThrowMsg(Commons::ConversionException, ex.GetMessage());
67 } catch (Commons::InvalidArgumentException& ex) {
68 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
69 } catch (Commons::Exception& ex) {
70 ThrowMsg(Commons::Exception, ex.GetMessage());
75 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
77 StringArrayPtr result(new StringArray());
79 if (!JSValueIsNull(m_context, jsValue)) {
80 JSObjectRef jsObject = toJSObjectRef(jsValue);
82 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
83 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
84 result->push_back(toString(element));
87 } catch (Commons::ConversionException& ex) {
88 ThrowMsg(Commons::ConversionException, ex.GetMessage());
89 } catch (Commons::InvalidArgumentException& ex) {
90 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
91 } catch (Commons::Exception& ex) {
92 ThrowMsg(Commons::Exception, ex.GetMessage());
97 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
99 RemotePartyListPtr result(new RemotePartyList());
101 if (!JSValueIsNull(m_context, jsValue)) {
102 JSObjectRef jsObject = toJSObjectRef(jsValue);
104 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
105 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
106 result->push_back(toRemoteParty(element));
109 } catch (Commons::ConversionException& ex) {
110 ThrowMsg(Commons::ConversionException, ex.GetMessage());
111 } catch (Commons::InvalidArgumentException& ex) {
112 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
113 } catch (Commons::Exception& ex) {
114 ThrowMsg(Commons::Exception, ex.GetMessage());
119 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
122 return JSRemoteParty::getRemoteParty(m_context, jsValue);
123 } catch (Commons::ConversionException& ex) {
124 ThrowMsg(Commons::ConversionException, ex.GetMessage());
125 } catch (Commons::Exception& ex) {
126 ThrowMsg(Commons::Exception, ex.GetMessage());
130 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
133 return JSCallHistoryEntry::getCallHistoryEntry(m_context, jsValue);
134 } catch (Commons::ConversionException& ex) {
135 ThrowMsg(Commons::ConversionException, ex.GetMessage());
136 } catch (Commons::Exception& ex) {
137 ThrowMsg(Commons::Exception, ex.GetMessage());
141 EventCallHistoryListenerPrivateDataPtr
142 Converter::toEventCallHistoryListenerPrivateData(const JSValueRef &jsValue, JSContextRef context)
147 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
148 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback is null or undefined");
150 if (!JSValueIsObject(m_context, jsValue) || JSObjectIsFunction(m_context, toJSObjectRef(jsValue))) {
151 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback");
154 JSObjectRef objectCallbacks = toJSObjectRef(jsValue);
155 CallHistoryChangeCB result;
157 result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onadded");
158 result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onchanged");
159 result.onRemoved = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onremoved");
161 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context);
162 JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context);
163 JSCallbackManagerPtr onRemovedCbm = JSCallbackManager::createObject(context);
165 if (!JSValueIsNull(m_context, result.onAdded) && !JSValueIsUndefined(m_context, result.onAdded)) {
166 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onAdded))) {
167 onAddedCbm->setOnSuccess(result.onAdded);
169 ThrowMsg(Commons::ConversionException, "Type missmatch error : onadded callback");
173 if (!JSValueIsNull(m_context, result.onChanged) && !JSValueIsUndefined(m_context, result.onChanged)) {
174 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onChanged))) {
175 onChangedCbm->setOnSuccess(result.onChanged);
177 ThrowMsg(Commons::ConversionException, "Type missmatch error : onchanged callback");
181 if (!JSValueIsNull(m_context, result.onRemoved) && !JSValueIsUndefined(m_context, result.onRemoved)) {
182 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onRemoved))) {
183 onRemovedCbm->setOnSuccess(result.onRemoved);
184 LoggerD("onRemoved register");
187 ThrowMsg(Commons::ConversionException, "Type missmatch error : onremoved callback");
192 return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm, onRemovedCbm));
195 JSValueRef Converter::toJSValueRef(const CallHistoryEntryListPtr& arg, JSContextRef context)
197 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
199 ThrowMsg(Commons::ConversionException, "Could not create js array object");
201 CallHistoryEntryProperties tmpCallEntry;
204 for (size_t i = 0; i < (*arg).size(); i++) {
205 tmpCallEntry = *((*arg)[i]);
206 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
208 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
211 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
212 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
215 } catch (Commons::ConversionException& ex) {
216 ThrowMsg(Commons::ConversionException, ex.GetMessage());
217 } catch (Commons::InvalidArgumentException& ex) {
218 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
219 } catch (Commons::Exception& ex) {
220 ThrowMsg(Commons::Exception, ex.GetMessage());
225 JSValueRef Converter::toJSValueRef(const StringArrayPtr &arg, JSContextRef context)
227 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
229 ThrowMsg(Commons::ConversionException, "Could not create js array object");
232 for (size_t i = 0; i < (*arg).size(); i++) {
233 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
234 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
241 JSValueRef Converter::toJSValueRef(const RemotePartyListPtr& arg, JSContextRef context)
243 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
245 ThrowMsg(Commons::ConversionException, "Could not create js array object");
248 RemoteParty tmpRemoteParty;
251 for (size_t i = 0; i < (*arg).size(); i++) {
252 tmpRemoteParty = *((*arg)[i]);
253 JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
255 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
258 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
259 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
262 } catch (Commons::ConversionException& ex) {
263 ThrowMsg(Commons::ConversionException, ex.GetMessage());
264 } catch (Commons::InvalidArgumentException& ex) {
265 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
266 } catch (Commons::Exception& ex) {
267 ThrowMsg(Commons::Exception, ex.GetMessage());
272 bool Converter::initializeAllowedProperties()