CpEpEngine.h
author Volker Birk <vb@pep.foundation>
Mon, 22 Oct 2018 10:14:50 +0200
branchsync
changeset 304 00916b0afaac
parent 297 a48f0545e221
child 307 eb5012d87405
permissions -rw-r--r--
start COM on sync thread
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@295
     7
#include "..\libpEpAdapter\locked_queue.hh"
vb@0
     8
#include "utf8_helper.h"
vb@33
     9
#include "pEp_utility.h"
vb@295
    10
#include "..\libpEpAdapter\Adapter.hh"
markus@165
    11
#include <queue>
krista@261
    12
#include <mutex>
vb@295
    13
#include <vector>
vb@297
    14
#include "..\libpEpAdapter\pc_container.hh"
vb@0
    15
vb@0
    16
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
vb@0
    17
#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
    18
#endif
vb@0
    19
vb@0
    20
using namespace ATL;
vb@0
    21
using namespace utility;
vb@10
    22
using namespace pEp::utility;
vb@295
    23
using namespace pEp::Adapter;
vb@0
    24
vb@0
    25
// CpEpEngine
vb@0
    26
vb@0
    27
class ATL_NO_VTABLE CpEpEngine :
vb@3
    28
    public CComObjectRootEx<CComObjectThreadModel>,
markus@267
    29
    public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
markus@267
    30
    public ISupportErrorInfo,
markus@271
    31
    public IpEpEngine
