toolkit/content/charsetOverlay.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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 function MultiplexHandler(aEvent)
     6 {
     7     MultiplexHandlerEx(
     8         aEvent,
     9         function Browser_SelectDetector(event) {
    10             BrowserCharsetReload();
    11             /* window.content.location.reload() will re-download everything */
    12             SelectDetector(event, null);
    13         },
    14         function Browser_SetForcedCharset(charset, isPredefined) {
    15             BrowserSetForcedCharacterSet(charset);
    16         }
    17     );
    18 }
    20 function MailMultiplexHandler(aEvent)
    21 {
    22     MultiplexHandlerEx(
    23         aEvent,
    24         function Mail_SelectDetector(event) {
    25             SelectDetector(
    26                 event,
    27                 function Mail_Reload() {
    28                     messenger.setDocumentCharset(msgWindow.mailCharacterSet);
    29                 }
    30             );
    31         },
    32         function Mail_SetForcedCharset(charset, isPredefined) {
    33             MessengerSetForcedCharacterSet(charset);
    34         }
    35     );
    36 }
    38 function ComposerMultiplexHandler(aEvent)
    39 {
    40     MultiplexHandlerEx(
    41         aEvent,
    42         function Composer_SelectDetector(event) {
    43             SelectDetector(
    44                 event,
    45                 function Composer_Reload() {
    46                     EditorLoadUrl(GetDocumentUrl());
    47                 }
    48             );
    49         },
    50         function Composer_SetForcedCharset(charset, isPredefined) {
    51             if ((!isPredefined) && charset.length > 0) {
    52                 gCharsetMenu.SetCurrentComposerCharset(charset);
    53             }
    54             EditorSetDocumentCharacterSet(charset);
    55         }
    56     );
    57 }
    59 function MultiplexHandlerEx(aEvent, aSelectDetector, aSetForcedCharset)
    60 {
    61     try {
    62         var node = aEvent.target;
    63         var name = node.getAttribute('name');
    65         if (name == 'detectorGroup') {
    66             aSelectDetector(aEvent);
    67         } else if (name == 'charsetGroup') {
    68             var charset = node.getAttribute('id');
    69             charset = charset.substring('charset.'.length, charset.length)
    70             aSetForcedCharset(charset, true);
    71         } else if (name == 'charsetCustomize') {
    72             //do nothing - please remove this else statement, once the charset prefs moves to the pref window
    73         } else {
    74             aSetForcedCharset(node.getAttribute('id'), false);
    75         }
    76     } catch(ex) {
    77         alert(ex);
    78     }
    79 }
    81 function SelectDetector(event, doReload)
    82 {
    83     dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n");
    85     var uri =  event.target.getAttribute("id");
    86     var prefvalue = uri.substring('chardet.'.length, uri.length);
    87     if ("off" == prefvalue) { // "off" is special value to turn off the detectors
    88         prefvalue = "";
    89     }
    91     try {
    92         var pref = Components.classes["@mozilla.org/preferences-service;1"]
    93                              .getService(Components.interfaces.nsIPrefBranch);
    94         var str =  Components.classes["@mozilla.org/supports-string;1"]
    95                              .createInstance(Components.interfaces.nsISupportsString);
    97         str.data = prefvalue;
    98         pref.setComplexValue("intl.charset.detector",
    99                              Components.interfaces.nsISupportsString, str);
   100         if (typeof doReload == "function") doReload();
   101     }
   102     catch (ex) {
   103         dump("Failed to set the intl.charset.detector preference.\n");
   104     }
   105 }
   107 var gPrevCharset = null;
   108 function UpdateCurrentCharset()
   109 {
   110     // extract the charset from DOM
   111     var wnd = document.commandDispatcher.focusedWindow;
   112     if ((window == wnd) || (wnd == null)) wnd = window.content;
   114     // Uncheck previous item
   115     if (gPrevCharset) {
   116         var pref_item = document.getElementById('charset.' + gPrevCharset);
   117         if (pref_item)
   118           pref_item.setAttribute('checked', 'false');
   119     }
   121     var menuitem = document.getElementById('charset.' + wnd.document.characterSet);
   122     if (menuitem) {
   123         menuitem.setAttribute('checked', 'true');
   124     }
   125 }
   127 function UpdateCurrentMailCharset()
   128 {
   129     var charset = msgWindow.mailCharacterSet;
   130     var menuitem = document.getElementById('charset.' + charset);
   132     if (menuitem) {
   133         menuitem.setAttribute('checked', 'true');
   134     }
   135 }
   137 function UpdateCharsetDetector()
   138 {
   139     var prefvalue;
   141     try {
   142         var pref = Components.classes["@mozilla.org/preferences-service;1"]
   143                              .getService(Components.interfaces.nsIPrefBranch);
   144         prefvalue = pref.getComplexValue("intl.charset.detector",
   145                                          Components.interfaces.nsIPrefLocalizedString).data;
   146     }
   147     catch (ex) {
   148         prefvalue = "";
   149     }
   151     if (prefvalue == "") prefvalue = "off";
   152     dump("intl.charset.detector = "+ prefvalue + "\n");
   154     prefvalue = 'chardet.' + prefvalue;
   155     var menuitem = document.getElementById(prefvalue);
   157     if (menuitem) {
   158         menuitem.setAttribute('checked', 'true');
   159     }
   160 }
   162 function UpdateMenus(event)
   163 {
   164     // use setTimeout workaround to delay checkmark the menu
   165     // when onmenucomplete is ready then use it instead of oncreate
   166     // see bug 78290 for the detail
   167     UpdateCurrentCharset();
   168     setTimeout(UpdateCurrentCharset, 0);
   169     UpdateCharsetDetector();
   170     setTimeout(UpdateCharsetDetector, 0);
   171 }
   173 function CreateMenu(node)
   174 {
   175   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
   176   observerService.notifyObservers(null, "charsetmenu-selected", node);
   177 }
   179 function UpdateMailMenus(event)
   180 {
   181     // use setTimeout workaround to delay checkmark the menu
   182     // when onmenucomplete is ready then use it instead of oncreate
   183     // see bug 78290 for the detail
   184     UpdateCurrentMailCharset();
   185     setTimeout(UpdateCurrentMailCharset, 0);
   186     UpdateCharsetDetector();
   187     setTimeout(UpdateCharsetDetector, 0);
   188 }
   190 var gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener);
   191 var gLastBrowserCharset = null;
   193 function charsetLoadListener (event)
   194 {
   195     var charset = window.content.document.characterSet;
   197     if (charset.length > 0 && (charset != gLastBrowserCharset)) {
   198         gCharsetMenu.SetCurrentCharset(charset);
   199         gPrevCharset = gLastBrowserCharset;
   200         gLastBrowserCharset = charset;
   201     }
   202 }
   205 function composercharsetLoadListener (event)
   206 {
   207     var charset = window.content.document.characterSet;
   210     if (charset.length > 0 ) {
   211        gCharsetMenu.SetCurrentComposerCharset(charset);
   212     }
   213  }
   215 function SetForcedEditorCharset(charset)
   216 {
   217     if (charset.length > 0 ) {
   218        gCharsetMenu.SetCurrentComposerCharset(charset);
   219     }
   220     EditorSetDocumentCharacterSet(charset);
   221 }
   224 var gLastMailCharset = null;
   226 function mailCharsetLoadListener (event)
   227 {
   228     if (msgWindow) {
   229         var charset = msgWindow.mailCharacterSet;
   230         if (charset.length > 0 && (charset != gLastMailCharset)) {
   231             gCharsetMenu.SetCurrentMailCharset(charset);
   232             gLastMailCharset = charset;
   233         }
   234     }
   235 }
   237 function InitCharsetMenu()
   238 {
   239     removeEventListener("load", InitCharsetMenu, true);
   241     var wintype = document.documentElement.getAttribute('windowtype');
   242     if (window && (wintype == "navigator:browser"))
   243     {
   244         var contentArea = window.document.getElementById("appcontent");
   245         if (contentArea)
   246             contentArea.addEventListener("pageshow", charsetLoadListener, true);
   247     }
   248     else
   249     {
   250         var arrayOfStrings = wintype.split(":");
   251         if (window && arrayOfStrings[0] == "mail")
   252         {
   253             var messageContent = window.document.getElementById("messagepane");
   254             if (messageContent)
   255                 messageContent.addEventListener("pageshow", mailCharsetLoadListener, true);
   256         }
   257         else
   258         if (window && arrayOfStrings[0] == "composer")
   259         {
   260             contentArea = window.document.getElementById("appcontent");
   261             if (contentArea)
   262                 contentArea.addEventListener("pageshow", composercharsetLoadListener, true);
   263         }
   264     }
   265 }
   267 addEventListener("load", InitCharsetMenu, true);

mercurial