1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/* 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 "PSMRunnable.h"
#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/RefPtr.h"
#include "nsCOMPtr.h"
#include "GeckoProfiler.h"
#include "nsPKCS11Slot.h"
#include "nsProtectedAuthThread.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "pk11func.h"
using namespace mozilla;
using namespace mozilla::psm;
NS_IMPL_ISUPPORTS(nsProtectedAuthThread, nsIProtectedAuthThread)
static void nsProtectedAuthThreadRunner(void* arg) {
AUTO_PROFILER_REGISTER_THREAD("Protected Auth");
NS_SetCurrentThreadName("Protected Auth");
nsProtectedAuthThread* self = static_cast<nsProtectedAuthThread*>(arg);
self->Run();
}
nsProtectedAuthThread::nsProtectedAuthThread()
: mMutex("nsProtectedAuthThread.mMutex"),
mIAmRunning(false),
mLoginReady(false),
mThreadHandle(nullptr),
mSlot(0),
mLoginResult(SECFailure) {}
nsProtectedAuthThread::~nsProtectedAuthThread() = default;
NS_IMETHODIMP nsProtectedAuthThread::Login(nsIObserver* aObserver) {
NS_ENSURE_ARG(aObserver);
if (!mSlot)
// We need pointer to the slot
return NS_ERROR_FAILURE;
MutexAutoLock lock(mMutex);
if (mIAmRunning || mLoginReady) {
return NS_OK;
}
if (aObserver) {
// We must AddRef aObserver here on the main thread, because it probably
// does not implement a thread-safe AddRef.
mNotifyObserver =
new NotifyObserverRunnable(aObserver, "operation-completed");
}
mIAmRunning = true;
mThreadHandle = PR_CreateThread(PR_USER_THREAD, nsProtectedAuthThreadRunner,
static_cast<void*>(this), PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
// bool thread_started_ok = (threadHandle != nullptr);
// we might want to return "thread started ok" to caller in the future
MOZ_ASSERT(mThreadHandle,
"Could not create nsProtectedAuthThreadRunner thread");
return NS_OK;
}
NS_IMETHODIMP nsProtectedAuthThread::GetTokenName(nsAString& _retval) {
MutexAutoLock lock(mMutex);
// Get token name
CopyUTF8toUTF16(nsDependentCString(PK11_GetTokenName(mSlot)), _retval);
return NS_OK;
}
NS_IMETHODIMP nsProtectedAuthThread::GetSlot(nsIPKCS11Slot** _retval) {
RefPtr<nsPKCS11Slot> slot;
{
MutexAutoLock lock(mMutex);
slot = new nsPKCS11Slot(mSlot);
}
slot.forget(_retval);
return NS_OK;
}
void nsProtectedAuthThread::SetParams(PK11SlotInfo* aSlot) {
MutexAutoLock lock(mMutex);
mSlot = (aSlot) ? PK11_ReferenceSlot(aSlot) : 0;
}
SECStatus nsProtectedAuthThread::GetResult() { return mLoginResult; }
void nsProtectedAuthThread::Run(void) {
// Login with null password. This call will also do C_Logout() but
// it is harmless here
mLoginResult = PK11_CheckUserPassword(mSlot, 0);
nsCOMPtr<nsIRunnable> notifyObserver;
{
MutexAutoLock lock(mMutex);
mLoginReady = true;
mIAmRunning = false;
// Forget the slot
if (mSlot) {
PK11_FreeSlot(mSlot);
mSlot = 0;
}
notifyObserver.swap(mNotifyObserver);
}
if (notifyObserver) {
DebugOnly<nsresult> rv = NS_DispatchToMainThread(notifyObserver);
MOZ_ASSERT(NS_SUCCEEDED(rv),
"Failed to dispatch protected auth observer to main thread");
}
}
void nsProtectedAuthThread::Join() {
if (!mThreadHandle) return;
PR_JoinThread(mThreadHandle);
mThreadHandle = nullptr;
}
|