irotp.c 11.3 KB
Newer Older
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
/*
 *	Running Object Table
 *
 *      Copyright 2007  Robert Shearman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
#include <string.h>

#include "winerror.h"
#include "windef.h"
#include "winbase.h"

#include "irot.h"

#include "wine/list.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(rpcss);

/* define the structure of the running object table elements */
struct rot_entry
{
    struct list        entry;
    InterfaceData *object; /* marshaled running object*/
    InterfaceData *moniker; /* marshaled moniker that identifies this object */
    MonikerComparisonData *moniker_data; /* moniker comparison data that identifies this object */
    DWORD              cookie; /* cookie identifying this object */
    FILETIME           last_modified;
44
    LONG               refs;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

static struct list RunningObjectTable = LIST_INIT(RunningObjectTable);

static CRITICAL_SECTION csRunningObjectTable;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &csRunningObjectTable,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": csRunningObjectTable") }
};
static CRITICAL_SECTION csRunningObjectTable = { &critsect_debug, -1, 0, 0, 0, 0 };

static LONG last_cookie = 1;

60
static inline void rot_entry_release(struct rot_entry *rot_entry)
61
{
62 63 64 65 66 67 68
    if (!InterlockedDecrement(&rot_entry->refs))
    {
        HeapFree(GetProcessHeap(), 0, rot_entry->object);
        HeapFree(GetProcessHeap(), 0, rot_entry->moniker);
        HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
        HeapFree(GetProcessHeap(), 0, rot_entry);
    }
69 70
}

71
HRESULT __cdecl IrotRegister(
72 73 74 75 76 77
    IrotHandle h,
    const MonikerComparisonData *data,
    const InterfaceData *obj,
    const InterfaceData *mk,
    const FILETIME *time,
    DWORD grfFlags,
78 79
    IrotCookie *cookie,
    IrotContextHandle *ctxt_handle)
80 81
{
    struct rot_entry *rot_entry;
82
    struct rot_entry *existing_rot_entry;
83 84 85 86 87 88 89 90 91 92 93 94
    HRESULT hr;

    if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
    {
        WINE_ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
        return E_INVALIDARG;
    }

    rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
    if (!rot_entry)
        return E_OUTOFMEMORY;

95
    rot_entry->refs = 1;
96 97 98
    rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[obj->ulCntData]));
    if (!rot_entry->object)
    {
99
        rot_entry_release(rot_entry);
100 101 102 103 104 105 106 107 108 109
        return E_OUTOFMEMORY;
    }
    rot_entry->object->ulCntData = obj->ulCntData;
    memcpy(&rot_entry->object->abData, obj->abData, obj->ulCntData);

    rot_entry->last_modified = *time;

    rot_entry->moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[mk->ulCntData]));
    if (!rot_entry->moniker)
    {
110
        rot_entry_release(rot_entry);
111 112 113 114 115 116 117 118
        return E_OUTOFMEMORY;
    }
    rot_entry->moniker->ulCntData = mk->ulCntData;
    memcpy(&rot_entry->moniker->abData, mk->abData, mk->ulCntData);

    rot_entry->moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[data->ulCntData]));
    if (!rot_entry->moniker_data)
    {
119
        rot_entry_release(rot_entry);
120 121 122 123 124 125 126 127 128
        return E_OUTOFMEMORY;
    }
    rot_entry->moniker_data->ulCntData = data->ulCntData;
    memcpy(&rot_entry->moniker_data->abData, data->abData, data->ulCntData);

    EnterCriticalSection(&csRunningObjectTable);

    hr = S_OK;

129
    LIST_FOR_EACH_ENTRY(existing_rot_entry, &RunningObjectTable, struct rot_entry, entry)
130 131 132 133 134 135 136 137 138 139
    {
        if ((existing_rot_entry->moniker_data->ulCntData == data->ulCntData) &&
            !memcmp(&data->abData, &existing_rot_entry->moniker_data->abData, data->ulCntData))
        {
            hr = MK_S_MONIKERALREADYREGISTERED;
            WINE_TRACE("moniker already registered with cookie %d\n", existing_rot_entry->cookie);
            break;
        }
    }

