COM-88: Add function to "update now" and expose to app
authorMarkus Schaber <markus@pep-security.net>
Sat, 28 Apr 2018 19:55:17 +0200
changeset 28483ad8d765099
parent 283 24204d8f422e
child 285 9a19b5dbb271
child 288 a674aeebe476
COM-88: Add function to "update now" and expose to app
CpEpEngine.cpp
CpEpEngine.h
GateKeeper.cpp
GateKeeper.h
pEpCOMServerAdapter.idl
     1.1 --- a/CpEpEngine.cpp	Sat Apr 28 18:45:16 2018 +0200
     1.2 +++ b/CpEpEngine.cpp	Sat Apr 28 19:55:17 2018 +0200
     1.3 @@ -3,6 +3,7 @@
     1.4  #include "stdafx.h"
     1.5  #include "CpEpEngine.h"
     1.6  #include <mutex>
     1.7 +#include "GateKeeper.h"
     1.8  
     1.9  using namespace std;
    1.10  using namespace pEp::utility;
    1.11 @@ -983,7 +984,7 @@
    1.12      ::pEp_identity *_target_id = new_identity(targetId);
    1.13  
    1.14      ::message *_src = text_message_to_C(src);
    1.15 -
    1.16 +
    1.17      ::stringlist_t* _extra = NULL;
    1.18      HRESULT result = S_OK;
    1.19      ::message *msg_dst = NULL;
    1.20 @@ -1472,6 +1473,20 @@
    1.21      return msg;
    1.22  }
    1.23  
    1.24 +// Force an update check now
    1.25 +STDMETHODIMP CpEpEngine::UpdateNow()
    1.26 +{
    1.27 +    try
    1.28 +    {
    1.29 +        ::pEp::GateKeeper::update_now();
    1.30 +    }
    1.31 +    catch (bad_alloc&) {
    1.32 +        return E_OUTOFMEMORY;
    1.33 +    }
    1.34 +    catch (exception& ex) {
    1.35 +        return FAIL(ex.what());;
    1.36 +    }
    1.37 +}
    1.38  
    1.39  // Event callbacks
    1.40  
     2.1 --- a/CpEpEngine.h	Sat Apr 28 18:45:16 2018 +0200
     2.2 +++ b/CpEpEngine.h	Sat Apr 28 19:55:17 2018 +0200
     2.3 @@ -270,7 +270,8 @@
     2.4      STDMETHOD(OpenPGPListKeyinfo)(BSTR search_pattern, LPSAFEARRAY* keyinfo_list);
     2.5  	STDMETHOD(SetOwnKey)(pEpIdentity * ident, BSTR fpr, struct pEpIdentity *result);
     2.6  
     2.7 -
     2.8 +    // Trigger an immediate update
     2.9 +    STDMETHOD(UpdateNow)();
    2.10  protected:
    2.11      HRESULT Fire_MessageToSend(
    2.12          /* [in] */ struct TextMessage *msg);
     3.1 --- a/GateKeeper.cpp	Sat Apr 28 18:45:16 2018 +0200
     3.2 +++ b/GateKeeper.cpp	Sat Apr 28 19:55:17 2018 +0200
     3.3 @@ -109,6 +109,9 @@
     3.4  }
     3.5  
     3.6  namespace pEp {
     3.7 +    std::mutex GateKeeper::update_wait_mtx;
     3.8 +    std::condition_variable GateKeeper::update_wait_var;
     3.9 +    bool GateKeeper::update_wait_forced = false;
    3.10  
    3.11      const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
    3.12      const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
    3.13 @@ -116,7 +119,7 @@
    3.14  
    3.15      const time_t GateKeeper::cycle = 7200;   // 7200 sec is 2 h
    3.16      const time_t GateKeeper::fraction = 10;  // first update is at 10% of cycle
    3.17 -    const DWORD GateKeeper::waiting = 10000; // 10000 ms is 10 sec
    3.18 +    const chrono::seconds GateKeeper::waiting = 10s; //  10 sec
    3.19  
    3.20      GateKeeper::GateKeeper(CpEpCOMServerAdapterModule * self)
    3.21          : _self(self), now(time(NULL)), next(now /*+ time_diff()*/), hkUpdater(NULL),
    3.22 @@ -174,13 +177,28 @@
    3.23              now = time(NULL);
    3.24              assert(now != -1);
    3.25  
    3.26 -            if (now > next) {
    3.27 +            bool force_check;
    3.28 +            // We need to sleep, but we should be interruptible by the update_now() method.
    3.29 +            {
    3.30 +                std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    3.31 +                GateKeeper::update_wait_var.wait_for(guard, waiting);
    3.32 +                force_check = GateKeeper::update_wait_forced;
    3.33 +                GateKeeper::update_wait_forced = false;
    3.34 +            }
    3.35 +
    3.36 +            if (force_check || now > next) {
    3.37                  next = now + GateKeeper::cycle;
    3.38                  keep_updated();
    3.39              }
    3.40 +        }
    3.41 +    }
    3.42  
    3.43 -            Sleep(waiting);
    3.44 -        }
    3.45 +    void GateKeeper::update_now() 
    3.46 +    {
    3.47 +        // Signal the GateKeeper thread that we need to check for updates now.
    3.48 +        std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    3.49 +        GateKeeper::update_wait_forced = true;
    3.50 +        GateKeeper::update_wait_var.notify_all();
    3.51      }
    3.52  
    3.53      void GateKeeper::keep_plugin()
     4.1 --- a/GateKeeper.h	Sat Apr 28 18:45:16 2018 +0200
     4.2 +++ b/GateKeeper.h	Sat Apr 28 19:55:17 2018 +0200
     4.3 @@ -31,6 +31,8 @@
     4.4  
     4.5          void keep();
     4.6  
     4.7 +        static void update_now();
     4.8 +
     4.9      protected:
    4.10          static const LPCTSTR plugin_reg_path;
    4.11          static const LPCTSTR plugin_reg_value_name;
    4.12 @@ -38,7 +40,7 @@
    4.13  
    4.14          static const time_t cycle;
    4.15          static const time_t fraction;
    4.16 -        static const DWORD waiting;
    4.17 +        static const std::chrono::seconds waiting;
    4.18  
    4.19          static time_t time_diff();
    4.20  
    4.21 @@ -65,5 +67,9 @@
    4.22          BCRYPT_ALG_HANDLE hRSA;
    4.23  
    4.24          CpEpCOMServerAdapterModule * _self;
    4.25 +
    4.26 +        static std::mutex update_wait_mtx;
    4.27 +        static std::condition_variable update_wait_var;
    4.28 +        static bool update_wait_forced;
    4.29      };
    4.30  }
     5.1 --- a/pEpCOMServerAdapter.idl	Sat Apr 28 18:45:16 2018 +0200
     5.2 +++ b/pEpCOMServerAdapter.idl	Sat Apr 28 19:55:17 2018 +0200
     5.3 @@ -429,6 +429,9 @@
     5.4          // Default is pEpEncFormat.pEpEncPep, all other formats are only 
     5.5          // for compatibility and not intended for normal use.
     5.6          [in, defaultvalue(pEpEncPep)] pEpEncFormat encFormat);
     5.7 +
     5.8 +    // Trigger an immediate update check.
     5.9 +    [id(40)] HRESULT UpdateNow();
    5.10  };
    5.11  
    5.12  [