vb@0
    32
{
krista@261
    33
vb@28
    34
protected:
vb@28
    35
    static int examine_identity(pEp_identity *ident, void *management);
vb@28
    36
vb@0
    37
public:
vb@51
    38
    CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
markus@267
    39
    {
markus@269
    40
        // See FinalConstruct() below for most initialization work, and an
markus@269
    41
        // explanation why it had to be moved there...
vb@296
    42
        ++count;
vb@0
    43
    }
vb@24
    44
vb@0
    45
    ~CpEpEngine()
vb@0
    46
    {
vb@296
    47
        --count;
vb@296
    48
        if (!count) {
vb@296
    49
            StopKeyserverLookup();
vb@296
    50
            ::log_event(session(), "Shutdown", "pEp COM Adapter", NULL, NULL);
vb@296
    51
            session(pEp::Adapter::release);
vb@296
    52
            shutdown();
vb@296
    53
        }
vb@0
    54
    }
vb@0
    55
markus@269
    56
    DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
vb@0
    57
markus@269
    58
    DECLARE_NOT_AGGREGATABLE(CpEpEngine)
vb@0
    59
markus@269
    60
    BEGIN_COM_MAP(CpEpEngine)
markus@269
    61
        COM_INTERFACE_ENTRY(IpEpEngine)
markus@269
    62
        COM_INTERFACE_ENTRY(ISupportErrorInfo)
markus@269
    63
    END_COM_MAP()
vb@0
    64
markus@269
    65
    // ISupportsErrorInfo
markus@267
    66
    STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
    67
markus@267
    68
    DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
    69
markus@269
    70
    // Unfortunately, neither FAIL nor error() work in the constructor, as 
markus@269
    71
    // CreateErrorInfo/SetErrorInfo cannot work when the instance is not constructed.
markus@269
    72
    // AtlThrow works, but the exception is caught in CComCreator.CreateInstance, and
markus@269
    73
    // unconditionally turned into E_OUTOFMEMORY. Thus, we need to do most constructor
markus@269
    74
    // work in FinalConstruct. CreateErrorInfo/SetErrorInfo still won't work, but at least,
markus@269
    75
    // we can return a meaningful HRESULT. Thus, we pack our PEP_STATUS into a custom HRESULT.	
markus@267
    76
    HRESULT FinalConstruct()
markus@267
    77
    {
markus@269
    78
        std::lock_guard<std::mutex> lock(init_mutex);
vb@295
    79
        try {
vb@295
    80
            session();
vb@295
    81
        }
vb@295
    82
        catch (pEp::RuntimeError& e) {
vb@295
    83
            HRESULT res = MAKE_HRESULT(1, FACILITY_ITF, (0xFFFF & e.status));
markus@269
    84
            return res;
markus@269
    85
        }
markus@268
    86
vb@295
    87
        ::register_examine_function(session(), CpEpEngine::examine_identity, (void *)this);
vb@295
    88
        ::log_event(session(), "Startup", "pEp COM Adapter", NULL, NULL);
vb@297
    89
vb@304
    90
        startup<CpEpEngine>(messageToSend, notifyHandshake, messageToSend_sync, notifyHandshake_sync, this, &CpEpEngine::Startup_sync);
vb@297
    91
markus@267
    92
        return S_OK;
markus@267
    93
    }
vb@0
    94
markus@267
    95
    void FinalRelease()
markus@267
    96
    {
markus@267
    97
    }
vb@0
    98
vb@0
    99
protected:
vb@0
   100
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   101
    static ::pEp_identity * retrieve_next_identity(void *management);
vb@297
   102
vb@297
   103
    static PEP_STATUS _messageToSend(message *msg, bool in_sync = false);
vb@297
   104
    static PEP_STATUS _notifyHandshake(pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal, bool in_sync = false);
vb@297
   105
vb@295
   106
    static PEP_STATUS messageToSend(message *msg);
vb@295
   107
    static PEP_STATUS notifyHandshake(pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal);
markus@165
   108
vb@297
   109
    static PEP_STATUS messageToSend_sync(message *msg);
vb@297
   110
    static PEP_STATUS notifyHandshake_sync(pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal);
vb@297
   111
vb@0
   112
    HRESULT error(_bstr_t msg);
markus@189
   113
    HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
vb@0
   114
vb@51
   115
    void verbose(string text)
vb@51
   116
    {
vb@51
   117
        if (verbose_mode) {
vb@51
   118
            stringstream ss;
vb@51
   119
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@295
   120
            ::log_event(session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   121
        }
vb@51
   122
    }
vb@51
   123
vb@0
   124
private:
vb@297
   125
    // callbacks for sync
vb@297
   126
    struct MarshaledCallbacks {
vb@297
   127
        IpEpEngineCallbacks *unmarshaled;
vb@297
   128
        LPSTREAM marshaled;
vb@297
   129
    };
vb@297
   130
vb@297
   131
    static pEp::pc_container< MarshaledCallbacks, IpEpEngineCallbacks > sync_callbacks;
vb@297
   132
vb@304
   133
    void Startup_sync()
vb@304
   134
    {
vb@304
   135
        HRESULT r = CoInitializeEx(NULL, COINIT_MULTITHREADED);
vb@304
   136
        if (!SUCCEEDED(r))
vb@304
   137
            throw runtime_error("CoInitializeEx() failed on sync thread");
vb@304
   138
    }
vb@304
   139
vb@25
   140
    atomic< identity_queue_t * > identity_queue;
vb@0
   141
    thread *keymanagement_thread;
vb@51
   142
    bool verbose_mode;
vb@0
   143
markus@267
   144
    IpEpEngineCallbacks* client_callbacks = NULL;
markus@232
   145
    bool client_last_signalled_polling_state = true;
markus@84
   146
markus@267
   147
    static std::mutex init_mutex;
vb@296
   148
    static atomic< int > count;
vb@296
   149
vb@0
   150
public:
vb@60
   151
    // runtime config of the adapter
vb@60
   152
markus@172
   153
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   154
vb@60
   155
    // runtime config of the engine
vb@60
   156
markus@172
   157
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   158
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   159
vb@60
   160
    // basic API
vb@51
   161
Dean@184
   162
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   163
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
Dean@202
   164
    STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
Dean@202
   165
    STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
markus@232
   166
    STDMETHOD(GetMessageTrustwords)(
markus@232
   167
        /* [in] */ struct TextMessage *msg,
markus@232
   168
        /* [in] */ struct pEpIdentity *receivedBy,
markus@232
   169
        /* [in] */ SAFEARRAY *keylist,
markus@232
   170
        /* [defaultvalue][in] */ BSTR lang,
markus@232
   171
        /* [defaultvalue][in] */ VARIANT_BOOL full,
markus@232
   172
        /* [retval][out] */ BSTR *words);
markus@172
   173
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   174
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   175
    STDMETHOD(GetLanguageList)(BSTR * languages);
markus@267
   176
    STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
markus@267
   177
    STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@11
   178
vb@11
   179
    // keymanagement API
vb@11
   180
markus@172
   181
    STDMETHOD(StartKeyserverLookup)();
markus@172
   182
    STDMETHOD(StopKeyserverLookup)();
vb@24
   183
markus@172
   184
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   185
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   186
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   187
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   188
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@267
   189
    STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* ownIdentities);
markus@165
   190
vb@295
   191
    // STDMETHOD(UndoLastMistrust)(); 
markus@274
   192
    
vb@295
   193
    STDMETHOD(IspEpUser)(
markus@274
   194
        /* [in] */ struct pEpIdentity *ident,
markus@274
   195
        /* [retval][out] */ VARIANT_BOOL *ispEp);
markus@274
   196
vb@65
   197
    // Blacklist API
vb@65
   198
markus@172
   199
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   200
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   201
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   202
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   203
vb@12
   204
    // Message API
vb@12
   205
markus@271
   206
    STDMETHOD(EncryptMessage)(
markus@271
   207
        /* [in] */ struct TextMessage *src,
markus@271
   208
        /* [out] */ struct TextMessage *dst,
markus@271
   209
        /* [in] */ SAFEARRAY * extra,
markus@271
   210
        /* [defaultvalue][in] */ pEpEncryptFlags flags = pEpEncryptFlagDefault,
markus@271
   211
        /* [defaultvalue][in] */ pEpEncFormat encFormat = pEpEncPep);
markus@271
   212
markus@283
   213
    STDMETHOD(EncryptMessageAndAddPrivKey)(
markus@283
   214
        /* [in] */ struct TextMessage *src,
markus@283
   215
        /* [out] */ struct TextMessage *dst,
markus@283
   216
        /* [in] */ BSTR to_fpr,
markus@283
   217
        /* [defaultvalue][in] */ pEpEncryptFlags flags = pEpEncryptFlagDefault,
markus@283
   218
        /* [defaultvalue][in] */ pEpEncFormat encFormat = pEpEncPep);
markus@283
   219
markus@172
   220
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@254
   221
    STDMETHOD(ReEvaluateMessageRating)(TextMessage * msg, SAFEARRAY * x_KeyList, pEpRating x_EncStatus, pEpRating *rating);
Thomas@292
   222
	STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
Thomas@292
   223
	STDMETHOD(OutgoingMessageRatingPreview)(TextMessage *msg, pEpRating * pVal);
markus@172
   224
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@267
   225
    STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   226
markus@240
   227
    STDMETHOD(EncryptMessageForSelf)(
markus@269
   228
        pEpIdentity * targetId,
markus@240
   229
        TextMessage* src,
markus@280
   230
        /* [in] */ SAFEARRAY *extra,
markus@240
   231
        TextMessage *dst,
markus@240
   232
        pEpEncryptFlags flags
markus@240
   233
        );
markus@238
   234
markus@267
   235
    // Event callbacks
markus@267
   236
markus@267
   237
    STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@267
   238
    STDMETHOD(UnregisterCallbacks)();
markus@169
   239
krista@154
   240
    // PGP compatibility functions
markus@177
   241
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
vb@278
   242
	STDMETHOD(SetOwnKey)(pEpIdentity * ident, BSTR fpr, struct pEpIdentity *result);
markus@84
   243
markus@284
   244
    // Trigger an immediate update
markus@284
   245
    STDMETHOD(UpdateNow)();
vb@0
   246
};
vb@0
   247
vb@0
   248
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)