CryptoTask.cpp (1246B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "CryptoTask.h" 8 #include "nsNSSComponent.h" 9 #include "nsNetCID.h" 10 11 namespace mozilla { 12 13 nsresult CryptoTask::Dispatch() { 14 // Ensure that NSS is initialized, since presumably CalculateResult 15 // will use NSS functions 16 if (!EnsureNSSInitializedChromeOrContent()) { 17 return NS_ERROR_FAILURE; 18 } 19 20 // The stream transport service (note: not the socket transport service) can 21 // be used to perform background tasks or I/O that would otherwise block the 22 // main thread. 23 nsCOMPtr<nsIEventTarget> target( 24 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID)); 25 if (!target) { 26 return NS_ERROR_FAILURE; 27 } 28 return target->Dispatch(this, NS_DISPATCH_NORMAL); 29 } 30 31 NS_IMETHODIMP 32 CryptoTask::Run() { 33 if (!NS_IsMainThread()) { 34 mRv = CalculateResult(); 35 NS_DispatchToMainThread(this); 36 } else { 37 // back on the main thread 38 CallCallback(mRv); 39 } 40 return NS_OK; 41 } 42 43 } // namespace mozilla