You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.1 KiB

  1. /*******************************************************************************
  2. µBlock - a Chromium browser extension to block requests.
  3. Copyright (C) 2014 The µBlock authors
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see {http://www.gnu.org/licenses/}.
  14. Home: https://github.com/gorhill/uBlock
  15. */
  16. // For non background pages
  17. /******************************************************************************/
  18. (function() {
  19. 'use strict';
  20. /******************************************************************************/
  21. self.vAPI = self.vAPI || {};
  22. self.vAPI.chrome = true;
  23. /******************************************************************************/
  24. var messagingConnector = function(response) {
  25. if ( !response ) {
  26. return;
  27. }
  28. var channel, listener;
  29. if ( response.broadcast === true ) {
  30. for ( channel in vAPI.messaging.channels ) {
  31. listener = vAPI.messaging.channels[channel].listener;
  32. if ( typeof listener === 'function' ) {
  33. listener(response.msg);
  34. }
  35. }
  36. return;
  37. }
  38. if ( response.requestId ) {
  39. listener = vAPI.messaging.listeners[response.requestId];
  40. delete vAPI.messaging.listeners[response.requestId];
  41. delete response.requestId;
  42. }
  43. if ( !listener ) {
  44. channel = vAPI.messaging.channels[response.portName];
  45. listener = channel && channel.listener;
  46. }
  47. if ( typeof listener === 'function' ) {
  48. listener(response.msg);
  49. }
  50. };
  51. /******************************************************************************/
  52. var uniqueId = function() {
  53. return parseInt(Math.random() * 1e10, 10).toString(36);
  54. };
  55. /******************************************************************************/
  56. self.vAPI.messaging = {
  57. port: null,
  58. channels: {},
  59. listeners: {},
  60. requestId: 1,
  61. connectorId: uniqueId(),
  62. setup: function() {
  63. this.port = chrome.runtime.connect({name: this.connectorId});
  64. this.port.onMessage.addListener(messagingConnector);
  65. },
  66. close: function() {
  67. if ( this.port === null ) {
  68. return;
  69. }
  70. this.port.disconnect();
  71. this.port.onMessage.removeListener(messagingConnector);
  72. this.port = null;
  73. this.channels = {};
  74. this.listeners = {};
  75. },
  76. channel: function(channelName, callback) {
  77. if ( !channelName ) {
  78. return;
  79. }
  80. this.channels[channelName] = {
  81. portName: channelName,
  82. listener: typeof callback === 'function' ? callback : null,
  83. send: function(message, callback) {
  84. if ( vAPI.messaging.port === null ) {
  85. vAPI.messaging.setup();
  86. }
  87. message = {
  88. portName: this.portName,
  89. msg: message
  90. };
  91. if ( callback ) {
  92. message.requestId = vAPI.messaging.requestId++;
  93. vAPI.messaging.listeners[message.requestId] = callback;
  94. }
  95. vAPI.messaging.port.postMessage(message);
  96. },
  97. close: function() {
  98. delete vAPI.messaging.channels[this.portName];
  99. }
  100. };
  101. return this.channels[channelName];
  102. }
  103. };
  104. /******************************************************************************/
  105. self.vAPI.canExecuteContentScript = function() {
  106. return true;
  107. };
  108. /******************************************************************************/
  109. })();
  110. /******************************************************************************/