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/. */
6 ChromeUtils.defineESModuleGetters(lazy, {
7 pktApi: "chrome://pocket/content/pktApi.sys.mjs",
8 SaveToPocket: "chrome://pocket/content/SaveToPocket.sys.mjs",
11 export class AboutPocketParent extends JSWindowActorParent {
12 sendResponseMessageToPanel(messageId, payload) {
13 this.sendAsyncMessage(`${messageId}_response`, payload);
17 return !!this.manager && !this.manager.isClosed;
20 async receiveMessage(message) {
21 switch (message.name) {
22 case "PKT_show_signup": {
23 this.browsingContext.topChromeWindow?.pktUI.onShowSignup();
26 case "PKT_show_saved": {
27 this.browsingContext.topChromeWindow?.pktUI.onShowSaved();
30 case "PKT_show_home": {
31 this.browsingContext.topChromeWindow?.pktUI.onShowHome();
35 this.browsingContext.topChromeWindow?.pktUI.closePanel();
38 case "PKT_openTabWithUrl": {
39 this.browsingContext.topChromeWindow?.pktUI.onOpenTabWithUrl(
41 this.browsingContext.embedderElement.contentPrincipal,
42 this.browsingContext.embedderElement.csp
46 case "PKT_openTabWithPocketUrl": {
47 this.browsingContext.topChromeWindow?.pktUI.onOpenTabWithPocketUrl(
49 this.browsingContext.embedderElement.contentPrincipal,
50 this.browsingContext.embedderElement.csp
54 case "PKT_resizePanel": {
55 this.browsingContext.topChromeWindow?.pktUI.resizePanel(message.data);
56 this.sendResponseMessageToPanel("PKT_resizePanel");
60 this.sendResponseMessageToPanel("PKT_getTags", lazy.pktApi.getTags());
63 case "PKT_getRecentTags": {
64 this.sendResponseMessageToPanel(
66 lazy.pktApi.getRecentTags()
70 case "PKT_getSuggestedTags": {
71 // Ask for suggested tags based on passed url
72 const result = await new Promise(resolve => {
73 lazy.pktApi.getSuggestedTagsForURL(message.data.url, {
75 var successResponse = {
78 suggestedTags: data.suggested_tags,
81 resolve(successResponse);
83 error: error => resolve({ status: "error", error }),
87 // If the doorhanger is still open, send the result.
88 if (this.isPanalAvailable()) {
89 this.sendResponseMessageToPanel("PKT_getSuggestedTags", result);
94 // Pass url and array list of tags, add to existing save item accordingly
95 const result = await new Promise(resolve => {
96 lazy.pktApi.addTagsToURL(message.data.url, message.data.tags, {
97 success: () => resolve({ status: "success" }),
98 error: error => resolve({ status: "error", error }),
102 // If the doorhanger is still open, send the result.
103 if (this.isPanalAvailable()) {
104 this.sendResponseMessageToPanel("PKT_addTags", result);
108 case "PKT_deleteItem": {
109 // Based on clicking "remove page" CTA, and passed unique item id, remove the item
110 const result = await new Promise(resolve => {
111 lazy.pktApi.deleteItem(message.data.itemId, {
113 resolve({ status: "success" });
114 lazy.SaveToPocket.itemDeleted();
116 error: error => resolve({ status: "error", error }),
120 // If the doorhanger is still open, send the result.
121 if (this.isPanalAvailable()) {
122 this.sendResponseMessageToPanel("PKT_deleteItem", result);
127 console.log(...Object.values(message.data));