~ubuntu-branches/ubuntu/utopic/ubufox/utopic-security

« back to all changes in this revision

Viewing changes to content/pluginAlternativeOverlay.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-06-27 20:37:53 UTC
  • mfrom: (1.1.33)
  • Revision ID: package-import@ubuntu.com-20120627203753-h33vgglw1fiozx7t
Tags: 2.1-0ubuntu1
* New upstream release.
  - Drop the alternative plugin selector, as it depends on a patch that
    we no longer carry in Firefox
* Drop the lsb-release build-depend now, as we don't hardcode distro
  information in the build now

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ***** BEGIN LICENSE BLOCK *****
2
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3
 
 *
4
 
 * The contents of this file are subject to the Mozilla Public License Version
5
 
 * 1.1 (the "License"); you may not use this file except in compliance with
6
 
 * the License. You may obtain a copy of the License at
7
 
 * http://www.mozilla.org/MPL/
8
 
 *
9
 
 * Software distributed under the License is distributed on an "AS IS" basis,
10
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
 
 * for the specific language governing rights and limitations under the
12
 
 * License.
13
 
 *
14
 
 * The Original Code is ubufox.
15
 
 *
16
 
 * The Initial Developer of the Original Code is
17
 
 * Canonical Ltd.
18
 
 * Portions created by the Initial Developer are Copyright (C) 2008
19
 
 * the Initial Developer. All Rights Reserved.
20
 
 *
21
 
 * Contributor(s):
22
 
 *   Alexander Sack <asac@jwsdot.com> - Canonical Ltd.
23
 
 *   Arzhel Younsi <xionox@gmail.com>
24
 
 *
25
 
 * Alternatively, the contents of this file may be used under the terms of
26
 
 * either the GNU General Public License Version 2 or later (the "GPL"), or
27
 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
 
 * in which case the provisions of the GPL or the LGPL are applicable instead
29
 
 * of those above. If you wish to allow use of your version of this file only
30
 
 * under the terms of either the GPL or the LGPL, and not to allow others to
31
 
 * use your version of this file under the terms of the MPL, indicate your
32
 
 * decision by deleting the provisions above and replace them with the notice
33
 
 * and other provisions required by the GPL or the LGPL. If you do not delete
34
 
 * the provisions above, a recipient may use your version of this file under
35
 
 * the terms of any one of the MPL, the GPL or the LGPL.
36
 
 *
37
 
 * ***** END LICENSE BLOCK ***** */
38
 
 
39
 
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
40
 
var pluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
41
 
 
42
 
const APP_ICON_ATTR_NAME = "appHandlerIcon";
43
 
 
44
 
window.addEventListener("load", showContentList, false);
45
 
 
46
 
var gBrowser = document.getElementById("content");
47
 
var usedMimeTypes = new Array();
48
 
var allHandledMimeTypes = new Array();
49
 
var filteredMimeTypes = null;
50
 
 
51
 
for (var item in window.arguments[0].plugins){
52
 
  usedMimeTypes.push(window.arguments[0].plugins[item].mimetype);  
53
 
}
54
 
 
55
 
let store = new Object();
56
 
for (let i = 0; i < navigator.mimeTypes.length; i++) {
57
 
  if(!navigator.mimeTypes.item(i).enabledPlugin)
58
 
    continue;
59
 
  let mtype = navigator.mimeTypes.item(i).type;
60
 
  if(!store[mtype])
61
 
    allHandledMimeTypes.push(mtype);
62
 
  store[mtype] = "x";
63
 
}
64
 
 
65
 
var filehintNameMap = null;
66
 
 
67
 
 
68
 
function parseFilehintNameText (text)
69
 
{
70
 
   var map = {};
71
 
   var array = new Array();
72
 
 
73
 
   var start = 0;
74
 
   var next  = 0;
75
 
   next = text.indexOf("\n", start);
76
 
 
77
 
   while (next  > -1) {
78
 
      array.push(text.substring(start, next));
79
 
      start = next+1;
80
 
      next = text.indexOf("\n", start);
81
 
   }
82
 
   array.push(text.substring(start));
83
 
 
84
 
   var key = null;
85
 
   var value = null;
86
 
   var line = null;
87
 
   var separator = 0;
88
 
   var left = null;
89
 
   var right = null;
90
 
   for (let a = 0; a < array.length; a++) {
91
 
      line = array[a];
92
 
      separator = line.indexOf(":");
93
 
      left = line.substring(0, separator);
94
 
      right = line.substring(separator+2);
95
 
      if (left == "filehint")
96
 
         key = right;
97
 
      else if(left == "name") {
98
 
         map[key] = right;
99
 
      }
100
 
   }
101
 
   return map;
102
 
}
103
 
 
104
 
