crashtesting: assert on reimport of docx export of ooo102874-2.doc
[LibreOffice.git] / cli_ure / source / uno_bridge / cli_environment.cxx
blob3c054b5be870071ea6dc9d6bdb0b60b222c503dd
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 #include "Cli_environment.h"
22 #using <cli_ure.dll>
23 #using <system.dll>
24 #include "osl/diagnose.h"
25 #include "cli_proxy.h"
27 using namespace System::Runtime::Remoting;
28 using namespace System::Runtime::Remoting::Proxies;
29 using namespace System::Collections;
30 using namespace System::Text;
31 using namespace System::Diagnostics;
32 using namespace System::Threading;
34 namespace cli_uno
37 inline System::String^ Cli_environment::createKey(System::String^ oid, System::Type^ t)
39 return System::String::Concat(oid, t->FullName);
43 inline Cli_environment::Cli_environment()
45 #if OSL_DEBUG_LEVEL >= 2
46 _numRegisteredObjects = 0;
47 #endif
50 Cli_environment::~Cli_environment() ///< IDisposable Cli_environment::Dispose()
52 this->!Cli_environment(); // call finalizer
55 Cli_environment::!Cli_environment() ///< Cli_environment::Finalize()
57 #if OSL_DEBUG_LEVEL >= 2
58 OSL_ENSURE(_numRegisteredObjects == 0,
59 "cli uno bridge: CLI environment contains unrevoked objects");
60 #endif
63 System::Object^ Cli_environment::registerInterface(
64 System::Object^ obj, System::String^ oid)
66 #if OSL_DEBUG_LEVEL >= 1
67 //obj must be a transparent proxy
68 OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
69 _numRegisteredObjects ++;
70 #endif
71 OSL_ASSERT( ! m_objects->ContainsKey(oid));
72 m_objects->Add(oid, gcnew WeakReference(obj));
73 return obj;
75 System::Object^ Cli_environment::registerInterface (
76 System::Object^ obj, System::String^ oid, System::Type^ type)
78 #if OSL_DEBUG_LEVEL >= 1
79 //obj must be a real cli object
80 OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
81 _numRegisteredObjects ++;
82 #endif
83 System::String^ key = createKey(oid, type);
84 //see synchronization in map_uno2cli in cli_data.cxx
85 OSL_ASSERT( ! m_objects->ContainsKey(key));
86 m_objects->Add(key, gcnew WeakReference(obj));
87 return obj;
90 void Cli_environment::revokeInterface(System::String^ oid, System::Type^ type)
92 System::String^ key = type != nullptr ? createKey(oid, type) : oid;
93 #if OSL_DEBUG_LEVEL >= 1
94 _numRegisteredObjects --;
95 #endif
96 #if OSL_DEBUG_LEVEL >= 2
97 int i = 1;
98 if (m_objects->ContainsKey(key) == false)
100 Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
101 Trace::WriteLine(oid);
102 i = 0;
104 Trace::WriteLine(System::String::Format(
105 gcnew System::String("cli uno bridge: {0} remaining registered interfaces"),
106 m_objects->Count - 1));
107 #endif
108 m_objects->Remove(key);
111 inline void Cli_environment::revokeInterface(System::String^ oid)
113 return revokeInterface(oid, nullptr);
116 System::Object^ Cli_environment::getRegisteredInterface(System::String^ oid,
117 System::Type^ type)
119 //try if it is a UNO interface
120 System::Object^ ret = m_objects[oid];
121 if (! ret)
123 //try if it is a proxy for a cli object
124 oid = createKey(oid, type);
125 ret = m_objects[ oid ];
127 if (ret != nullptr)
129 System::WeakReference^ weakIface =
130 static_cast< System::WeakReference ^ >( ret );
131 ret = weakIface->Target;
133 if (ret == nullptr)
134 m_objects->Remove( oid );
135 return ret;
138 System::String^ Cli_environment::getObjectIdentifier(System::Object^ obj)
140 System::String^ oId= nullptr;
141 RealProxy^ aProxy= RemotingServices::GetRealProxy(obj);
142 if (aProxy)
144 UnoInterfaceProxy^ proxyImpl= dynamic_cast<UnoInterfaceProxy^>(aProxy);
145 if (proxyImpl)
146 oId= proxyImpl->getOid();
149 if (oId == nullptr)
151 StringBuilder ^ buf= gcnew StringBuilder(256);
152 bool bFirst = new bool(false);
153 System::Threading::Monitor::Enter(Cli_environment::typeid);
154 try {
155 buf->Append(m_IDGen->GetId(obj, bFirst));
156 } finally
158 System::Threading::Monitor::Exit(Cli_environment::typeid);
161 buf->Append(sOidPart);
162 oId= buf->ToString();
164 return oId;
166 } //namespace cli_uno
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */