summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere-eff@eff.org/chrome/content/code/AndroidUI.jsm
blob: a269e04a813d32d4634d798ec1fea4b1158ba86f (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const CC = Components.classes;
const CI = Components.interfaces;
const CU = Components.utils;

var HTTPSEverywhere = CC["@eff.org/https-everywhere;1"]
                      .getService(CI.nsISupports).wrappedJSObject;

CU.import("resource://gre/modules/Prompt.jsm");

var menuToggleId;
var menuParentId;
var menuRulesId;
var menuDefaultsId;
var aWindow = getWindow();


/*
 * Setup/Teardown for the UI
 */

function loadIntoWindow() {
  if (!aWindow) {
    return;
  }
  var enabled = HTTPSEverywhere.prefs.getBoolPref("globalEnabled");
  addMenuItems(enabled);

  // When navigating away from a page, we want to clear the applicable list for
  // that page. There are a few different APIs to do this, but this is the one
  // that work on mobile. We trigger on pagehide rather than pageshow because we
  // want to capture subresources during load.
  var BrowserApp = aWindow.BrowserApp;
  BrowserApp.deck.addEventListener("pagehide", function(evt) {
    var browser = BrowserApp.getBrowserForDocument(evt.target);
    HTTPSEverywhere.resetApplicableList(browser);
  }, true);
}

function unloadFromWindow() {
  if (!aWindow) {
    return;
  }
  aWindow.NativeWindow.menu.remove(menuParentId);
}


/*
 * Add a menu item to toggle HTTPS Everywhere
 */

function addToggleItemToMenu(enabled) {
  if (menuToggleId) { aWindow.NativeWindow.menu.remove(menuToggleId); }
  var menuLabel = enabled ? "Disable" : "Enable";
  menuToggleId = aWindow.NativeWindow.menu.add({
    name: menuLabel,
    callback: function() {
      popupToggleMenu(aWindow, enabled);
    },
    parent: menuParentId
  });
}

function addRulesItemToMenu(enabled){
  if (menuRulesId) { aWindow.NativeWindow.menu.remove(menuRulesId); }

  if (enabled) {
    menuRulesId = aWindow.NativeWindow.menu.add({
      name: "Enable/disable rules",
      callback: function() {
        popupInfo.fill();
        rulesPrompt.setMultiChoiceItems(popupInfo.ruleItems);
        rulesPrompt.show(function(data) {
          var db = data.button;
          if (db === -1) { return null; } // user didn't click the accept button

          for (var i=0; i<popupInfo.rules.length; i++) {
            var ruleOn = popupInfo.ruleStatus[i];
            var ruleChecked = (data.list.indexOf(i) == -1 ? false : true);
            if (ruleOn !== ruleChecked) {
              HTTPSEverywhere.log(4, "toggling: "+JSON.stringify(popupInfo.rules[i]));
              popupInfo.rules[i].toggle();
            }
          }
          reloadTab();
          return null;
        });
      },
      parent: menuParentId
    });
  }
}

function addDefaultsItemToMenu(enabled){
  if (menuDefaultsId) { aWindow.NativeWindow.menu.remove(menuDefaultsId); }

  if (enabled) {
    menuDefaultsId = aWindow.NativeWindow.menu.add({
      name: "Reset to Defaults",
      callback: function() {
        popupResetDefaultsMenu(aWindow);
      },
      parent: menuParentId
    });
  }
}

function addMenuItems(enabled){
  if(!menuParentId){
    menuParentId = aWindow.NativeWindow.menu.add({
      name: "HTTPS Everywhere",
    });
  }
  addToggleItemToMenu(enabled);
  addRulesItemToMenu(enabled);
  addDefaultsItemToMenu(enabled);
}

function popupToggleMenu(aWindow, enabled) {
  var buttons = [
    {
      label: "Yes",
      callback: function() {
        toggleEnabledState();
        var msg = enabled ? "HTTPS Everywhere disabled!" : "HTTPS Everywhere enabled!";
        aWindow.NativeWindow.toast.show(msg, "short");
        return true;
      },
      positive: true
    }, {
      label: "No",
      callback: function() { return false; }
    }
  ];
  var newState = enabled ? "off?" : "on?";
  aWindow.NativeWindow.doorhanger.show("Would you like to turn HTTPS Everywhere "+newState,
                                       "doorhanger-toggle", buttons);
}


/*
 * The HTTPS Everywhere icon in the URL bar shows a popup of rules that the
 * user can activate/deactivate. On long click, reset all rules to defaults.
 */

var popupInfo = {
  rules: [],
  ruleItems: [],
  ruleStatus: [],
  alist: null,
  getApplicableList: function() {
    var browser = aWindow.BrowserApp.selectedBrowser;
    return HTTPSEverywhere.getApplicableListForBrowser(browser);
  },
  fill: function() {
    this.clear();
    this.alist = this.getApplicableList();
    HTTPSEverywhere.log(4,"applicable list active: "+JSON.stringify(this.alist.active));
    HTTPSEverywhere.log(4,"applicable list inactive: "+JSON.stringify(this.alist.inactive));
    for (var rule in this.alist.all) {
      if (this.alist.active.hasOwnProperty(rule)) {
        // active rules are checked and toggleable
        this.ruleItems.push({ label: rule, selected: true });
        this.ruleStatus.push(true);
        this.rules.push(this.alist.active[rule]);
      } else if (this.alist.moot.hasOwnProperty(rule)) {
        // moot rules are checked and toggleable too
        this.ruleItems.push({ label: rule, selected: true });
        this.ruleStatus.push(true);
        this.rules.push(this.alist.moot[rule]);
      } else if (this.alist.inactive.hasOwnProperty(rule)) {
        // inactive rules are unchecked and toggleable
        this.ruleItems.push({ label: rule });
        this.ruleStatus.push(false);
        this.rules.push(this.alist.inactive[rule]);
      } else if (this.alist.breaking.hasOwnProperty(rule)) {
        // breaking rules are get a unicode clockwise arrow next to them
        var ruleLabel = "\u21B7"+rule;
        var isSelected = this.alist.breaking[rule].active;
        this.ruleItems.push({ label: ruleLabel, selected: isSelected });
        this.ruleStatus.push(isSelected);
        this.rules.push(this.alist.breaking[rule]);
      }
    }
  },
  clear: function() {
    this.rules = [];
    this.ruleItems = [];
    this.ruleStatus = [];
    this.alist = {};
  }
};

var rulesPrompt = new Prompt({
  window: aWindow,
  title: "Enable/disable rules",
  buttons: ["Apply changes"]
});

function popupResetDefaultsMenu(aWindow) {
  var buttons = [
    {
      label: "Yes",
      callback: function() {
        resetToDefaults();
        var msg = "Default rules reset.";
        aWindow.NativeWindow.toast.show(msg, "short");
        return true;
      },
      positive: true
    }, {
      label: "No",
      callback: function() { return false; }
    }
  ];
  aWindow.NativeWindow.doorhanger.show("Reset all HTTPS Everywhere rules to defaults?",
                                       "doorhanger-reset", buttons);
}


/*
 * Some useful utils
 */

function reloadTab() {
  // There seems to be no API to do this directly?
  aWindow.BrowserApp.selectedTab.window.location.reload();
}

function toggleEnabledState(){
  HTTPSEverywhere.toggleEnabledState();
  loadIntoWindow();
  reloadTab();
}

function resetToDefaults() {
  HTTPSEverywhere.https_rules.resetRulesetsToDefaults();
  reloadTab();
}

function getWindow() {
  return CC['@mozilla.org/appshell/window-mediator;1']
      .getService(CI.nsIWindowMediator)
      .getMostRecentWindow('navigator:browser');
}


/*
 *  Here's the external API to this UI module
 */

var AndroidUI = {
  init: function() {
    loadIntoWindow();
  },
  shutdown: function() {
    unloadFromWindow();
  }
};

var EXPORTED_SYMBOLS = ["AndroidUI"];