1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Petr Kostka.
18 : * Portions created by the Initial Developer are Copyright (C) 2007
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "pk11func.h"
38 : #include "nsCOMPtr.h"
39 : #include "nsAutoPtr.h"
40 : #include "PSMRunnable.h"
41 : #include "nsString.h"
42 : #include "nsReadableUtils.h"
43 : #include "nsPKCS11Slot.h"
44 : #include "nsProtectedAuthThread.h"
45 :
46 : using namespace mozilla;
47 : using namespace mozilla::psm;
48 :
49 0 : NS_IMPL_THREADSAFE_ISUPPORTS1(nsProtectedAuthThread, nsIProtectedAuthThread)
50 :
51 0 : static void PR_CALLBACK nsProtectedAuthThreadRunner(void *arg)
52 : {
53 0 : nsProtectedAuthThread *self = static_cast<nsProtectedAuthThread *>(arg);
54 0 : self->Run();
55 0 : }
56 :
57 0 : nsProtectedAuthThread::nsProtectedAuthThread()
58 : : mMutex("nsProtectedAuthThread.mMutex")
59 : , mIAmRunning(false)
60 : , mLoginReady(false)
61 : , mThreadHandle(nsnull)
62 : , mSlot(0)
63 0 : , mLoginResult(SECFailure)
64 : {
65 : NS_INIT_ISUPPORTS();
66 0 : }
67 :
68 0 : nsProtectedAuthThread::~nsProtectedAuthThread()
69 : {
70 0 : }
71 :
72 0 : NS_IMETHODIMP nsProtectedAuthThread::Login(nsIObserver *aObserver)
73 : {
74 0 : NS_ENSURE_ARG(aObserver);
75 :
76 0 : if (!mSlot)
77 : // We need pointer to the slot
78 0 : return NS_ERROR_FAILURE;
79 :
80 0 : MutexAutoLock lock(mMutex);
81 :
82 0 : if (mIAmRunning || mLoginReady) {
83 0 : return NS_OK;
84 : }
85 :
86 0 : if (aObserver) {
87 : // We must AddRef aObserver here on the main thread, because it probably
88 : // does not implement a thread-safe AddRef.
89 : mNotifyObserver = new NotifyObserverRunnable(aObserver,
90 0 : "operation-completed");
91 : }
92 :
93 0 : mIAmRunning = true;
94 :
95 : mThreadHandle = PR_CreateThread(PR_USER_THREAD, nsProtectedAuthThreadRunner, static_cast<void*>(this),
96 0 : PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
97 :
98 : // bool thread_started_ok = (threadHandle != nsnull);
99 : // we might want to return "thread started ok" to caller in the future
100 0 : NS_ASSERTION(mThreadHandle, "Could not create nsProtectedAuthThreadRunner thread\n");
101 :
102 0 : return NS_OK;
103 : }
104 :
105 0 : NS_IMETHODIMP nsProtectedAuthThread::GetTokenName(nsAString &_retval)
106 : {
107 0 : MutexAutoLock lock(mMutex);
108 :
109 : // Get token name
110 0 : CopyUTF8toUTF16(nsDependentCString(PK11_GetTokenName(mSlot)), _retval);
111 :
112 0 : return NS_OK;
113 : }
114 :
115 0 : NS_IMETHODIMP nsProtectedAuthThread::GetSlot(nsIPKCS11Slot **_retval)
116 : {
117 0 : nsRefPtr<nsPKCS11Slot> slot;
118 : {
119 0 : MutexAutoLock lock(mMutex);
120 0 : slot = new nsPKCS11Slot(mSlot);
121 : }
122 0 : if (!slot)
123 0 : return NS_ERROR_OUT_OF_MEMORY;
124 :
125 0 : return CallQueryInterface (slot.get(), _retval);
126 : }
127 :
128 0 : void nsProtectedAuthThread::SetParams(PK11SlotInfo* aSlot)
129 : {
130 0 : MutexAutoLock lock(mMutex);
131 :
132 0 : mSlot = (aSlot) ? PK11_ReferenceSlot(aSlot) : 0;
133 0 : }
134 :
135 0 : SECStatus nsProtectedAuthThread::GetResult()
136 : {
137 0 : return mLoginResult;
138 : }
139 :
140 0 : void nsProtectedAuthThread::Run(void)
141 : {
142 : // Login with null password. This call will also do C_Logout() but
143 : // it is harmless here
144 0 : mLoginResult = PK11_CheckUserPassword(mSlot, 0);
145 :
146 0 : nsCOMPtr<nsIRunnable> notifyObserver;
147 : {
148 0 : MutexAutoLock lock(mMutex);
149 :
150 0 : mLoginReady = true;
151 0 : mIAmRunning = false;
152 :
153 : // Forget the slot
154 0 : if (mSlot)
155 : {
156 0 : PK11_FreeSlot(mSlot);
157 0 : mSlot = 0;
158 : }
159 :
160 0 : notifyObserver.swap(mNotifyObserver);
161 : }
162 :
163 0 : if (notifyObserver) {
164 0 : nsresult rv = NS_DispatchToMainThread(notifyObserver);
165 0 : NS_ASSERTION(NS_SUCCEEDED(rv),
166 : "failed to dispatch protected auth observer to main thread");
167 : }
168 0 : }
169 :
170 0 : void nsProtectedAuthThread::Join()
171 : {
172 0 : if (!mThreadHandle)
173 0 : return;
174 :
175 0 : PR_JoinThread(mThreadHandle);
176 0 : mThreadHandle = nsnull;
177 : }
|