CpEpEngine.h
author Markus Schaber <markus@pep-security.net>
Fri, 16 Jun 2017 23:49:49 +0200
branchENGINE-179
changeset 254 70e5127cfb62
parent 249 2ac6226ff198
child 261 db956fa86d4e
permissions -rw-r--r--
ENGINE-179

Fix freeing of wrong variable.
Move method to the end of IpEpEngine2 (preserve COM backwards compatibility
for existing clients. Forward compatibility does not matter yet, as we install
plugin and adapter together, and there are no 3rd party clients yet).
Fix some naming conventions of parameters.
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,
markus@232
    27
	public IpEpEngine2
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)
markus@233
    55
    COM_INTERFACE_ENTRY(IpEpEngine2)
vb@5
    56
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
vb@0
    57
END_COM_MAP()
vb@0
    58
vb@0
    59
// ISupportsErrorInfo
vb@0
    60
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
vb@0
    61
vb@0
    62
vb@0
    63
	DECLARE_PROTECT_FINAL_CONSTRUCT()
vb@0
    64
vb@0
    65
	HRESULT FinalConstruct()
vb@0
    66
	{
vb@0
    67
		return S_OK;
vb@0
    68
	}
vb@0
    69
vb@0
    70
	void FinalRelease()
vb@0
    71
	{
vb@0
    72
	}
vb@0
    73
vb@0
    74
vb@0
    75
protected:
vb@0
    76
    class session
vb@0
    77
    {
vb@0
    78
    private:
vb@0
    79
        CpEpEngine *me;
vb@0
    80
vb@0
    81
    public:
vb@0
    82
        session(CpEpEngine *myself)
vb@0
    83
        {
vb@0
    84
            me = myself;
vb@0
    85
            me->session_mutex.lock();
vb@0
    86
        }
vb@0
    87
vb@0
    88
        ~session()
vb@0
    89
        {
vb@0
    90
            me->session_mutex.unlock();
vb@0
    91
        }
vb@0
    92
markus@169
    93
        operator PEP_SESSION const ()
vb@0
    94
        {
vb@0
    95
            return me->m_session;
vb@0
    96
        }
vb@0
    97
    };
vb@0
    98
vb@0
    99
    session get_session()
vb@0
   100
    {
vb@0
   101
        return session(this);
vb@0
   102
    }
vb@0
   103
vb@0
   104
    typedef locked_queue<pEp_identity_cpp> identity_queue_t;
vb@0
   105
    static ::pEp_identity * retrieve_next_identity(void *management);
markus@165
   106
    static PEP_STATUS messageToSend(void * obj, message *msg);
vb@227
   107
    static PEP_STATUS notifyHandshake(void * obj, pEp_identity *self, pEp_identity *partner, sync_handshake_signal signal);
vb@74
   108
markus@165
   109
vb@0
   110
    HRESULT error(_bstr_t msg);
markus@189
   111
    HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
vb@0
   112
vb@51
   113
    void verbose(string text)
vb@51
   114
    {
vb@51
   115
        if (verbose_mode) {
vb@51
   116
            stringstream ss;
vb@51
   117
            ss << __FILE__ << ":" << __LINE__ << " " << text;
vb@51
   118
            ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
vb@51
   119
        }
vb@51
   120
    }
vb@51
   121
vb@0
   122
private:
vb@0
   123
    PEP_SESSION m_session;
vb@0
   124
    mutex session_mutex;
vb@25
   125
    atomic< identity_queue_t * > identity_queue;
vb@0
   126
    thread *keymanagement_thread;
vb@51
   127
    bool verbose_mode;
vb@0
   128
markus@188
   129
	IpEpEngineCallbacks* client_callbacks = NULL;
markus@188
   130
    IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
markus@232
   131
    IpEpEngineCallbacks2* client_callbacks2_on_sync_thread = NULL;
markus@232
   132
    bool client_last_signalled_polling_state = true;
markus@84
   133
markus@165
   134
	// Keysync members
markus@169
   135
    static int inject_sync_msg(void *msg, void* management);
vb@227
   136
    static void* retrieve_next_sync_msg(void* management, time_t *timeout);
markus@169
   137
    void start_keysync();
markus@188
   138
    static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
markus@169
   139
    void stop_keysync();
markus@169
   140
markus@235
   141
    std::recursive_mutex keysync_mutex;
