Path: blob/trunk/third_party/closure/goog/net/xpc/xpc.js
1865 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Provides the namesspace for client-side communication16* between pages originating from different domains (it works also17* with pages from the same domain, but doing that is kinda18* pointless).19*20* The only publicly visible class is goog.net.xpc.CrossPageChannel.21*22* Note: The preferred name for the main class would have been23* CrossDomainChannel. But as there already is a class named like24* that (which serves a different purpose) in the maps codebase,25* CrossPageChannel was chosen to avoid confusion.26*27* CrossPageChannel abstracts the underlying transport mechanism to28* provide a common interface in all browsers.29*30*31* @suppress {underscore}32*/3334/*35TODO(user)36- resolve fastback issues in Safari (IframeRelayTransport)37*/383940/**41* Namespace for CrossPageChannel42*/43goog.provide('goog.net.xpc');44goog.provide('goog.net.xpc.CfgFields');45goog.provide('goog.net.xpc.ChannelStates');46goog.provide('goog.net.xpc.TransportNames');47goog.provide('goog.net.xpc.TransportTypes');48goog.provide('goog.net.xpc.UriCfgFields');49goog.require('goog.log');5051goog.forwardDeclare('goog.net.xpc.CrossPageChannel'); // circular525354/**55* Enum used to identify transport types.56* @enum {number}57*/58goog.net.xpc.TransportTypes = {59NATIVE_MESSAGING: 1,60FRAME_ELEMENT_METHOD: 2,61IFRAME_RELAY: 3,62IFRAME_POLLING: 4,63FLASH: 5,64NIX: 6,65DIRECT: 766};676869/**70* Enum containing transport names. These need to correspond to the71* transport class names for createTransport_() to work.72* @const {!Object<string,string>}73*/74goog.net.xpc.TransportNames = {75'1': 'NativeMessagingTransport',76'2': 'FrameElementMethodTransport',77'3': 'IframeRelayTransport',78'4': 'IframePollingTransport',79'5': 'FlashTransport',80'6': 'NixTransport',81'7': 'DirectTransport'82};838485// TODO(user): Add auth token support to other methods.868788/**89* Field names used on configuration object.90* @const91*/92goog.net.xpc.CfgFields = {93/**94* Channel name identifier.95* Both peers have to be initialized with96* the same channel name. If not present, a channel name is97* generated (which then has to transferred to the peer somehow).98*/99CHANNEL_NAME: 'cn',100/**101* Authorization token. If set, NIX will use this authorization token102* to validate the setup.103*/104AUTH_TOKEN: 'at',105/**106* Remote party's authorization token. If set, NIX will validate this107* authorization token against that sent by the other party.108*/109REMOTE_AUTH_TOKEN: 'rat',110/**111* The URI of the peer page.112*/113PEER_URI: 'pu',114/**115* Ifame-ID identifier.116* The id of the iframe element the peer-document lives in.117*/118IFRAME_ID: 'ifrid',119/**120* Transport type identifier.121* The transport type to use. Possible values are entries from122* goog.net.xpc.TransportTypes. If not present, the transport is123* determined automatically based on the useragent's capabilities.124*/125TRANSPORT: 'tp',126/**127* Local relay URI identifier (IframeRelayTransport-specific).128* The URI (can't contain a fragment identifier) used by the peer to129* relay data through.130*/131LOCAL_RELAY_URI: 'lru',132/**133* Peer relay URI identifier (IframeRelayTransport-specific).134* The URI (can't contain a fragment identifier) used to relay data135* to the peer.136*/137PEER_RELAY_URI: 'pru',138/**139* Local poll URI identifier (IframePollingTransport-specific).140* The URI (can't contain a fragment identifier)which is polled141* to receive data from the peer.142*/143LOCAL_POLL_URI: 'lpu',144/**145* Local poll URI identifier (IframePollingTransport-specific).146* The URI (can't contain a fragment identifier) used to send data147* to the peer.148*/149PEER_POLL_URI: 'ppu',150/**151* The hostname of the peer window, including protocol, domain, and port152* (if specified). Used for security sensitive applications that make153* use of NativeMessagingTransport (i.e. most applications).154*/155PEER_HOSTNAME: 'ph',156/**157* Usually both frames using a connection initially send a SETUP message to158* each other, and each responds with a SETUP_ACK. A frame marks itself159* connected when it receives that SETUP_ACK. If this parameter is true160* however, the channel it is passed to will not send a SETUP, but rather will161* wait for one from its peer and mark itself connected when that arrives.162* Peer iframes created using such a channel will send SETUP however, and will163* wait for SETUP_ACK before marking themselves connected. The goal is to164* cope with a situation where the availability of the URL for the peer frame165* cannot be relied on, eg when the application is offline. Without this166* setting, the primary frame will attempt to send its SETUP message every167* 100ms, forever. This floods the javascript console with uncatchable168* security warnings, and fruitlessly burns CPU. There is one scenario this169* mode will not support, and that is reconnection by the outer frame, ie the170* creation of a new channel object to connect to a peer iframe which was171* already communicating with a previous channel object of the same name. If172* that behavior is needed, this mode should not be used. Reconnection by173* inner frames is supported in this mode however.174*/175ONE_SIDED_HANDSHAKE: 'osh',176/**177* The frame role (inner or outer). Used to explicitly indicate the role for178* each peer whenever the role cannot be reliably determined (e.g. the two179* peer windows are not parent/child frames). If unspecified, the role will180* be dynamically determined, assuming a parent/child frame setup.181*/182ROLE: 'role',183/**184* Which version of the native transport startup protocol should be used, the185* default being '2'. Version 1 had various timing vulnerabilities, which186* had to be compensated for by introducing delays, and is deprecated. V1187* and V2 are broadly compatible, although the more robust timing and lack188* of delays is not gained unless both sides are using V2. The only189* unsupported case of cross-protocol interoperation is where a connection190* starts out with V2 at both ends, and one of the ends reconnects as a V1.191* All other initial startup and reconnection scenarios are supported.192*/193NATIVE_TRANSPORT_PROTOCOL_VERSION: 'nativeProtocolVersion',194/**195* Whether the direct transport runs in synchronous mode. The default is to196* emulate the other transports and run asyncronously but there are some197* circumstances where syncronous calls are required. If this property is198* set to true, the transport will send the messages synchronously.199*/200DIRECT_TRANSPORT_SYNC_MODE: 'directSyncMode'201};202203204/**205* Config properties that need to be URL sanitized.206* @type {Array<string>}207*/208goog.net.xpc.UriCfgFields = [209goog.net.xpc.CfgFields.PEER_URI, goog.net.xpc.CfgFields.LOCAL_RELAY_URI,210goog.net.xpc.CfgFields.PEER_RELAY_URI, goog.net.xpc.CfgFields.LOCAL_POLL_URI,211goog.net.xpc.CfgFields.PEER_POLL_URI212];213214215/**216* @enum {number}217*/218goog.net.xpc.ChannelStates = {219NOT_CONNECTED: 1,220CONNECTED: 2,221CLOSED: 3222};223224225/**226* The name of the transport service (used for internal signalling).227* @type {string}228* @suppress {underscore|visibility}229*/230goog.net.xpc.TRANSPORT_SERVICE_ = 'tp';231232233/**234* Transport signaling message: setup.235* @type {string}236*/237goog.net.xpc.SETUP = 'SETUP';238239240/**241* Transport signaling message: setup for native transport protocol v2.242* @type {string}243*/244goog.net.xpc.SETUP_NTPV2 = 'SETUP_NTPV2';245246247/**248* Transport signaling message: setup acknowledgement.249* @type {string}250* @suppress {underscore|visibility}251*/252goog.net.xpc.SETUP_ACK_ = 'SETUP_ACK';253254255/**256* Transport signaling message: setup acknowledgement.257* @type {string}258*/259goog.net.xpc.SETUP_ACK_NTPV2 = 'SETUP_ACK_NTPV2';260261262/**263* Object holding active channels.264*265* @package {Object<string, goog.net.xpc.CrossPageChannel>}266*/267goog.net.xpc.channels = {};268269270/**271* Returns a random string.272* @param {number} length How many characters the string shall contain.273* @param {string=} opt_characters The characters used.274* @return {string} The random string.275*/276goog.net.xpc.getRandomString = function(length, opt_characters) {277var chars = opt_characters || goog.net.xpc.randomStringCharacters_;278var charsLength = chars.length;279var s = '';280while (length-- > 0) {281s += chars.charAt(Math.floor(Math.random() * charsLength));282}283return s;284};285286287/**288* The default characters used for random string generation.289* @type {string}290* @private291*/292goog.net.xpc.randomStringCharacters_ =293'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';294295296/**297* The logger.298* @type {goog.log.Logger}299*/300goog.net.xpc.logger = goog.log.getLogger('goog.net.xpc');301302303