From 6145c27ad68f3f49bdc8e5c621ef7209edf81545 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Mon, 7 Sep 2020 23:06:07 +0300 Subject: LookupWidget: Finding identities --- src/gmcerts.c | 40 ++++++++++++++++++++++++++++---------- src/gmcerts.h | 3 +++ src/ui/lookupwidget.c | 54 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/src/gmcerts.c b/src/gmcerts.c index a39b303a..0dc52041 100644 --- a/src/gmcerts.c +++ b/src/gmcerts.c @@ -179,7 +179,7 @@ iDefineTypeConstruction(GmIdentity) /*-----------------------------------------------------------------------------------------------*/ struct Impl_GmCerts { - iMutex mtx; + iMutex *mtx; iString saveDir; iStringHash *trusted; iPtrArray idents; @@ -337,7 +337,7 @@ static void load_GmCerts_(iGmCerts *d) { } void init_GmCerts(iGmCerts *d, const char *saveDir) { - init_Mutex(&d->mtx); + d->mtx = new_Mutex(); initCStr_String(&d->saveDir, saveDir); d->trusted = new_StringHash(); init_PtrArray(&d->idents); @@ -345,7 +345,7 @@ void init_GmCerts(iGmCerts *d, const char *saveDir) { } void deinit_GmCerts(iGmCerts *d) { - iGuardMutex(&d->mtx, { + iGuardMutex(d->mtx, { saveIdentities_GmCerts_(d); iForEach(PtrArray, i, &d->idents) { delete_GmIdentity(i.ptr); @@ -354,7 +354,7 @@ void deinit_GmCerts(iGmCerts *d) { iRelease(d->trusted); deinit_String(&d->saveDir); }); - deinit_Mutex(&d->mtx); + delete_Mutex(d->mtx); } iBool checkTrust_GmCerts(iGmCerts *d, iRangecc domain, const iTlsCertificate *cert) { @@ -372,7 +372,7 @@ iBool checkTrust_GmCerts(iGmCerts *d, iRangecc domain, const iTlsCertificate *ce iDate until; validUntil_TlsCertificate(cert, &until); iBlock *fingerprint = fingerprint_TlsCertificate(cert); - lock_Mutex(&d->mtx); + lock_Mutex(d->mtx); iTrustEntry *trust = value_StringHash(d->trusted, key); if (trust) { /* We already have it, check if it matches the one we trust for this domain (if it's @@ -382,7 +382,7 @@ iBool checkTrust_GmCerts(iGmCerts *d, iRangecc domain, const iTlsCertificate *ce if (secondsSince_Time(&trust->validUntil, &now) > 0) { /* Trusted cert is still valid. */ const iBool isTrusted = cmp_Block(fingerprint, &trust->fingerprint) == 0; - unlock_Mutex(&d->mtx); + unlock_Mutex(d->mtx); delete_Block(fingerprint); delete_String(key); return isTrusted; @@ -395,7 +395,7 @@ iBool checkTrust_GmCerts(iGmCerts *d, iRangecc domain, const iTlsCertificate *ce insert_StringHash(d->trusted, key, iClob(new_TrustEntry(fingerprint, &until))); } save_GmCerts_(d); - unlock_Mutex(&d->mtx); + unlock_Mutex(d->mtx); delete_Block(fingerprint); delete_String(key); return iTrue; @@ -410,16 +410,21 @@ const iGmIdentity *constIdentity_GmCerts(const iGmCerts *d, unsigned int id) { } const iGmIdentity *identityForUrl_GmCerts(const iGmCerts *d, const iString *url) { + lock_Mutex(d->mtx); + const iGmIdentity *found = NULL; iConstForEach(PtrArray, i, &d->idents) { const iGmIdentity *ident = i.ptr; iConstForEach(StringSet, j, ident->useUrls) { const iString *used = j.value; if (startsWithCase_String(url, cstr_String(used))) { - return ident; + found = ident; + goto done; } } } - return NULL; +done: + unlock_Mutex(d->mtx); + return found; } iGmIdentity *newIdentity_GmCerts(iGmCerts *d, int flags, iDate validUntil, const iString *commonName, @@ -454,7 +459,7 @@ iGmIdentity *newIdentity_GmCerts(iGmCerts *d, int flags, iDate validUntil, const return NULL; } } - pushBack_PtrArray(&d->idents, id); + iGuardMutex(d->mtx, pushBack_PtrArray(&d->idents, id)); return id; } @@ -467,6 +472,7 @@ static const char *certPath_GmCerts_(const iGmCerts *d, const iGmIdentity *ident } void deleteIdentity_GmCerts(iGmCerts *d, iGmIdentity *identity) { + lock_Mutex(d->mtx); /* Only delete the files if we created them. */ const char *filename = certPath_GmCerts_(d, identity); if (filename) { @@ -475,6 +481,7 @@ void deleteIdentity_GmCerts(iGmCerts *d, iGmIdentity *identity) { } removeOne_PtrArray(&d->idents, identity); collect_GmIdentity(identity); + unlock_Mutex(d->mtx); } const iString *certificatePath_GmCerts(const iGmCerts *d, const iGmIdentity *identity) { @@ -499,3 +506,16 @@ void signOut_GmCerts(iGmCerts *d, const iString *url) { setUse_GmIdentity(i.ptr, url, iFalse); } } + +const iPtrArray *listIdentities_GmCerts(const iGmCerts *d, iGmCertsIdentityFilterFunc filter, + void *context) { + iPtrArray *list = collectNew_PtrArray(); + lock_Mutex(d->mtx); + iConstForEach(PtrArray, i, &d->idents) { + if (!filter || filter(context, i.ptr)) { + pushBack_PtrArray(list, i.ptr); + } + } + unlock_Mutex(d->mtx); + return list; +} diff --git a/src/gmcerts.h b/src/gmcerts.h index 92a12a6a..2ab4396e 100644 --- a/src/gmcerts.h +++ b/src/gmcerts.h @@ -57,6 +57,8 @@ const iString *name_GmIdentity(const iGmIdentity *); iDeclareType(GmCerts) iDeclareTypeConstructionArgs(GmCerts, const char *saveDir) +typedef iBool (*iGmCertsIdentityFilterFunc)(void *context, const iGmIdentity *); + iBool checkTrust_GmCerts (iGmCerts *, iRangecc domain, const iTlsCertificate *cert); /** @@ -83,6 +85,7 @@ iGmIdentity * identity_GmCerts (iGmCerts *, unsigned int id); const iGmIdentity * constIdentity_GmCerts (const iGmCerts *, unsigned int id); const iGmIdentity * identityForUrl_GmCerts (const iGmCerts *, const iString *url); const iPtrArray * identities_GmCerts (const iGmCerts *); +const iPtrArray * listIdentities_GmCerts (const iGmCerts *, iGmCertsIdentityFilterFunc filter, void *context); void signIn_GmCerts (iGmCerts *, iGmIdentity *identity, const iString *url); void signOut_GmCerts (iGmCerts *, const iString *url); diff --git a/src/ui/lookupwidget.c b/src/ui/lookupwidget.c index 5fcbaacd..dbe734bf 100644 --- a/src/ui/lookupwidget.c +++ b/src/ui/lookupwidget.c @@ -21,17 +21,19 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "lookupwidget.h" + +#include "app.h" +#include "bookmarks.h" +#include "command.h" #include "documentwidget.h" -#include "lookup.h" -#include "listwidget.h" +#include "gmcerts.h" +#include "gmutil.h" +#include "history.h" #include "inputwidget.h" +#include "listwidget.h" +#include "lookup.h" #include "util.h" -#include "command.h" -#include "bookmarks.h" -#include "history.h" #include "visited.h" -#include "gmutil.h" -#include "app.h" #include #include @@ -156,6 +158,14 @@ static float bookmarkRelevance_LookupJob_(const iLookupJob *d, const iBookmark * return h + iMax(p, t) + 2 * g; /* extra weight for tags */ } +static float identityRelevance_LookupJob_(const iLookupJob *d, const iGmIdentity *identity) { + iString *cn = subject_TlsCertificate(identity->cert); + const float c = scoreMatch_(d->term, range_String(cn)); + const float n = scoreMatch_(d->term, range_String(&identity->notes)); + delete_String(cn); + return c + 2 * n; /* extra weight for notes */ +} + static float visitedRelevance_LookupJob_(const iLookupJob *d, const iVisitedUrl *vis) { iUrl parts; init_Url(&parts, &vis->url); @@ -169,6 +179,10 @@ static iBool matchBookmark_LookupJob_(void *context, const iBookmark *bm) { return bookmarkRelevance_LookupJob_(context, bm) > 0; } +static iBool matchIdentity_LookupJob_(void *context, const iGmIdentity *identity) { + return identityRelevance_LookupJob_(context, identity) > 0; +} + static void searchBookmarks_LookupJob_(iLookupJob *d) { /* Note: Called in a background thread. */ /* TODO: Thread safety! What if a bookmark gets deleted while its being accessed here? */ @@ -230,6 +244,25 @@ static void searchHistory_LookupJob_(iLookupJob *d) { } } +static void searchIdentities_LookupJob_(iLookupJob *d) { + /* Note: Called in a background thread. */ + iConstForEach(PtrArray, i, listIdentities_GmCerts(certs_App(), matchIdentity_LookupJob_, d)) { + const iGmIdentity *identity = i.ptr; + iLookupResult *res = new_LookupResult(); + res->type = identity_LookupResultType; + res->relevance = identityRelevance_LookupJob_(d, identity); + appendChar_String(&res->label, identity->icon); + appendChar_String(&res->label, ' '); + iString *cn = subject_TlsCertificate(identity->cert); + append_String(&res->label, cn); + delete_String(cn); + set_String(&res->meta, + collect_String( + hexEncode_Block(collect_Block(fingerprint_TlsCertificate(identity->cert))))); + pushBack_PtrArray(&d->results, res); + } +} + static iThreadResult worker_LookupWidget_(iThread *thread) { iLookupWidget *d = userData_Thread(thread); printf("[LookupWidget] worker is running\n"); fflush(stdout); @@ -271,6 +304,7 @@ static iThreadResult worker_LookupWidget_(iThread *thread) { if (termLen >= 3) { searchHistory_LookupJob_(job); } + searchIdentities_LookupJob_(job); } /* Submit the result. */ lock_Mutex(d->mtx); @@ -433,6 +467,12 @@ static void presentResults_LookupWidget_(iLookupWidget *d) { format_String(&item->command, "open url:%s", cstr_String(&res->url)); break; } + case identity_LookupResultType: { + item->fg = uiText_ColorId; + item->font = uiContent_FontId; + format_String(&item->text, "%s", cstr_String(&res->label)); + break; + } } addItem_ListWidget(d->list, item); iRelease(item); -- cgit v1.2.3