markus@235
   142
    std::condition_variable_any keysync_condition;
markus@169
   143
    std::thread *keysync_thread = NULL;
markus@169
   144
    std::queue<void*> keysync_queue;
markus@169
   145
    bool keysync_abort_requested = false;
markus@169
   146
    PEP_SESSION keysync_session;
markus@165
   147
markus@234
   148
    // Members used for handshake notification dispatch to the background thread.
markus@234
   149
    static void notify_handshake_background_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
markus@234
   150
    void notify_handshake_deliver_result();
markus@234
   151
    bool notify_handshake_active = false;
markus@234
   152
    bool notify_handshake_finished = false;
markus@234
   153
    std::thread *notify_handshake_thread = NULL;
markus@234
   154
    pEpIdentity notify_handshake_self;
markus@234
   155
    pEpIdentity notify_handshake_partner;
markus@234
   156
    SyncHandshakeSignal notify_handshake_signal;
markus@234
   157
    SyncHandshakeResult notify_handshake_result;
markus@234
   158
    LPSTREAM notify_handshake_error_info = NULL;
markus@234
   159
    HRESULT notify_handshake_error;
markus@234
   160
vb@0
   161
public:
vb@60
   162
    // runtime config of the adapter
vb@60
   163
markus@172
   164
    STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
markus@169
   165
vb@60
   166
    // runtime config of the engine
vb@60
   167
markus@172
   168
    STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
markus@172
   169
    STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
vb@60
   170
vb@60
   171
    // basic API
vb@51
   172
Dean@184
   173
    STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
markus@177
   174
    STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
Dean@202
   175
    STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
Dean@202
   176
    STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
markus@232
   177
    STDMETHOD(GetMessageTrustwords)(
markus@232
   178
        /* [in] */ struct TextMessage *msg,
markus@232
   179
        /* [in] */ struct pEpIdentity *receivedBy,
markus@232
   180
        /* [in] */ SAFEARRAY *keylist,
markus@232
   181
        /* [defaultvalue][in] */ BSTR lang,
markus@232
   182
        /* [defaultvalue][in] */ VARIANT_BOOL full,
markus@232
   183
        /* [retval][out] */ BSTR *words);
markus@172
   184
    STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
markus@177
   185
    STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
markus@177
   186
    STDMETHOD(GetLanguageList)(BSTR * languages);
vb@219
   187
	STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@219
   188
	STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
vb@11
   189
vb@11
   190
    // keymanagement API
vb@11
   191
markus@172
   192
    STDMETHOD(StartKeyserverLookup)();
markus@172
   193
    STDMETHOD(StopKeyserverLookup)();
vb@24
   194
markus@172
   195
    STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   196
    STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@172
   197
    STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
markus@172
   198
    STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
markus@172
   199
    STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
markus@254
   200
	STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* ownIdentities);
markus@165
   201
vb@65
   202
    // Blacklist API
vb@65
   203
markus@172
   204
    STDMETHOD(BlacklistAdd)(BSTR fpr);
markus@172
   205
    STDMETHOD(BlacklistDelete)(BSTR fpr);
markus@172
   206
    STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
Dean@187
   207
    STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
vb@65
   208
vb@12
   209
    // Message API
vb@12
   210
markus@172
   211
    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
markus@172
   212
    STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
markus@254
   213
    STDMETHOD(ReEvaluateMessageRating)(TextMessage * msg, SAFEARRAY * x_KeyList, pEpRating x_EncStatus, 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@240
   218
    STDMETHOD(EncryptMessageForSelf)(
markus@254
   219
        pEpIdentity * targetId, 
markus@240
   220
        TextMessage* src,
markus@240
   221
        TextMessage *dst,
markus@240
   222
        pEpEncryptFlags flags
markus@240
   223
        );
markus@238
   224
markus@84
   225
	// Event callbacks
markus@84
   226
markus@172
   227
	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
markus@188
   228
	STDMETHOD(UnregisterCallbacks)();
markus@169
   229
krista@154
   230
    // PGP compatibility functions
markus@177
   231
    STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
markus@84
   232
markus@84
   233
protected:
markus@84
   234
	HRESULT Fire_MessageToSend(
markus@172
   235
		/* [in] */ struct TextMessage *msg);
vb@0
   236
};
vb@0
   237
vb@0
   238
OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)