-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: Replace DOM events with broadcast channels
Broadcast channels are more suited to uBO than DOM events to dispatch notifications to different parts of uBO. DOM events can only be dispatched to local context, broadcast channels dispatch to all contexts (i.e. background process, workers, auxiliary pages) -- this last behavior is better suited to uBO to communicate internal changes to all potential listeners, not just those in the local context. Additionally, broadcasting to content scripts is now done through tabs.sendMessage() instead of through potentially opened message ports, this simplifies broadcasting to content scripts, and this doesn't require to have long-lived message ports in content scripts.
- Loading branch information
Showing
16 changed files
with
186 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/******************************************************************************* | ||
uBlock Origin - a browser extension to block requests. | ||
Copyright (C) 2014-present Raymond Hill | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see {http://www.gnu.org/licenses/}. | ||
Home: https://github.com/gorhill/uBlock | ||
*/ | ||
|
||
/* globals browser */ | ||
|
||
'use strict'; | ||
|
||
/******************************************************************************/ | ||
|
||
// Broadcast a message to all uBO contexts | ||
|
||
let broadcastChannel; | ||
|
||
export function broadcast(message) { | ||
if ( broadcastChannel === undefined ) { | ||
broadcastChannel = new self.BroadcastChannel('uBO'); | ||
} | ||
broadcastChannel.postMessage(message); | ||
} | ||
|
||
/******************************************************************************/ | ||
|
||
// Broadcast a message to all uBO contexts and all uBO's content scripts | ||
|
||
export async function broadcastToAll(message) { | ||
broadcast(message); | ||
const tabs = await vAPI.tabs.query({ | ||
discarded: false, | ||
}); | ||
const bcmessage = Object.assign({ broadcast: true }, message); | ||
for ( const tab of tabs ) { | ||
browser.tabs.sendMessage(tab.id, bcmessage); | ||
} | ||
} | ||
|
||
/******************************************************************************/ | ||
|
||
export function onBroadcast(listener) { | ||
const bc = new self.BroadcastChannel('uBO'); | ||
bc.onmessage = ev => listener(ev.data || {}); | ||
return bc; | ||
} | ||
|
||
/******************************************************************************/ | ||
|
||
export function filteringBehaviorChanged(details = {}) { | ||
if ( typeof details.direction !== 'number' || details.direction >= 0 ) { | ||
filteringBehaviorChanged.throttle.offon(727); | ||
} | ||
broadcast(Object.assign({ what: 'filteringBehaviorChanged' }, details)); | ||
} | ||
|
||
filteringBehaviorChanged.throttle = vAPI.defer.create(( ) => { | ||
vAPI.net.handlerBehaviorChanged(); | ||
}); | ||
|
||
/******************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.