Bug 776792 - Fire an Activity for application/pdf document. r=gal
authorVivien Nicolas <21@vingtetun.org>
Tue, 24 Jul 2012 18:34:12 +0200
changeset 105772 511943aecd6000868f5f480a63c078904ca9b267
parent 105771 5e2318fc6d1afb9fe9a79cd8a5b408e931fcb6b1
child 105773 03fae2283d808405bee71d2d97a5e4f93b19d6ef
push idunknown
push userunknown
push dateunknown
reviewersgal
bugs776792
milestone17.0a1
Bug 776792 - Fire an Activity for application/pdf document. r=gal
b2g/chrome/content/shell.js
b2g/components/B2GComponents.manifest
b2g/components/ContentHandler.js
b2g/components/Makefile.in
b2g/installer/package-manifest.in
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -596,8 +596,20 @@ window.addEventListener('ContentStart', 
       dump('exception while creating screenshot: ' + e + '\n');
       shell.sendChromeEvent({
         type: 'take-screenshot-error',
         error: String(e)
       });
     }
   });
 });
+
+Services.obs.addObserver(function ContentHandler(subject, topic, data) {
+  let handler = JSON.parse(data);
+  new MozActivity({
+    name: 'view',
+    data: {
+      type: handler.type,
+      url: handler.url
+    }
+  });
+}, 'content-handler', false);
+
--- a/b2g/components/B2GComponents.manifest
+++ b/b2g/components/B2GComponents.manifest
@@ -33,8 +33,13 @@ category xpcom-directory-providers brows
 # 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
+
+# ContentHandler.js
+component {d18d0216-d50c-11e1-ba54-efb18d0ef0ac} ContentHandler.js
+contract @mozilla.org/uriloader/content-handler;1?type=application/pdf {d18d0216-d50c-11e1-ba54-efb18d0ef0ac}
+
new file mode 100644
--- /dev/null
+++ b/b2g/components/ContentHandler.js
@@ -0,0 +1,48 @@
+/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
+
+"use strict";
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cr = Components.results;
+const Cu = Components.utils;
+
+const PDF_CONTENT_TYPE = "application/pdf";
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+function log(aMsg) {
+  let msg = "ContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
+  Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
+                                     .logStringMessage(msg);
+  dump(msg + "\n");
+}
+
+const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
+function ContentHandler() {
+}
+
+ContentHandler.prototype = {
+  handleContent: function handleContent(aMimetype, aContext, aRequest) {
+    if (aMimetype != PDF_CONTENT_TYPE)
+      throw NS_ERROR_WONT_HANDLE_CONTENT;
+
+    if (!(aRequest instanceof Ci.nsIChannel))
+      throw NS_ERROR_WONT_HANDLE_CONTENT;
+
+    let detail = {
+      "type": aMimetype,
+      "url": aRequest.URI.spec
+    };
+    Services.obs.notifyObservers(this, "content-handler", JSON.stringify(detail)); 
+
+    aRequest.cancel(Cr.NS_BINDING_ABORTED);
+  },
+
+  classID: Components.ID("{d18d0216-d50c-11e1-ba54-efb18d0ef0ac}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler])
+};
+
+var NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentHandler]);
--- a/b2g/components/Makefile.in
+++ b/b2g/components/Makefile.in
@@ -12,23 +12,24 @@ include $(DEPTH)/config/autoconf.mk
 MODULE = B2GComponents
 XPIDL_MODULE = B2GComponents
 
 XPIDLSRCS = \
         b2g.idl \
         $(NULL)
 
 EXTRA_PP_COMPONENTS = \
+        ActivitiesGlue.js \
         AlertsService.js \
         B2GComponents.manifest \
         CameraContent.js \
+        ContentHandler.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
@@ -679,8 +679,9 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 @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
+@BINPATH@/components/ContentHandler.js