function getFilehintNameMap () {
105
 
  if (filehintNameMap)
106
 
    return filehintNameMap;
107
 
 
108
 
  var url = "http://localhost/~asac/cgi-bin/plugin-finder.py?op=filehint2name&distributionID=9.04";
109
 
  try {
110
 
    url = prefBranch.getCharPref("pfs.filehint.url");
111
 
  } catch (e) {}
112
 
 
113
 
  var distID = prefBranch.getCharPref("extensions.ubufox@ubuntu.com.release");
114
 
  url.replace(/%DIST_ID%/g, distID);
115
 
 
116
 
  var req = new XMLHttpRequest();
117
 
  req.open('GET', url, true); /* 3rd argument, true, marks this as async */
118
 
  req.onreadystatechange = function (aEvt) {
119
 
    if (req.readyState == 4) {
120
 
       if(req.status == 200) {
121
 
         filehintNameMap = parseFilehintNameText (req.responseText);
122
 
         showContentList();
123
 
       }
124
 
    }
125
 
  };
126
 
  req.send(null); 
127
 
  return null;
128
 
}
129
 
 
130
 
if (!filehintNameMap)
131
 
  getFilehintNameMap();
132
 
 
133
 
function showContentList(){
134
 
  var contentName;
135
 
  if(!filteredMimeTypes) {
136
 
    let showSelectedRadio = document.getElementById ("radioShowSelectedPlugins");
137
 
    let showAllRadio = document.getElementById ("radioShowAllPlugins");
138
 
    let containingRadio = document.getElementById ("radioShowParent");
139
 
    if(usedMimeTypes.length <= 0)
140
 
      filteredMimeTypes = allHandledMimeTypes;
141
 
    else
142
 
      filteredMimeTypes = usedMimeTypes;
143
 
 
144
 
    // select the right initial choice
145
 
    containingRadio.selectedItem = usedMimeTypes.length <= 0 ? showAllRadio : showSelectedRadio;
146
 
  }
147
 
  var _list = document.getElementById("pluginsList");   
148
 
  while (_list.hasChildNodes())
149
 
    _list.removeChild(_list.lastChild);
150
 
  for each (let mimeType in filteredMimeTypes) {
151
 
        if(mimeType != ''){
152
 
                let item = document.createElement("richlistitem");
153
 
                item.setAttribute("type", mimeType);
154
 
                contentName = getDescByMime(mimeType);
155
 
                item.setAttribute("typeDescription",contentName);
156
 
                item.setAttribute("actionDescription", describePreferredAction(mimeType));
157
 
                item.setAttribute(APP_ICON_ATTR_NAME, "plugin");
158
 
                item.setAttribute("typeIcon", "moz-icon://goat?size=" + 16 + "&contentType=" + mimeType);
159
 
                _list.appendChild(item);
160
 
    }
161
 
  }
162
 
}
163
 
 
164
 
function describePreferredAction(mimeType){
165
 
  var filehintNameMap = getFilehintNameMap();
166
 
 
167
 
  var preferredFileName = null;
168
 
  try {
169
 
    preferredFileName = prefBranch.getCharPref("modules.plugins.mimetype." + mimeType);
170
 
  } catch (e) {}
171
 
 
172
 
  var perfectMatchPlugin = null;
173
 
 
174
 
  if (preferredFileName) {
175
 
    for (let i =0; i < navigator.plugins.length; i++) {
176
 
      var plugIn = navigator.plugins.item(i);
177
 
      if(preferredFileName == plugIn.filename) {
178
 
        perfectMatchPlugin = plugIn;
179
 
        break;
180
 
      }
181
 
    }
182
 
  }
183
 
 
184
 
  var L = navigator.mimeTypes.length;
185
 
  for(var i=0; i<L && perfectMatchPlugin == null; i++) {
186
 
     if (navigator.mimeTypes.item(i).type == mimeType)
187
 
     {
188
 
       perfectMatchPlugin = navigator.mimeTypes.item(i).enabledPlugin;
189
 
     }
190
 
   }
191
 
 
192
 
  if(!perfectMatchPlugin)
193
 
    return "...";
194
 
 
195
 
  for (betterNameCarrierKey in filehintNameMap) {
196
 
     if (perfectMatchPlugin.filename && perfectMatchPlugin.filename.indexOf(betterNameCarrierKey) > -1) {
197
 
       return filehintNameMap[betterNameCarrierKey];
198
 
     }
199
 
  }
200
 
 
201
 
  if(perfectMatchPlugin.name.indexOf("Shockwave Flash") >= 0 ||
202
 
     perfectMatchPlugin.name.indexOf("Windows Media Player") >= 0)
203
 
    return perfectMatchPlugin.description;
204
 
  return perfectMatchPlugin.name;
205
 
}
206
 
 
207
 
