irotp.c 11 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
    if (!InterlockedDecrement(&rot_entry->refs))
    {
64 65 66 67
        free(rot_entry->object);
        free(rot_entry->moniker);
        free(rot_entry->moniker_data);
        free(rot_entry);
68
    }
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
    HRESULT hr;

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

91
    if (!(rot_entry = calloc(1, sizeof(*rot_entry))))
92 93
        return E_OUTOFMEMORY;

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

    rot_entry->last_modified = *time;

106
    rot_entry->moniker = malloc(FIELD_OFFSET(InterfaceData, abData[mk->ulCntData]));
107 108
    if (!rot_entry->moniker)
    {
109
        rot_entry_release(rot_entry);
110 111 112 113 114
        return E_OUTOFMEMORY;
    }
    rot_entry->moniker->ulCntData = mk->ulCntData;
    memcpy(&rot_entry->moniker->abData, mk->abData, mk->ulCntData);

115
    if (!(rot_entry->moniker_data = malloc(FIELD_OFFSET(MonikerComparisonData, abData[data->ulCntData]))))
116
    {
117
        rot_entry_release(rot_entry);
118 119 120 121 122 123 124 125 126
        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;

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

138
    list_add_tail(&RunningObjectTable, &rot_entry->entry);
139 140 141

    LeaveCriticalSection(&csRunningObjectTable);

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

146 147 148
    return hr;
}

149
HRESULT __cdecl IrotRevoke(
150 151
    IrotHandle h,
    IrotCookie cookie,
152
    IrotContextHandle *ctxt_handle,
153 154 155 156 157
    PInterfaceData *obj,
    PInterfaceData *mk)
{
    struct rot_entry *rot_entry;

158
    WINE_TRACE("%ld\n", cookie);
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

    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;
            }

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

    return E_INVALIDARG;
}

196
HRESULT __cdecl IrotIsRunning(
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    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;
}

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

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

231 232
    *cookie = 0;

233 234 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
    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;
}

263
HRESULT __cdecl IrotNoteChangeTime(
264 265 266 267 268 269
    IrotHandle h,
    IrotCookie cookie,
    const FILETIME *last_modified_time)
{
    struct rot_entry *rot_entry;

270
    WINE_TRACE("%ld %p\n", cookie, last_modified_time);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

    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;
}

287
HRESULT __cdecl IrotGetTimeOfLastChange(
288 289 290 291 292 293 294 295 296
    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);

297 298
    memset(time, 0, sizeof(*time));

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    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;
}

315
HRESULT __cdecl IrotEnumRunning(
316 317 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
    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;
}

360 361 362 363 364 365 366 367 368
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);
}

369
void * __RPC_USER MIDL_user_allocate(SIZE_T size)
370
{
371
    return I_RpcAllocate(size);
372 373 374 375
}

void __RPC_USER MIDL_user_free(void * p)
{
376
    I_RpcFree(p);
377
}