Bug 736628 - Move mozKeyboard to a distinct xpcom component [r=cjones]
authorFabrice Desré <fabrice@mozilla.com>
Mon, 19 Mar 2012 10:30:51 -0700
changeset 89734 6c4d82e99f421e52e7f0b964c1e77fb3bd2d4020
parent 89733 c47e0e62d6d073b54b779cde2911de554e216b87
child 89735 007fd86d0eab0d8ae8173e58f9cfe1e1461909a6
push id7291
push userfdesre@mozilla.com
push dateMon, 19 Mar 2012 17:31:42 +0000
treeherdermozilla-inbound@6c4d82e99f42 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs736628
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 736628 - Move mozKeyboard to a distinct xpcom component [r=cjones]
b2g/chrome/content/webapi.js
b2g/components/B2GComponents.manifest
b2g/components/Makefile.in
b2g/components/MozKeyboard.js
b2g/components/b2g.idl
b2g/installer/package-manifest.in
--- a/b2g/chrome/content/webapi.js
+++ b/b2g/chrome/content/webapi.js
@@ -1,83 +1,28 @@
 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-dump('======================= webapi+apps.js ======================= \n');
+'use strict';
 
-'use strict';
+dump('======================= webapi+apps.js ======================= \n');
 
 let { classes: Cc, interfaces: Ci, utils: Cu }  = Components;
 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
 Cu.import('resource://gre/modules/Services.jsm');
+Cu.import('resource://gre/modules/Geometry.jsm');
 
 XPCOMUtils.defineLazyGetter(Services, 'fm', function() {
   return Cc['@mozilla.org/focus-manager;1']
            .getService(Ci.nsIFocusManager);
 });
 
-(function() {
-  function generateAPI(window) {
-    let navigator = window.navigator;
-
-    XPCOMUtils.defineLazyGetter(navigator, 'mozKeyboard', function() {
-      return new MozKeyboard();
-    });
-  };
-
-  let progressListener = {
-    onStateChange: function onStateChange(progress, request,
-                                          flags, status) {
-    },
-
-    onProgressChange: function onProgressChange(progress, request,
-                                                curSelf, maxSelf,
-                                                curTotal, maxTotal) {
-    },
-
-    onLocationChange: function onLocationChange(progress, request,
-                                                locationURI, flags) {
-      content.addEventListener('appwillopen', function(evt) {
-        let appManager = content.wrappedJSObject.Gaia.AppManager;
-        let topWindow = appManager.foregroundWindow.contentWindow;
-        generateAPI(topWindow);
-      });
-
-      generateAPI(content.wrappedJSObject);
-    },
-
-    onStatusChange: function onStatusChange(progress, request,
-                                            status, message) {
-    },
-
-    onSecurityChange: function onSecurityChange(progress, request,
-                                                state) {
-    },
-
-    QueryInterface: function QueryInterface(aIID) {
-      if (aIID.equals(Ci.nsIWebProgressListener) ||
-          aIID.equals(Ci.nsISupportsWeakReference) ||
-          aIID.equals(Ci.nsISupports)) {
-          return this;
-      }
-
-      throw Components.results.NS_ERROR_NO_INTERFACE;
-    }
-  };
-
-  let flags = Ci.nsIWebProgress.NOTIFY_LOCATION;
-  let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
-                            .getInterface(Ci.nsIWebProgress);
-  flags = Ci.nsIWebProgress.NOTIFY_ALL;
-  webProgress.addProgressListener(progressListener, flags);
-})();
-
 // 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 : {});
@@ -134,37 +79,16 @@ XPCOMUtils.defineLazyGetter(Services, 'f
   };
 
   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);
-    });
-  }
-};
-
-let { classes: Cc, interfaces: Ci, utils: Cu } = Components;
-
-Cu.import('resource://gre/modules/Geometry.jsm');
-Cu.import('resource://gre/modules/Services.jsm');
-
 const ContentPanning = {
   init: function cp_init() {
     ['mousedown', 'mouseup', 'mousemove'].forEach(function(type) {
       addEventListener(type, ContentPanning, true);
     });
   },
 
   handleEvent: function cp_handleEvent(evt) {
@@ -323,17 +247,16 @@ const ContentPanning = {
 
     const kStateActive = 0x00000001;
     this._domUtils.setContentState(root.documentElement, kStateActive);
   }
 };
 
 ContentPanning.init();
 
-
 // Min/max velocity of kinetic panning. This is in pixels/millisecond.
 const kMinVelocity = 0.4;
 const kMaxVelocity = 6;
 
 // Constants that affect the "friction" of the scroll pane.
 const kExponentialC = 1000;
 const kPolynomialC = 100 / 1000000;
 
@@ -492,9 +415,8 @@ const KineticPanning = {
       }
 
       content.mozRequestAnimationFrame(callback);
     }).bind(this);
 
     content.mozRequestAnimationFrame(callback);
   }
 };
