summaryrefslogtreecommitdiffstats
path: root/cli_ure/source/uno_bridge/cli_environment.cxx
blob: 3c054b5be870071ea6dc9d6bdb0b60b222c503dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "Cli_environment.h"

#using <cli_ure.dll>
#using <system.dll>
#include "osl/diagnose.h"
#include "cli_proxy.h"

using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Proxies;
using namespace System::Collections;
using namespace System::Text;
using namespace System::Diagnostics;
using namespace System::Threading;

namespace cli_uno
{

inline System::String^ Cli_environment::createKey(System::String^ oid, System::Type^ t)
{
    return System::String::Concat(oid, t->FullName);
}


inline Cli_environment::Cli_environment()
{
#if OSL_DEBUG_LEVEL >= 2
    _numRegisteredObjects = 0;
#endif
}

Cli_environment::~Cli_environment() ///< IDisposable Cli_environment::Dispose()
{
    this->!Cli_environment(); // call finalizer
}

Cli_environment::!Cli_environment() ///< Cli_environment::Finalize()
{
#if OSL_DEBUG_LEVEL >= 2
    OSL_ENSURE(_numRegisteredObjects == 0,
               "cli uno bridge: CLI environment contains unrevoked objects");
#endif
}

System::Object^ Cli_environment::registerInterface(
    System::Object^ obj, System::String^ oid)
{
#if OSL_DEBUG_LEVEL >= 1
    //obj must be a transparent proxy
    OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
    _numRegisteredObjects ++;
#endif
    OSL_ASSERT( ! m_objects->ContainsKey(oid));
    m_objects->Add(oid, gcnew WeakReference(obj));
    return obj;
}
System::Object^ Cli_environment::registerInterface      (
    System::Object^ obj, System::String^ oid, System::Type^ type)
{
#if OSL_DEBUG_LEVEL >= 1
    //obj must be a real cli object
    OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
    _numRegisteredObjects ++;
#endif
    System::String^ key = createKey(oid, type);
    //see synchronization in map_uno2cli in cli_data.cxx
    OSL_ASSERT( ! m_objects->ContainsKey(key));
    m_objects->Add(key, gcnew WeakReference(obj));
    return obj;
}

void Cli_environment::revokeInterface(System::String^ oid, System::Type^ type)
{
    System::String^ key = type != nullptr ? createKey(oid, type) : oid;
#if OSL_DEBUG_LEVEL >= 1
    _numRegisteredObjects --;
#endif
#if OSL_DEBUG_LEVEL >= 2
    int i = 1;
    if (m_objects->ContainsKey(key) == false)
    {
        Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
        Trace::WriteLine(oid);
        i = 0;
    }
    Trace::WriteLine(System::String::Format(
                         gcnew System::String("cli uno bridge: {0} remaining registered interfaces"),
                         m_objects->Count - 1));
#endif
    m_objects->Remove(key);
}

inline void Cli_environment::revokeInterface(System::String^ oid)
{
    return revokeInterface(oid, nullptr);
}

System::Object^ Cli_environment::getRegisteredInterface(System::String^ oid,
                                                        System::Type^ type)
{
    //try if it is a UNO interface
    System::Object^ ret = m_objects[oid];
    if (! ret)
    {
        //try if it is a proxy for a cli object
        oid = createKey(oid, type);
        ret = m_objects[ oid ];
    }
    if (ret != nullptr)
    {
        System::WeakReference^ weakIface =
            static_cast< System::WeakReference ^ >( ret );
        ret = weakIface->Target;
    }
    if (ret == nullptr)
        m_objects->Remove( oid );
    return ret;
}

System::String^ Cli_environment::getObjectIdentifier(System::Object^ obj)
{
    System::String^ oId= nullptr;
    RealProxy^ aProxy= RemotingServices::GetRealProxy(obj);
    if (aProxy)
    {
        UnoInterfaceProxy^ proxyImpl= dynamic_cast<UnoInterfaceProxy^>(aProxy);
        if (proxyImpl)
            oId= proxyImpl->getOid();
    }

    if (oId == nullptr)
    {
        StringBuilder ^ buf= gcnew StringBuilder(256);
        bool bFirst = new bool(false);
        System::Threading::Monitor::Enter(Cli_environment::typeid);
        try {
            buf->Append(m_IDGen->GetId(obj, bFirst));
        } finally
        {
            System::Threading::Monitor::Exit(Cli_environment::typeid);
        }

        buf->Append(sOidPart);
        oId= buf->ToString();
    }
    return oId;
}
} //namespace cli_uno

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */