1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6 ChromeUtils.defineESModuleGetters(lazy, {
7 RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
8 Utils: "resource://services-settings/Utils.sys.mjs",
14 } from "resource://activity-stream/common/Actions.mjs";
16 const PREF_WALLPAPERS_ENABLED =
17 "browser.newtabpage.activity-stream.newtabWallpapers.enabled";
19 export class WallpaperFeed {
22 this.wallpaperClient = "";
23 this.wallpaperDB = "";
24 this.baseAttachmentURL = "";
28 * This thin wrapper around global.fetch makes it easier for us to write
29 * automated tests that simulate responses from this fetch.
32 return fetch(...args);
36 * This thin wrapper around lazy.RemoteSettings makes it easier for us to write
37 * automated tests that simulate responses from this fetch.
39 RemoteSettings(...args) {
40 return lazy.RemoteSettings(...args);
43 async wallpaperSetup() {
44 const wallpapersEnabled = Services.prefs.getBoolPref(
45 PREF_WALLPAPERS_ENABLED
48 if (wallpapersEnabled) {
49 if (!this.wallpaperClient) {
50 this.wallpaperClient = this.RemoteSettings("newtab-wallpapers");
53 await this.getBaseAttachment();
54 this.wallpaperClient.on("sync", () => this.updateWallpapers());
55 this.updateWallpapers();
59 async getBaseAttachment() {
60 if (!this.baseAttachmentURL) {
61 const SERVER = lazy.Utils.SERVER_URL;
62 const serverInfo = await (
63 await this.fetch(`${SERVER}/`, {
67 const { base_url } = serverInfo.capabilities.attachments;
68 this.baseAttachmentURL = base_url;
72 async updateWallpapers() {
73 const records = await this.wallpaperClient.get();
74 if (!records?.length) {
78 if (!this.baseAttachmentURL) {
79 await this.getBaseAttachment();
81 const wallpapers = records.map(record => {
84 wallpaperUrl: `${this.baseAttachmentURL}${record.attachment.location}`,
89 ac.BroadcastToContent({
90 type: at.WALLPAPERS_SET,
96 async onAction(action) {
97 switch (action.type) {
99 await this.wallpaperSetup();
105 case at.PREF_CHANGED:
106 if (action.data.name === "newtabWallpapers.enabled") {
107 await this.wallpaperSetup();
110 case at.WALLPAPERS_SET: