feat (addon): #724 set home page to Activity Stream (#785)

* feat (addon): #724 set home page to Activity Stream

* keep track if user overrode the home page

* better I think
This commit is contained in:
ricky rosario 2016-06-10 08:18:28 -04:00 коммит произвёл GitHub
Родитель be02057fb6
Коммит 2bc5b11d81
2 изменённых файлов: 72 добавлений и 1 удалений

Просмотреть файл

@ -11,6 +11,8 @@ const tabs = require("sdk/tabs");
const simplePrefs = require("sdk/simple-prefs");
const privateBrowsing = require("sdk/private-browsing");
const windows = require("sdk/windows").browserWindows;
const prefService = require("sdk/preferences/service");
const ss = require("sdk/simple-storage");
const {Memoizer} = require("lib/Memoizer");
const {PlacesProvider} = require("lib/PlacesProvider");
const {SearchProvider} = require("lib/SearchProvider");
@ -57,14 +59,19 @@ const PLACES_CHANGES_EVENTS = [
"bookmarkChanged"
];
const HOME_PAGE_PREF = "browser.startup.homepage";
function ActivityStreams(options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
EventEmitter.decorate(this);
this._newTabURL = `${this.options.pageURL}#/`;
this._setupPageMod();
this._setupListeners();
this._setupButton();
NewTabURL.override(`${this.options.pageURL}#/`);
NewTabURL.override(this._newTabURL);
this._setHomePage();
Services.prefs.setIntPref("places.favicons.optimizeToDimension", 64);
@ -517,6 +524,31 @@ ActivityStreams.prototype = {
});
},
/*
* Replace the home page with the ActivityStream new tab page.
*/
_setHomePage() {
// Only hijack the home page if it isn't set by user or if it is set to
// about:home/about:blank
// AND the user didn't previously override the preference.
if (!ss.storage.homepageOverriden &&
(!prefService.isSet(HOME_PAGE_PREF) ||
["about:home", "about:blank"].includes(prefService.get(HOME_PAGE_PREF)))) {
prefService.set(HOME_PAGE_PREF, this._newTabURL);
}
},
_unsetHomePage() {
if (prefService.get(HOME_PAGE_PREF) === this._newTabURL) {
// Reset home page back if user didn't change it.
prefService.reset(HOME_PAGE_PREF);
} else {
// The user changed the pref. Keep track of that so next time we don't
// hijack it again.
ss.storage.homepageOverriden = true;
}
},
/**
* The URLs for the app.
*/
@ -550,6 +582,7 @@ ActivityStreams.prototype = {
clearTimeout(this._placesCacheTimeoutID);
this._previewProvider.uninit();
NewTabURL.reset();
this._unsetHomePage();
Services.prefs.clearUserPref("places.favicons.optimizeToDimension");
this.workers.clear();
this._removeListeners();

Просмотреть файл

@ -0,0 +1,38 @@
"use strict";
const test = require("sdk/test");
const prefService = require("sdk/preferences/service");
const ss = require("sdk/simple-storage");
const {ActivityStreams} = require("lib/ActivityStreams");
exports["test activity stream loads on home page when appropriate"] = function*(assert) {
prefService.reset("browser.startup.homepage");
let url = "http://foo.bar/baz";
let app = new ActivityStreams({pageURL: url});
// By default, the home page should be set to ActivityStream.
assert.equal(url + "#/", prefService.get("browser.startup.homepage"));
// Unload ActivityStream and it should be unset.
app.unload();
assert.ok(!prefService.isSet("browser.startup.homepage"));
// If the pref is already overriden, ActivityStream shouldn't change it.
prefService.set("browser.startup.homepage", "https://example.com");
app = new ActivityStreams({pageURL: url});
assert.equal("https://example.com", prefService.get("browser.startup.homepage"));
app.unload();
assert.equal("https://example.com", prefService.get("browser.startup.homepage"));
// If we override the pref and the user changes it back to about:home,
// ActivityStream shouldn't change it on next load.
prefService.reset("browser.startup.homepage");
ss.storage.homepageOverriden = true;
app = new ActivityStreams({pageURL: url});
assert.ok(!prefService.isSet("browser.startup.homepage"));
app.unload();
assert.ok(!prefService.isSet("browser.startup.homepage"));
};
test.run(exports);