Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / browser / actors / LinkHandlerParent.sys.mjs
blobb720fddb30b5523d508078da0f3d2272dbe22bf0
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 http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   PlacesUIUtils: "resource:///modules/PlacesUIUtils.sys.mjs",
9 });
11 let gTestListeners = new Set();
13 export class LinkHandlerParent extends JSWindowActorParent {
14   static addListenerForTests(listener) {
15     gTestListeners.add(listener);
16   }
18   static removeListenerForTests(listener) {
19     gTestListeners.delete(listener);
20   }
22   receiveMessage(aMsg) {
23     let browser = this.browsingContext.top.embedderElement;
24     if (!browser) {
25       return;
26     }
28     let win = browser.ownerGlobal;
30     let gBrowser = win.gBrowser;
32     switch (aMsg.name) {
33       case "Link:LoadingIcon":
34         if (!gBrowser) {
35           return;
36         }
38         if (aMsg.data.canUseForTab) {
39           let tab = gBrowser.getTabForBrowser(browser);
40           if (tab.hasAttribute("busy")) {
41             tab.setAttribute("pendingicon", "true");
42           }
43         }
45         this.notifyTestListeners("LoadingIcon", aMsg.data);
46         break;
48       case "Link:SetIcon":
49         // Cache the most recent icon and rich icon locally.
50         if (aMsg.data.canUseForTab) {
51           this.icon = aMsg.data;
52         } else {
53           this.richIcon = aMsg.data;
54         }
56         if (!gBrowser) {
57           return;
58         }
60         this.setIconFromLink(gBrowser, browser, aMsg.data);
62         this.notifyTestListeners("SetIcon", aMsg.data);
63         break;
65       case "Link:SetFailedIcon":
66         if (!gBrowser) {
67           return;
68         }
70         if (aMsg.data.canUseForTab) {
71           this.clearPendingIcon(gBrowser, browser);
72         }
74         this.notifyTestListeners("SetFailedIcon", aMsg.data);
75         break;
77       case "Link:AddSearch":
78         if (!gBrowser) {
79           return;
80         }
82         let tab = gBrowser.getTabForBrowser(browser);
83         if (!tab) {
84           break;
85         }
87         if (win.BrowserSearch) {
88           win.BrowserSearch.addEngine(browser, aMsg.data.engine);
89         }
90         break;
91     }
92   }
94   notifyTestListeners(name, data) {
95     for (let listener of gTestListeners) {
96       listener(name, data);
97     }
98   }
100   clearPendingIcon(gBrowser, aBrowser) {
101     let tab = gBrowser.getTabForBrowser(aBrowser);
102     tab.removeAttribute("pendingicon");
103   }
105   setIconFromLink(
106     gBrowser,
107     browser,
108     { pageURL, originalURL, canUseForTab, expiration, iconURL, canStoreIcon }
109   ) {
110     let tab = gBrowser.getTabForBrowser(browser);
111     if (!tab) {
112       return;
113     }
115     if (canUseForTab) {
116       this.clearPendingIcon(gBrowser, browser);
117     }
119     let iconURI;
120     try {
121       iconURI = Services.io.newURI(iconURL);
122     } catch (ex) {
123       console.error(ex);
124       return;
125     }
126     if (iconURI.scheme != "data") {
127       try {
128         Services.scriptSecurityManager.checkLoadURIWithPrincipal(
129           browser.contentPrincipal,
130           iconURI,
131           Services.scriptSecurityManager.ALLOW_CHROME
132         );
133       } catch (ex) {
134         return;
135       }
136     }
137     if (canStoreIcon) {
138       try {
139         lazy.PlacesUIUtils.loadFavicon(
140           browser,
141           Services.scriptSecurityManager.getSystemPrincipal(),
142           Services.io.newURI(pageURL),
143           Services.io.newURI(originalURL),
144           expiration,
145           iconURI
146         );
147       } catch (ex) {
148         console.error(ex);
149       }
150     }
152     if (canUseForTab) {
153       gBrowser.setIcon(tab, iconURL, originalURL);
154     }
155   }