wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Bookmark / JSBookmarkManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <JSTizenExceptionFactory.h>
19 #include <JSTizenException.h>
20 #include <SecurityExceptions.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <GlobalContextManager.h>
23 #include "JSBookmarkManager.h"
24 #include "JSBookmarkFolder.h"
25 #include "JSBookmarkItem.h"
26 #include "BookmarkManager.h"
27 #include "plugin_config.h"
28 #include <ArgumentValidator.h>
29 #include <PlatformException.h>
30 #include <JSWebAPIException.h>
31 #include <CommonsJavaScript/Converter.h>
32 #include <TimeTracer.h>
33
34 #include <string>
35 #include <vector>
36 #include <Logger.h>
37
38 using namespace std;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace WrtDeviceApis::Commons;
41 using namespace DeviceAPI::Common;
42
43
44 namespace DeviceAPI {
45 namespace Bookmark {
46
47 JSClassRef JSBookmarkManager::m_jsClassRef = NULL;
48
49 JSClassDefinition JSBookmarkManager::m_jsClassInfo = {
50     0,                                       // current (and only) version is 0
51     kJSClassAttributeNone,        //attributes
52     "BookmarkManager",                        //class name
53     NULL,                                 // parent class
54     NULL,                                 //static values
55     JSBookmarkManager::m_function,     // static functions
56     JSBookmarkManager::initialize,         // initialize
57     JSBookmarkManager::finalize,          //finalize
58     NULL,                                //hasProperty
59     NULL,   //getProperty
60     NULL,                                //setProperty
61     NULL,                                //deleteProperty
62     NULL,                                //getPropertyNames
63     NULL,                               // callAsConstructor
64     NULL,                               // constructor
65     JSBookmarkManager::hasInstance,
66     NULL // convertToType
67 };
68
69
70 JSStaticFunction JSBookmarkManager::m_function[] = {
71     { "get", JSBookmarkManager::get, kJSPropertyAttributeNone },
72     { "add", JSBookmarkManager::add, kJSPropertyAttributeNone },
73     { "remove", JSBookmarkManager::remove, kJSPropertyAttributeNone },
74     { 0, 0, 0 }
75 };
76
77 const JSClassRef JSBookmarkManager::getClassRef()
78 {
79     if (!m_jsClassRef) {
80         m_jsClassRef = JSClassCreate(&m_jsClassInfo);
81     }
82     return m_jsClassRef;
83 }
84
85 const JSClassDefinition* JSBookmarkManager::getClassInfo()
86 {
87     return &m_jsClassInfo;
88 }
89
90 void JSBookmarkManager::initialize(JSContextRef ctx, JSObjectRef object)
91 {
92 }
93
94 void JSBookmarkManager::finalize(JSObjectRef object)
95 {
96 }
97
98 bool JSBookmarkManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
99     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
100 }
101
102 JSValueRef JSBookmarkManager::get(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
103         LoggerD("Entered");
104         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
105         AceSecurityStatus status = BOOKMARK_CHECK_ACCESS(BOOKMARK_FUNCTION_API_READ_FUNCS);
106         TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
107
108     try{
109         ArgumentValidator validator(ctx, argumentCount, arguments);
110
111                 bool reculsive = validator.toBool(1, true);
112
113                 JSObjectRef parentobj = NULL;
114
115                 parentobj = validator.toObject(0, JSBookmarkFolder::getClassRef(), true);
116
117                 BookmarkData * parentFolder = NULL;
118                 if (parentobj) {
119                         parentFolder = static_cast<BookmarkData *>(JSObjectGetPrivate(parentobj));
120
121                         if (!parentFolder)
122                                 throw TypeMismatchException("Private data is null");
123                 }
124
125                 BookmarkSearchDataPtr searchData(new BookmarkSearchData(parentFolder,reculsive));
126                 std::vector<BookmarkDataPtr> bookmarkList = BookmarkManager::getInstance()->get(searchData);
127                 unsigned short bookmarksCount = bookmarkList.size();
128                 LoggerD("count : " << bookmarksCount);
129                 std::vector<JSValueRef> bookmarksArray;
130                 Converter converter(ctx);
131                 for (int i = 0; i < bookmarksCount; i++) {
132                         JSValueRef bookmark;
133                         LoggerD("bookmarkList["<<i<<"]:" << (bookmarkList[i]->m_title));
134                         if (bookmarkList[i]->m_type == BOOKMARKITEM_TYPE)
135                                 bookmark = static_cast<JSValueRef>(JSBookmarkItem::createJSObject(ctx, bookmarkList[i]->m_title, bookmarkList[i]->m_url, bookmarkList[i]->m_id, bookmarkList[i]->m_parentId));
136                         else
137                                 bookmark = static_cast<JSValueRef>(JSBookmarkFolder::createJSObject(ctx, bookmarkList[i]->m_title, bookmarkList[i]->m_id, bookmarkList[i]->m_parentId));
138                         bookmarksArray.push_back(bookmark);
139                 }
140                 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
141                 return converter.toJSValueRef(bookmarksArray);
142
143     }catch( const BasePlatformException& err){
144         return JSWebAPIException::throwException(ctx, exception, err);
145     }
146 }
147
148 JSValueRef JSBookmarkManager::add(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
149         LoggerD("Entered");
150         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
151         AceSecurityStatus status = BOOKMARK_CHECK_ACCESS(BOOKMARK_FUNCTION_API_WRITE_FUNCS);
152         TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
153
154     try{
155         ArgumentValidator validator(ctx, argumentCount, arguments);
156                 //convert bookmark
157                 JSObjectRef bookmarkobj = NULL;
158                 try {
159                         bookmarkobj = validator.toObject(0, JSBookmarkItem::getClassRef());
160                 } catch (const TypeMismatchException& err){
161                         LoggerD("Bookmark's type is not BookmarkItem");
162                         bookmarkobj = validator.toObject(0, JSBookmarkFolder::getClassRef());
163                 }
164                 BookmarkData * bookmark = static_cast<BookmarkData *>(JSObjectGetPrivate(bookmarkobj));
165
166                 if (!bookmark)
167                         throw TypeMismatchException("Bookmark private data is null");
168
169                 //convert parentFolder
170                 JSObjectRef parentobj = NULL;
171
172                 parentobj = validator.toObject(1, JSBookmarkFolder::getClassRef(), true);
173
174                 BookmarkData * parentFolder = NULL;
175                 if (parentobj) {
176                         parentFolder = static_cast<BookmarkData *>(JSObjectGetPrivate(parentobj));
177                         if (!parentFolder)
178                                 throw TypeMismatchException("Parent Folder private data is null");
179                 }
180
181                 int parentId = UNDEFINED_ID;
182                 if (parentFolder)
183                         parentId = parentFolder->m_id;
184                 else
185                         parentId = BookmarkManager::getInstance()->getRootFolderId();
186
187                 if (parentId == UNDEFINED_ID)
188                         throw Common::NotFoundException("Parent doesn't exist in db");
189
190         bookmark->m_id = BookmarkManager::getInstance()->add(bookmark->m_type, parentId, bookmark->m_title, bookmark->m_url);
191                 bookmark->m_parentId = parentId;
192                 LoggerD("bookmark id:" << bookmark->m_id);
193                 LoggerD("bookmark parent id:" << bookmark->m_parentId);
194                 
195     }catch( const BasePlatformException& err){
196         return JSWebAPIException::throwException(ctx, exception, err);
197     }
198         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
199     return JSValueMakeUndefined(ctx);
200
201 }
202
203 JSValueRef JSBookmarkManager::remove(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
204         LoggerD("Entered");
205         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
206         AceSecurityStatus status = BOOKMARK_CHECK_ACCESS(BOOKMARK_FUNCTION_API_WRITE_FUNCS);
207         TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
208
209     try{
210         ArgumentValidator validator(ctx, argumentCount, arguments);
211
212                 JSObjectRef bookmarkobj = NULL;
213                 try {
214                         bookmarkobj = validator.toObject(0, JSBookmarkItem::getClassRef(), true);
215                 } catch (const TypeMismatchException& err){
216                         LoggerD("Bookmark's type is not BookmarkItem");
217                         bookmarkobj = validator.toObject(0, JSBookmarkFolder::getClassRef(), true);
218                 }
219
220                 BookmarkData *bookmark = NULL;
221                 if (bookmarkobj) {
222                         bookmark = static_cast<BookmarkData *>(JSObjectGetPrivate(bookmarkobj));
223                         if (!bookmark)
224                                 throw TypeMismatchException("Bookmark private data is null");
225                 }
226         
227                 BookmarkSearchDataPtr searchData(new BookmarkSearchData(bookmark));
228
229         BookmarkManager::getInstance()->remove(searchData);
230
231     }catch( const BasePlatformException& err){
232         return JSWebAPIException::throwException(ctx, exception, err);
233     }
234         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
235     return JSValueMakeUndefined(ctx);
236
237 }
238 } // Bookmark
239 } // TizenApis
240
241