Upstream version 7.35.139.0
[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 application = requireNative('application');
6 var common = requireNative('widget_common');
7
8 var empty = "";
9 var zero = 0;
10
11 var widgetStringInfo = {
12   "author"      : empty,
13   "description" : empty,
14   "name"        : empty,
15   "shortName"   : empty,
16   "version"     : empty,
17   "id"          : empty,
18   "authorEmail" : empty,
19   "authorHref"  : empty,
20   "width"       : zero,
21   "height"      : zero
22 };
23
24 function defineReadOnlyProperty(object, key, value) {
25   Object.defineProperty(object, key, {
26     configurable: false,
27     enumerable: true,
28     get: function() {
29       if (key == "width") {
30         value = window.innerWidth;
31       } else if (key == "height") {
32         value = window.innerHeight;
33       } else if (value == empty) {
34         value = extension.internal.sendSyncMessage(
35             { cmd: 'GetWidgetInfo', widgetKey: key });
36       }
37
38       return value;
39     }
40   });
41 }
42
43 for (var key in widgetStringInfo) {
44   defineReadOnlyProperty(exports, key, widgetStringInfo[key]);
45 }
46
47 var WidgetStorage = function() {
48   var _keyList = new Array();
49
50   this.init = function() {
51     var result = extension.internal.sendSyncMessage(
52         { cmd: 'GetAllItems' });
53     for (var itemKey in result) {
54       var itemValue = result[itemKey];
55       this[itemKey] = itemValue;
56       _keyList.push(itemKey);
57     }
58   }
59
60   this.__defineGetter__('length', function() {
61     return _keyList.length;
62   });
63
64   this.key = function(index) {
65     return _keyList[index];
66   }
67
68   this.getItem = function(itemKey) {
69     return this[String(itemKey)];
70   } 
71
72   this.setItem = function(itemKey, itemValue) {
73     var result = extension.internal.sendSyncMessage({
74         cmd: 'SetPreferencesItem',
75         preferencesItemKey: String(itemKey),
76         preferencesItemValue: String(itemValue) });
77
78     if (result) {
79       this[String(itemKey)] = String(itemValue);
80       _keyList.push(String(itemKey));
81     } else {
82       throw new common.CustomDOMException(
83           common.CustomDOMException.NO_MODIFICATION_ALLOWED_ERR,
84           'The object can not be modified.');
85     }
86   };
87   
88   this.removeItem = function(itemKey) {
89     var result = extension.internal.sendSyncMessage({
90         cmd: 'RemovePreferencesItem',
91         preferencesItemKey: String(itemKey) });
92
93     if (result) {
94       delete this[itemKey];
95       delete _keyList[_keyList.indexOf(String(itemKey))];
96     } else {
97       throw new common.CustomDOMException(
98           common.CustomDOMException.NO_MODIFICATION_ALLOWED_ERR,
99           'The object can not be modified.');
100     }
101   }
102
103   this.clear = function() {
104     var itemKey;
105     var result = extension.internal.sendSyncMessage({
106         cmd: 'ClearAllItems' });
107
108     if (!result)
109       return;
110
111     for (var i = _keyList.length-1; i >= 0; --i) {
112       // if the itemKey is still in DB(e.g. readonly),
113       // we should keep it in JS side.
114       var exists = extension.internal.sendSyncMessage({
115           cmd: 'KeyExists',
116           preferencesItemKey: _keyList[i] });
117
118       if (!exists) {
119         delete this[_keyList[i]];
120         _keyList.splice(i, 1);
121       }
122     }
123   }
124
125   this.init();
126 };
127
128 var widgetStorage = new WidgetStorage();
129 exports.preferences = widgetStorage;
130
131 exports.toString = function() {
132   return '[object Widget]';
133 }
134
135 Object.defineProperty(exports, 'preferences', {
136   configurable: false,
137   enumerable: false,
138   get: function() {
139     return widgetStorage;
140   }
141 });