Bug 1555633 - New URI handling for JS Account based schemes. r=BenB
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Sat, 22 Jun 2019 00:00:59 +0200
changeset 26954 ec6a2a472a5e479c0ada2560e935d25f57eed2df
parent 26953 ff3f3dc92e3a2a9ab271d571c58ca9ccbbaec23c
child 26955 da0b65238d248bfdc0b40c14d6ef1c209c5b41a1
push id16082
push usermozilla@jorgk.com
push dateFri, 21 Jun 2019 22:02:03 +0000
treeherdercomm-central@da0b65238d24 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBenB
bugs1555633
Bug 1555633 - New URI handling for JS Account based schemes. r=BenB Creates a new interface for JS Account-based add-ons to implement. Explicitly proxies calls to the main thread as necessary.
mailnews/base/public/moz.build
mailnews/base/public/nsIMsgProtocolHandler.idl
mailnews/base/util/nsNewMailnewsURI.cpp
--- a/mailnews/base/public/moz.build
+++ b/mailnews/base/public/moz.build
@@ -40,16 +40,17 @@ XPIDL_SOURCES += [
     'nsIMsgMailNewsUrl.idl',
     'nsIMsgMailSession.idl',
     'nsIMsgMdnGenerator.idl',
     'nsIMsgMessageService.idl',
     'nsIMsgOfflineManager.idl',
     'nsIMsgPluggableStore.idl',
     'nsIMsgPrintEngine.idl',
     'nsIMsgProgress.idl',
+    'nsIMsgProtocolHandler.idl',
     'nsIMsgProtocolInfo.idl',
     'nsIMsgPurgeService.idl',
     'nsIMsgShutdown.idl',
     'nsIMsgStatusFeedback.idl',
     'nsIMsgTagService.idl',
     'nsIMsgThread.idl',
     'nsIMsgUserFeedbackListener.idl',
     'nsIMsgWindow.idl',
new file mode 100644
--- /dev/null
+++ b/mailnews/base/public/nsIMsgProtocolHandler.idl
@@ -0,0 +1,13 @@
+/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#include "nsISupports.idl"
+
+interface nsIURI;
+
+[scriptable, uuid(4e9e4a43-343a-4309-a88b-08c5f37f5965)]
+interface nsIMsgProtocolHandler : nsISupports {
+  nsIURI newURI(in AUTF8String aSpec, in string aOriginCharset, in nsIURI aBaseURI);
+};
--- a/mailnews/base/util/nsNewMailnewsURI.cpp
+++ b/mailnews/base/util/nsNewMailnewsURI.cpp
@@ -2,16 +2,21 @@
 /* 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/. */
 
 #include "nsNewMailnewsURI.h"
 #include "nsURLHelper.h"
 #include "nsSimpleURI.h"
 #include "nsStandardURL.h"
+#include "nsThreadUtils.h"
+#include "MainThreadUtils.h"
+#include "mozilla/SyncRunnable.h"
+#include "nsIMsgProtocolHandler.h"
+#include "nsIComponentRegistrar.h"
 
 #include "../../local/src/nsPop3Service.h"
 #include "../../local/src/nsMailboxService.h"
 #include "../../compose/src/nsSmtpService.h"
 #include "../../../ldap/xpcom/src/nsLDAPURL.h"
 #include "../../imap/src/nsImapService.h"
 #include "../../news/src/nsNntpService.h"
 #include "../../addrbook/src/nsAddbookProtocolHandler.h"
@@ -80,13 +85,38 @@ nsresult NS_NewMailnewsURI(nsIURI** aURI
         .Finalize(aURI);
   }
   if (scheme.EqualsLiteral("webcal") || scheme.EqualsLiteral("webcals")) {
     return NS_MutateURI(new mozilla::net::nsStandardURL::Mutator())
         .SetSpec(aSpec)
         .Finalize(aURI);
   }
 
-  // XXX TODO: What about JS Account?
+  nsCOMPtr<nsIComponentRegistrar> compMgr;
+  NS_GetComponentRegistrar(getter_AddRefs(compMgr));
+  if (compMgr) {
+    nsAutoCString contractID(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX);
+    contractID += scheme;
+    bool isRegistered = false;
+    compMgr->IsContractIDRegistered(contractID.get(), &isRegistered);
+    if (isRegistered) {
+      auto NewURI =
+          [&aSpec, &aCharset, &aBaseURI, aURI, &contractID, &rv ]() -> auto {
+        nsCOMPtr<nsIMsgProtocolHandler> handler(
+            do_GetService(contractID.get(), &rv));
+        if (handler) {
+          rv = handler->NewURI(aSpec, aCharset, aBaseURI, aURI);
+        }
+      };
+      if (NS_IsMainThread()) {
+        NewURI();
+      } else {
+        nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction("NewURI", NewURI);
+        mozilla::SyncRunnable::DispatchToThread(
+            mozilla::GetMainThreadEventTarget(), task);
+      }
+      return rv;
+    }
+  }
 
   // None of the above, return an error and let M-C handle it.
   return NS_ERROR_UNKNOWN_PROTOCOL;
 }