2015-06-15 22:36:43 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["AboutNewTab"];
|
2015-06-15 22:36:43 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2015-06-15 22:36:43 +03:00
|
|
|
|
2018-07-16 18:58:35 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
ActivityStream: "resource://activity-stream/lib/ActivityStream.jsm",
|
2018-08-31 08:59:17 +03:00
|
|
|
RemotePages:
|
|
|
|
"resource://gre/modules/remotepagemanager/RemotePageManagerParent.jsm",
|
2018-07-16 18:58:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const BROWSER_READY_NOTIFICATION = "sessionstore-windows-restored";
|
2015-06-15 22:36:43 +03:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var AboutNewTab = {
|
2018-07-16 18:58:35 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
]),
|
|
|
|
|
|
|
|
// AboutNewTab
|
2015-06-15 22:36:43 +03:00
|
|
|
|
|
|
|
pageListener: null,
|
|
|
|
|
2017-03-07 22:22:33 +03:00
|
|
|
isOverridden: false,
|
|
|
|
|
2018-07-16 18:58:35 +03:00
|
|
|
activityStream: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* init - Initializes an instance of Activity Stream if one doesn't exist already
|
|
|
|
* and creates the instance of Remote Page Manager which Activity Stream
|
|
|
|
* uses for message passing.
|
|
|
|
*
|
|
|
|
* @param {obj} pageListener - Optional argument. An existing instance of RemotePages
|
|
|
|
* which Activity Stream has previously made, and we
|
|
|
|
* would like to re-use.
|
|
|
|
*/
|
2017-08-01 22:08:16 +03:00
|
|
|
init(pageListener) {
|
2017-03-07 22:22:33 +03:00
|
|
|
if (this.isOverridden) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 18:58:35 +03:00
|
|
|
|
|
|
|
// Since `init` can be called via `reset` at a later time with an existing
|
|
|
|
// pageListener, we want to only add the observer if we are initializing
|
|
|
|
// without this pageListener argument. This means it was the first call to `init`
|
|
|
|
if (!pageListener) {
|
|
|
|
Services.obs.addObserver(this, BROWSER_READY_NOTIFICATION);
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:47:57 +03:00
|
|
|
this.pageListener =
|
|
|
|
pageListener ||
|
|
|
|
new RemotePages(["about:home", "about:newtab", "about:welcome"]);
|
2017-05-24 11:56:00 +03:00
|
|
|
},
|
|
|
|
|
2018-07-16 18:58:35 +03:00
|
|
|
/**
|
|
|
|
* onBrowserReady - Continues the initialization of Activity Stream after browser is ready.
|
|
|
|
*/
|
|
|
|
onBrowserReady() {
|
|
|
|
if (this.activityStream && this.activityStream.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 22:36:43 +03:00
|
|
|
|
2018-07-16 18:58:35 +03:00
|
|
|
this.activityStream = new ActivityStream();
|
|
|
|
try {
|
|
|
|
this.activityStream.init();
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
2015-06-15 22:36:43 +03:00
|
|
|
},
|
|
|
|
|
2018-07-16 18:58:35 +03:00
|
|
|
/**
|
|
|
|
* uninit - Uninitializes Activity Stream if it exists, and destroys the pageListener
|
|
|
|
* if it exists.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
uninit() {
|
2018-07-16 18:58:35 +03:00
|
|
|
if (this.activityStream) {
|
|
|
|
this.activityStream.uninit();
|
|
|
|
this.activityStream = null;
|
|
|
|
}
|
|
|
|
|
2017-03-07 22:22:33 +03:00
|
|
|
if (this.pageListener) {
|
|
|
|
this.pageListener.destroy();
|
|
|
|
this.pageListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-01 22:08:16 +03:00
|
|
|
override(shouldPassPageListener) {
|
2017-08-01 21:55:03 +03:00
|
|
|
this.isOverridden = true;
|
2017-08-01 22:08:16 +03:00
|
|
|
const pageListener = this.pageListener;
|
|
|
|
if (!pageListener) {
|
|
|
|
return null;
|
2019-07-05 10:55:19 +03:00
|
|
|
}
|
2017-08-01 22:08:16 +03:00
|
|
|
if (shouldPassPageListener) {
|
|
|
|
this.pageListener = null;
|
|
|
|
return pageListener;
|
|
|
|
}
|
|
|
|
this.uninit();
|
|
|
|
return null;
|
2015-06-15 22:36:43 +03:00
|
|
|
},
|
2017-03-07 22:22:33 +03:00
|
|
|
|
2017-08-01 22:08:16 +03:00
|
|
|
reset(pageListener) {
|
2017-03-07 22:22:33 +03:00
|
|
|
this.isOverridden = false;
|
2017-08-01 22:08:16 +03:00
|
|
|
this.init(pageListener);
|
2018-07-16 18:58:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver implementation
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case BROWSER_READY_NOTIFICATION:
|
|
|
|
Services.obs.removeObserver(this, BROWSER_READY_NOTIFICATION);
|
|
|
|
// Avoid running synchronously during this event that's used for timing
|
|
|
|
Services.tm.dispatchToMainThread(() => this.onBrowserReady());
|
|
|
|
break;
|
|
|
|
}
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2015-06-15 22:36:43 +03:00
|
|
|
};
|