140
    list_add_tail(&RunningObjectTable, &rot_entry->entry);
141 142 143

    LeaveCriticalSection(&csRunningObjectTable);

144 145 146 147
    /* gives a registration identifier to the registered object*/
    *cookie = rot_entry->cookie = InterlockedIncrement(&last_cookie);
    *ctxt_handle = rot_entry;

148 149 150
    return hr;
}

151
HRESULT __cdecl IrotRevoke(
152 153
    IrotHandle h,
    IrotCookie cookie,
154
    IrotContextHandle *ctxt_handle,
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    PInterfaceData *obj,
    PInterfaceData *mk)
{
    struct rot_entry *rot_entry;

    WINE_TRACE("%d\n", cookie);

    EnterCriticalSection(&csRunningObjectTable);
    LIST_FOR_EACH_ENTRY(rot_entry, &RunningObjectTable, struct rot_entry, entry)
    {
        if (rot_entry->cookie == cookie)
        {
            HRESULT hr = S_OK;

            list_remove(&rot_entry->entry);
            LeaveCriticalSection(&csRunningObjectTable);

            *obj = MIDL_user_allocate(FIELD_OFFSET(InterfaceData, abData[rot_entry->object->ulCntData]));
            *mk = MIDL_user_allocate(FIELD_OFFSET(InterfaceData, abData[rot_entry->moniker->ulCntData]));
            if (*obj && *mk)
            {
                (*obj)->ulCntData = rot_entry->object->ulCntData;
                memcpy((*obj)->abData, rot_entry->object->abData, (*obj)->ulCntData);
                (*mk)->ulCntData = rot_entry->moniker->ulCntData;
                memcpy((*mk)->abData, rot_entry->moniker->abData, (*mk)->ulCntData);
            }
            else
            {
                MIDL_user_free(*obj);
                MIDL_user_free(*mk);
                hr = E_OUTOFMEMORY;
            }

188 189
            rot_entry_release(rot_entry);
            *ctxt_handle = NULL;
190 191 192 193 194 195 196 197
            return hr;
        }
    }
    LeaveCriticalSection(&csRunningObjectTable);

    return E_INVALIDARG;
}

198
HRESULT __cdecl IrotIsRunning(
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    IrotHandle h,
    const MonikerComparisonData *data)
{
    const struct rot_entry *rot_entry;
    HRESULT hr = S_FALSE;

    WINE_TRACE("\n");

    EnterCriticalSection(&csRunningObjectTable);

    LIST_FOR_EACH_ENTRY(rot_entry, &RunningObjectTable, const struct rot_entry, entry)
    {
        if ((rot_entry->moniker_data->ulCntData == data->ulCntData) &&
            !memcmp(&data->abData, &rot_entry->moniker_data->abData, data->ulCntData))
        {
            hr = S_OK;
            break;
        }
    }
    LeaveCriticalSection(&csRunningObjectTable);

    return hr;
}

223
HRESULT __cdecl IrotGetObject(
224 225 226 227 228 229 230 231 232
    IrotHandle h,
    const MonikerComparisonData *moniker_data,
    PInterfaceData *obj,
    IrotCookie *cookie)
{
    const struct rot_entry *rot_entry;

    WINE_TRACE("%p\n", moniker_data);

233 234
    *cookie = 0;

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    EnterCriticalSection(&csRunningObjectTable);

    LIST_FOR_EACH_ENTRY(rot_entry, &RunningObjectTable, const struct rot_entry, entry)
    {
        HRESULT hr = S_OK;
        if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
            !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
        {
            *obj = MIDL_user_allocate(FIELD_OFFSET(InterfaceData, abData[rot_entry->object->ulCntData]));
            if (*obj)
            {
                (*obj)->ulCntData = rot_entry->object->ulCntData;
                memcpy((*obj)->abData, rot_entry->object->abData, (*obj)->ulCntData);

                *cookie = rot_entry->cookie;
            }
            else
                hr = E_OUTOFMEMORY;

            LeaveCriticalSection(&csRunningObjectTable);

            return hr;
        }
    }

    LeaveCriticalSection(&csRunningObjectTable);

    return MK_E_UNAVAILABLE;
}

