CpEpEngine.h
author Markus Schaber <markus@pep-security.net>
Wed, 01 Feb 2017 21:37:07 +0100
changeset 232 4c6d44bed519
parent 227 5bc3d8cb9415
child 233 27848ed0067c
permissions -rw-r--r--
COM-48 Implement timeout, and signal the app to poll inbox faster when waiting
on KeySync queue with a timeout.

First implementation draft.
vb@0
     1
// CpEpEngine.h : Declaration of the CpEpEngine
vb@0
     2
vb@0
     3
#pragma once
vb@0
     4
#include "resource.h"       // main symbols
vb@0
     5
vb@0
     6
#include "pEpComServerAdapter_i.h"
vb@0
     7
#include "locked_queue.hh"
vb@0
     8
#include "utf8_helper.h"
vb@33
     9
#include "pEp_utility.h"
markus@165
    10
#include <queue>
vb@0
    11
vb@0
    12
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
vb@0
    13
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
vb@0
    14
#endif
vb@0
    15
vb@0
    16
using namespace ATL;
vb@0
    17
using namespace utility;
vb@10
    18
using namespace pEp::utility;
vb@0
    19
markus@172
    20
vb@0
    21
// CpEpEngine
vb@0
    22
vb@0
    23
class ATL_NO_VTABLE CpEpEngine :
vb@3
    24
    public CComObjectRootEx<CComObjectThreadModel>,
vb@0
    25
	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
vb@0
    26
	public ISupportErrorInfo,
markus@232
    27
	public IpEpEngine2
