compartmentmgr.c 17 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
/*
 *  ITfCompartmentMgr implementation
 *
 *  Copyright 2009 Aric Stewart, CodeWeavers
 *
 * 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 "config.h"

#include <stdarg.h>

#define COBJMACROS

#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winuser.h"
#include "shlwapi.h"
#include "winerror.h"
#include "objbase.h"
#include "oleauto.h"
36
#include "olectl.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#include "wine/unicode.h"

#include "msctf.h"
#include "msctf_internal.h"

WINE_DEFAULT_DEBUG_CHANNEL(msctf);

typedef struct tagCompartmentValue {
    struct list entry;
    GUID guid;
    TfClientId owner;
    ITfCompartment *compartment;
} CompartmentValue;

typedef struct tagCompartmentMgr {
53
    ITfCompartmentMgr ITfCompartmentMgr_iface;
54 55 56 57 58 59 60
    LONG refCount;

    IUnknown *pUnkOuter;

    struct list values;
} CompartmentMgr;

61
typedef struct tagCompartmentEnumGuid {
62
    IEnumGUID IEnumGUID_iface;
63 64 65 66 67 68
    LONG refCount;

    struct list *values;
    struct list *cursor;
} CompartmentEnumGuid;

69
typedef struct tagCompartment {
70 71
    ITfCompartment ITfCompartment_iface;
    ITfSource ITfSource_iface;
72 73 74 75 76
    LONG refCount;

    /* Only VT_I4, VT_UNKNOWN and VT_BSTR data types are allowed */
    VARIANT variant;
    CompartmentValue *valueData;
77
    struct list CompartmentEventSink;
78 79
} Compartment;

80
static HRESULT CompartmentEnumGuid_Constructor(struct list* values, IEnumGUID **ppOut);
81
static HRESULT Compartment_Constructor(CompartmentValue *value, ITfCompartment **ppOut);
82

83
static inline CompartmentMgr *impl_from_ITfCompartmentMgr(ITfCompartmentMgr *iface)
84
{
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    return CONTAINING_RECORD(iface, CompartmentMgr, ITfCompartmentMgr_iface);
}

static inline Compartment *impl_from_ITfCompartment(ITfCompartment *iface)
{
    return CONTAINING_RECORD(iface, Compartment, ITfCompartment_iface);
}

static inline Compartment *impl_from_ITfSource(ITfSource *iface)
{
    return CONTAINING_RECORD(iface, Compartment, ITfSource_iface);
}

static inline CompartmentEnumGuid *impl_from_IEnumGUID(IEnumGUID *iface)
{
    return CONTAINING_RECORD(iface, CompartmentEnumGuid, IEnumGUID_iface);
101 102
}

103 104
HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
{
105
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    struct list *cursor, *cursor2;

    LIST_FOR_EACH_SAFE(cursor, cursor2, &This->values)
    {
        CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
        list_remove(cursor);
        ITfCompartment_Release(value->compartment);
        HeapFree(GetProcessHeap(),0,value);
    }

    HeapFree(GetProcessHeap(),0,This);
    return S_OK;
}

/*****************************************************
 * ITfCompartmentMgr functions
 *****************************************************/
static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, REFIID iid, LPVOID *ppvOut)
{
125
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
126
    if (This->pUnkOuter)
127
        return IUnknown_QueryInterface(This->pUnkOuter, iid, ppvOut);
128 129 130 131 132 133
    else
    {
        *ppvOut = NULL;

        if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartmentMgr))
        {
134
            *ppvOut = &This->ITfCompartmentMgr_iface;
135 136 137 138
        }

        if (*ppvOut)
        {
139
            ITfCompartmentMgr_AddRef(iface);
140 141 142 143 144 145 146 147 148 149
            return S_OK;
        }

        WARN("unsupported interface: %s\n", debugstr_guid(iid));
        return E_NOINTERFACE;
    }
}

