CpEpEngine.h
author Volker Birk <vb@pep-project.org>
Wed, 23 Nov 2016 17:04:34 +0100
changeset 218 ef80732697e6
parent 202 92fcb5658a34
child 219 03531c4d3fda
permissions -rw-r--r--
adding flags functions
     1 // CpEpEngine.h : Declaration of the CpEpEngine
     2 
     3 #pragma once
     4 #include "resource.h"       // main symbols
     5 
     6 #include "pEpComServerAdapter_i.h"
     7 #include "locked_queue.hh"
     8 #include "utf8_helper.h"
     9 #include "pEp_utility.h"
    10 #include <queue>
    11 
    12 #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
    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."
    14 #endif
    15 
    16 using namespace ATL;
    17 using namespace utility;
    18 using namespace pEp::utility;
    19 
    20 
    21 // CpEpEngine
    22 
    23 class ATL_NO_VTABLE CpEpEngine :
    24     public CComObjectRootEx<CComObjectThreadModel>,
    25 	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
    26 	public ISupportErrorInfo,
    27 	public IpEpEngine
    28 {
    29 protected:
    30     static int examine_identity(pEp_identity *ident, void *management);
    31 
    32 public:
    33     CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
    34 	{
    35         PEP_STATUS status = ::init(&m_session);
    36         assert(status == PEP_STATUS_OK);
    37         ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    38         ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    39     }
    40 
    41     ~CpEpEngine()
    42     {
    43         stop_keysync();
    44         StopKeyserverLookup();
    45         ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    46         ::release(m_session);
    47     }
    48 
    49 DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    50 
    51 DECLARE_NOT_AGGREGATABLE(CpEpEngine)
    52 
    53 BEGIN_COM_MAP(CpEpEngine)
    54     COM_INTERFACE_ENTRY(IpEpEngine)
    55     COM_INTERFACE_ENTRY(ISupportErrorInfo)
    56 END_COM_MAP()
    57 
    58 // ISupportsErrorInfo
    59 	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
    60 
    61 
    62 	DECLARE_PROTECT_FINAL_CONSTRUCT()
    63 
    64 	HRESULT FinalConstruct()
    65 	{
    66 		return S_OK;
    67 	}
    68 
    69 	void FinalRelease()
    70 	{
    71 	}
    72 
    73 
    74 protected:
    75     class session
    76     {
    77     private:
    78         CpEpEngine *me;
    79 
    80     public:
    81         session(CpEpEngine *myself)
    82         {
    83             me = myself;
    84             me->session_mutex.lock();
    85         }
    86 
    87         ~session()
    88         {
    89             me->session_mutex.unlock();
    90         }
    91 
    92         operator PEP_SESSION const ()
    93         {
    94             return me->m_session;
    95         }
    96     };
    97 
    98     session get_session()
    99     {
   100         return session(this);
   101     }
   102 
   103     typedef locked_queue<pEp_identity_cpp> identity_queue_t;
   104     static ::pEp_identity * retrieve_next_identity(void *management);
   105     static PEP_STATUS messageToSend(void * obj, message *msg);
   106     static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
   107 
   108 
   109     HRESULT error(_bstr_t msg);
   110     HRESULT error(_bstr_t msg, PEP_STATUS errorcode);
   111 
   112     void verbose(string text)
   113     {
   114         if (verbose_mode) {
   115             stringstream ss;
   116             ss << __FILE__ << ":" << __LINE__ << " " << text;
   117             ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
   118         }
   119     }
   120 
   121 private:
   122     PEP_SESSION m_session;
   123     mutex session_mutex;
   124     atomic< identity_queue_t * > identity_queue;
   125     thread *keymanagement_thread;
   126     bool verbose_mode;
   127 
   128 	IpEpEngineCallbacks* client_callbacks = NULL;
   129     IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
   130 
   131 	// Keysync members
   132     static int inject_sync_msg(void *msg, void* management);
   133     static void* retrieve_next_sync_msg(void* management);
   134     void start_keysync();
   135     static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
   136     void stop_keysync();
   137 
   138     std::mutex keysync_mutex;
   139     std::condition_variable keysync_condition;
   140     std::thread *keysync_thread = NULL;
   141     std::queue<void*> keysync_queue;
   142     bool keysync_abort_requested = false;
   143     PEP_SESSION keysync_session;
   144 
   145 public:
   146     // runtime config of the adapter
   147 
   148     STDMETHOD(VerboseLogging)(VARIANT_BOOL enable);
   149 
   150     // runtime config of the engine
   151 
   152     STDMETHOD(PassiveMode)(VARIANT_BOOL enable);
   153     STDMETHOD(UnencryptedSubject)(VARIANT_BOOL enable);
   154 
   155     // basic API
   156 
   157     STDMETHOD(ExportKey)(BSTR fpr, BSTR * keyData);
   158     STDMETHOD(Log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
   159     STDMETHOD(Trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
   160     STDMETHOD(GetTrustwords)(struct pEpIdentity *id1, struct pEpIdentity *id2, BSTR lang, VARIANT_BOOL full, BSTR *words);
   161     STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
   162     STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
   163     STDMETHOD(GetLanguageList)(BSTR * languages);
   164 	STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, LONG flags);
   165 	STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, LONG flags);
   166 
   167     // keymanagement API
   168 
   169     STDMETHOD(StartKeyserverLookup)();
   170     STDMETHOD(StopKeyserverLookup)();
   171 
   172     STDMETHOD(Myself)(struct pEpIdentity *ident, struct pEpIdentity *result);
   173     STDMETHOD(UpdateIdentity)(struct pEpIdentity *ident, struct pEpIdentity *result);
   174     STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
   175     STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
   176     STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
   177 
   178 
   179     // Blacklist API
   180 
   181     STDMETHOD(BlacklistAdd)(BSTR fpr);
   182     STDMETHOD(BlacklistDelete)(BSTR fpr);
   183     STDMETHOD(BlacklistIsListed)(BSTR fpr, VARIANT_BOOL *listed);
   184     STDMETHOD(BlacklistRetrieve)(SAFEARRAY **blacklist);
   185 
   186     // Message API
   187 
   188     STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
   189     STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
   190     STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
   191     STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
   192 	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
   193 
   194 	// Event callbacks
   195 
   196 	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
   197 	STDMETHOD(UnregisterCallbacks)();
   198 
   199     // PGP compatibility functions
   200     STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
   201 
   202 protected:
   203 	HRESULT Fire_MessageToSend(
   204 		/* [in] */ struct TextMessage *msg);
   205 
   206 	HRESULT Fire_ShowHandshake(
   207 		/* [in] */ struct pEpIdentity *self,
   208 		/* [in] */ struct pEpIdentity *partner,
   209 		/* [retval][out] */ SyncHandshakeResult *result);
   210 };
   211 
   212 OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)