CpEpEngine.h
author Dean
Thu, 20 Oct 2016 17:53:00 +0200
changeset 187 4ba92936a9d9
parent 184 1a2c49263cba
child 188 cf4a4e973ab2
permissions -rw-r--r--
Spelling correction.
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
markus@84
   103
	class callbacks
markus@84
   104
	{
markus@84
   105
	private:
markus@84
   106
		CpEpEngine *me;
markus@84
   107
markus@84
   108
	public:
markus@84
   109
		callbacks(CpEpEngine *myself)
markus@84
   110
		{
markus@84
   111
			me = myself;
markus@84
   112
			me->callback_mutex.lock();
markus@84
   113
		}
markus@84
   114
markus@84
   115
		~callbacks()
markus@84
   116
		{
markus@84
   117
			me->callback_mutex.unlock();
markus@84
   118
		}
markus@84
   119
markus@84
   120
		operator vector<IpEpEngineCallbacks *>& ()
markus@84
   121
		{
markus@84
   122
			return me->callback_vector;
markus@84
   123
		}
markus@84
   124
	};
markus@84
   125
markus@84
   126
	callbacks get_callbacks()
markus@84
   127
	{
markus@84
   128
		return callbacks(this);
markus@84
   129
	}
markus@84
   130
vb@0
   131
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   132
    static ::pEp_identity * retrieve_next_identity(void *management);
markus@165
   133
    static PEP_STATUS messageToSend(void * obj, message *msg);
markus@164
   134
    static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
vb@74
   135
markus@165
   136
vb@0
   137
    HRESULT error(_bstr_t msg);
vb@0
   138
vb@51
   139
    void verbose(string text)
vb@51
   140
    {
vb@51
   141
        if (verbose_mode) {
vb@51
   142
            stringstream ss;
vb@51
   143
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@51
   144
            ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   145
        }
vb@51
   146
    }
vb@51
   147
vb@0
   148
private:
vb@0
   149
    PEP_SESSION m_session;
vb@0
   150
    mutex session_mutex;
vb@25
   151
    atomic< identity_queue_t * > identity_queue;
vb@0
   152
    thread *keymanagement_thread;
vb@51
   153
    bool verbose_mode;
vb@0
   154
markus@84
   155
	mutex callback_mutex;
markus@84
   156
	vector<IpEpEngineCallbacks*> callback_vector;
markus@84
   157
markus@165
   158
	// Keysync members
markus@169
   159
    static int inject_sync_msg(void *msg, void* management);
Dean@187
   160
    static void* retrieve_next_sync_msg(void* management);
markus@169
   161
    void start_keysync();
markus@169
   162
    void stop_keysync();
markus@169
   163
markus@169
   164
    std::mutex keysync_mutex;
markus@169
   165
    std::condition_variable keysync_condition;
markus@169
   166
    std::thread *keysync_thread = NULL;
markus@169
   167
    std::queue<void*> keysync_queue;
markus@169
   168
    bool keysync_thread_running = false;
markus@169
   169
    bool keysync_abort_requested = false;
markus@169
   170
    PEP_SESSION keysync_session;
markus@165
   171
vb@0
   172
public:
vb@60
   173
    // runtime config of the adapter
vb@60
   174
markus@172
   175
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   176
vb@60
   177
    // runtime config of the engine
vb@60
   178
markus@172
   179
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   180
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   181
vb@60
   182
    // basic API
vb@51
   183
Dean@184
   184
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   185
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
markus@172
   186
    STDMETHOD(TrustWords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
markus@172
   187
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   188
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   189
    STDMETHOD(GetLanguageList)(BSTR * languages);
vb@11
   190
vb@11
   191
    // keymanagement API
vb@11
   192
markus@172
   193
    STDMETHOD(StartKeyserverLookup)();
markus@172
   194
    STDMETHOD(StopKeyserverLookup)();
vb@24
   195
markus@172
   196
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   197
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   198
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   199
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   200
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
vb@48
   201
markus@165
   202
vb@65
   203
    // Blacklist API
vb@65
   204
markus@172
   205
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   206
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   207
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   208
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   209
vb@12
   210
    // Message API
vb@12
   211
markus@172
   212
    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@172
   213
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@172
   214
    STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
markus@172
   215
    STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
markus@172
   216
	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
vb@18
   217
markus@84
   218
	// Event callbacks
markus@84
   219
markus@172
   220
	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@172
   221
	STDMETHOD(UnregisterCallbacks)(IpEpEngineCallbacks *obsolete_callback);
markus@169
   222
krista@154
   223
    // PGP compatibility functions
markus@177
   224
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   225
markus@84
   226
protected:
markus@84
   227
	HRESULT Fire_MessageToSend(
markus@172
   228
		/* [in] */ struct TextMessage *msg);
markus@84
   229
markus@84
   230
	HRESULT Fire_ShowHandshake(
markus@172
   231
		/* [in] */ struct pEpIdentity *self,
markus@172
   232
		/* [in] */ struct pEpIdentity *partner,
markus@172
   233
		/* [retval][out] */ SyncHandshakeResult *result);
vb@0
   234
};
vb@0
   235
vb@0
   236
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)