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