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.

148 lines
4.2 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. vAPI.firefox = true;
  23. /******************************************************************************/
  24. var messagingConnector = function(response) {
  25. if ( !response ) {
  26. return;
  27. }
  28. var channels = vAPI.messaging.channels;
  29. var channel, listener;
  30. if ( response.broadcast === true ) {
  31. for ( channel in channels ) {
  32. if ( channels.hasOwnProperty(channel) === false ) {
  33. continue;
  34. }
  35. listener = channels[channel].listener;
  36. if ( typeof listener === 'function' ) {
  37. listener(response.msg);
  38. }
  39. }
  40. return;
  41. }
  42. if ( response.requestId ) {
  43. listener = vAPI.messaging.listeners[response.requestId];
  44. delete vAPI.messaging.listeners[response.requestId];
  45. delete response.requestId;
  46. }
  47. if ( !listener ) {
  48. channel = channels[response.portName];
  49. listener = channel && channel.listener;
  50. }
  51. if ( typeof listener === 'function' ) {
  52. listener(response.msg);
  53. }
  54. };
  55. /******************************************************************************/
  56. var uniqueId = function() {
  57. return parseInt(Math.random() * 1e10, 10).toString(36);
  58. };
  59. /******************************************************************************/
  60. vAPI.messaging = {
  61. channels: {},
  62. listeners: {},
  63. requestId: 1,
  64. connectorId: uniqueId(),
  65. setup: function() {
  66. this.connector = function(msg) {
  67. messagingConnector(JSON.parse(msg));
  68. };
  69. addMessageListener(this.connectorId, this.connector);
  70. },
  71. close: function() {
  72. if (this.connector) {
  73. removeMessageListener(this.connectorId, this.connector);
  74. this.connector = null;
  75. this.channels = {};
  76. this.listeners = {};
  77. }
  78. },
  79. channel: function(channelName, callback) {
  80. if ( !channelName ) {
  81. return;
  82. }
  83. this.channels[channelName] = {
  84. portName: channelName,
  85. listener: typeof callback === 'function' ? callback : null,
  86. send: function(message, callback) {
  87. if ( !vAPI.messaging.connector ) {
  88. vAPI.messaging.setup();
  89. }
  90. message = {
  91. portName: vAPI.messaging.connectorId + '|' + this.portName,
  92. msg: message
  93. };
  94. if ( callback ) {
  95. message.requestId = vAPI.messaging.requestId++;
  96. vAPI.messaging.listeners[message.requestId] = callback;
  97. }
  98. sendAsyncMessage('ublock:background', message);
  99. },
  100. close: function() {
  101. delete vAPI.messaging.channels[this.portName];
  102. }
  103. };
  104. return this.channels[channelName];
  105. }
  106. };
  107. /******************************************************************************/
  108. vAPI.canExecuteContentScript = function() {
  109. return true;
  110. };
  111. /******************************************************************************/
  112. })();
  113. /******************************************************************************/