vb@0
    28
{
vb@28
    29
protected:
vb@28
    30
    static int examine_identity(pEp_identity *ident, void *management);
vb@28
    31
vb@0
    32
public:
vb@51
    33
    CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
vb@0
    34
	{
vb@0
    35
        PEP_STATUS status = ::init(&m_session);
vb@0
    36
        assert(status == PEP_STATUS_OK);
vb@28
    37
        ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
vb@0
    38
        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
vb@0
    39
    }
vb@24
    40
vb@0
    41
    ~CpEpEngine()
vb@0
    42
    {
markus@169
    43
        stop_keysync();
markus@172
    44
        StopKeyserverLookup();
vb@0
    45
        ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
vb@0
    46
        ::release(m_session);
vb@0
    47
    }
vb@0
    48
vb@0
    49
DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
vb@0
    50
vb@0
    51
DECLARE_NOT_AGGREGATABLE(CpEpEngine)
vb@0
    52
vb@0
    53
BEGIN_COM_MAP(CpEpEngine)
vb@5
    54
    COM_INTERFACE_ENTRY(IpEpEngine)
vb@5
    55
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
vb@0
    56
END_COM_MAP()
vb@0
    57
vb@0
    58
// ISupportsErrorInfo
vb@0
    59
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
    60
vb@0
    61
vb@0
    62
	DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
    63
vb@0
    64
	HRESULT FinalConstruct()
vb@0
    65
	{
vb@0
    66
		return S_OK;
vb@0
    67
	}
vb@0
    68
vb@0
    69
	void FinalRelease()
vb@0
    70
	{
vb@0
    71
	}
vb@0
    72
vb@0
    73
vb@0
    74
protected:
vb@0
    75
    class session
vb@0
    76
    {
vb@0
    77
    private:
vb@0
    78
        CpEpEngine *me;
vb@0
    79
vb@0
    80
    public:
vb@0
    81
        session(CpEpEngine *myself)
vb@0
    82
        {
vb@0
    83
            me = myself;
vb@0
    84
            me->session_mutex.lock();
vb@0
    85
        }
vb@0
    86
vb@0
    87
        ~session()
vb@0
    88
        {
vb@0
    89
            me->session_mutex.unlock();
vb@0
    90
        }
vb@0
    91
markus@169
    92
        operator PEP_SESSION const ()
vb@0
    93
        {
vb@0
    94
            return me->m_session;
vb@0
    95
        }
vb@0
    96
    };
vb@0
    97
vb@0
    98
    session get_session()
vb@0
    99
    {
vb@0
   100
        return session(this);
vb@0
   101
    }
vb@0
   102
vb@0
   103
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   104
    static ::pEp_identity * retrieve_next_identity(void *management);
markus@165
   105
    static PEP_STATUS messageToSend(void * obj, message *msg);
vb@227
   106
    static PEP_STATUS notifyHandshake(void * obj, pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal);
vb@74
   107
markus@165
   108
vb@0
   109
    HRESULT error(_bstr_t msg);
markus@189
   110
    HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
vb@0
   111
vb@51
   112
    void verbose(string text)
vb@51
   113
    {
vb@51
   114
        if (verbose_mode) {
vb@51
   115
            stringstream ss;
vb@51
   116
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@51
   117
            ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   118
        }
vb@51
   119
    }
vb@51
   120
vb@0
   121
private:
vb@0
   122
    PEP_SESSION m_session;
vb@0
   123
    mutex session_mutex;
vb@25
   124
    atomic< identity_queue_t * > identity_queue;
vb@0
   125
    thread *keymanagement_thread;
vb@51
   126
    bool verbose_mode;
vb@0
   127
markus@188
   128
	IpEpEngineCallbacks* client_callbacks = NULL;
markus@188
   129
    IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
markus@232
   130
    IpEpEngineCallbacks2* client_callbacks2_on_sync_thread = NULL;
markus@232
   131
    bool client_last_signalled_polling_state = true;
markus@84
   132
markus@165
   133
	// Keysync members
markus@169
   134
    static int inject_sync_msg(void *msg, void* management);
vb@227
   135
    static void* retrieve_next_sync_msg(void* management, time_t *timeout);
markus@169
   136
    void start_keysync();
markus@188
   137
    static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
markus@169
   138
    void stop_keysync();
markus@169
   139
markus@169
   140
    std::mutex keysync_mutex;
markus@169
   141
    std::condition_variable keysync_condition;
markus@169
   142
    std::thread *keysync_thread = NULL;
markus@169
   143
    std::queue<void*> keysync_queue;
markus@169
   144
    bool keysync_abort_requested = false;
markus@169
   145
    PEP_SESSION keysync_session;
markus@165
   146
vb@0
   147
public:
vb@60
   148
    // runtime config of the adapter
vb@60
   149
markus@172
   150
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   151
vb@60
   152
    // runtime config of the engine
vb@60
   153
markus@172
   154
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   155
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   156
vb@60
   157
    // basic API
vb@51
   158
Dean@184
   159
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   160
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
Dean@202
   161
    STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
Dean@202
   162
    STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
markus@232
   163
    STDMETHOD(GetMessageTrustwords)(
markus@232
   164
        /* [in] */ struct TextMessage *msg,
markus@232
   165
        /* [in] */ struct pEpIdentity *receivedBy,
markus@232
   166
        /* [in] */ SAFEARRAY *keylist,
markus@232
   167
        /* [defaultvalue][in] */ BSTR lang,
markus@232
   168
        /* [defaultvalue][in] */ VARIANT_BOOL full,
markus@232
   169
        /* [retval][out] */ BSTR *words);
markus@172
   170
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   171
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   172
    STDMETHOD(GetLanguageList)(BSTR * languages);
vb@219
   173
	STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@219
   174
	STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@11
   175
vb@11
   176
    // keymanagement API
vb@11
   177
markus@172
   178
    STDMETHOD(StartKeyserverLookup)();
markus@172
   179
    STDMETHOD(StopKeyserverLookup)();
vb@24
   180
markus@172
   181
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   182
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   183
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   184
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   185
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
vb@219
   186
	STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* own_identities);
markus@165
   187
vb@65
   188
    // Blacklist API
vb@65
   189
markus@172
   190
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   191
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   192
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   193
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   194
vb@12
   195
    // Message API
vb@12
   196
markus@172
   197
    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@172
   198
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@172
   199
    STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
markus@172
   200
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@172
   201
	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   202
markus@84
   203
	// Event callbacks
markus@84
   204
markus@172
   205
	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@188
   206
	STDMETHOD(UnregisterCallbacks)();
markus@169
   207
krista@154
   208
    // PGP compatibility functions
markus@177
   209
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   210
markus@84
   211
protected:
markus@84
   212
	HRESULT Fire_MessageToSend(
markus@172
   213
		/* [in] */ struct TextMessage *msg);
markus@84
   214
vb@227
   215
	HRESULT Fire_NotifyHandshake(
markus@172
   216
		/* [in] */ struct pEpIdentity *self,
markus@172
   217
		/* [in] */ struct pEpIdentity *partner,
vb@227
   218
		/* [in] */ SyncHandshakeSignal signal,
markus@172
   219
		/* [retval][out] */ SyncHandshakeResult *result);
vb@0
   220
};
vb@0
   221
vb@0
   222
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)