2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 var validator_ = xwalk.utils.validator;
18 var privilege_ = xwalk.utils.privilege;
19 var types_ = validator_.Types;
20 var native_ = new xwalk.utils.NativeManager(extension);
22 function EditManager() {
26 EditManager.prototype.allow = function() {
30 EditManager.prototype.disallow = function() {
34 var _edit = new EditManager();
36 function BookmarkManager() {}
38 BookmarkManager.prototype.get = function() {
39 xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_READ);
41 var args = validator_.validateArgs(arguments, [
44 type: types_.PLATFORM_OBJECT,
45 values: [tizen.BookmarkFolder, tizen.BookmarkItem],
58 if (arguments.length === 0 || args.parentFolder === null) {
59 result = provider.getFolderItems(provider.getRootId(), args.recursive);
61 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
64 if (args.parentFolder.id === null || args.parentFolder.id === 0)
65 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
67 result = provider.getFolderItems(args.parentFolder.id, args.recursive);
69 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
73 BookmarkManager.prototype.add = function() {
74 xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_WRITE);
76 var args = validator_.validateArgs(arguments, [
79 type: types_.PLATFORM_OBJECT,
80 values: [tizen.BookmarkFolder, tizen.BookmarkItem],
86 type: types_.PLATFORM_OBJECT,
87 values: tizen.BookmarkFolder,
92 if (arguments.length == 1 || args.parentFolder === null) {
93 if (args.bookmark.id) {
94 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
96 if (!provider.addToFolder(args.bookmark, provider.getRootId())) {
97 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
101 if (!args.parentFolder.id) {
102 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
104 if (!provider.addToFolder(args.bookmark, args.parentFolder.id)) {
105 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
109 BookmarkManager.prototype.remove = function() {
110 xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_WRITE);
112 var args = validator_.validateArgs(arguments, [
115 type: types_.PLATFORM_OBJECT,
116 values: [tizen.BookmarkFolder, tizen.BookmarkItem],
122 if (!arguments.length || args.bookmark === null) {
123 if (native_.isFailure(native_.callSync('Bookmark_removeAll')))
124 throw new WebAPIException(WebAPIException.SECURITY_ERR);
127 if (!args.bookmark.id)
128 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
129 if (native_.isFailure(native_.callSync('Bookmark_remove', {
130 id: args.bookmark.id})))
131 throw new WebAPIException(WebAPIException.SECURITY_ERR);
134 args.bookmark.id = null;
135 args.bookmark.parent = undefined;
139 function BookmarkProvider() {}
141 BookmarkProvider.prototype.addToFolder = function() {
142 var args = validator_.validateArgs(arguments, [
145 type: types_.PLATFORM_OBJECT,
146 values: [tizen.BookmarkFolder, tizen.BookmarkItem],
156 var ret = native_.callSync('Bookmark_add',
158 title: args.bookmark.title,
159 url: String(args.bookmark.url),
160 parentId: args.parentId,
161 type: args.bookmark instanceof tizen.BookmarkFolder ? 1 : 0
164 if (native_.isFailure(ret)) {
167 var ret_id = native_.getResultObject(ret);
169 args.bookmark.id = ret_id;
170 args.bookmark.parent = this.getFolder(args.parentId);
175 BookmarkProvider.prototype.getFolder = function() {
176 var args = validator_.validateArgs(arguments, [
184 if (arguments.length === 0 || args.id <= 0)
186 if (args.id == this.getRootId())
189 var ret = native_.callSync('Bookmark_get', {
194 if (native_.isFailure(ret)) {
195 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
198 var folder = native_.getResultObject(ret);
199 if (folder === undefined || folder === null)
200 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
202 var obj = new tizen.BookmarkFolder(folder[0].title);
203 obj.id = folder[0].id;
204 obj.parent = this.getFolder(folder[0].parentId);
208 BookmarkProvider.prototype.getFolderItems = function() {
209 var args = validator_.validateArgs(arguments, [
218 type: types_.BOOLEAN,
224 var ret = native_.callSync('Bookmark_get', {
229 if (native_.isFailure(ret)) {
230 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
233 var folder = native_.getResultObject(ret);
234 if (folder === undefined)
235 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
240 var len = folder.length;
242 for (var i = 0; item = folder[i], i < len; i++) {
243 if (Number(item.type) === 0)
244 obj = new tizen.BookmarkItem(item.title, item.url);
246 obj = new tizen.BookmarkFolder(item.title);
250 obj.parent = this.getFolder(item.parentId);
254 if (args.recursive && Number(item.type) !== 0)
255 result = result.concat(this.getFolderItems(item.id, true));
260 BookmarkProvider.prototype.getRootId = function() {
261 var ret = native_.callSync('Bookmark_getRootId');
262 if (native_.isFailure(ret)) {
263 throw native_.getErrorObject(ret);
265 var rootId = native_.getResultObject(ret);
266 return Number(rootId);
269 var provider = new BookmarkProvider();
271 tizen.BookmarkItem = function() {
272 validator_.isConstructorCall(this, tizen.BookmarkItem);
273 var args = validator_.validateArgs(arguments, [
288 Object.defineProperties(this, {
293 set: function(new_parent) {
295 parent_ = new_parent;
309 if (args.url === "undefined")
310 args.url = undefined;
320 set: function(new_id) {
330 tizen.BookmarkFolder = function() {
331 validator_.isConstructorCall(this, tizen.BookmarkFolder);
332 var args = validator_.validateArgs(arguments, [
344 Object.defineProperties(this, {
349 set: function(new_parent) {
351 parent_ = new_parent;
367 set: function(new_id) {
376 exports = new BookmarkManager();