1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +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;
const hostName = 'ublock';
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const restartObserver = {
get observer() {
return Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
const restartListener = {
get messageManager() {
return Components.classes['@mozilla.org/parentprocessmessagemanager;1']
.getService(Components.interfaces.nsIMessageListenerManager);
},
QueryInterface: (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() {
receiveMessage: function() {
shutdown();
startup();
}
@ -66,8 +48,8 @@ function startup(data, reason) {
this.removeEventListener(e.type, onReady);
}
let hDoc = Cc['@mozilla.org/appshell/appShellService;1']
.getService(Ci.nsIAppShellService)
let hDoc = Components.classes['@mozilla.org/appshell/appShellService;1']
.getService(Components.interfaces.nsIAppShellService)
.hiddenDOMWindow.document;
bgProcess = hDoc.documentElement.appendChild(
@ -77,7 +59,11 @@ function startup(data, reason) {
'src',
'chrome://' + hostName + '/content/background.html'
);
restartObserver.register();
restartListener.messageManager.addMessageListener(
hostName + '-restart',
restartListener
);
};
if ( reason !== APP_STARTUP ) {
@ -85,8 +71,8 @@ function startup(data, reason) {
return;
}
let ww = Cc['@mozilla.org/embedcomp/window-watcher;1']
.getService(Ci.nsIWindowWatcher);
let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1']
.getService(Components.interfaces.nsIWindowWatcher);
ww.registerNotification({
observe: function(win) {
@ -107,7 +93,10 @@ function shutdown(data, reason) {
// Remove the restartObserver only when the extension is being disabled
if ( data !== undefined ) {
restartObserver.unregister();
restartListener.messageManager.removeMessageListener(
hostName + '-restart',
restartListener
);
}
}
@ -115,8 +104,8 @@ function shutdown(data, reason) {
function install() {
// https://bugzil.la/719376
Cc['@mozilla.org/intl/stringbundle;1']
.getService(Ci.nsIStringBundleService).flushBundles();
Components.classes['@mozilla.org/intl/stringbundle;1']
.getService(Components.interfaces.nsIStringBundleService).flushBundles();
}
/******************************************************************************/

View File

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

View File

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