update_product() sync
authorVolker Birk <vb@pep.foundation>
Mon, 21 Oct 2019 11:59:42 +0200
branchsync
changeset 366f369f8053681
parent 363 4527e87ee120
child 367 38698c75fbbf
update_product()
CpEpEngine.cpp
CpEpEngine.h
GateKeeper.cpp
GateKeeper.h
pEpCOMServerAdapter.idl
     1.1 --- a/CpEpEngine.cpp	Mon Oct 21 10:56:57 2019 +0200
     1.2 +++ b/CpEpEngine.cpp	Mon Oct 21 11:59:42 2019 +0200
     1.3 @@ -1512,20 +1512,33 @@
     1.4  }
     1.5  
     1.6  // Force an update check now
     1.7 -STDMETHODIMP CpEpEngine::UpdateNow()
     1.8 +STDMETHODIMP CpEpEngine::UpdateNow(BSTR productCode, BOOL *didUpdate)
     1.9  {
    1.10 -    try
    1.11 +	BOOL result = FALSE;
    1.12 +
    1.13 +	try
    1.14      {
    1.15 -        ::pEp::GateKeeper::update_now();
    1.16 -    }
    1.17 +		_bstr_t pc(productCode);
    1.18 +		wstring _pc = pc;
    1.19 +
    1.20 +		auto products = pEp::GateKeeper::gatekeeper()->registered_products();
    1.21 +		for (auto p = products.begin(); p != products.end(); ++p) {
    1.22 +			if (_pc == p->first) {
    1.23 +				result = pEp::GateKeeper::gatekeeper()->update_product(*p);
    1.24 +				break;
    1.25 +			}
    1.26 +		}
    1.27 +		result = TRUE;
    1.28 +	}
    1.29      catch (bad_alloc&) {
    1.30 -        return E_OUTOFMEMORY;
    1.31 +		return E_OUTOFMEMORY;
    1.32      }
    1.33      catch (exception& ex) {
    1.34 -        return FAIL(ex.what());;
    1.35 +		return FAIL(ex.what());;
    1.36      }
    1.37  
    1.38 -    return S_OK;
    1.39 +	*didUpdate = result;
    1.40 +	return S_OK;
    1.41  }
    1.42  
    1.43  // Event callbacks
     2.1 --- a/CpEpEngine.h	Mon Oct 21 10:56:57 2019 +0200
     2.2 +++ b/CpEpEngine.h	Mon Oct 21 11:59:42 2019 +0200
     2.3 @@ -271,7 +271,7 @@
     2.4  	STDMETHOD(TrustOwnKey)(pEpIdentity * ident);
     2.5  
     2.6      // Trigger an immediate update
     2.7 -    STDMETHOD(UpdateNow)();
     2.8 +    STDMETHOD(UpdateNow)(BSTR productCode, BOOL *didUpdate);
     2.9  
    2.10  	STDMETHOD(Startup)();
    2.11  
     3.1 --- a/GateKeeper.cpp	Mon Oct 21 10:56:57 2019 +0200
     3.2 +++ b/GateKeeper.cpp	Mon Oct 21 11:59:42 2019 +0200
     3.3 @@ -109,10 +109,6 @@
     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      const LPCTSTR GateKeeper::updater_reg_path = _T("Software\\pEp\\Updater");
    3.14 @@ -125,6 +121,9 @@
    3.15          : _self(self), now(time(NULL)), next(now /*+ time_diff()*/), hkUpdater(NULL),
    3.16          internet(NULL), hAES(NULL), hRSA(NULL)
    3.17      {
    3.18 +		if (the_gatekeeper)
    3.19 +			throw runtime_error("second instance of GateKeeper was initialized");
    3.20 +
    3.21          DeleteFile(get_lockFile().c_str());
    3.22  
    3.23          LONG lResult = RegOpenCurrentUser(KEY_READ, &cu);
    3.24 @@ -139,10 +138,14 @@
    3.25              if (lResult != ERROR_SUCCESS)
    3.26                  RegCreateKeyEx(cu, updater_reg_path, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &hkUpdater, NULL);
    3.27          }
    3.28 +
    3.29 +		the_gatekeeper = this;
    3.30      }
    3.31  
    3.32      GateKeeper::~GateKeeper()
    3.33      {
    3.34 +		the_gatekeeper = nullptr;
    3.35 +
    3.36          if (cu_open) {
    3.37              if (hkUpdater)
    3.38                  RegCloseKey(hkUpdater);
    3.39 @@ -177,30 +180,13 @@
    3.40              now = time(NULL);
    3.41              assert(now != -1);
    3.42  
    3.43 -            bool force_check;
    3.44 -            // We need to sleep, but we should be interruptible by the update_now() method.
    3.45 -            {
    3.46 -                std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    3.47 -                GateKeeper::update_wait_var.wait_for(guard, waiting);
    3.48 -                force_check = GateKeeper::update_wait_forced;
    3.49 -                GateKeeper::update_wait_forced = false;
    3.50 -            }
    3.51 -
    3.52 -            if (force_check || now > next) {
    3.53 +            if (now > next) {
    3.54                  next = now + GateKeeper::cycle;
    3.55                  keep_updated();
    3.56              }
    3.57          }
    3.58      }
    3.59  
    3.60 -    void GateKeeper::update_now() 
    3.61 -    {
    3.62 -        // Signal the GateKeeper thread that we need to check for updates now.
    3.63 -        std::unique_lock<std::mutex> guard(GateKeeper::update_wait_mtx);
    3.64 -        GateKeeper::update_wait_forced = true;
    3.65 -        GateKeeper::update_wait_var.notify_all();
    3.66 -    }
    3.67 -
    3.68      void GateKeeper::keep_plugin()
    3.69      {
    3.70          HKEY hkPluginStart = NULL;
    3.71 @@ -631,4 +617,6 @@
    3.72          hRSA = NULL;
    3.73      }
    3.74  
    3.75 +	GateKeeper *GateKeeper::the_gatekeeper = nullptr;
    3.76 +
    3.77  } // namespace pEp
     4.1 --- a/GateKeeper.h	Mon Oct 21 10:56:57 2019 +0200
     4.2 +++ b/GateKeeper.h	Mon Oct 21 11:59:42 2019 +0200
     4.3 @@ -34,8 +34,10 @@
     4.4          }
     4.5  
     4.6          void keep();
     4.7 +		product_list registered_products();
     4.8 +		bool update_product(product p, DWORD context = 0);
     4.9  
    4.10 -        static void update_now();
    4.11 +		static GateKeeper *gatekeeper() { return the_gatekeeper; }
    4.12  
    4.13      protected:
    4.14          static const LPCTSTR plugin_reg_path;
    4.15 @@ -55,10 +57,9 @@
    4.16          BCRYPT_KEY_HANDLE delivery_key();
    4.17          string wrapped_delivery_key(BCRYPT_KEY_HANDLE hDeliveryKey);
    4.18  
    4.19 -        bool update_product(product p, DWORD context);
    4.20 -        product_list registered_products();
    4.21          void keep_updated();
    4.22          static tstring get_lockFile();
    4.23 +		static GateKeeper *the_gatekeeper;
    4.24  
    4.25      private:
    4.26          time_t now;
    4.27 @@ -71,9 +72,5 @@
    4.28          BCRYPT_ALG_HANDLE hRSA;
    4.29  
    4.30          CpEpCOMServerAdapterModule * _self;
    4.31 -
    4.32 -        static std::mutex update_wait_mtx;
    4.33 -        static std::condition_variable update_wait_var;
    4.34 -        static bool update_wait_forced;
    4.35      };
    4.36  }
     5.1 --- a/pEpCOMServerAdapter.idl	Mon Oct 21 10:56:57 2019 +0200
     5.2 +++ b/pEpCOMServerAdapter.idl	Mon Oct 21 11:59:42 2019 +0200
     5.3 @@ -437,7 +437,7 @@
     5.4  		[in, defaultvalue(pEpEncPep)] pEpEncFormat encFormat);
     5.5  
     5.6  	// Trigger an immediate update check.
     5.7 -	[id(40)] HRESULT UpdateNow();
     5.8 +	[id(40)] HRESULT UpdateNow([in] BSTR productCode, [out] BOOL *didUpdate);
     5.9  
    5.10  	// Get a preview of the outgoint message rating
    5.11  	[id(41)] HRESULT OutgoingMessageRatingPreview([in] struct TextMessage *msg, [out, retval] pEpRating *rating);