static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
{
150
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
151 152 153 154 155 156 157 158
    if (This->pUnkOuter)
        return IUnknown_AddRef(This->pUnkOuter);
    else
        return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
{
159
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
160 161 162 163 164 165 166 167 168 169 170 171 172
    if (This->pUnkOuter)
        return IUnknown_Release(This->pUnkOuter);
    else
    {
        ULONG ret;

        ret = InterlockedDecrement(&This->refCount);
        if (ret == 0)
            CompartmentMgr_Destructor(iface);
        return ret;
    }
}

173
static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
174 175
        REFGUID rguid, ITfCompartment **ppcomp)
{
176
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    CompartmentValue* value;
    struct list *cursor;
    HRESULT hr;

    TRACE("(%p) %s  %p\n",This,debugstr_guid(rguid),ppcomp);

    LIST_FOR_EACH(cursor, &This->values)
    {
        value = LIST_ENTRY(cursor,CompartmentValue,entry);
        if (IsEqualGUID(rguid,&value->guid))
        {
            ITfCompartment_AddRef(value->compartment);
            *ppcomp = value->compartment;
            return S_OK;
        }
    }

    value = HeapAlloc(GetProcessHeap(),0,sizeof(CompartmentValue));
    value->guid = *rguid;
    value->owner = 0;
    hr = Compartment_Constructor(value,&value->compartment);
    if (SUCCEEDED(hr))
    {
        list_add_head(&This->values,&value->entry);
        ITfCompartment_AddRef(value->compartment);
        *ppcomp = value->compartment;
    }
    else
    {
        HeapFree(GetProcessHeap(),0,value);
        *ppcomp = NULL;
    }
    return hr;
210 211
}

212
static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
213 214
    TfClientId tid, REFGUID rguid)
{
215
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
216
    struct list *cursor;
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    TRACE("(%p) %i %s\n",This,tid,debugstr_guid(rguid));

    LIST_FOR_EACH(cursor, &This->values)
    {
        CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
        if (IsEqualGUID(rguid,&value->guid))
        {
            if (value->owner && tid != value->owner)
                return E_UNEXPECTED;
            list_remove(cursor);
            ITfCompartment_Release(value->compartment);
            HeapFree(GetProcessHeap(),0,value);
            return S_OK;
        }
    }

    return CONNECT_E_NOCONNECTION;
235 236
}

237
static HRESULT WINAPI CompartmentMgr_EnumCompartments(ITfCompartmentMgr *iface,
238 239
 IEnumGUID **ppEnum)
{
240 241
    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);

242 243 244 245
    TRACE("(%p) %p\n",This,ppEnum);
    if (!ppEnum)
        return E_INVALIDARG;
    return CompartmentEnumGuid_Constructor(&This->values, ppEnum);
246 247
}

248
static const ITfCompartmentMgrVtbl CompartmentMgrVtbl =
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
    CompartmentMgr_QueryInterface,
    CompartmentMgr_AddRef,
    CompartmentMgr_Release,
    CompartmentMgr_GetCompartment,
    CompartmentMgr_ClearCompartment,
    CompartmentMgr_EnumCompartments
};

HRESULT CompartmentMgr_Constructor(IUnknown *pUnkOuter, REFIID riid, IUnknown **ppOut)
{
    CompartmentMgr *This;

    if (!ppOut)
        return E_POINTER;

    if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
        return CLASS_E_NOAGGREGATION;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentMgr));
    if (This == NULL)
        return E_OUTOFMEMORY;

272
    This->ITfCompartmentMgr_iface.lpVtbl = &CompartmentMgrVtbl;
273 274 275 276 277
    This->pUnkOuter = pUnkOuter;
    list_init(&This->values);

    if (pUnkOuter)
    {
278 279
        *ppOut = (IUnknown*)&This->ITfCompartmentMgr_iface;
        TRACE("returning %p\n", *ppOut);
280 281 282 283 284
        return S_OK;
    }
    else
    {
        HRESULT hr;
285
        hr = ITfCompartmentMgr_QueryInterface(&This->ITfCompartmentMgr_iface, riid, (void**)ppOut);
286 287 288 289 290
        if (FAILED(hr))
            HeapFree(GetProcessHeap(),0,This);
        return hr;
    }
}
291 292

/**************************************************
293
 * IEnumGUID implementation for ITfCompartmentMgr::EnumCompartments
294 295 296 297 298 299 300 301 302
 **************************************************/
