PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/js/vendor/socket.js

https://github.com/rberger/kippt-chrome
JavaScript | 151 lines | 78 code | 16 blank | 57 comment | 14 complexity | efaac9790c50fee6d18563fa3d3ee0c6 MD5 | raw file
  1/*
  2 * Chrome Extension Socket
  3 * <https://github.com/jjNford/chrome-extension-socket>
  4 * 
  5 * Copyright (C) 2012, JJ Ford (jj.n.ford@gmail.com)
  6 * 
  7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8 * this software and associated documentation files (the "Software"), to deal in
  9 * the Software without restriction, including without limitation the rights to
 10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 11 * of the Software, and to permit persons to whom the Software is furnished to do
 12 * so, subject to the following conditions:
 13 * 
 14 * The above copyright notice and this permission notice shall be included in all
 15 * copies or substantial portions of the Software.
 16 * 
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 23 * SOFTWARE.
 24 * 
 25 */
 26(function() {
 27
 28	// ---------------------------------------------------------------------------------------------
 29	// Override these methods <---------------------------------------------------------------------
 30	// ---------------------------------------------------------------------------------------------
 31
 32	/**
 33	 * Triggered when the extension popup receives a message from the extension background page. 
 34	 * Executes on the extension popup.
 35	 * 
 36	 * @param msg A message object.
 37	 */
 38	function popupMessageReceived(msg) {};
 39	
 40	/**
 41	 * Triggered when extension background page receives a message from the extension popup. 
 42	 * Executes on the extension background page.
 43	 * 
 44	 * @param msg A message object.
 45	 */
 46	function backgroundMessageReceived(msg) {};
 47	
 48	/**
 49	 * Triggered when the extension background page receives a new task message. 
 50	 * Executes on the extension background page.
 51	 * 
 52	 * @param A message object.
 53	 */
 54	function taskReceived(msg) {
 55	    KipptExtension.taskReceived(msg);
 56	};
 57	
 58	/**
 59	 * Triggered when an extension background task is started. 
 60	 * Executes on the extension popup.
 61	 */
 62	function taskStarted() {};
 63	
 64	/**
 65	 * Triggered when all extension background tasks have been completed. 
 66	 * Executes on the extension popup.
 67	 */
 68	function tasksComplete() {};
 69
 70	// ---------------------------------------------------------------------------------------------
 71	// WARNING: IF SOURCE IS ALTERED BEYOND THIS POINT THERE IS RISK OF BREAKING SOMETHING. --------
 72	// ---------------------------------------------------------------------------------------------
 73
 74	window.Socket = {
 75
 76		init: function() {
 77			this.tasks = 0;
 78			this.port = chrome.extension.connect({name: "down"});
 79			this.bind();
 80		},
 81		
 82		bind: function() {				
 83			chrome.extension.onConnect.addListener( function(port) {
 84				if(port.name === "down") {
 85					Socket.port = chrome.extension.connect({name: "up"});
 86				}				
 87				port.onMessage.addListener( function(msg) {
 88					Socket.onMessage(msg);
 89				});				
 90				Socket.port.onDisconnect.addListener( function(port) {
 91					port.onMessage.removeListener();
 92					Socket.port.onMessage.removeListener();
 93				});
 94			});
 95		},
 96		
 97		onMessage: function(msg) {
 98			try {
 99				if(msg.type === "message") {
100					if(Socket.port.name == "up") {
101						backgroundMessageReceived(msg);
102					} else {
103						popupMessageReceived(msg);
104					}
105				} else if(msg.type === "task") {
106					Socket.tasks++;
107					taskReceived(msg);
108				} else if(msg.type === "taskComplete") {
109					tasksComplete();					
110				}
111			} catch(UnknownMesssageType) {}
112		},
113		
114		postMessage: function(msg) {
115			try {
116				if(!msg) {
117					msg = {};
118				}
119				msg.type = "message";
120				Socket.port.postMessage(msg);
121			} catch(PortPostException) {}
122		},
123
124		postTask: function(msg) {
125			if(Socket.port.name === "down") {
126				taskStarted(msg);
127			}	
128			try {
129				if(!msg) {
130					msg = {};
131				}
132				msg.type = "task";
133				Socket.port.postMessage(msg);
134			} catch(PortPostException) {}
135		},
136		
137		postTaskComplete: function() {
138			if(Socket.tasks > 0) {
139				--Socket.tasks;
140			}
141			if(Socket.tasks === 0) {
142				try {
143					Socket.port.postMessage({type: "taskComplete"});
144				} catch(PortPostException) {}
145			}
146		}
147	};
148
149	Socket.init();
150	
151})();