Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/labs/net/webchannel/wirev8.js
1865 views
1
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Codec functions of the v8 wire protocol. Eventually we'd want
17
* to support pluggable wire-format to improve wire efficiency and to enable
18
* binary encoding. Such support will require an interface class, which
19
* will be added later.
20
*
21
* @visibility {:internal}
22
*/
23
24
25
goog.provide('goog.labs.net.webChannel.WireV8');
26
27
goog.require('goog.asserts');
28
goog.require('goog.json');
29
goog.require('goog.json.NativeJsonProcessor');
30
goog.require('goog.labs.net.webChannel.Wire');
31
goog.require('goog.structs');
32
33
34
35
/**
36
* The v8 codec class.
37
*
38
* @constructor
39
* @struct
40
*/
41
goog.labs.net.webChannel.WireV8 = function() {
42
/**
43
* Parser for a response payload. The parser should return an array.
44
* @private {!goog.string.Parser}
45
*/
46
this.parser_ = new goog.json.NativeJsonProcessor();
47
};
48
49
50
goog.scope(function() {
51
var WireV8 = goog.labs.net.webChannel.WireV8;
52
var Wire = goog.labs.net.webChannel.Wire;
53
54
55
/**
56
* Encodes a standalone message into the wire format.
57
*
58
* May throw exception if the message object contains any invalid elements.
59
*
60
* @param {!Object|!goog.structs.Map} message The message data.
61
* V8 only support JS objects (or Map).
62
* @param {!Array<string>} buffer The text buffer to write the message to.
63
* @param {string=} opt_prefix The prefix for each field of the object.
64
*/
65
WireV8.prototype.encodeMessage = function(message, buffer, opt_prefix) {
66
var prefix = opt_prefix || '';
67
try {
68
goog.structs.forEach(message, function(value, key) {
69
var encodedValue = value;
70
if (goog.isObject(value)) {
71
encodedValue = goog.json.serialize(value);
72
} // keep the fast-path for primitive types
73
buffer.push(prefix + key + '=' + encodeURIComponent(encodedValue));
74
});
75
} catch (ex) {
76
// We send a map here because lots of the retry logic relies on map IDs,
77
// so we have to send something (possibly redundant).
78
buffer.push(
79
prefix + 'type' +
80
'=' + encodeURIComponent('_badmap'));
81
throw ex;
82
}
83
};
84
85
86
/**
87
* Encodes all the buffered messages of the forward channel.
88
*
89
* @param {!Array<Wire.QueuedMap>} messageQueue The message data.
90
* V8 only support JS objects.
91
* @param {number} count The number of messages to be encoded.
92
* @param {?function(!Object)} badMapHandler Callback for bad messages.
93
* @return {string} the encoded messages
94
*/
95
WireV8.prototype.encodeMessageQueue = function(
96
messageQueue, count, badMapHandler) {
97
var sb = ['count=' + count];
98
var offset;
99
if (count > 0) {
100
// To save a bit of bandwidth, specify the base mapId and the rest as
101
// offsets from it.
102
offset = messageQueue[0].mapId;
103
sb.push('ofs=' + offset);
104
} else {
105
offset = 0;
106
}
107
for (var i = 0; i < count; i++) {
108
var mapId = messageQueue[i].mapId;
109
var map = messageQueue[i].map;
110
mapId -= offset;
111
try {
112
this.encodeMessage(map, sb, 'req' + mapId + '_');
113
} catch (ex) {
114
if (badMapHandler) {
115
badMapHandler(map);
116
}
117
}
118
}
119
return sb.join('&');
120
};
121
122
123
/**
124
* Decodes a standalone message received from the wire. May throw exception
125
* if text is ill-formatted.
126
*
127
* Must be valid JSON as it is insecure to use eval() to decode JS literals;
128
* and eval() is disallowed in Chrome apps too.
129
*
130
* Invalid JS literals include null array elements, quotas etc.
131
*
132
* @param {string} messageText The string content as received from the wire.
133
* @return {*} The decoded message object.
134
*/
135
WireV8.prototype.decodeMessage = function(messageText) {
136
var response = this.parser_.parse(messageText);
137
goog.asserts.assert(goog.isArray(response)); // throw exception
138
return response;
139
};
140
}); // goog.scope
141
142