Diffstat (limited to 'frontend/delta/js/Clipperz/PM/Proxy.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/delta/js/Clipperz/PM/Proxy.js | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/PM/Proxy.js b/frontend/delta/js/Clipperz/PM/Proxy.js new file mode 100644 index 0000000..2ac684a --- a/dev/null +++ b/frontend/delta/js/Clipperz/PM/Proxy.js | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2013 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz, the online password manager. | ||
6 | For further information about its features and functionalities please | ||
7 | refer to http://www.clipperz.com. | ||
8 | |||
9 | * Clipperz is free software: you can redistribute it and/or modify it | ||
10 | under the terms of the GNU Affero General Public License as published | ||
11 | by the Free Software Foundation, either version 3 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | * Clipperz is distributed in the hope that it will be useful, but | ||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | See the GNU Affero General Public License for more details. | ||
18 | |||
19 | * You should have received a copy of the GNU Affero General Public | ||
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | ||
21 | |||
22 | */ | ||
23 | |||
24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
25 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
26 | |||
27 | //============================================================================= | ||
28 | |||
29 | Clipperz.PM.Proxy = function(args) { | ||
30 | args = args || {}; | ||
31 | |||
32 | this._shouldPayTolls = args.shouldPayTolls || false; | ||
33 | |||
34 | this._tolls = { | ||
35 | 'CONNECT':[], | ||
36 | 'REGISTER':[], | ||
37 | 'MESSAGE':[] | ||
38 | }; | ||
39 | |||
40 | if (args.isDefault === true) { | ||
41 | Clipperz.PM.Proxy.defaultProxy = this; | ||
42 | } | ||
43 | |||
44 | return this; | ||
45 | } | ||
46 | |||
47 | Clipperz.PM.Proxy.prototype = MochiKit.Base.update(null, { | ||
48 | |||
49 | 'toString': function() { | ||
50 | return "Clipperz.PM.Proxy"; | ||
51 | }, | ||
52 | |||
53 | //========================================================================= | ||
54 | |||
55 | 'shouldPayTolls': function() { | ||
56 | return this._shouldPayTolls; | ||
57 | }, | ||
58 | |||
59 | //------------------------------------------------------------------------- | ||
60 | |||
61 | 'tolls': function() { | ||
62 | return this._tolls; | ||
63 | }, | ||
64 | |||
65 | //------------------------------------------------------------------------- | ||
66 | |||
67 | 'payToll': function(aRequestType, someParameters) { | ||
68 | vardeferredResult; | ||
69 | |||
70 | if (this.shouldPayTolls()) { | ||
71 | deferredResult = new Clipperz.Async.Deferred("Proxy.payToll", {trace:false}); | ||
72 | |||
73 | if (this.tolls()[aRequestType].length == 0) { | ||
74 | deferredResult.addMethod(this, 'sendMessage', 'knock', {requestType:aRequestType}); | ||
75 | deferredResult.addMethod(this, 'setTollCallback'); | ||
76 | } | ||
77 | deferredResult.addMethod(this.tolls()[aRequestType], 'pop'); | ||
78 | deferredResult.addCallback(MochiKit.Base.methodcaller('deferredPay')); | ||
79 | deferredResult.addCallback(function(aToll) { | ||
80 | var result; | ||
81 | |||
82 | result = { | ||
83 | parameters: someParameters, | ||
84 | toll: aToll | ||
85 | } | ||
86 | |||
87 | return result; | ||
88 | }); | ||
89 | |||
90 | deferredResult.callback(); | ||
91 | } else { | ||
92 | deferredResult = MochiKit.Async.succeed({parameters:someParameters}); | ||
93 | } | ||
94 | |||
95 | return deferredResult; | ||
96 | }, | ||
97 | |||
98 | //------------------------------------------------------------------------- | ||
99 | |||
100 | 'addToll': function(aToll) { | ||
101 | this.tolls()[aToll.requestType()].push(aToll); | ||
102 | }, | ||
103 | |||
104 | //========================================================================= | ||
105 | |||
106 | 'setTollCallback': function(someParameters) { | ||
107 | if (typeof(someParameters['toll']) != 'undefined') { | ||
108 | this.addToll(new Clipperz.PM.Toll(someParameters['toll'])); | ||
109 | } | ||
110 | |||
111 | return someParameters['result']; | ||
112 | }, | ||
113 | |||
114 | //========================================================================= | ||
115 | |||
116 | 'registration': function (someParameters) { | ||
117 | return this.processMessage('registration', someParameters, 'REGISTER'); | ||
118 | }, | ||
119 | |||
120 | 'handshake': function (someParameters) { | ||
121 | return this.processMessage('handshake', someParameters, 'CONNECT'); | ||
122 | }, | ||
123 | |||
124 | 'message': function (someParameters) { | ||
125 | return this.processMessage('message', someParameters, 'MESSAGE'); | ||
126 | }, | ||
127 | |||
128 | 'logout': function (someParameters) { | ||
129 | return this.processMessage('logout', someParameters, 'MESSAGE'); | ||
130 | }, | ||
131 | |||
132 | //========================================================================= | ||
133 | |||
134 | 'processMessage': function (aFunctionName, someParameters, aRequestType) { | ||
135 | vardeferredResult; | ||
136 | |||
137 | deferredResult = new Clipperz.Async.Deferred("Proxy.processMessage", {trace:false}); | ||
138 | deferredResult.addMethod(this, 'payToll', aRequestType); | ||
139 | deferredResult.addMethod(this, 'sendMessage', aFunctionName); | ||
140 | deferredResult.addMethod(this, 'setTollCallback'); | ||
141 | deferredResult.callback(someParameters); | ||
142 | |||
143 | return deferredResult; | ||
144 | }, | ||
145 | |||
146 | //========================================================================= | ||
147 | |||
148 | '_sendMessage': function (aFunctionName, aVersion, someParameters) { | ||
149 | throw Clipperz.Base.exception.AbstractMethod; | ||
150 | }, | ||
151 | |||
152 | 'sendMessage': function (aFunctionName, someParameters) { | ||
153 | var deferredResult; | ||
154 | |||
155 | //TODO: read actual application version for a property set at build time | ||
156 | deferredResult = new Clipperz.Async.Deferred("Proxy.sendMessage", {trace:false}); | ||
157 | deferredResult.addMethod(this, '_sendMessage', aFunctionName, 'fake-app-version'); | ||
158 | deferredResult.addErrback(MochiKit.Base.method(this, 'handleError')); | ||
159 | deferredResult.callback(someParameters); | ||
160 | |||
161 | return deferredResult; | ||
162 | }, | ||
163 | |||
164 | //------------------------------------------------------------------------- | ||
165 | |||
166 | 'handleError': function (anError) { | ||
167 | if (anError['message'] == 'Wrong application version') { | ||
168 | anError['isPermanent'] = true; | ||
169 | } | ||
170 | return anError; | ||
171 | }, | ||
172 | |||
173 | //========================================================================= | ||
174 | |||
175 | 'isReadOnly': function () { | ||
176 | return false; | ||
177 | }, | ||
178 | |||
179 | 'canRegisterNewUsers': function () { | ||
180 | return true; | ||
181 | }, | ||
182 | |||
183 | //========================================================================= | ||
184 | __syntaxFix__: "syntax fix" | ||
185 | |||
186 | }); | ||