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.

154 lines
4.3 KiB

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