static void CompartmentEnumGuid_Destructor(CompartmentEnumGuid *This)
{
    TRACE("destroying %p\n", This);
    HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
{
303
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
304 305 306 307
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
    {
308
        *ppvOut = &This->IEnumGUID_iface;
309 310 311 312
    }

    if (*ppvOut)
    {
313
        IEnumGUID_AddRef(iface);
314 315 316 317 318 319 320 321 322
        return S_OK;
    }

    WARN("unsupported interface: %s\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI CompartmentEnumGuid_AddRef(IEnumGUID *iface)
{
323
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
324 325 326 327 328
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
{
329
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
330 331 332 333 334 335 336 337 338 339 340
    ULONG ret;

    ret = InterlockedDecrement(&This->refCount);
    if (ret == 0)
        CompartmentEnumGuid_Destructor(This);
    return ret;
}

/*****************************************************
 * IEnumGuid functions
 *****************************************************/
341
static HRESULT WINAPI CompartmentEnumGuid_Next(IEnumGUID *iface,
342 343
    ULONG celt, GUID *rgelt, ULONG *pceltFetched)
{
344
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    ULONG fetched = 0;

    TRACE("(%p)\n",This);

    if (rgelt == NULL) return E_POINTER;

    while (fetched < celt && This->cursor)
    {
        CompartmentValue* value = LIST_ENTRY(This->cursor,CompartmentValue,entry);
        if (!value)
            break;

        This->cursor = list_next(This->values,This->cursor);
        *rgelt = value->guid;

        ++fetched;
        ++rgelt;
    }

    if (pceltFetched) *pceltFetched = fetched;
    return fetched == celt ? S_OK : S_FALSE;
}

368
static HRESULT WINAPI CompartmentEnumGuid_Skip(IEnumGUID *iface, ULONG celt)
369
{
370
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
371 372 373 374 375 376
    TRACE("(%p)\n",This);

    This->cursor = list_next(This->values,This->cursor);
    return S_OK;
}

377
static HRESULT WINAPI CompartmentEnumGuid_Reset(IEnumGUID *iface)
378
{
379
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
380 381 382 383 384
    TRACE("(%p)\n",This);
    This->cursor = list_head(This->values);
    return S_OK;
}

385
static HRESULT WINAPI CompartmentEnumGuid_Clone(IEnumGUID *iface,
386 387
    IEnumGUID **ppenum)
{
388
    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
389 390 391 392 393 394 395 396 397
    HRESULT res;

    TRACE("(%p)\n",This);

    if (ppenum == NULL) return E_POINTER;

    res = CompartmentEnumGuid_Constructor(This->values, ppenum);
    if (SUCCEEDED(res))
    {
398
        CompartmentEnumGuid *new_This = impl_from_IEnumGUID(*ppenum);
399 400 401 402 403
        new_This->cursor = This->cursor;
    }
    return res;
}

404 405
static const IEnumGUIDVtbl EnumGUIDVtbl =
{
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    CompartmentEnumGuid_QueryInterface,
    CompartmentEnumGuid_AddRef,
    CompartmentEnumGuid_Release,
    CompartmentEnumGuid_Next,
    CompartmentEnumGuid_Skip,
    CompartmentEnumGuid_Reset,
    CompartmentEnumGuid_Clone
};

static HRESULT CompartmentEnumGuid_Constructor(struct list *values, IEnumGUID **ppOut)
{
    CompartmentEnumGuid *This;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentEnumGuid));
    if (This == NULL)
        return E_OUTOFMEMORY;

423
    This->IEnumGUID_iface.lpVtbl= &EnumGUIDVtbl;
424 425 426 427 428
    This->refCount = 1;

    This->values = values;
    This->cursor = list_head(values);

429 430
    *ppOut = &This->IEnumGUID_iface;
    TRACE("returning %p\n", *ppOut);
431 432
    return S_OK;
}
433 434 435 436 437 438 439 440

/**************************************************
 * ITfCompartment
 **************************************************/
static void Compartment_Destructor(Compartment *This)
{
    TRACE("destroying %p\n", This);
    VariantClear(&This->variant);
441
    free_sinks(&This->CompartmentEventSink);
442 443 444 445 446
    HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID iid, LPVOID *ppvOut)
{
447 448
    Compartment *This = impl_from_ITfCompartment(iface);

449 450 451 452
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartment))
    {
453
        *ppvOut = &This->ITfCompartment_iface;
454
    }
455 456
    else if (IsEqualIID(iid, &IID_ITfSource))
    {
457
        *ppvOut = &This->ITfSource_iface;
458
    }
459 460 461

    if (*ppvOut)
    {
462
        ITfCompartment_AddRef(iface);
463 464 465 466 467 468 469 470 471
        return S_OK;
    }

    WARN("unsupported interface: %s\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI Compartment_AddRef(ITfCompartment *iface)
{
472
    Compartment *This = impl_from_ITfCompartment(iface);
473 474 475 476 477
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
{
478
    Compartment *This = impl_from_ITfCompartment(iface);
479 480 481 482 483 484 485 486 487 488 489
    ULONG ret;

    ret = InterlockedDecrement(&This->refCount);
    if (ret == 0)
        Compartment_Destructor(This);
    return ret;
}

static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
    TfClientId tid, const VARIANT *pvarValue)
{
490
    Compartment *This = impl_from_ITfCompartment(iface);
491
    ITfCompartmentEventSink *sink;
492
    struct list *cursor;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    TRACE("(%p) %i %p\n",This,tid,pvarValue);

    if (!pvarValue)
        return E_INVALIDARG;

    if (!(V_VT(pvarValue) == VT_BSTR || V_VT(pvarValue) == VT_I4 ||
          V_VT(pvarValue) == VT_UNKNOWN))
        return E_INVALIDARG;

    if (!This->valueData->owner)
        This->valueData->owner = tid;

    VariantClear(&This->variant);

    /* Shallow copy of value and type */
    This->variant = *pvarValue;

    if (V_VT(pvarValue) == VT_BSTR)
        V_BSTR(&This->variant) = SysAllocStringByteLen((char*)V_BSTR(pvarValue),
                SysStringByteLen(V_BSTR(pvarValue)));
    else if (V_VT(pvarValue) == VT_UNKNOWN)
        IUnknown_AddRef(V_UNKNOWN(&This->variant));

517
    SINK_FOR_EACH(cursor, &This->CompartmentEventSink, ITfCompartmentEventSink, sink)
518
    {
519
        ITfCompartmentEventSink_OnChange(sink, &This->valueData->guid);
520 521
    }

522
    return S_OK;
523 524 525 526 527
}

static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
    VARIANT *pvarValue)
{
528
    Compartment *This = impl_from_ITfCompartment(iface);
529 530 531 532 533
    TRACE("(%p) %p\n",This, pvarValue);

    if (!pvarValue)
        return E_INVALIDARG;

534 535 536
    VariantInit(pvarValue);
    if (V_VT(&This->variant) == VT_EMPTY) return S_FALSE;
    return VariantCopy(pvarValue,&This->variant);
537 538
}

539 540
static const ITfCompartmentVtbl CompartmentVtbl =
{
541 542 543 544 545 546 547
    Compartment_QueryInterface,
    Compartment_AddRef,
    Compartment_Release,
    Compartment_SetValue,
    Compartment_GetValue
};

548 549 550 551
/*****************************************************
 * ITfSource functions
 *****************************************************/

552
static HRESULT WINAPI CompartmentSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
553
{
554
    Compartment *This = impl_from_ITfSource(iface);
555
    return ITfCompartment_QueryInterface(&This->ITfCompartment_iface, iid, ppvOut);
556 557
}

558
static ULONG WINAPI CompartmentSource_AddRef(ITfSource *iface)
559
{
560 561
    Compartment *This = impl_from_ITfSource(iface);
    return ITfCompartment_AddRef(&This->ITfCompartment_iface);
562 563
}

564
static ULONG WINAPI CompartmentSource_Release(ITfSource *iface)
565
{
566 567
    Compartment *This = impl_from_ITfSource(iface);
    return ITfCompartment_Release(&This->ITfCompartment_iface);
568 569
}

570
static HRESULT WINAPI CompartmentSource_AdviseSink(ITfSource *iface,
571 572
        REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
573
    Compartment *This = impl_from_ITfSource(iface);
574 575 576 577 578 579

    TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);

    if (!riid || !punk || !pdwCookie)
        return E_INVALIDARG;

580
    if (IsEqualIID(riid, &IID_ITfCompartmentEventSink))
581 582
        return advise_sink(&This->CompartmentEventSink, &IID_ITfCompartmentEventSink,
                           COOKIE_MAGIC_COMPARTMENTSINK, punk, pdwCookie);
583

584 585
    FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
    return E_NOTIMPL;
586 587
}

