Bug 715814 - Implement Web Activities : B2G Specific [r=cjones]
authorFabrice Desré <fabrice@mozilla.com>
Fri, 20 Jul 2012 17:41:30 +0200
changeset 105394 6a3791473b56e1a2052b0fbb47c94a4e4cc3779f
parent 105393 7938ab0b63db406228cf8f2a5ce5b0dd346c2e99
child 105395 48d0e85546ea866460d909a7c9ae3fbef2afc3c5
push idunknown
push userunknown
push dateunknown
reviewerscjones
bugs715814
milestone17.0a1
Bug 715814 - Implement Web Activities : B2G Specific [r=cjones]
b2g/chrome/content/shell.js
b2g/components/ActivitiesGlue.js
b2g/components/B2GComponents.manifest
b2g/components/Makefile.in
b2g/installer/package-manifest.in
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -10,16 +10,17 @@ const Cu = Components.utils;
 const Cr = Components.results;
 
 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
 Cu.import('resource://gre/modules/Services.jsm');
 Cu.import('resource://gre/modules/ContactService.jsm');
 Cu.import('resource://gre/modules/SettingsChangeNotifier.jsm');
 Cu.import('resource://gre/modules/Webapps.jsm');
 Cu.import('resource://gre/modules/AlarmService.jsm');
+Cu.import('resource://gre/modules/ActivitiesService.jsm');
 
 XPCOMUtils.defineLazyServiceGetter(Services, 'env',
                                    '@mozilla.org/process/environment;1',
                                    'nsIEnvironment');
 
 XPCOMUtils.defineLazyServiceGetter(Services, 'ss',
                                    '@mozilla.org/content/style-sheet-service;1',
                                    'nsIStyleSheetService');
@@ -301,25 +302,26 @@ nsBrowserAccess.prototype = {
   },
 
   isTabContentWindow: function isTabContentWindow(contentWindow) {
     return contentWindow == window;
   }
 };
 
 // Listen for system messages and relay them to Gaia.
-Services.obs.addObserver(function(aSubject, aTopic, aData) {
-  let msg = JSON.parse(aData);
+Services.obs.addObserver(function onSystemMessage(subject, topic, data) {
+  let msg = JSON.parse(data);
   let origin = Services.io.newURI(msg.manifest, null, null).prePath;
-  shell.sendEvent(shell.contentBrowser.contentWindow,
-                  "mozChromeEvent", { type: "open-app",
-                                      url: msg.uri,
-                                      origin: origin,
-                                      manifest: msg.manifest } );
-}, "system-messages-open-app", false);
+  shell.sendEvent(shell.contentBrowser.contentWindow, 'mozChromeEvent', {
+    type: 'open-app',
+    url: msg.uri,
+    origin: origin,
+    manifest: msg.manifest
+  });
+}, 'system-messages-open-app', false);
 
 (function Repl() {
   if (!Services.prefs.getBoolPref('b2g.remote-js.enabled')) {
     return;
   }
   const prompt = 'JS> ';
   let output;
   let reader = {
new file mode 100644
--- /dev/null
+++ b/b2g/components/ActivitiesGlue.js
@@ -0,0 +1,72 @@
+/* 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 Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+function ActivitiesDialog() {
+  this._id = 0;
+
+  this.activities = [];
+}
+
+ActivitiesDialog.prototype = {
+  run: function ap_run() {
+    let id = "activity-choice" + this._id++;
+    let activity = this.activities.shift();
+
+    let choices = [];
+    activity.list.forEach(function(item) {
+      choices.push({ title: item.title, icon: item.icon });
+    });
+
+
+    // Keep up the frond-end of an activity choice. The messages contains
+    // a list of {names, icons} for applications able to handle this particular
+    // activity. The front-end should display a UI to pick one.
+    let browser = Services.wm.getMostRecentWindow("navigator:browser");
+    let content = browser.getContentWindow();
+    let event = content.document.createEvent("CustomEvent");
+    event.initCustomEvent("mozChromeEvent", true, true, {
+      type: "activity-choice",
+      id: id,
+      name: activity.name,
+      choices: choices
+    });
+
+    // Listen the resulting choice from the front-end. If there is no choice,
+    // let's return -1, which means the user has cancelled the dialog.
+    content.addEventListener("mozContentEvent", function act_getChoice(evt) {
+      if (evt.detail.id != id)
+        return;
+
+      content.removeEventListener("mozContentEvent", act_getChoice);
+      activity.callback.handleEvent(evt.detail.value ? evt.detail.value : -1);
+    });
+
+    content.dispatchEvent(event);
+  },
+
+  chooseActivity: function ap_chooseActivity(aName, aActivities, aCallback) {
+    this.activities.push({
+      name: aName,
+      list: aActivities,
+      callback: aCallback
+    });
+    Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL);
+  },
+
+  classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"),
+
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable])
+}
+
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]);
+
--- a/b2g/components/B2GComponents.manifest
+++ b/b2g/components/B2GComponents.manifest
@@ -25,12 +25,16 @@ component {397a7fdf-2254-47be-b74e-76625
 contract @mozilla.org/b2g-keyboard;1 {397a7fdf-2254-47be-b74e-76625a1a66d5}
 category JavaScript-navigator-property mozKeyboard @mozilla.org/b2g-keyboard;1
 
 # DirectoryProvider.js
 component {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5} DirectoryProvider.js
 contract @mozilla.org/browser/directory-provider;1 {9181eb7c-6f87-11e1-90b1-4f59d80dd2e5}
 category xpcom-directory-providers browser-directory-provider @mozilla.org/browser/directory-provider;1
 
+# ActivitiesGlue.js
+component {70a83123-7467-4389-a309-3e81c74ad002} ActivitiesGlue.js
+contract @mozilla.org/dom/activities/ui-glue;1 {70a83123-7467-4389-a309-3e81c74ad002}
+
 # ProcessGlobal.js
 component {1a94c87a-5ece-4d11-91e1-d29c29f21b28} ProcessGlobal.js
 contract @mozilla.org/b2g-process-global;1 {1a94c87a-5ece-4d11-91e1-d29c29f21b28}
 category app-startup ProcessGlobal service,@mozilla.org/b2g-process-global;1
--- a/b2g/components/Makefile.in
+++ b/b2g/components/Makefile.in
@@ -19,15 +19,16 @@ XPIDLSRCS = \
 EXTRA_PP_COMPONENTS = \
         AlertsService.js \
         B2GComponents.manifest \
         CameraContent.js \
         ContentPermissionPrompt.js \
         DirectoryProvider.js \
         MozKeyboard.js \
         ProcessGlobal.js \
+        ActivitiesGlue.js \
         $(NULL)
 
 ifdef MOZ_UPDATER
 EXTRA_PP_COMPONENTS += UpdatePrompt.js
 endif
 
 include $(topsrcdir)/config/rules.mk
--- a/b2g/installer/package-manifest.in
+++ b/b2g/installer/package-manifest.in
@@ -677,9 +677,10 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 @BINPATH@/components/marionettecomponent.js
 @BINPATH@/components/AlertsService.js
 @BINPATH@/components/ContentPermissionPrompt.js
 #ifdef MOZ_UPDATER
 @BINPATH@/components/UpdatePrompt.js
 #endif
 @BINPATH@/components/MozKeyboard.js
 @BINPATH@/components/DirectoryProvider.js
+@BINPATH@/components/ActivitiesGlue.js
 @BINPATH@/components/ProcessGlobal.js