Bug 1240823, add observer to compose to allow extensions to manipulate outgoing server, r-mkmelin a=rkent
authorR Kent James <rkent@caspia.com>
Thu, 21 Jan 2016 12:32:40 -0800
changeset 26678 d667a01136d05994a0d04a3419859d1da4d91fc3
parent 26677 51c54450105d3e2dcc5d6b7444d2550eed86e5a2
child 26679 13c6068843b19c7ae5bba0913d3858db03397b93
push id1850
push userclokep@gmail.com
push dateWed, 08 Mar 2017 19:29:12 +0000
treeherdercomm-esr52@028df196b2d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrkent
bugs1240823
Bug 1240823, add observer to compose to allow extensions to manipulate outgoing server, r-mkmelin a=rkent
mailnews/compose/src/nsMsgCompose.cpp
mailnews/compose/test/unit/head_compose.js
mailnews/compose/test/unit/test_sendObserver.js
mailnews/compose/test/unit/xpcshell.ini
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -1070,19 +1070,44 @@ nsMsgCompose::SendMsgToServer(MSG_Delive
       m_compFields->SetFrom(sender.IsEmpty() ? email.get() : sender.get());
     }
 
     m_compFields->SetOrganization(organization);
 
     // We need an nsIMsgSend instance to send the message. Allow extensions
     // to override the default SMTP sender by observing mail-set-sender.
     mMsgSend = nullptr;
-    mDeliverMode = deliverMode;  // save for possible access by observer;
-
-    // TODO: Add observer to allow manipulation of mMsgSend, bug 1240823.
+    mDeliverMode = deliverMode;  // save for possible access by observer.
+
+    // Allow extensions to specify an outgoing server.
+    nsCOMPtr<nsIObserverService> observerService =
+      mozilla::services::GetObserverService();
+    NS_ENSURE_STATE(observerService);
+
+    // Assemble a string with sending parameters.
+    nsAutoString sendParms;
+
+    // First parameter: account key. This may be null.
+    sendParms.AppendASCII(accountKey && *accountKey ? accountKey : "");
+    sendParms.AppendLiteral(",");
+
+    // Second parameter: deliverMode.
+    sendParms.AppendInt(deliverMode);
+    sendParms.AppendLiteral(",");
+
+    // Third parameter: identity (as identity key).
+    nsAutoCString identityKey;
+    identity->GetKey(identityKey);
+    sendParms.AppendASCII(identityKey.get());
+
+    observerService->NotifyObservers(
+      NS_ISUPPORTS_CAST(nsIMsgCompose*, this),
+      "mail-set-sender",
+      sendParms.get());
+
     if (!mMsgSend)
       mMsgSend = do_CreateInstance(NS_MSGSEND_CONTRACTID);
 
     if (mMsgSend)
     {
       nsCString bodyString(m_compFields->GetBody());
 
       // Create the listener for the send operation...
--- a/mailnews/compose/test/unit/head_compose.js
+++ b/mailnews/compose/test/unit/head_compose.js
@@ -142,17 +142,18 @@ function createMessage(aAttachment) {
       attachment.url = "data:,";
       attachment.name = aAttachment;
     }
     attachments = [attachment];
   }
   return richCreateMessage(fields, attachments);
 }
 
-function richCreateMessage(fields, attachments=[], identity=null) {
+function richCreateMessage(fields, attachments=[], identity=null,
+                           account=null) {
   let params = Cc["@mozilla.org/messengercompose/composeparams;1"]
                  .createInstance(Ci.nsIMsgComposeParams);
   params.composeFields = fields;
 
   let msgCompose = MailServices.compose.initCompose(params);
   if (identity === null)
     identity = getSmtpIdentity(null, getBasicSmtpServer());
 
@@ -180,17 +181,18 @@ function richCreateMessage(fields, attac
 
   let progress = Cc["@mozilla.org/messenger/progress;1"]
                    .createInstance(Ci.nsIMsgProgress);
   let promise = new Promise((resolve, reject) => {
     progressListener.resolve = resolve;
     progressListener.reject = reject;
   });
   progress.registerListener(progressListener);
-  msgCompose.SendMsg(Ci.nsIMsgSend.nsMsgSaveAsDraft, identity, "", null,
+  msgCompose.SendMsg(Ci.nsIMsgSend.nsMsgSaveAsDraft, identity,
+                     account ? account.key : "", null,
                      progress);
   return promise;
 }
 
 function getAttachmentFromContent(aContent) {
   function getBoundaryStringFromContent(aContent) {
     let found = aContent.match(/Content-Type: multipart\/mixed;\s+boundary="(.*?)"/);
     do_check_neq(found, null);
new file mode 100644
--- /dev/null
+++ b/mailnews/compose/test/unit/test_sendObserver.js
@@ -0,0 +1,52 @@
+/*
+ * Tests that the mail-set-sender observer, used by extensions to modify the
+ * outgoing server, works.
+ *
+ * This is adapted from test_messageHeaders.js
+ */
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource:///modules/mailServices.js");
+Components.utils.import("resource:///modules/mimeParser.jsm");
+
+var CompFields = CC("@mozilla.org/messengercompose/composefields;1",
+                    Ci.nsIMsgCompFields);
+
+// nsIObserver implementation.
+var gData = "";
+var observer = {
+  observe: function (aSubject, aTopic, aData) {
+    if (aTopic == "mail-set-sender") {
+      Assert.ok(aSubject instanceof Ci.nsIMsgCompose);
+      gData = aData;
+    }
+  }
+}
+
+add_task(function* testObserver() {
+  let fields = new CompFields();
+  let identity = getSmtpIdentity("from@tinderbox.invalid",
+    getBasicSmtpServer());
+  identity.fullName = "Observer Tester";
+  fields.to = "Emile <nobody@tinderbox.invalid>";
+  fields.cc = "Alex <alex@tinderbox.invalid>";
+  fields.subject = "Let's test the observer";
+
+  yield richCreateMessage(fields, [], identity);
+  // observer data should have:
+  // (no account), Ci.nsIMsgSend.nsMsgSaveAsDraft, identity.key
+  Assert.equal(gData, ",4,id1");
+
+  // Now try with an account
+  yield richCreateMessage(fields, [], identity, localAccountUtils.msgAccount);
+  // observer data should have:
+  // (local account key), Ci.nsIMsgSend.nsMsgSaveAsDraft, identity.key
+  Assert.equal(gData, "account1,4,id1");
+});
+
+function run_test() {
+  // Ensure we have at least one mail account
+  localAccountUtils.loadLocalMailAccount();
+  Services.obs.addObserver(observer, "mail-set-sender", false);
+  run_next_test();
+}
--- a/mailnews/compose/test/unit/xpcshell.ini
+++ b/mailnews/compose/test/unit/xpcshell.ini
@@ -23,16 +23,17 @@ skip-if = os == 'mac'
 [test_saveDraft.js]
 [test_sendBackground.js]
 [test_sendMailAddressIDN.js]
 [test_sendMailMessage.js]
 [test_sendMessageFile.js]
 [test_sendMessageLater.js]
 [test_sendMessageLater2.js]
 [test_sendMessageLater3.js]
+[test_sendObserver.js]
 [test_smtp8bitMime.js]
 [test_smtpAuthMethods.js]
 [test_smtpPassword.js]
 [test_smtpPassword2.js]
 [test_smtpPasswordFailure1.js]
 [test_smtpPasswordFailure2.js]
 [test_smtpPasswordFailure3.js]
 [test_smtpProtocols.js]