1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Firefox: simplify vAPI.app.restart

This commit is contained in:
Deathamns 2015-01-04 13:58:17 +01:00
parent 41ac8ad39f
commit a31c30d55e
3 changed files with 55 additions and 60 deletions

View File

@ -28,31 +28,13 @@
let bgProcess; let bgProcess;
const hostName = 'ublock'; const hostName = 'ublock';
const {classes: Cc, interfaces: Ci, utils: Cu} = Components; const restartListener = {
const restartObserver = { get messageManager() {
get observer() { return Components.classes['@mozilla.org/parentprocessmessagemanager;1']
return Cc["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIMessageListenerManager);
.getService(Ci.nsIObserverService);
}, },
QueryInterface: (function() { receiveMessage: function() {
let {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {});
return XPCOMUtils.generateQI([
Ci.nsIObserver,
Ci.nsISupportsWeakReference
]);
})(),
register: function() {
this.observer.addObserver(this, hostName + '-restart', true);
},
unregister: function() {
this.observer.removeObserver(this, hostName + '-restart');
},
observe: function() {
shutdown(); shutdown();
startup(); startup();
} }
@ -66,8 +48,8 @@ function startup(data, reason) {
this.removeEventListener(e.type, onReady); this.removeEventListener(e.type, onReady);
} }
let hDoc = Cc['@mozilla.org/appshell/appShellService;1'] let hDoc = Components.classes['@mozilla.org/appshell/appShellService;1']
.getService(Ci.nsIAppShellService) .getService(Components.interfaces.nsIAppShellService)
.hiddenDOMWindow.document; .hiddenDOMWindow.document;
bgProcess = hDoc.documentElement.appendChild( bgProcess = hDoc.documentElement.appendChild(
@ -77,7 +59,11 @@ function startup(data, reason) {
'src', 'src',
'chrome://' + hostName + '/content/background.html' 'chrome://' + hostName + '/content/background.html'
); );
restartObserver.register();
restartListener.messageManager.addMessageListener(
hostName + '-restart',
restartListener
);
}; };
if ( reason !== APP_STARTUP ) { if ( reason !== APP_STARTUP ) {
@ -85,8 +71,8 @@ function startup(data, reason) {
return; return;
} }
let ww = Cc['@mozilla.org/embedcomp/window-watcher;1'] let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
.getService(Ci.nsIWindowWatcher); .getService(Components.interfaces.nsIWindowWatcher);
ww.registerNotification({ ww.registerNotification({
observe: function(win) { observe: function(win) {
@ -107,7 +93,10 @@ function shutdown(data, reason) {
// Remove the restartObserver only when the extension is being disabled // Remove the restartObserver only when the extension is being disabled
if ( data !== undefined ) { if ( data !== undefined ) {
restartObserver.unregister(); restartListener.messageManager.removeMessageListener(
hostName + '-restart',
restartListener
);
} }
} }
@ -115,8 +104,8 @@ function shutdown(data, reason) {
function install() { function install() {
// https://bugzil.la/719376 // https://bugzil.la/719376
Cc['@mozilla.org/intl/stringbundle;1'] Components.classes['@mozilla.org/intl/stringbundle;1']
.getService(Ci.nsIStringBundleService).flushBundles(); .getService(Components.interfaces.nsIStringBundleService).flushBundles();
} }
/******************************************************************************/ /******************************************************************************/

View File

@ -19,20 +19,17 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global Services, Components, XPCOMUtils, __URI__ */
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
this.EXPORTED_SYMBOLS = ['contentObserver']; this.EXPORTED_SYMBOLS = ['contentObserver'];
const {interfaces: Ci, utils: Cu} = Components; const {interfaces: Ci, utils: Cu} = this.Components;
const appName = __URI__.match(/:\/\/([^\/]+)/)[1]; const appName = this.__URI__.match(/:\/\/([^\/]+)/)[1];
Cu['import']('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Services.jsm');
Cu['import']('resource://gre/modules/XPCOMUtils.jsm'); Cu.import('resource://gre/modules/devtools/Console.jsm');
// Cu['import']('resource://gre/modules/devtools/Console.jsm');
/******************************************************************************/ /******************************************************************************/
@ -66,12 +63,16 @@ const contentObserver = {
.getService(Ci.nsICategoryManager); .getService(Ci.nsICategoryManager);
}, },
QueryInterface: XPCOMUtils.generateQI([ QueryInterface: (function() {
let {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {});
return XPCOMUtils.generateQI([
Ci.nsIFactory, Ci.nsIFactory,
Ci.nsIObserver, Ci.nsIObserver,
Ci.nsIContentPolicy, Ci.nsIContentPolicy,
Ci.nsISupportsWeakReference Ci.nsISupportsWeakReference
]), ]);
})(),
createInstance: function(outer, iid) { createInstance: function(outer, iid) {
if ( outer ) { if ( outer ) {
@ -117,7 +118,7 @@ const contentObserver = {
return this.ACCEPT; return this.ACCEPT;
} }
let opener; var opener;
if ( location.scheme !== 'http' && location.scheme !== 'https' ) { if ( location.scheme !== 'http' && location.scheme !== 'https' ) {
if ( type !== this.MAIN_FRAME ) { if ( type !== this.MAIN_FRAME ) {
@ -208,6 +209,7 @@ const contentObserver = {
this.initContentScripts(win); this.initContentScripts(win);
} }
// What about data: and about:blank?
return; return;
} }

View File

@ -19,8 +19,6 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global Services, XPCOMUtils, CustomizableUI */
// For background page // For background page
/******************************************************************************/ /******************************************************************************/
@ -33,9 +31,7 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components; const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu['import']('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Services.jsm');
Cu['import']('resource://gre/modules/XPCOMUtils.jsm');
Cu['import']('resource:///modules/CustomizableUI.jsm');
/******************************************************************************/ /******************************************************************************/
@ -53,8 +49,10 @@ vAPI.app = {
/******************************************************************************/ /******************************************************************************/
vAPI.app.restart = function() { vAPI.app.restart = function() {
// Observing in bootstrap.js // Listening in bootstrap.js
Services.obs.notifyObservers(null, location.host + '-restart', null); Cc['@mozilla.org/childprocessmessagemanager;1']
.getService(Ci.nsIMessageSender)
.sendAsyncMessage(location.host + '-restart');
}; };
/******************************************************************************/ /******************************************************************************/
@ -641,6 +639,8 @@ vAPI.toolbarButton = {
/******************************************************************************/ /******************************************************************************/
vAPI.toolbarButton.init = function() { vAPI.toolbarButton.init = function() {
const {CustomizableUI} = Cu.import('resource:///modules/CustomizableUI.jsm', {});
CustomizableUI.createWidget({ CustomizableUI.createWidget({
id: this.widgetId, id: this.widgetId,
type: 'view', type: 'view',
@ -936,12 +936,16 @@ var httpObserver = {
.getService(Ci.nsICategoryManager); .getService(Ci.nsICategoryManager);
}, },
QueryInterface: XPCOMUtils.generateQI([ QueryInterface: (function() {
var {XPCOMUtils} = Cu.import('resource://gre/modules/XPCOMUtils.jsm', {});
return XPCOMUtils.generateQI([
Ci.nsIFactory, Ci.nsIFactory,
Ci.nsIObserver, Ci.nsIObserver,
Ci.nsIChannelEventSink, Ci.nsIChannelEventSink,
Ci.nsISupportsWeakReference Ci.nsISupportsWeakReference
]), ]);
})(),
createInstance: function(outer, iid) { createInstance: function(outer, iid) {
if ( outer ) { if ( outer ) {
@ -1048,12 +1052,12 @@ var httpObserver = {
} }
try { try {
// [tabId, type, sourceTabId - given if it was a popup]
channelData = channel.getProperty(location.host + 'reqdata'); channelData = channel.getProperty(location.host + 'reqdata');
} catch (ex) { } catch (ex) {
return; return;
} }
// [tabId, type, sourceTabId - given if it was a popup]
if ( !channelData || channelData[0] !== this.MAIN_FRAME ) { if ( !channelData || channelData[0] !== this.MAIN_FRAME ) {
return; return;
} }
@ -1402,7 +1406,7 @@ window.addEventListener('unload', function() {
// frameModule needs to be cleared too // frameModule needs to be cleared too
var frameModule = {}; var frameModule = {};
Cu['import'](vAPI.getURL('frameModule.js'), frameModule); Cu.import(vAPI.getURL('frameModule.js'), frameModule);
frameModule.contentObserver.unregister(); frameModule.contentObserver.unregister();
Cu.unload(vAPI.getURL('frameModule.js')); Cu.unload(vAPI.getURL('frameModule.js'));
}); });