CpEpEngine.h
changeset 277 6d26da1671b3
parent 275 08f4040eca91
parent 274 b4f390f0a123
child 278 ae41fbcb1fd6
     1.1 --- a/CpEpEngine.h	Sat Feb 24 20:59:45 2018 +0100
     1.2 +++ b/CpEpEngine.h	Sat Feb 24 21:04:09 2018 +0100
     1.3 @@ -22,9 +22,9 @@
     1.4  
     1.5  class ATL_NO_VTABLE CpEpEngine :
     1.6      public CComObjectRootEx<CComObjectThreadModel>,
     1.7 -	public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
     1.8 -	public ISupportErrorInfo,
     1.9 -	public IpEpEngine2
    1.10 +    public CComCoClass<CpEpEngine, &CLSID_pEpEngine>,
    1.11 +    public ISupportErrorInfo,
    1.12 +    public IpEpEngine
    1.13  {
    1.14  
    1.15  protected:
    1.16 @@ -32,48 +32,61 @@
    1.17  
    1.18  public:
    1.19      CpEpEngine() : keymanagement_thread(NULL), identity_queue(NULL), verbose_mode(false)
    1.20 -	{
    1.21 -		std::lock_guard<std::mutex> lock(init_mutex);
    1.22 -		PEP_STATUS status = ::init(&m_session);
    1.23 -		assert(status == PEP_STATUS_OK);
    1.24 -
    1.25 -        ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    1.26 -        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    1.27 +    {
    1.28 +        // See FinalConstruct() below for most initialization work, and an
    1.29 +        // explanation why it had to be moved there...
    1.30      }
    1.31  
    1.32      ~CpEpEngine()
    1.33      {
    1.34          stop_keysync();
    1.35          StopKeyserverLookup();
    1.36 -        ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    1.37 -		std::lock_guard<std::mutex> lock(init_mutex);
    1.38 -		::release(m_session);
    1.39 +        if (m_session) // may be zero when FinalConstruct failed to initialize the engine
    1.40 +        {
    1.41 +            ::log_event(m_session, "Shutdown", "pEp COM Adapter", NULL, NULL);
    1.42 +            std::lock_guard<std::mutex> lock(init_mutex);
    1.43 +            ::release(m_session);
    1.44 +        }
    1.45      }
    1.46  
    1.47 -DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    1.48 +    DECLARE_REGISTRY_RESOURCEID(IDR_PEPENGINE)
    1.49  
    1.50 -DECLARE_NOT_AGGREGATABLE(CpEpEngine)
    1.51 +    DECLARE_NOT_AGGREGATABLE(CpEpEngine)
    1.52  
    1.53 -BEGIN_COM_MAP(CpEpEngine)
    1.54 -    COM_INTERFACE_ENTRY(IpEpEngine)
    1.55 -    COM_INTERFACE_ENTRY(IpEpEngine2)
    1.56 -    COM_INTERFACE_ENTRY(ISupportErrorInfo)
    1.57 -END_COM_MAP()
    1.58 +    BEGIN_COM_MAP(CpEpEngine)
    1.59 +        COM_INTERFACE_ENTRY(IpEpEngine)
    1.60 +        COM_INTERFACE_ENTRY(ISupportErrorInfo)
    1.61 +    END_COM_MAP()
    1.62  
    1.63 -// ISupportsErrorInfo
    1.64 -	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
    1.65 +    // ISupportsErrorInfo
    1.66 +    STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
    1.67  
    1.68 +    DECLARE_PROTECT_FINAL_CONSTRUCT()
    1.69  
    1.70 -	DECLARE_PROTECT_FINAL_CONSTRUCT()
    1.71 +    // Unfortunately, neither FAIL nor error() work in the constructor, as 
    1.72 +    // CreateErrorInfo/SetErrorInfo cannot work when the instance is not constructed.
    1.73 +    // AtlThrow works, but the exception is caught in CComCreator.CreateInstance, and
    1.74 +    // unconditionally turned into E_OUTOFMEMORY. Thus, we need to do most constructor
    1.75 +    // work in FinalConstruct. CreateErrorInfo/SetErrorInfo still won't work, but at least,
    1.76 +    // we can return a meaningful HRESULT. Thus, we pack our PEP_STATUS into a custom HRESULT.	
    1.77 +    HRESULT FinalConstruct()
    1.78 +    {
    1.79 +        std::lock_guard<std::mutex> lock(init_mutex);
    1.80 +        PEP_STATUS status = ::init(&m_session);
    1.81 +        assert(status == PEP_STATUS_OK);
    1.82 +        if (status != PEP_STATUS_OK) {
    1.83 +            HRESULT res = MAKE_HRESULT(1, FACILITY_ITF, (0xFFFF & status));
    1.84 +            return res;
    1.85 +        }
    1.86  
    1.87 -	HRESULT FinalConstruct()
    1.88 -	{
    1.89 -		return S_OK;
    1.90 -	}
    1.91 +        ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    1.92 +        ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    1.93 +        return S_OK;
    1.94 +    }
    1.95  
    1.96 -	void FinalRelease()
    1.97 -	{
    1.98 -	}
    1.99 +    void FinalRelease()
   1.100 +    {
   1.101 +    }
   1.102  
   1.103  
   1.104  protected:
   1.105 @@ -131,19 +144,18 @@
   1.106      bool verbose_mode;
   1.107  
   1.108  
   1.109 -	IpEpEngineCallbacks* client_callbacks = NULL;
   1.110 +    IpEpEngineCallbacks* client_callbacks = NULL;
   1.111      IpEpEngineCallbacks* client_callbacks_on_sync_thread = NULL;
   1.112 -    IpEpEngineCallbacks2* client_callbacks2_on_sync_thread = NULL;
   1.113      bool client_last_signalled_polling_state = true;
   1.114  
   1.115 -	// Keysync members
   1.116 +    // Keysync members
   1.117      static int inject_sync_msg(void *msg, void* management);
   1.118      static void* retrieve_next_sync_msg(void* management, time_t *timeout);
   1.119      void start_keysync();
   1.120      static void do_keysync_in_thread(CpEpEngine* self, LPSTREAM marshaled_callbacks);
   1.121      void stop_keysync();
   1.122  
   1.123 -	static std::mutex init_mutex;
   1.124 +    static std::mutex init_mutex;
   1.125  
   1.126      std::recursive_mutex keysync_mutex;
   1.127      std::condition_variable_any keysync_condition;
   1.128 @@ -191,8 +203,8 @@
   1.129      STDMETHOD(GetCrashdumpLog)(LONG maxlines, BSTR * log);
   1.130      STDMETHOD(GetEngineVersion)(BSTR * engineVersion);
   1.131      STDMETHOD(GetLanguageList)(BSTR * languages);
   1.132 -	STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
   1.133 -	STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
   1.134 +    STDMETHOD(SetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
   1.135 +    STDMETHOD(UnsetIdentityFlags)(struct pEpIdentity *identity, pEpIdentityFlags flags);
   1.136  
   1.137      // keymanagement API
   1.138  
   1.139 @@ -204,7 +216,13 @@
   1.140      STDMETHOD(KeyMistrusted)(struct pEpIdentity *ident);
   1.141      STDMETHOD(KeyResetTrust)(struct pEpIdentity *ident);
   1.142      STDMETHOD(TrustPersonalKey)(struct pEpIdentity *ident, struct pEpIdentity *result);
   1.143 -	STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* ownIdentities);
   1.144 +    STDMETHOD(OwnIdentitiesRetrieve)(LPSAFEARRAY* ownIdentities);
   1.145 +
   1.146 +    STDMETHOD(UndoLastMistrust)(); 
   1.147 +    
   1.148 +    STDMETHOD(IsPepUser)(
   1.149 +        /* [in] */ struct pEpIdentity *ident,
   1.150 +        /* [retval][out] */ VARIANT_BOOL *ispEp);
   1.151  
   1.152      // Blacklist API
   1.153  
   1.154 @@ -215,34 +233,38 @@
   1.155  
   1.156      // Message API
   1.157  
   1.158 -    STDMETHOD(EncryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY * extra, pEpEncryptFlags flags);
   1.159 +    STDMETHOD(EncryptMessage)(
   1.160 +        /* [in] */ struct TextMessage *src,
   1.161 +        /* [out] */ struct TextMessage *dst,
   1.162 +        /* [in] */ SAFEARRAY * extra,
   1.163 +        /* [defaultvalue][in] */ pEpEncryptFlags flags = pEpEncryptFlagDefault,
   1.164 +        /* [defaultvalue][in] */ pEpEncFormat encFormat = pEpEncPep);
   1.165 +
   1.166      STDMETHOD(DecryptMessage)(TextMessage * src, TextMessage * dst, SAFEARRAY ** keylist, pEpDecryptFlags* flags, pEpRating *rating);
   1.167      STDMETHOD(ReEvaluateMessageRating)(TextMessage * msg, SAFEARRAY * x_KeyList, pEpRating x_EncStatus, pEpRating *rating);
   1.168      STDMETHOD(OutgoingMessageRating)(TextMessage *msg, pEpRating * pVal);
   1.169      STDMETHOD(IdentityRating)(pEpIdentity * ident, pEpRating * pVal);
   1.170 -	STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
   1.171 +    STDMETHOD(ColorFromRating)(pEpRating rating, pEpColor * pVal);
   1.172  
   1.173      STDMETHOD(EncryptMessageForSelf)(
   1.174 -        pEpIdentity * targetId, 
   1.175 +        pEpIdentity * targetId,
   1.176          TextMessage* src,
   1.177          TextMessage *dst,
   1.178          pEpEncryptFlags flags
   1.179          );
   1.180  
   1.181 -	// Event callbacks
   1.182 +    // Event callbacks
   1.183  
   1.184 -	STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
   1.185 -	STDMETHOD(UnregisterCallbacks)();
   1.186 +    STDMETHOD(RegisterCallbacks)(IpEpEngineCallbacks *new_callback);
   1.187 +    STDMETHOD(UnregisterCallbacks)();
   1.188  
   1.189      // PGP compatibility functions
   1.190      STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
   1.191  
   1.192 -	STDMETHOD(UndoLastMistrust)();
   1.193 -	STDMETHOD(SetOwnKey)(pEpIdentity * ident, BSTR fpr, struct pEpIdentity *result);
   1.194  
   1.195  protected:
   1.196 -	HRESULT Fire_MessageToSend(
   1.197 -		/* [in] */ struct TextMessage *msg);
   1.198 +    HRESULT Fire_MessageToSend(
   1.199 +        /* [in] */ struct TextMessage *msg);
   1.200  };
   1.201  
   1.202  OBJECT_ENTRY_AUTO(__uuidof(pEpEngine), CpEpEngine)