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 <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>
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace WrtDeviceApis::Commons;
41 using namespace DeviceAPI::Common;
47 JSClassRef JSBookmarkManager::m_jsClassRef = NULL;
49 JSClassDefinition JSBookmarkManager::m_jsClassInfo = {
50 0, // current (and only) version is 0
51 kJSClassAttributeNone, //attributes
52 "BookmarkManager", //class name
55 JSBookmarkManager::m_function, // static functions
56 JSBookmarkManager::initialize, // initialize
57 JSBookmarkManager::finalize, //finalize
61 NULL, //deleteProperty
62 NULL, //getPropertyNames
63 NULL, // callAsConstructor
65 JSBookmarkManager::hasInstance,
70 JSStaticFunction JSBookmarkManager::m_function[] = {
71 { "get", JSBookmarkManager::get, kJSPropertyAttributeNone },
72 { "add", JSBookmarkManager::add, kJSPropertyAttributeNone },
73 { "remove", JSBookmarkManager::remove, kJSPropertyAttributeNone },
77 const JSClassRef JSBookmarkManager::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
85 const JSClassDefinition* JSBookmarkManager::getClassInfo()
87 return &m_jsClassInfo;
90 void JSBookmarkManager::initialize(JSContextRef ctx, JSObjectRef object)
94 void JSBookmarkManager::finalize(JSObjectRef object)
98 bool JSBookmarkManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
99 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
102 JSValueRef JSBookmarkManager::get(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
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);
109 ArgumentValidator validator(ctx, argumentCount, arguments);
111 bool reculsive = validator.toBool(1, true);
113 JSObjectRef parentobj = NULL;
115 parentobj = validator.toObject(0, JSBookmarkFolder::getClassRef(), true);
117 BookmarkData * parentFolder = NULL;
119 parentFolder = static_cast<BookmarkData *>(JSObjectGetPrivate(parentobj));
122 throw TypeMismatchException("Private data is null");
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++) {
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));
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);
140 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
141 return converter.toJSValueRef(bookmarksArray);
143 }catch( const BasePlatformException& err){
144 return JSWebAPIException::throwException(ctx, exception, err);
148 JSValueRef JSBookmarkManager::add(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
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);
155 ArgumentValidator validator(ctx, argumentCount, arguments);
157 JSObjectRef bookmarkobj = NULL;
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());
164 BookmarkData * bookmark = static_cast<BookmarkData *>(JSObjectGetPrivate(bookmarkobj));
167 throw TypeMismatchException("Bookmark private data is null");
169 //convert parentFolder
170 JSObjectRef parentobj = NULL;
172 parentobj = validator.toObject(1, JSBookmarkFolder::getClassRef(), true);
174 BookmarkData * parentFolder = NULL;
176 parentFolder = static_cast<BookmarkData *>(JSObjectGetPrivate(parentobj));
178 throw TypeMismatchException("Parent Folder private data is null");
181 int parentId = UNDEFINED_ID;
183 parentId = parentFolder->m_id;
185 parentId = BookmarkManager::getInstance()->getRootFolderId();
187 if (parentId == UNDEFINED_ID)
188 throw Common::NotFoundException("Parent doesn't exist in db");
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);
195 }catch( const BasePlatformException& err){
196 return JSWebAPIException::throwException(ctx, exception, err);
198 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
199 return JSValueMakeUndefined(ctx);
203 JSValueRef JSBookmarkManager::remove(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
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);
210 ArgumentValidator validator(ctx, argumentCount, arguments);
212 JSObjectRef bookmarkobj = NULL;
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);
220 BookmarkData *bookmark = NULL;
222 bookmark = static_cast<BookmarkData *>(JSObjectGetPrivate(bookmarkobj));
224 throw TypeMismatchException("Bookmark private data is null");
227 BookmarkSearchDataPtr searchData(new BookmarkSearchData(bookmark));
229 BookmarkManager::getInstance()->remove(searchData);
231 }catch( const BasePlatformException& err){
232 return JSWebAPIException::throwException(ctx, exception, err);
234 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
235 return JSValueMakeUndefined(ctx);