Bug 1881588 - Add Wallpaper component r=home-newtab-reviewers,fluent-reviewers,bolsso...
[gecko.git] / browser / components / newtab / lib / WallpaperFeed.sys.mjs
blobb280cce996984d83f51fa084d4c3c33a4d2664ce
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/. */
5 const lazy = {};
6 ChromeUtils.defineESModuleGetters(lazy, {
7   RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
8   Utils: "resource://services-settings/Utils.sys.mjs",
9 });
11 import {
12   actionTypes as at,
13   actionCreators as ac,
14 } from "resource://activity-stream/common/Actions.mjs";
16 const PREF_WALLPAPERS_ENABLED =
17   "browser.newtabpage.activity-stream.newtabWallpapers.enabled";
19 export class WallpaperFeed {
20   constructor() {
21     this.loaded = false;
22     this.wallpaperClient = "";
23     this.wallpaperDB = "";
24     this.baseAttachmentURL = "";
25   }
27   /**
28    * This thin wrapper around global.fetch makes it easier for us to write
29    * automated tests that simulate responses from this fetch.
30    */
31   fetch(...args) {
32     return fetch(...args);
33   }
35   /**
36    * This thin wrapper around lazy.RemoteSettings makes it easier for us to write
37    * automated tests that simulate responses from this fetch.
38    */
39   RemoteSettings(...args) {
40     return lazy.RemoteSettings(...args);
41   }
43   async wallpaperSetup() {
44     const wallpapersEnabled = Services.prefs.getBoolPref(
45       PREF_WALLPAPERS_ENABLED
46     );
48     if (wallpapersEnabled) {
49       if (!this.wallpaperClient) {
50         this.wallpaperClient = this.RemoteSettings("newtab-wallpapers");
51       }
53       await this.getBaseAttachment();
54       this.wallpaperClient.on("sync", () => this.updateWallpapers());
55       this.updateWallpapers();
56     }
57   }
59   async getBaseAttachment() {
60     if (!this.baseAttachmentURL) {
61       const SERVER = lazy.Utils.SERVER_URL;
62       const serverInfo = await (
63         await this.fetch(`${SERVER}/`, {
64           credentials: "omit",
65         })
66       ).json();
67       const { base_url } = serverInfo.capabilities.attachments;
68       this.baseAttachmentURL = base_url;
69     }
70   }
72   async updateWallpapers() {
73     const records = await this.wallpaperClient.get();
74     if (!records?.length) {
75       return;
76     }
78     if (!this.baseAttachmentURL) {
79       await this.getBaseAttachment();
80     }
81     const wallpapers = records.map(record => {
82       return {
83         ...record,
84         wallpaperUrl: `${this.baseAttachmentURL}${record.attachment.location}`,
85       };
86     });
88     this.store.dispatch(
89       ac.BroadcastToContent({
90         type: at.WALLPAPERS_SET,
91         data: wallpapers,
92       })
93     );
94   }
96   async onAction(action) {
97     switch (action.type) {
98       case at.INIT:
99         await this.wallpaperSetup();
100         break;
101       case at.UNINIT:
102         break;
103       case at.SYSTEM_TICK:
104         break;
105       case at.PREF_CHANGED:
106         if (action.data.name === "newtabWallpapers.enabled") {
107           await this.wallpaperSetup();
108         }
109         break;
110       case at.WALLPAPERS_SET:
111         break;
112     }
113   }