1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_COMPHELPER_SOURCE_MISC_INSTANCELOCKER_HXX
21 #define INCLUDED_COMPHELPER_SOURCE_MISC_INSTANCELOCKER_HXX
22 
23 #include <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/util/XCloseListener.hpp>
25 #include <com/sun/star/frame/XTerminateListener.hpp>
26 #include <com/sun/star/lang/XInitialization.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <cppuhelper/weakref.hxx>
29 #include <osl/mutex.hxx>
30 #include <cppuhelper/implbase.hxx>
31 #include <rtl/ref.hxx>
32 #include <memory>
33 
34 namespace com::sun::star::embed { class XActionsApproval; }
35 namespace comphelper { class OInterfaceContainerHelper2; }
36 
37 
38 class OLockListener;
39 
40 // the service is implemented as a wrapper to be able to die by refcount
41 // the disposing mechanics is required for java related scenarios
42 class OInstanceLocker : public ::cppu::WeakImplHelper< css::lang::XComponent,
43                                                        css::lang::XInitialization,
44                                                        css::lang::XServiceInfo >
45 {
46     ::osl::Mutex m_aMutex;
47 
48     rtl::Reference< OLockListener > m_xLockListener;
49 
50     std::unique_ptr<::comphelper::OInterfaceContainerHelper2> m_pListenersContainer; // list of listeners
51 
52     bool m_bDisposed;
53     bool m_bInitialized;
54 
55 public:
56     explicit OInstanceLocker();
57     virtual ~OInstanceLocker() override;
58 
59 // XComponent
60     virtual void SAL_CALL dispose() override;
61     virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override;
62     virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
63 
64 // XInitialization
65     virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
66 
67 // XServiceInfo
68     virtual OUString SAL_CALL getImplementationName(  ) override;
69     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
70     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) override;
71 
72 };
73 
74 
75 class OLockListener : public ::cppu::WeakImplHelper< css::util::XCloseListener,
76                                                      css::frame::XTerminateListener >
77 {
78     ::osl::Mutex m_aMutex;
79     css::uno::Reference< css::uno::XInterface > m_xInstance;
80     css::uno::Reference< css::embed::XActionsApproval > m_xApproval;
81 
82     css::uno::WeakReference< css::lang::XComponent > m_xWrapper;
83 
84     bool m_bDisposed;
85     bool m_bInitialized;
86 
87     sal_Int32 m_nMode;
88 
89 public:
90     OLockListener(  const css::uno::WeakReference< css::lang::XComponent >& xWrapper,
91                     const css::uno::Reference< css::uno::XInterface >& xInstance,
92                     sal_Int32 nMode,
93                     const css::uno::Reference< css::embed::XActionsApproval >& rApproval );
94 
95     virtual ~OLockListener() override;
96 
97     void Init();
98     void Dispose();
99 
100 // XEventListener
101     virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
102 
103 // XCloseListener
104     virtual void SAL_CALL queryClosing( const css::lang::EventObject& Source, sal_Bool GetsOwnership ) override;
105     virtual void SAL_CALL notifyClosing( const css::lang::EventObject& Source ) override;
106 
107 // XTerminateListener
108     virtual void SAL_CALL queryTermination( const css::lang::EventObject& Event ) override;
109     virtual void SAL_CALL notifyTermination( const css::lang::EventObject& Event ) override;
110 
111 };
112 
113 #endif
114 
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
116