PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms 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. // Override these methods <---------------------------------------------------------------------
  29. // ---------------------------------------------------------------------------------------------
  30. /**
  31. * Triggered when the extension popup receives a message from the extension background page.
  32. * Executes on the extension popup.
  33. *
  34. * @param msg A message object.
  35. */
  36. function popupMessageReceived(msg) {};
  37. /**
  38. * Triggered when extension background page receives a message from the extension popup.
  39. * Executes on the extension background page.
  40. *
  41. * @param msg A message object.
  42. */
  43. function backgroundMessageReceived(msg) {};
  44. /**
  45. * Triggered when the extension background page receives a new task message.
  46. * Executes on the extension background page.
  47. *
  48. * @param A message object.
  49. */
  50. function taskReceived(msg) {
  51. KipptExtension.taskReceived(msg);
  52. };
  53. /**
  54. * Triggered when an extension background task is started.
  55. * Executes on the extension popup.
  56. */
  57. function taskStarted() {};
  58. /**
  59. * Triggered when all extension background tasks have been completed.
  60. * Executes on the extension popup.
  61. */
  62. function tasksComplete() {};
  63. // ---------------------------------------------------------------------------------------------
  64. // WARNING: IF SOURCE IS ALTERED BEYOND THIS POINT THERE IS RISK OF BREAKING SOMETHING. --------
  65. // ---------------------------------------------------------------------------------------------
  66. window.Socket = {
  67. init: function() {
  68. this.tasks = 0;
  69. this.port = chrome.extension.connect({name: "down"});
  70. this.bind();
  71. },
  72. bind: function() {
  73. chrome.extension.onConnect.addListener( function(port) {
  74. if(port.name === "down") {
  75. Socket.port = chrome.extension.connect({name: "up"});
  76. }
  77. port.onMessage.addListener( function(msg) {
  78. Socket.onMessage(msg);
  79. });
  80. Socket.port.onDisconnect.addListener( function(port) {
  81. port.onMessage.removeListener();
  82. Socket.port.onMessage.removeListener();
  83. });
  84. });
  85. },
  86. onMessage: function(msg) {
  87. try {
  88. if(msg.type === "message") {
  89. if(Socket.port.name == "up") {
  90. backgroundMessageReceived(msg);
  91. } else {
  92. popupMessageReceived(msg);
  93. }
  94. } else if(msg.type === "task") {
  95. Socket.tasks++;
  96. taskReceived(msg);
  97. } else if(msg.type === "taskComplete") {
  98. tasksComplete();
  99. }
  100. } catch(UnknownMesssageType) {}
  101. },
  102. postMessage: function(msg) {
  103. try {
  104. if(!msg) {
  105. msg = {};
  106. }
  107. msg.type = "message";
  108. Socket.port.postMessage(msg);
  109. } catch(PortPostException) {}
  110. },
  111. postTask: function(msg) {
  112. if(Socket.port.name === "down") {
  113. taskStarted(msg);
  114. }
  115. try {
  116. if(!msg) {
  117. msg = {};
  118. }
  119. msg.type = "task";
  120. Socket.port.postMessage(msg);
  121. } catch(PortPostException) {}
  122. },
  123. postTaskComplete: function() {
  124. if(Socket.tasks > 0) {
  125. --Socket.tasks;
  126. }
  127. if(Socket.tasks === 0) {
  128. try {
  129. Socket.port.postMessage({type: "taskComplete"});
  130. } catch(PortPostException) {}
  131. }
  132. }
  133. };
  134. Socket.init();
  135. })();