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