~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to extensions/enigmail/src/nsEnigMsgCompose.cpp

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-05-28 14:05:04 UTC
  • mfrom: (0.12.11) (38.1.1 precise-security)
  • Revision ID: package-import@ubuntu.com-20120528140504-bu081xar9zks8vw9
Tags: 2:1.4.2-0ubuntu1
New upstream release v1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include "nsMsgBaseCID.h"
45
45
#include "nsMsgCompCID.h"
46
46
#include "nsIMsgMailSession.h"
47
 
#include "nsIIPCService.h"
48
47
#include "nsIEnigMsgCompFields.h"
49
48
#include "nsEnigMsgCompose.h"
50
49
#include "nspr.h"
161
160
    mMimeListener(nsnull),
162
161
 
163
162
    mWriter(nsnull),
164
 
    mPipeTrans(nsnull),
165
 
    mTargetThread(nsnull)
 
163
    mPipeTrans(nsnull)
166
164
{
167
165
  nsresult rv;
168
166
 
205
203
{
206
204
  DEBUG_LOG(("nsEnigMsgCompose::Finalize:\n"));
207
205
 
208
 
  if (mTargetThread) {
209
 
    mTargetThread->Shutdown();
210
 
    mTargetThread = nsnull;
211
 
  }
212
 
 
213
206
  mMsgComposeSecure = nsnull;
214
207
  mMimeListener = nsnull;
215
208
 
697
690
    if (NS_FAILED(rv)) return rv;
698
691
  }
699
692
 
700
 
  // Wait for input event queue to be completely processed
701
 
  if (mTargetThread) {
702
 
    nsCOMPtr<nsIOutputStream> outStream = do_QueryInterface(mPipeTrans);
703
 
 
704
 
    nsEnigComposeWriter* dispatchWriter = new nsEnigComposeWriter(outStream, nsnull, 0);
705
 
    dispatchWriter->CompleteEvents();
706
 
    mTargetThread->Dispatch(dispatchWriter, nsIEventTarget::DISPATCH_SYNC);
707
 
  }
708
 
 
709
693
  // Wait for STDOUT to close
710
694
  rv = mPipeTrans->Join();
711
695
  if (NS_FAILED(rv)) return rv;
905
889
  tmpStr.Assign(aBuf, aLen);
906
890
  DEBUG_LOG(("nsEnigMimeWriter::WriteToPipe: data: '%s'\n", tmpStr.get()));
907
891
 
908
 
  if (mMultipartSigned) {
909
 
    rv = mPipeTrans->WriteSync(aBuf, aLen);
910
 
  }
911
 
  else {
912
 
    if (! mTargetThread) {
913
 
      rv = NS_NewThread(&mTargetThread);
914
 
      if (NS_FAILED(rv)) return rv;
915
 
    }
916
 
 
917
 
    // dispatch message to different thread to avoid deadlock with input queue
918
 
 
919
 
    nsCOMPtr<nsIOutputStream> outStream = do_QueryInterface(mPipeTrans);
920
 
 
921
 
    nsEnigComposeWriter* dispatchWriter = new nsEnigComposeWriter(outStream, aBuf, aLen);
922
 
    rv = mTargetThread->Dispatch(dispatchWriter, nsIEventTarget::DISPATCH_NORMAL);
923
 
  }
924
 
 
 
892
  rv = mPipeTrans->Write(aBuf, aLen);
925
893
  return rv;
926
894
}
927
895
 
1136
1104
 
1137
1105
  return NS_OK;
1138
1106
}
1139
 
 
1140
 
 
1141
 
///////////////////////////////////////////////////////////////////////////////
1142
 
// nsEnigComposeWriter
1143
 
///////////////////////////////////////////////////////////////////////////////
1144
 
 
1145
 
NS_IMPL_THREADSAFE_ISUPPORTS1 (nsEnigComposeWriter,
1146
 
                               nsIRunnable)
1147
 
 
1148
 
 
1149
 
// nsStdinWriter implementation
1150
 
nsEnigComposeWriter::nsEnigComposeWriter(nsCOMPtr<nsIOutputStream>  pipeTrans,
1151
 
                    const char* buf,
1152
 
                    PRUint32 count) :
