aboutsummaryrefslogtreecommitdiff
path: root/bg/ExternalLicenses.js
blob: 1a8e58c40cdb7a59a8a55529b3090f6577c4e1c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
*
* Copyright (C) 2018 Giorgio Maone <giorgio@maone.net>
*
* This file is part of GNU LibreJS.
*
* GNU LibreJS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GNU LibreJS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU LibreJS.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
  Singleton to handle external licenses, e.g. WebLabels
*/

'use strict';

let licensesByLabel = new Map();
let licensesByUrl = new Map();
{
  let { licenses } = require('../license_definitions');
  let mapByLabel = (label, license) => licensesByLabel.set(label.toUpperCase(), license);
  for (let [id, l] of Object.entries(licenses)) {
    let { identifier, canonicalUrl, licenseName } = l;
    if (identifier) {
      mapByLabel(identifier, l);
    } else {
      l.identifier = id;
    }
    if (id !== identifier) {
      mapByLabel(id, l);
    }
    if (licenseName) {
      mapByLabel(licenseName, l);
    }
    if (Array.isArray(canonicalUrl)) {
      for (let url of canonicalUrl) {
        licensesByUrl.set(url, l);
      }
    }
  }
}

let cachedHrefs = new Map();

var ExternalLicenses = {
  purgeCache(tabId) {
    cachedHrefs.delete(tabId);
  },

  async check(script) {
    let { url, tabId, frameId, documentUrl } = script;
    let tabCache = cachedHrefs.get(tabId);
    let frameCache = tabCache && tabCache.get(frameId);
    let cache = frameCache && frameCache.get(documentUrl);
    let scriptInfo = await browser.tabs.sendMessage(tabId, {
      action: 'checkLicensedScript',
      url,
      cache,
    }, { frameId });

    if (!(scriptInfo && scriptInfo.licenseLinks.length)) {
      return null;
    }
    scriptInfo.licenses = new Set();
    scriptInfo.toString = function() {
      let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(', ');
      return licenseIds
        ? `Free license${this.licenses.size > 1 ? 's' : ''} (${licenseIds})`
        : 'Unknown license(s)';
    }
    let match = (map, key) => {
      if (map.has(key)) {
        scriptInfo.licenses.add(map.get(key));
        return true;
      }
      return false;
    };

    for (let { label, url } of scriptInfo.licenseLinks) {
      match(licensesByLabel, label = label.trim().toUpperCase()) ||
        match(licensesByUrl, url) ||
        match(licensesByLabel, label.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, ''));
    }
    scriptInfo.free = scriptInfo.licenses.size > 0;
    return scriptInfo;
  },

  /**
  * moves / creates external license references before any script in the page
  * if needed, to have them ready when the first script load is triggered.
  * It also caches the external licens href by page URL, to help not actually
  * modify the rendered HTML but rather feed the content script on demand.
  * Returns true if the document has been actually modified, false otherwise.
  */
  optimizeDocument(doc, cachePointer) {
    let cache = {};
    let { tabId, frameId, documentUrl } = cachePointer;
    let frameCache = cachedHrefs.get(tabId);
    if (!frameCache) {
      cachedHrefs.set(tabId, frameCache = new Map());
    }
    frameCache.set(frameId, new Map([[documentUrl, cache]]));

    let link = doc.querySelector('link[rel="jslicense"], link[data-jslicense="1"], a[rel="jslicense"], a[data-jslicense="1"]');
    if (link) {
      let href = link.getAttribute('href');
      cache.webLabels = { href };
      let move = () => !!doc.head.insertBefore(link, doc.head.firstChild);
      if (link.parentNode === doc.head) {
        let node = link.previousElementSibling;
        for (; node; node = node.previousElementSibling) {
          if (node.tagName.toUpperCase() === 'SCRIPT') {
            return move();
          }
        }
      } else { // the reference is only in the body
        if (link.tagName.toUpperCase() === 'A') {
          let newLink = doc.createElement('link');
          newLink.rel = 'jslicense';
          newLink.setAttribute('href', href);
          link = newLink;
        }
        return move();
      }
    }

    return false;
  }
};


module.exports = { ExternalLicenses };