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