-
--- a/b2g/components/B2GComponents.manifest
+++ b/b2g/components/B2GComponents.manifest
@@ -8,8 +8,13 @@ category JavaScript-navigator-property m
 
 # AlertsService.js
 component {5dce03b2-8faa-4b6e-9242-6ddb0411750c} AlertsService.js
 contract @mozilla.org/alerts-service;1 {5dce03b2-8faa-4b6e-9242-6ddb0411750c}
 
 # ContentPermissionPrompt.js
 component {8c719f03-afe0-4aac-91ff-6c215895d467} ContentPermissionPrompt.js
 contract @mozilla.org/content-permission/prompt;1 {8c719f03-afe0-4aac-91ff-6c215895d467}
+
+# MozKeyboard.js
+component {397a7fdf-2254-47be-b74e-76625a1a66d5} MozKeyboard.js
+contract @mozilla.org/b2g-keyboard;1 {397a7fdf-2254-47be-b74e-76625a1a66d5}
+category JavaScript-navigator-property mozKeyboard @mozilla.org/b2g-keyboard;1
--- a/b2g/components/Makefile.in
+++ b/b2g/components/Makefile.in
@@ -48,11 +48,12 @@ XPIDLSRCS = \
         b2g.idl \
         $(NULL)
 
 EXTRA_PP_COMPONENTS = \
         B2GComponents.manifest \
         CameraContent.js \
         AlertsService.js \
         ContentPermissionPrompt.js \
+        MozKeyboard.js \
         $(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/b2g/components/MozKeyboard.js
@@ -0,0 +1,52 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+// -----------------------------------------------------------------------
+// MozKeyboard
+// -----------------------------------------------------------------------
+
+function MozKeyboard() { } 
+
+MozKeyboard.prototype = {
+  classID: Components.ID("{397a7fdf-2254-47be-b74e-76625a1a66d5}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIB2GKeyboard, Ci.nsIDOMGlobalPropertyInitializer, Ci.nsIObserver]),
+  classInfo: XPCOMUtils.generateCI({classID: Components.ID("{397a7fdf-2254-47be-b74e-76625a1a66d5}"),
+                                    contractID: "@mozilla.org/b2g-keyboard;1",
+                                    interfaces: [Ci.nsIB2GKeyboard],
+                                    flags: Ci.nsIClassInfo.DOM_OBJECT,
+                                    classDescription: "B2G Virtual Keyboard"}),
+
+  init: function(aWindow) {
+    this._utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
+    Services.obs.addObserver(this, "inner-window-destroyed", false);
+    this.innerWindowID = this._utils.currentInnerWindowID;
+  },
+
+  observe: function(aSubject, aTopic, aData) {
+    if (aTopic == "inner-window-destroyed") {
+      let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
+      if (wId == this.innerWindowID) {
+        Services.obs.removeObserver(this, "inner-window-destroyed");
+        this._utils = null;
+      }
+    }
+  },
+
+  sendKey: function mozKeyboardSendKey(keyCode, charCode) {
+    charCode = (charCode == undefined) ? keyCode : charCode;
+    ['keydown', 'keypress', 'keyup'].forEach((function sendKey(type) {
+      this._utils.sendKeyEvent(type, keyCode, charCode, null);
+    }).bind(this));
+  }
+};
+
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([MozKeyboard]);
--- a/b2g/components/b2g.idl
+++ b/b2g/components/b2g.idl
@@ -3,10 +3,16 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "domstubs.idl"
 
 [scriptable, uuid(3615a616-571d-4194-bf54-ccf546067b14)]
 interface nsIB2GCameraContent : nsISupports
 {
     /* temporary solution, waiting for getUserMedia */
-	DOMString getCameraURI([optional] in jsval options);
+    DOMString getCameraURI([optional] in jsval options);
 };
+
+[scriptable, uuid(80ad05f8-e5f6-4a36-b25d-5d5a969b365d)]
+interface nsIB2GKeyboard : nsISupports
+{
+    void sendKey(in long aKeyCode, in long aCharCode);
+};
--- a/b2g/installer/package-manifest.in
+++ b/b2g/installer/package-manifest.in
@@ -614,8 +614,9 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 @BINPATH@/chrome/icons/
 @BINPATH@/chrome/chrome@JAREXT@
 @BINPATH@/chrome/chrome.manifest
 @BINPATH@/components/B2GComponents.manifest
 @BINPATH@/components/B2GComponents.xpt
 @BINPATH@/components/CameraContent.js
 @BINPATH@/components/AlertsService.js
 @BINPATH@/components/ContentPermissionPrompt.js
+@BINPATH@/components/MozKeyboard.js