function getDescByMime(aMimeType){
208
 
  for(let i = 0; i < navigator.mimeTypes.length; i++) {
209
 
     let mimeType = navigator.mimeTypes[i];
210
 
     if (mimeType.type == aMimeType && mimeType.description != null && mimeType.description.length > 0)
211
 
       return mimeType.description + " (" + aMimeType + ")";
212
 
   }
213
 
  return aMimeType;  
214
 
}
215
 
 
216
 
function onSelectionChanged() {
217
 
  var _list = document.getElementById("pluginsList");
218
 
  if (_list.selectedItem)
219
 
    _list.setAttribute("lastSelectedType", _list.selectedItem.getAttribute("type"));
220
 
}
221
 
 
222
 
function onSelectUsedPlugins (e) {
223
 
        filteredMimeTypes = usedMimeTypes;
224
 
        showContentList();
225
 
}
226
 
 
227
 
function onSelectAllPlugins (e) {
228
 
        filteredMimeTypes = allHandledMimeTypes;
229
 
        showContentList();
230
 
}
231
 
   
232
 
var gApplicationsPane = {  
233
 
 
234
 
  rebuildActionsMenu: function() {
235
 
    var filehintNameMap = getFilehintNameMap();
236
 
    var _list = document.getElementById("pluginsList");
237
 
    var typeItem = _list.selectedItem;
238
 
    var menu = document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
239
 
    var menuPopup = menu.menupopup;
240
 
    var pluginArrayLength = navigator.plugins.length;
241
 
    
242
 
    while (menuPopup.hasChildNodes())
243
 
      menuPopup.removeChild(menuPopup.lastChild);
244
 
 
245
 
    var menuItem = null;
246
 
    for each (let mimeType in filteredMimeTypes) {
247
 
      for (var i = 0; i < pluginArrayLength; i++) {
248
 
        var plugin = navigator.plugins[i];
249
 
        for (var j = 0; j < plugin.length; j++) {
250
 
          if(plugin.item(j).type == mimeType && typeItem.type == mimeType){  
251
 
            menuItem = document.createElement("menuitem");
252
 
            var pluginName = plugin.name;
253
 
            if(pluginName.indexOf("Shockwave Flash") >= 0)
254
 
              pluginName = plugin.description;
255
 
            for (betterNameCarrierKey in filehintNameMap) {
256
 
               if (plugin.filename && plugin.filename.indexOf(betterNameCarrierKey) > -1) {
257
 
                  pluginName = filehintNameMap[betterNameCarrierKey];
258
 
               }
259
 
            }
260
 
            menuItem.setAttribute("label", pluginName);
261
 
            menuItem.setAttribute("id", pluginName);
262
 
            menuItem.ubufoxPluginFilename = plugin.filename;
263
 
            menuItem.setAttribute(APP_ICON_ATTR_NAME, "plugin");
264
 
            menuPopup.appendChild(menuItem);
265
 
          }
266
 
        }
267
 
      }
268
 
    }
269
 
    var strbundle = document.getElementById("ubufox-alt-strings")
270
 
    var search = strbundle.getString("ubufox.altplugins.search");
271
 
 
272
 
    menuItem = document.createElement("menuitem");
273
 
    menuItem.setAttribute("id", "install-plugin-handler");
274
 
    menuItem.setAttribute("label", search);
275
 
    menuItem.type = typeItem.type;
276
 
    menuPopup.appendChild(menuItem);
277
 
  },
278
 
  
279
 
  onSelectAction: function(aActionItem) {
280
 
    var _list = document.getElementById("pluginsList");
281
 
    var typeItem = _list.selectedItem;
282
 
 
283
 
    var id = aActionItem.getAttribute ("id");
284
 
    if (id == "install-plugin-handler") {
285
 
      var pluginInfoArray = {};
286
 
      var fakePluginInfo = new Object();
287
 
      fakePluginInfo.mimetype = typeItem.type;
288
 
      fakePluginInfo.pluginsPage = "";
289
 
      pluginInfoArray[typeItem.type] = fakePluginInfo;
290
 
 
291
 
      window.openDialog("chrome://mozapps/content/plugins/pluginInstallerWizard.xul",
292
 
                 "PFSWindow", "chrome,centerscreen,resizable=yes,width=600,height=600",
293
 
                 {plugins: pluginInfoArray, browser: gBrowser});
294
 
 
295
 
      pluginHost.reloadPlugins(true);
296
 
      showContentList();
297
 
    }
298
 
    else {
299
 
      prefBranch.setCharPref("modules.plugins.mimetype." + typeItem.type, aActionItem.ubufoxPluginFilename);
300
 
      pluginHost.reloadPlugins(true);
301
 
      showContentList();
302
 
    }
303
 
    return;
304
 
  }
305
 
};
306