cid#1607171 Data race condition
[LibreOffice.git] / framework / inc / classes / protocolhandlercache.hxx
blob9ecb06abda13697f5a195ba32051d4a5ec4317c9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <unordered_map>
23 #include <optional>
25 #include <com/sun/star/util/URL.hpp>
27 #include <unotools/configitem.hxx>
28 #include <rtl/ustring.hxx>
30 namespace framework{
32 inline constexpr OUString PACKAGENAME_PROTOCOLHANDLER = u"Office.ProtocolHandler"_ustr; /// name of our configuration package
34 #define CFG_PATH_SEPARATOR "/" /// separator for configuration paths
36 #define PROPERTY_PROTOCOLS "Protocols" /// properties of a protocol handler
38 /**
39 Programmer can register his own services to handle different protocols.
40 Don't forget: It doesn't mean "handling of documents" ... these services could handle protocols ...
41 e.g. "mailto:", "file://", ".java:"
42 This struct holds the information about one such registered protocol handler.
43 A list of handler objects is defined as ProtocolHandlerHash. see below
45 struct ProtocolHandler
47 /* member */
48 public:
50 /// the uno implementation name of this handler
51 OUString m_sUNOName;
52 /// list of URL pattern which defines the protocols which this handler is registered for
53 std::vector<OUString> m_lProtocols;
56 /**
57 This hash use registered pattern of all protocol handlers as keys and provide her
58 uno implementation names as value. Overloading of the index operator makes it possible
59 to search for a key by using a full qualified URL on list of all possible pattern keys.
61 typedef std::unordered_map<OUString, OUString> PatternHash;
63 /**
64 This hash holds protocol handler structs by her names.
66 typedef std::unordered_map<OUString, ProtocolHandler> HandlerHash;
68 /**
69 @short this hash makes it easy to find a protocol handler by using his uno implementation name.
70 @descr It holds two lists of information:
71 - first holds all handler by her uno implementation names and
72 can be used to get her other properties
73 - another one maps her registered pattern to her uno names to
74 perform search on such data
75 But this lists a static for all instances of this class. So it's possible to
76 create new objects without opening configuration twice and free memory automatically
77 if last object will gone.
79 @attention We implement a singleton concept - so we don't need any mutex member here.
80 Because to safe access on static member we must use a static global lock
81 here too.
83 @devstatus ready to use
84 @threadsafe yes
87 class HandlerCFGAccess;
88 class HandlerCache final
90 /* member */
91 private:
93 /// list of all registered handler registered by her uno implementation names
94 static std::optional<HandlerHash> s_pHandler;
95 /// maps URL pattern to handler names
96 static std::optional<PatternHash> s_pPattern;
97 /// informs about config updates
98 static HandlerCFGAccess* s_pConfig;
99 /// ref count to construct/destruct internal member lists on demand by using singleton mechanism
100 static sal_Int32 m_nRefCount;
102 /* interface */
103 public:
105 HandlerCache();
106 ~HandlerCache();
108 static bool search( const OUString& sURL, ProtocolHandler* pReturn );
109 static bool search( const css::util::URL& aURL, ProtocolHandler* pReturn );
111 static void takeOver(HandlerHash aHandler, PatternHash aPattern);
115 @short implements configuration access for handler configuration
116 @descr We use the ConfigItem mechanism to read/write values from/to configuration.
117 We set a data container pointer for filling or reading ... this class use it temp.
118 After successfully calling of read(), we can use filled container directly or merge it with an existing one.
119 After successfully calling of write() all values of given data container are flushed to our configuration -
120 but current implementation doesn't support writing really.
122 @base ::utl::ConfigItem
123 base mechanism for configuration access
125 @devstatus ready to use
126 @threadsafe no
128 class HandlerCFGAccess final : public ::utl::ConfigItem
130 private:
131 HandlerCache* m_pCache;
133 virtual void ImplCommit() override;
135 /* interface */
136 public:
137 HandlerCFGAccess( const OUString& sPackage );
138 void read ( HandlerHash& rHandlerHash, PatternHash& rPatternHash );
140 void setCache(HandlerCache* pCache) {m_pCache = pCache;};
141 virtual void Notify(const css::uno::Sequence< OUString >& lPropertyNames) override;
144 } // namespace framework
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */