Bug 722031 - Move mozKeyboard from shell.js to webapi.js r=cjones
authorVivien Nicolas <21@vingtetun.org>
Thu, 02 Feb 2012 01:46:50 +0100
changeset 86008 24e3a040eaa4ec9be0b327e6a9906b15cdd6d4fe
parent 86007 4342b1492c38ae36263c62964c04012e82e29086
child 86009 2dafdbe36220032f00a4a0ff9aab9ce61126ccc2
push idunknown
push userunknown
push dateunknown
reviewerscjones
bugs722031
milestone13.0a1
Bug 722031 - Move mozKeyboard from shell.js to webapi.js r=cjones
b2g/chrome/content/shell.js
b2g/chrome/content/webapi.js
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -42,28 +42,26 @@ const Cu = Components.utils;
 const CC = Components.Constructor;
 
 const LocalFile = CC('@mozilla.org/file/local;1',
                      'nsILocalFile',
                      'initWithPath');
 
 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
 Cu.import('resource://gre/modules/Services.jsm');
+
 XPCOMUtils.defineLazyGetter(Services, 'env', function() {
   return Cc['@mozilla.org/process/environment;1']
            .getService(Ci.nsIEnvironment);
 });
+
 XPCOMUtils.defineLazyGetter(Services, 'ss', function() {
   return Cc['@mozilla.org/content/style-sheet-service;1']
            .getService(Ci.nsIStyleSheetService);
 });
-XPCOMUtils.defineLazyGetter(Services, 'fm', function() {
-  return Cc['@mozilla.org/focus-manager;1']
-           .getService(Ci.nsIFocusManager);
-});
 
 // In order to use http:// scheme instead of file:// scheme
 // (that is much more restricted) the following code kick-off
 // a local http server listening on http://127.0.0.1:7777 and
 // http://localhost:7777.
 function startupHttpd(baseDir, port) {
   const httpdURL = 'chrome://browser/content/httpd.js';
   let httpd = {};
@@ -283,87 +281,16 @@ var shell = {
     screen.mozBrightness = 0.0;
   },
   turnScreenOn: function shell_turnScreenOn() {
     screen.mozEnabled = true;
     screen.mozBrightness = this.preferredScreenBrightness;
   }
 };
 
-(function VirtualKeyboardManager() {
-  let activeElement = null;
-  let isKeyboardOpened = false;
-
-  let constructor = {
-    handleEvent: function vkm_handleEvent(evt) {
-      let contentWindow = shell.home.contentWindow.wrappedJSObject;
-
-      switch (evt.type) {
-        case 'ContentStart':
-          contentWindow.navigator.mozKeyboard = new MozKeyboard();
-          break;
-        case 'keypress':
-          if (evt.keyCode != evt.DOM_VK_ESCAPE || !isKeyboardOpened)
-            return;
-
-          shell.sendEvent(contentWindow, 'hideime');
-          isKeyboardOpened = false;
-
-          evt.preventDefault();
-          evt.stopPropagation();
-          break;
-        case 'mousedown':
-          if (evt.target != activeElement || isKeyboardOpened)
-            return;
-
-          let type = activeElement.type;
-          shell.sendEvent(contentWindow, 'showime', { type: type });
-          isKeyboardOpened = true;
-          break;
-      }
-    },
-    observe: function vkm_observe(subject, topic, data) {
-      let contentWindow = shell.home.contentWindow;
-
-      let shouldOpen = parseInt(data);
-      if (shouldOpen && !isKeyboardOpened) {
-        activeElement = Services.fm.focusedElement;
-        if (!activeElement)
-          return;
-
-        let type = activeElement.type;
-        shell.sendEvent(contentWindow, 'showime', { type: type });
-      } else if (!shouldOpen && isKeyboardOpened) {
-        shell.sendEvent(contentWindow, 'hideime');
-      }
-      isKeyboardOpened = shouldOpen;
-    }
-  };
-
-  Services.obs.addObserver(constructor, 'ime-enabled-state-changed', false);
-  ['ContentStart', 'keypress', 'mousedown'].forEach(function vkm_events(type) {
-    window.addEventListener(type, constructor, true);
-  });
-})();
-
-
-function MozKeyboard() {
-}
-
-MozKeyboard.prototype = {
-  sendKey: function mozKeyboardSendKey(keyCode, charCode) {
-    charCode = (charCode == undefined) ? keyCode : charCode;
-
-    var utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
-                      .getInterface(Ci.nsIDOMWindowUtils);
-    ['keydown', 'keypress', 'keyup'].forEach(function sendKeyEvents(type) {
-      utils.sendKeyEvent(type, keyCode, charCode, null);
-    });
-  }
-};
 
 
 function nsBrowserAccess() {
 }
 
 nsBrowserAccess.prototype = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow]),
 
