c9226cae145ce69db65a39f9bd083973b5625f10
[platform/framework/web/crosswalk.git] / src / xwalk / application / extension / application_widget_api.js
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var common = requireNative('widget_common');
6
7 var empty = "";
8 var zero = 0;
9
10 var widgetStringInfo = {
11   "author"      : empty,
12   "description" : empty,
13   "name"        : empty,
14   "shortName"   : empty,
15   "version"     : empty,
16   "id"          : empty,
17   "authorEmail" : empty,
18   "authorHref"  : empty,
19   "width"       : zero,
20   "height"      : zero
21 };
22
23 function defineReadOnlyProperty(object, key, value) {
24   Object.defineProperty(object, key, {
25     configurable: false,
26     enumerable: true,
27     get: function() {
28       if (key == "width") {
29         value = window.innerWidth;
30       } else if (key == "height") {
31         value = window.innerHeight;
32       } else if (value == empty) {
33         value = extension.internal.sendSyncMessage(
34             { cmd: 'GetWidgetInfo', widgetKey: key });
35       }
36
37       return value;
38     }
39   });
40 }
41
42 for (var key in widgetStringInfo) {
43   defineReadOnlyProperty(exports, key, widgetStringInfo[key]);
44 }
45
46 var WidgetStorage = function() {
47   var _SetItem = function(itemKey, itemValue) {
48     var result = extension.internal.sendSyncMessage({
49         cmd: 'SetPreferencesItem',
50         preferencesItemKey: String(itemKey),
51         preferencesItemValue: String(itemValue) });
52
53     if (result) {
54       return itemValue;
55     } else {
56       throw new common.CustomDOMException(
57         common.CustomDOMException.NO_MODIFICATION_ALLOWED_ERR,
58         'The object can not be modified.');
59     }
60   }
61
62   var _GetSetter = function(itemKey) {
63     var _itemKey = itemKey;
64     return function(itemValue) {
65       return _SetItem(_itemKey, itemValue);
66     }
67   }
68
69   var _GetGetter = function(itemKey) {
70     var _itemKey = itemKey;
71     return function() {
72       var result = extension.internal.sendSyncMessage(
73           { cmd: 'GetItemValueByKey',
74             preferencesItemKey: String(itemKey) });
75       return result == empty ? null : result;
76     }
77   }
78
79   this.init = function() {
80     var result = extension.internal.sendSyncMessage({cmd: 'GetAllItems'});
81     for (var itemKey in result) {
82       this.__defineSetter__(String(itemKey), _GetSetter(itemKey));
83       this.__defineGetter__(String(itemKey), _GetGetter(itemKey));
84     }
85   }
86
87   this.__defineGetter__('length', function() {
88     var result = extension.internal.sendSyncMessage({cmd: 'GetAllItems'});
89     return Object.keys(result).length;
90   });
91
92   this.key = function(index) {
93     var result = extension.internal.sendSyncMessage({ cmd: 'GetAllItems'});
94     return Object.keys(result)[index];
95   }
96
97   this.getItem = function(itemKey) {
98     var result = extension.internal.sendSyncMessage({
99         cmd: 'GetItemValueByKey',
100         preferencesItemKey: String(itemKey)});
101     return result == empty ? null : result;
102   }
103
104   this.setItem = function(itemKey, itemValue) {
105     return _SetItem(itemKey, itemValue);
106   }
107
108   this.removeItem = function(itemKey) {
109     var result = extension.internal.sendSyncMessage({
110         cmd: 'RemovePreferencesItem',
111         preferencesItemKey: String(itemKey)});
112
113     if (!result) {
114       throw new common.CustomDOMException(
115           common.CustomDOMException.NO_MODIFICATION_ALLOWED_ERR,
116           'The object can not be modified.');
117     }
118   }
119
120   this.clear = function() {
121     extension.internal.sendSyncMessage({cmd: 'ClearAllItems'});
122   }
123
124   this.init();
125 };
126
127 var widgetStorage = new WidgetStorage();
128 exports.preferences = widgetStorage;
129
130 exports.toString = function() {
131   return '[object Widget]';
132 }
133
134 Object.defineProperty(exports, 'preferences', {
135   configurable: false,
136   enumerable: false,
137   get: function() {
138     return widgetStorage;
139   }
140 });