CpEpEngine.h
author Volker Birk <vb@pep-project.org>
Thu, 12 May 2016 00:26:42 +0200
changeset 80 3862a304c0a9
parent 77 aac0bf974fbf
child 83 9b7371990473
permissions -rw-r--r--
adding error status
     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 "_IpEpEngineEvents_CP.h"
     8 #include "locked_queue.hh"
     9 #include "utf8_helper.h"
    10 #include "pEp_utility.h"
    11 
    12 
    13 #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
    14 #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."
    15 #endif
    16 
    17 using namespace ATL;
    18 using namespace utility;
    19 using namespace pEp::utility;
    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 IConnectionPointContainerImpl<CpEpEngine>,
    28 	public CProxy_IpEpEngineEvents<CpEpEngine>,
    29 	public IpEpEngine
    30 {
    31 protected:
    32     static int examine_identity(pEp_identity *ident, void *management);
    33 
    34 public:
    35     CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
    36 	{
    37         PEP_STATUS status = ::init(&m_session);
    38         assert(status == PEP_STATUS_OK);
    39         ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    40         ::register_sync_callbacks(m_session, (void*)this, messageToSend, showHandshake);
    41         ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    42     }
    43 
    44     ~CpEpEngine()
    45     {
    46         stop_keyserver_lookup();
    47         ::unregister_sync_callbacks(m_session);
    48         ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    49         ::release(m_session);
    50     }
    51 
    52 DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    53 
    54 DECLARE_NOT_AGGREGATABLE(CpEpEngine)
    55 
    56 BEGIN_COM_MAP(CpEpEngine)
    57     COM_INTERFACE_ENTRY(IpEpEngine)
    58     COM_INTERFACE_ENTRY(ISupportErrorInfo)
    59     COM_INTERFACE_ENTRY(IConnectionPointContainer)
    60 END_COM_MAP()
    61 
    62 BEGIN_CONNECTION_POINT_MAP(CpEpEngine)
    63 	CONNECTION_POINT_ENTRY(__uuidof(_IpEpEngineEvents))
    64 END_CONNECTION_POINT_MAP()
    65 // ISupportsErrorInfo
    66 	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
    67 
    68 
    69 	DECLARE_PROTECT_FINAL_CONSTRUCT()
    70 
    71 	HRESULT FinalConstruct()
    72 	{
    73 		return S_OK;
    74 	}
    75 
    76 	void FinalRelease()
    77 	{
    78 	}
    79 
    80 
    81 protected:
    82     class session
    83     {
    84     private:
    85         CpEpEngine *me;
    86 
    87     public:
    88         session(CpEpEngine *myself)
    89         {
    90             me = myself;
    91             me->session_mutex.lock();
    92         }
    93 
    94         ~session()
    95         {
    96             me->session_mutex.unlock();
    97         }
    98 
    99         operator PEP_SESSION const () 
   100         {
   101             return me->m_session;
   102         }
   103     };
   104 
   105     session get_session()
   106     {
   107         return session(this);
   108     }
   109 
   110     typedef locked_queue<pEp_identity_cpp> identity_queue_t;
   111     static ::pEp_identity * retrieve_next_identity(void *management);
   112     static PEP_STATUS messageToSend(void *obj, const message *msg);
   113     static PEP_STATUS showHandshake(void * obj, const pEp_identity *self, const pEp_identity *partner, sync_handshake_result *result);
   114 
   115     HRESULT error(_bstr_t msg);
   116 
   117     void verbose(string text)
   118     {
   119         if (verbose_mode) {
   120             stringstream ss;
   121             ss << __FILE__ << ":" << __LINE__ << " " << text;
   122             ::log_event(get_session(), "verbose", "pEp COM Server Adapter", ss.str().c_str(), NULL);
   123         }
   124     }
   125 
   126 private:
   127     PEP_SESSION m_session;
   128     mutex session_mutex;
   129     atomic< identity_queue_t * > identity_queue;
   130     thread *keymanagement_thread;
   131     bool verbose_mode;
   132 
   133 public:
   134     // runtime config of the adapter
   135 
   136     STDMETHOD(verbose_logging)(VARIANT_BOOL enable);
   137     
   138     // runtime config of the engine
   139 
   140     STDMETHOD(passive_mode)(VARIANT_BOOL enable);
   141     STDMETHOD(unencrypted_subject)(VARIANT_BOOL enable);
   142 
   143     // basic API
   144 
   145     STDMETHOD(log)(BSTR title, BSTR entity, BSTR description, BSTR comment);
   146     STDMETHOD(decrypt)(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
   147     STDMETHOD(decrypt_b)(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEp_STATUS * decrypt_status);
   148     STDMETHOD(encrypt)(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEp_STATUS * status);
   149     STDMETHOD(encrypt_b)(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEp_STATUS * status);
   150     STDMETHOD(trustword)(LONG value, BSTR lang, BSTR * word);
   151     STDMETHOD(trustwords)(BSTR fpr, BSTR lang, LONG max_words, BSTR * words);
   152     STDMETHOD(get_identity)(BSTR address, BSTR user_id, pEp_identity_s * ident);
   153     STDMETHOD(set_identity)(pEp_identity_s * ident);
   154     STDMETHOD(generate_keypair)(pEp_identity_s * ident, BSTR * fpr);
   155     STDMETHOD(delete_keypair)(BSTR fpr);
   156     STDMETHOD(import_key)(BSTR key_data);
   157     STDMETHOD(import_key_b)(SAFEARRAY * key_data);
   158     STDMETHOD(export_key)(BSTR fpr, BSTR * key_data);
   159     STDMETHOD(recv_key)(BSTR pattern);
   160     STDMETHOD(find_keys)(BSTR pattern, LPSAFEARRAY * key_list);
   161     STDMETHOD(send_key)(BSTR pattern);
   162     STDMETHOD(get_crashdump_log)(LONG maxlines, BSTR * log);
   163     STDMETHOD(get_languagelist)(BSTR * languages);
   164     STDMETHOD(get_phrase)(BSTR lang, LONG phrase_id, BSTR * phrase);
   165 
   166     // keymanagement API
   167 
   168     STDMETHOD(start_keyserver_lookup)();
   169     STDMETHOD(stop_keyserver_lookup)();
   170 
   171     STDMETHOD(examine_identity)(pEp_identity_s * ident);
   172     STDMETHOD(verify)(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEp_STATUS * verify_status);
   173     STDMETHOD(myself)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   174     STDMETHOD(update_identity)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   175     STDMETHOD(key_compromized)(struct pEp_identity_s *ident);
   176     STDMETHOD(key_reset_trust)(struct pEp_identity_s *ident);
   177     STDMETHOD(trust_personal_key)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
   178 
   179     // Blacklist API
   180 
   181     STDMETHOD(blacklist_add)(BSTR fpr);
   182     STDMETHOD(blacklist_delete)(BSTR fpr);
   183     STDMETHOD(blacklist_is_listed)(BSTR fpr, VARIANT_BOOL *listed);
   184     STDMETHOD(blacklist_retrieve)(SAFEARRAY **blacklist);
   185 
   186     // Message API
   187 
   188     STDMETHOD(encrypt_message)(text_message * src, text_message * dst, SAFEARRAY * extra);
   189     STDMETHOD(decrypt_message)(text_message * src, text_message * dst, SAFEARRAY ** keylist, pEp_color *rating);
   190     STDMETHOD(outgoing_message_color)(text_message *msg, pEp_color * pVal);
   191     STDMETHOD(identity_color)(pEp_identity_s * ident, pEp_color * pVal);
   192 
   193 };
   194 
   195 OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)