--- a/b2g/chrome/content/webapi.js
+++ b/b2g/chrome/content/webapi.js
@@ -1,18 +1,25 @@
 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
 dump('======================= webapi+apps.js ======================= \n');
 
 'use strict';
 
+let { classes: Cc, interfaces: Ci, utils: Cu }  = Components;
+Cu.import('resource://gre/modules/XPCOMUtils.jsm');
+Cu.import('resource://gre/modules/Services.jsm');
+
+XPCOMUtils.defineLazyGetter(Services, 'fm', function() {
+  return Cc['@mozilla.org/focus-manager;1']
+           .getService(Ci.nsIFocusManager);
+});
+
 (function() {
-  let { interfaces: Ci, utils: Cu }  = Components;
 
-  Cu.import('resource://gre/modules/XPCOMUtils.jsm');
   function generateAPI(window) {
     let navigator = window.navigator;
     XPCOMUtils.defineLazyGetter(navigator, 'mozSettings', function() {
       return new Settings();
     });
 
     XPCOMUtils.defineLazyGetter(navigator, 'mozContacts', function() {
       return ContactsManager;
@@ -22,16 +29,20 @@ dump('======================= webapi+app
       let mozApps = {
         enumerate: function mozAppsEnumerate(callback) {
           callback(webapps);
         }
       };
       return mozApps;
     });
 
+    XPCOMUtils.defineLazyGetter(navigator, 'mozKeyboard', function() {
+      return new MozKeyboard();
+    });
+
     TouchHandler.start(window);
     updateApplicationCache(window);
   };
 
   let progressListener = {
     onStateChange: function onStateChange(progress, request,
                                           flags, status) {
     },
@@ -92,16 +103,88 @@ function updateApplicationCache(window) 
       // XXX Add a nice UI when an update is ready asking if the user
       // want to reload the application now.
       cache.swapCache();
       window.document.location.reload();
     });
   } catch (e) {}
 }
 
+// MozKeyboard
+(function VirtualKeyboardManager() {
+  let activeElement = null;
+  let isKeyboardOpened = false;
+  
+  function fireEvent(type, details) {
+    let event = content.document.createEvent('CustomEvent');
+    event.initCustomEvent(type, true, true, details ? details : {});
+    content.dispatchEvent(event);
+  }
+
+  let constructor = {
+    handleEvent: function vkm_handleEvent(evt) {
+      switch (evt.type) {
+        case 'keypress':
+          if (evt.keyCode != evt.DOM_VK_ESCAPE || !isKeyboardOpened)
+            return;
+
+          fireEvent('hideime');
+          isKeyboardOpened = false;
+
+          evt.preventDefault();
+          evt.stopPropagation();
+          break;
+
+        case 'mousedown':
+          if (evt.target != activeElement || isKeyboardOpened)
+            return;
+
+          let type = activeElement.type;
+          fireEvent('showime', { type: type });
+          isKeyboardOpened = true;
+          break;
+      }
+    },
+    observe: function vkm_observe(subject, topic, data) {
+      let shouldOpen = parseInt(data);
+      if (shouldOpen && !isKeyboardOpened) {
+        activeElement = Services.fm.focusedElement;
+        if (!activeElement)
+          return;
+
+        let type = activeElement.type;
+        fireEvent('showime', { type: type });
+      } else if (!shouldOpen && isKeyboardOpened) {
+        fireEvent('hideime');
+      }
+      isKeyboardOpened = shouldOpen;
+    }
+  };
+
+  Services.obs.addObserver(constructor, 'ime-enabled-state-changed', false);
+  ['keypress', 'mousedown'].forEach(function vkm_events(type) {
+    addEventListener(type, constructor, true);
+  });
+})();
+
+
+function MozKeyboard() {
+}
+
+MozKeyboard.prototype = {
+  sendKey: function mozKeyboardSendKey(keyCode, charCode) {
+    charCode = (charCode == undefined) ? keyCode : charCode;
+
+    let utils = content.QueryInterface(Ci.nsIInterfaceRequestor)
+                       .getInterface(Ci.nsIDOMWindowUtils);
+    ['keydown', 'keypress', 'keyup'].forEach(function sendKey(type) {
+      utils.sendKeyEvent(type, keyCode, charCode, null);
+    });
+  }
+};
 
 // MozSettings - Bug 678695
 function Settings() {
   content.addEventListener('message', this);
 }
 
 Settings.prototype = {
   ERROR_SETTING_UNKNOWN: 0x0001,