1153
 
  mBuf(nsnull),
1154
 
  mCompleteEvents(PR_FALSE)
1155
 
{
1156
 
    NS_INIT_ISUPPORTS();
1157
 
 
1158
 
#ifdef FORCE_PR_LOG
1159
 
  nsCOMPtr<nsIThread> myThread;
1160
 
  ENIG_GET_THREAD(myThread);
1161
 
  DEBUG_LOG(("nsEnigComposeWriter:: <<<<<<<<< CTOR(%p): myThread=%p\n",
1162
 
         this, myThread.get()));
1163
 
#endif
1164
 
 
1165
 
  mPipeTrans = pipeTrans;
1166
 
  mCount = count;
1167
 
 
1168
 
  if (count) {
1169
 
    mBuf = reinterpret_cast<char*>(nsMemory::Alloc(count));
1170
 
    if (!mBuf)
1171
 
      return;
1172
 
 
1173
 
    memcpy(mBuf, buf, count);
1174
 
  }
1175
 
}
1176
 
 
1177
 
 
1178
 
nsEnigComposeWriter::~nsEnigComposeWriter()
1179
 
{
1180
 
#ifdef FORCE_PR_LOG
1181
 
  nsCOMPtr<nsIThread> myThread;
1182
 
  ENIG_GET_THREAD(myThread);
1183
 
  DEBUG_LOG(("nsEnigComposeWriter:: >>>>>>>>> DTOR(%p): myThread=%p\n",
1184
 
         this, myThread.get()));
1185
 
#endif
1186
 
 
1187
 
 
1188
 
  // Release references
1189
 
  mPipeTrans = nsnull;
1190
 
  if (mBuf)
1191
 
    nsMemory::Free(mBuf);
1192
 
}
1193
 
 
1194
 
 
1195
 
NS_IMETHODIMP nsEnigComposeWriter::Run()
1196
 
{
1197
 
  nsresult rv;
1198
 
 
1199
 
  nsCOMPtr<nsIThread> myThread;
1200
 
  rv = ENIG_GET_THREAD(myThread);
1201
 
  NS_ENSURE_SUCCESS(rv, rv);
1202
 
  DEBUG_LOG(("nsEnigComposeWriter::Run: myThread=%p\n", myThread.get()));
1203
 
 
1204
 
  if (!mCompleteEvents) {
1205
 
    PRUint32 writeCount;
1206
 
    rv = mPipeTrans->Write(mBuf, mCount, &writeCount);
1207
 
    NS_ENSURE_SUCCESS(rv, rv);
1208
 
 
1209
 
    if (writeCount != mCount) {
1210
 
      DEBUG_LOG(("nsEnigComposeWriter::Run: written %d instead of %d bytes\n",
1211
 
        writeCount, mCount));
1212
 
      return NS_ERROR_FAILURE;
1213
 
    }
1214
 
  }
1215
 
  else {
1216
 
 
1217
 
    DEBUG_LOG(("nsEnigComposeWriter::Run: draining event queue\n"));
1218
 
 
1219
 
    EMBool pendingEvents;
1220
 
    rv = myThread->HasPendingEvents(&pendingEvents);
1221
 
    NS_ENSURE_SUCCESS(rv, rv);
1222
 
 
1223
 
    // in theory there should be no pending events here be
1224
 
 
1225
 
    while(pendingEvents) {
1226
 
      myThread->ProcessNextEvent(PR_FALSE, &pendingEvents);
1227
 
    }
1228
 
  }
1229
 
  return NS_OK;
1230
 
}
1231
 
 
1232
 
nsresult nsEnigComposeWriter::CompleteEvents() {
1233
 
  DEBUG_LOG(("nsEnigComposeWriter::CompleteEvents"));
1234
 
 
1235
 
  // dispatching of CompleteEvents needs to be done synchronously: this will ensure that
1236
 
  // the event is added at the end of the queue and the queue is emptied automagically
1237
 
 
1238
 
  mCompleteEvents = PR_TRUE;
1239
 
 
1240
 
  return NS_OK;
1241
 
}