ucb: webdav-curl: Related: tdf#82677, implement a PROPFIND 'propname' request cache

PROPFIND 'propname' is the special usage to retrieve all the
properties available on the URI resource, their names only.

See <https://tools.ietf.org/html/rfc4918#section-9.1> for
PROPFIND 'propname' definition.

Add cache usage in Content::getProperties as well.
The caching model is simple: a simple lifetime limit of 10 seconds
to declare the property name list stale and request another list,
accessing the Net.

This should reduce the number of PROPFIND calls on the Net.

[ port of commit 98bd24f8b479132ca3f2d884749b738e9e6203e3 ]

Change-Id: I48ae38f706370557698dd80e31840b44e05bfef6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123481
Tested-by: Michael Stahl <michael.stahl@allotropia.de>
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
diff --git a/solenv/clang-format/excludelist b/solenv/clang-format/excludelist
index af38ed9..2608eb5 100644
--- a/solenv/clang-format/excludelist
+++ b/solenv/clang-format/excludelist
@@ -14006,6 +14006,8 @@ ucb/source/ucp/webdav-curl/DAVTypes.hxx
ucb/source/ucp/webdav-curl/DateTimeHelper.cxx
ucb/source/ucp/webdav-curl/DateTimeHelper.hxx
ucb/source/ucp/webdav-curl/PropertyMap.hxx
ucb/source/ucp/webdav-curl/PropfindCache.cxx
ucb/source/ucp/webdav-curl/PropfindCache.hxx
ucb/source/ucp/webdav-curl/SerfLockStore.cxx
ucb/source/ucp/webdav-curl/SerfLockStore.hxx
ucb/source/ucp/webdav-curl/UCBDeadPropertyValue.cxx
diff --git a/ucb/Library_ucpdav1.mk b/ucb/Library_ucpdav1.mk
index ee1c944..b8cbb2b 100644
--- a/ucb/Library_ucpdav1.mk
+++ b/ucb/Library_ucpdav1.mk
@@ -48,6 +48,7 @@ $(eval $(call gb_Library_add_exception_objects,ucpdav1,\
	ucb/source/ucp/webdav-curl/DAVSessionFactory \
	ucb/source/ucp/webdav-curl/DAVTypes \
	ucb/source/ucp/webdav-curl/DateTimeHelper \
	ucb/source/ucp/webdav-curl/PropfindCache \
	ucb/source/ucp/webdav-curl/SerfLockStore \
	ucb/source/ucp/webdav-curl/UCBDeadPropertyValue \
	ucb/source/ucp/webdav-curl/webdavcontent \
diff --git a/ucb/source/ucp/webdav-curl/DAVResource.hxx b/ucb/source/ucp/webdav-curl/DAVResource.hxx
index 8bbe895..4b031f0 100644
--- a/ucb/source/ucp/webdav-curl/DAVResource.hxx
+++ b/ucb/source/ucp/webdav-curl/DAVResource.hxx
@@ -49,6 +49,11 @@ struct DAVResource
struct DAVResourceInfo
{
    std::vector < OUString > properties;

    bool operator==( const struct DAVResourceInfo& a ) const
    {
        return (properties == a.properties );
    }
};

} // namespace http_dav_ucp
diff --git a/ucb/source/ucp/webdav-curl/PropfindCache.cxx b/ucb/source/ucp/webdav-curl/PropfindCache.cxx
new file mode 100644
index 0000000..4bf9bf9
--- /dev/null
+++ b/ucb/source/ucp/webdav-curl/PropfindCache.cxx
@@ -0,0 +1,91 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <osl/time.h>
#include "PropfindCache.hxx"

namespace http_dav_ucp
{

    // PropertyNames implementation

    PropertyNames::PropertyNames() :
        m_nStaleTime( 0 ),
        m_sURL(),
        m_aPropertiesNames()
    {
    }

    PropertyNames::PropertyNames( const OUString& rURL ) :
        m_nStaleTime( 0 ),
        m_sURL( rURL ),
        m_aPropertiesNames()
    {
    }

    //PropertyNamesCache implementation

    PropertyNamesCache::PropertyNamesCache()
    {
    }

    PropertyNamesCache::~PropertyNamesCache()
    {
    }

    bool PropertyNamesCache::getCachedPropertyNames( const OUString& rURL, PropertyNames& rCacheElement )
    {
        // search the URL in the static map
        osl::MutexGuard aGuard( m_aMutex );
        PropNameCache::const_iterator it;
        it = m_aTheCache.find( rURL );
        if ( it == m_aTheCache.end() )
            return false;
        else
        {
            // check if the element is stale, before restoring
            TimeValue t1;
            osl_getSystemTime( &t1 );
            if ( (*it).second.getStaleTime() < t1.Seconds )
            {
                // if stale, remove from cache, do not restore
                m_aTheCache.erase( it );
                return false;
                // return false instead
            }
            rCacheElement = (*it).second;
            return true;
        }
    }

    void PropertyNamesCache::removeCachedPropertyNames( const OUString& rURL )
    {
        osl::MutexGuard aGuard( m_aMutex );
        PropNameCache::const_iterator it;
        it = m_aTheCache.find( rURL );
        if ( it != m_aTheCache.end() )
        {
            m_aTheCache.erase( it );
        }
    }

    void PropertyNamesCache::addCachePropertyNames( PropertyNames& rCacheElement, const sal_uInt32 nLifeTime )
    {
        osl::MutexGuard aGuard( m_aMutex );
        OUString aURL( rCacheElement.getURL() );
        TimeValue t1;
        osl_getSystemTime( &t1 );
        rCacheElement.setStaleTime( t1.Seconds + nLifeTime );

        m_aTheCache[ aURL ] = rCacheElement;
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/ucb/source/ucp/webdav-curl/PropfindCache.hxx b/ucb/source/ucp/webdav-curl/PropfindCache.hxx
new file mode 100644
index 0000000..4f7b665
--- /dev/null
+++ b/ucb/source/ucp/webdav-curl/PropfindCache.hxx
@@ -0,0 +1,81 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_UCB_SOURCE_UCP_WEBDAV_NEON_PROPFINDCACHE_HXX
#define INCLUDED_UCB_SOURCE_UCP_WEBDAV_NEON_PROPFINDCACHE_HXX

#include <sal/types.h>
#include <rtl/ustring.hxx>
#include <osl/mutex.hxx>
#include <list>
#include <map>
#include <vector>

#include "DAVResource.hxx"

namespace http_dav_ucp
{
    // A property names cache mechanism, URL driven.
    // It is used to cache the property names received
    // from the WebDAV server, to minimize the need of
    // net transactions (e.g. PROPFIND).
    // The cache lifetime should be short
    // just to remove the annoying slowness when
    // typing text or moving cursor around when the
    // net link is slow.

    // Define the properties cache element
    class PropertyNames
    {
        /// target time when this element becomes stale
        sal_uInt32 m_nStaleTime;
        OUString    m_sURL;
        // the property name list received from WebDAV server
        std::vector< DAVResourceInfo > m_aPropertiesNames;

    public:
        PropertyNames();
        explicit PropertyNames( const OUString& rURL );

        sal_uInt32 getStaleTime() const { return m_nStaleTime; };
        void setStaleTime( const sal_uInt32 nStaleTime ) { m_nStaleTime = nStaleTime; };

        OUString& getURL() { return m_sURL; };

        const std::vector< DAVResourceInfo >& getPropertiesNames() { return m_aPropertiesNames; };
        void setPropertiesNames( const std::vector< DAVResourceInfo >& aPropertiesNames ) { m_aPropertiesNames = aPropertiesNames; };
    };

    // Define the PropertyNames cache
    // TODO: the OUString key element in std::map needs to be changed with a URI representation
    // with a specific compare (std::less) implementation, this last one implementing
    // as suggested in <https://tools.ietf.org/html/rfc3986#section-6>.
    // To find by URI and not by string equality.
    typedef std::map< OUString, PropertyNames,
                      std::less< OUString > >PropNameCache;

    class PropertyNamesCache
    {
        PropNameCache       m_aTheCache;
        osl::Mutex          m_aMutex;

    public:
        PropertyNamesCache();
        ~PropertyNamesCache();

        bool getCachedPropertyNames( const OUString& URL, PropertyNames& rCacheElement );
        void removeCachedPropertyNames( const OUString& URL );
        void addCachePropertyNames( PropertyNames& rCacheElement, const sal_uInt32 nLifeTime );
    };

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/ucb/source/ucp/webdav-curl/webdavcontentcaps.cxx b/ucb/source/ucp/webdav-curl/webdavcontentcaps.cxx
index e45156c..ca1fde4 100644
--- a/ucb/source/ucp/webdav-curl/webdavcontentcaps.cxx
+++ b/ucb/source/ucp/webdav-curl/webdavcontentcaps.cxx
@@ -37,6 +37,7 @@
#include "webdavprovider.hxx"
#include "DAVProperties.hxx"
#include "ContentProperties.hxx"
#include "PropfindCache.hxx"

using namespace com::sun::star;
using namespace http_dav_ucp;