265
HRESULT __cdecl IrotNoteChangeTime(
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    IrotHandle h,
    IrotCookie cookie,
    const FILETIME *last_modified_time)
{
    struct rot_entry *rot_entry;

    WINE_TRACE("%d %p\n", cookie, last_modified_time);

    EnterCriticalSection(&csRunningObjectTable);
    LIST_FOR_EACH_ENTRY(rot_entry, &RunningObjectTable, struct rot_entry, entry)
    {
        if (rot_entry->cookie == cookie)
        {
            rot_entry->last_modified = *last_modified_time;
            LeaveCriticalSection(&csRunningObjectTable);
            return S_OK;
        }
    }
    LeaveCriticalSection(&csRunningObjectTable);

    return E_INVALIDARG;
}

289
HRESULT __cdecl IrotGetTimeOfLastChange(
290 291 292 293 294 295 296 297 298
    IrotHandle h,
    const MonikerComparisonData *moniker_data,
    FILETIME *time)
{
    const struct rot_entry *rot_entry;
    HRESULT hr = MK_E_UNAVAILABLE;

    WINE_TRACE("%p\n", moniker_data);

299 300
    memset(time, 0, sizeof(*time));

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    EnterCriticalSection(&csRunningObjectTable);
    LIST_FOR_EACH_ENTRY(rot_entry, &RunningObjectTable, const struct rot_entry, entry)
    {
        if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
            !memcmp(&moniker_data->abData, &rot_entry->moniker_data->abData, moniker_data->ulCntData))
        {
            *time = rot_entry->last_modified;
            hr = S_OK;
            break;
        }
    }
    LeaveCriticalSection(&csRunningObjectTable);

    return hr;
}

317
HRESULT __cdecl IrotEnumRunning(
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    IrotHandle h,
    PInterfaceList *list)
{
    const struct rot_entry *rot_entry;
    HRESULT hr = S_OK;
    ULONG moniker_count = 0;
    ULONG i = 0;

    WINE_TRACE("\n");

    EnterCriticalSection(&csRunningObjectTable);

    LIST_FOR_EACH_ENTRY( rot_entry, &RunningObjectTable, const struct rot_entry, entry )
        moniker_count++;

    *list = MIDL_user_allocate(FIELD_OFFSET(InterfaceList, interfaces[moniker_count]));
    if (*list)
    {
        (*list)->size = moniker_count;
        LIST_FOR_EACH_ENTRY( rot_entry, &RunningObjectTable, const struct rot_entry, entry )
        {
            (*list)->interfaces[i] = MIDL_user_allocate(FIELD_OFFSET(InterfaceData, abData[rot_entry->moniker->ulCntData]));
            if (!(*list)->interfaces[i])
            {
                ULONG end = i - 1;
                for (i = 0; i < end; i++)
                    MIDL_user_free((*list)->interfaces[i]);
                MIDL_user_free(*list);
                hr = E_OUTOFMEMORY;
                break;
            }
            (*list)->interfaces[i]->ulCntData = rot_entry->moniker->ulCntData;
            memcpy((*list)->interfaces[i]->abData, rot_entry->moniker->abData, rot_entry->moniker->ulCntData);
            i++;
        }
    }
    else
        hr = E_OUTOFMEMORY;

    LeaveCriticalSection(&csRunningObjectTable);

    return hr;
}

362 363 364 365 366 367 368 369 370
void __RPC_USER IrotContextHandle_rundown(IrotContextHandle ctxt_handle)
{
    struct rot_entry *rot_entry = ctxt_handle;
    EnterCriticalSection(&csRunningObjectTable);
    list_remove(&rot_entry->entry);
    LeaveCriticalSection(&csRunningObjectTable);
    rot_entry_release(rot_entry);
}

371
void * __RPC_USER MIDL_user_allocate(SIZE_T size)
372 373 374 375 376 377 378 379
{
    return HeapAlloc(GetProcessHeap(), 0, size);
}

void __RPC_USER MIDL_user_free(void * p)
{
    HeapFree(GetProcessHeap(), 0, p);
}