588
static HRESULT WINAPI CompartmentSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
589
{
590
    Compartment *This = impl_from_ITfSource(iface);
591 592 593 594 595 596

    TRACE("(%p) %x\n",This,pdwCookie);

    if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_COMPARTMENTSINK)
        return E_INVALIDARG;

597
    return unadvise_sink(pdwCookie);
598 599
}

600
static const ITfSourceVtbl CompartmentSourceVtbl =
601
{
602 603 604
    CompartmentSource_QueryInterface,
    CompartmentSource_AddRef,
    CompartmentSource_Release,
605 606 607 608
    CompartmentSource_AdviseSink,
    CompartmentSource_UnadviseSink,
};

609 610 611 612 613 614 615 616
static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartment **ppOut)
{
    Compartment *This;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Compartment));
    if (This == NULL)
        return E_OUTOFMEMORY;

617 618
    This->ITfCompartment_iface.lpVtbl= &CompartmentVtbl;
    This->ITfSource_iface.lpVtbl = &CompartmentSourceVtbl;
619 620 621 622 623
    This->refCount = 1;

    This->valueData = valueData;
    VariantInit(&This->variant);

624 625
    list_init(&This->CompartmentEventSink);

626 627
    *ppOut = &This->ITfCompartment_iface;
    TRACE("returning %p\n", *ppOut);
628 629
    return S_OK;
}