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