CpEpEngine.h
author Volker Birk <vb@pep-project.org>
Wed, 23 Nov 2016 20:22:17 +0100
changeset 219 03531c4d3fda
parent 218 ef80732697e6
child 227 5bc3d8cb9415
permissions -rw-r--r--
switch to pEpIdentityFlags data type
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,
vb@15
    27
	public IpEpEngine
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);
markus@164
   106
    static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
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@84
   130
markus@165
   131
	// Keysync members
markus@169
   132
    static int inject_sync_msg(void *msg, void* management);
Dean@187
   133
    static void* retrieve_next_sync_msg(void* management);
markus@169
   134
    void start_keysync();
markus@188
   135
    static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
markus@169
   136
    void stop_keysync();
markus@169
   137
markus@169
   138
    std::mutex keysync_mutex;
markus@169
   139
    std::condition_variable keysync_condition;
markus@169
   140
    std::thread *keysync_thread = NULL;
markus@169
   141
    std::queue<void*> keysync_queue;
markus@169
   142
    bool keysync_abort_requested = false;
markus@169
   143
    PEP_SESSION keysync_session;
markus@165
   144
vb@0
   145
public:
vb@60
   146
    // runtime config of the adapter
vb@60
   147
markus@172
   148
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   149
vb@60
   150
    // runtime config of the engine
vb@60
   151
markus@172
   152
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   153
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   154
vb@60
   155
    // basic API
vb@51
   156
Dean@184
   157
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   158
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
Dean@202
   159
    STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
Dean@202
   160
    STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
markus@172
   161
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   162
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   163
    STDMETHOD(GetLanguageList)(BSTR * languages);
vb@219
   164
	STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@219
   165
	STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@11
   166
vb@11
   167
    // keymanagement API
vb@11
   168
markus@172
   169
    STDMETHOD(StartKeyserverLookup)();
markus@172
   170
    STDMETHOD(StopKeyserverLookup)();
vb@24
   171
markus@172
   172
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   173
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   174
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   175
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   176
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
vb@219
   177
	STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* own_identities);
markus@165
   178
vb@65
   179
    // Blacklist API
vb@65
   180
markus@172
   181
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   182
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   183
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   184
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   185
vb@12
   186
    // Message API
vb@12
   187
markus@172
   188
    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@172
   189
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@172
   190
    STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
markus@172
   191
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@172
   192
	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   193
markus@84
   194
	// Event callbacks
markus@84
   195
markus@172
   196
	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@188
   197
	STDMETHOD(UnregisterCallbacks)();
markus@169
   198
krista@154
   199
    // PGP compatibility functions
markus@177
   200
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   201
markus@84
   202
protected:
markus@84
   203
	HRESULT Fire_MessageToSend(
markus@172
   204
		/* [in] */ struct TextMessage *msg);
markus@84
   205
markus@84
   206
	HRESULT Fire_ShowHandshake(
markus@172
   207
		/* [in] */ struct pEpIdentity *self,
markus@172
   208
		/* [in] */ struct pEpIdentity *partner,
markus@172
   209
		/* [retval][out] */ SyncHandshakeResult *result);
vb@0
   210
};
vb@0
   211
vb@0
   212
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)