CpEpEngine.h
author Volker Birk <vb@pep.foundation>
Mon, 22 Oct 2018 17:54:28 +0200
branchsync
changeset 307 eb5012d87405
parent 304 00916b0afaac
child 308 0017f25f860d
permissions -rw-r--r--
simplify
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@307
    90
        startup<CpEpEngine>(messageToSend, notifyHandshake, 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@295
   103
    static PEP_STATUS messageToSend(message *msg);
vb@295
   104
    static PEP_STATUS notifyHandshake(pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal);
markus@165
   105
vb@0
   106
    HRESULT error(_bstr_t msg);
markus@189
   107
    HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
vb@0
   108
vb@51
   109
    void verbose(string text)
vb@51
   110
    {
vb@51
   111
        if (verbose_mode) {
vb@51
   112
            stringstream ss;
vb@51
   113
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@295
   114
            ::log_event(session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   115
        }
vb@51
   116
    }
vb@51
   117
vb@0
   118
private:
vb@297
   119
    // callbacks for sync
vb@297
   120
    struct MarshaledCallbacks {
vb@297
   121
        IpEpEngineCallbacks *unmarshaled;
vb@297
   122
        LPSTREAM marshaled;
vb@297
   123
    };
vb@297
   124
vb@297
   125
    static pEp::pc_container< MarshaledCallbacks, IpEpEngineCallbacks > sync_callbacks;
vb@297
   126
vb@304
   127
    void Startup_sync()
vb@304
   128
    {
vb@304
   129
        HRESULT r = CoInitializeEx(NULL, COINIT_MULTITHREADED);
vb@304
   130
        if (!SUCCEEDED(r))
vb@304
   131
            throw runtime_error("CoInitializeEx() failed on sync thread");
vb@304
   132
    }
vb@304
   133
vb@25
   134
    atomic< identity_queue_t * > identity_queue;
vb@0
   135
    thread *keymanagement_thread;
vb@51
   136
    bool verbose_mode;
vb@0
   137
markus@267
   138
    IpEpEngineCallbacks* client_callbacks = NULL;
markus@232
   139
    bool client_last_signalled_polling_state = true;
markus@84
   140
markus@267
   141
    static std::mutex init_mutex;
vb@296
   142
    static atomic< int > count;
vb@296
   143
vb@0
   144
public:
vb@60
   145
    // runtime config of the adapter
vb@60
   146
markus@172
   147
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   148
vb@60
   149
    // runtime config of the engine
vb@60
   150
markus@172
   151
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   152
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   153
vb@60
   154
    // basic API
vb@51
   155
Dean@184
   156
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   157
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
Dean@202
   158
    STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
Dean@202
   159
    STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
markus@232
   160
    STDMETHOD(GetMessageTrustwords)(
markus@232
   161
        /* [in] */ struct TextMessage *msg,
markus@232
   162
        /* [in] */ struct pEpIdentity *receivedBy,
markus@232
   163
        /* [in] */ SAFEARRAY *keylist,
markus@232
   164
        /* [defaultvalue][in] */ BSTR lang,
markus@232
   165
        /* [defaultvalue][in] */ VARIANT_BOOL full,
markus@232
   166
        /* [retval][out] */ BSTR *words);
markus@172
   167
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   168
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   169
    STDMETHOD(GetLanguageList)(BSTR * languages);
markus@267
   170
    STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
markus@267
   171
    STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@11
   172
vb@11
   173
    // keymanagement API
vb@11
   174
markus@172
   175
    STDMETHOD(StartKeyserverLookup)();
markus@172
   176
    STDMETHOD(StopKeyserverLookup)();
vb@24
   177
markus@172
   178
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   179
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   180
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   181
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   182
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@267
   183
    STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* ownIdentities);
markus@165
   184
vb@295
   185
    // STDMETHOD(UndoLastMistrust)(); 
markus@274
   186
    
vb@295
   187
    STDMETHOD(IspEpUser)(
markus@274
   188
        /* [in] */ struct pEpIdentity *ident,
markus@274
   189
        /* [retval][out] */ VARIANT_BOOL *ispEp);
markus@274
   190
vb@65
   191
    // Blacklist API
vb@65
   192
markus@172
   193
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   194
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   195
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   196
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   197
vb@12
   198
    // Message API
vb@12
   199
markus@271
   200
    STDMETHOD(EncryptMessage)(
markus@271
   201
        /* [in] */ struct TextMessage *src,
markus@271
   202
        /* [out] */ struct TextMessage *dst,
markus@271
   203
        /* [in] */ SAFEARRAY * extra,
markus@271
   204
        /* [defaultvalue][in] */ pEpEncryptFlags flags = pEpEncryptFlagDefault,
markus@271
   205
        /* [defaultvalue][in] */ pEpEncFormat encFormat = pEpEncPep);
markus@271
   206
markus@283
   207
    STDMETHOD(EncryptMessageAndAddPrivKey)(
markus@283
   208
        /* [in] */ struct TextMessage *src,
markus@283
   209
        /* [out] */ struct TextMessage *dst,
markus@283
   210
        /* [in] */ BSTR to_fpr,
markus@283
   211
        /* [defaultvalue][in] */ pEpEncryptFlags flags = pEpEncryptFlagDefault,
markus@283
   212
        /* [defaultvalue][in] */ pEpEncFormat encFormat = pEpEncPep);
markus@283
   213
markus@172
   214
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@254
   215
    STDMETHOD(ReEvaluateMessageRating)(TextMessage * msg, SAFEARRAY * x_KeyList, pEpRating x_EncStatus, pEpRating *rating);
Thomas@292
   216
	STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
Thomas@292
   217
	STDMETHOD(OutgoingMessageRatingPreview)(TextMessage *msg, pEpRating * pVal);
markus@172
   218
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@267
   219
    STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   220
markus@240
   221
    STDMETHOD(EncryptMessageForSelf)(
markus@269
   222
        pEpIdentity * targetId,
markus@240
   223
        TextMessage* src,
markus@280
   224
        /* [in] */ SAFEARRAY *extra,
markus@240
   225
        TextMessage *dst,
markus@240
   226
        pEpEncryptFlags flags
markus@240
   227
        );
markus@238
   228
markus@267
   229
    // Event callbacks
markus@267
   230
markus@267
   231
    STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@267
   232
    STDMETHOD(UnregisterCallbacks)();
markus@169
   233
krista@154
   234
    // PGP compatibility functions
markus@177
   235
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
vb@278
   236
	STDMETHOD(SetOwnKey)(pEpIdentity * ident, BSTR fpr, struct pEpIdentity *result);
markus@84
   237
markus@284
   238
    // Trigger an immediate update
markus@284
   239
    STDMETHOD(UpdateNow)();
vb@0
   240
};
vb@0
   241
vb@0
   242
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)