shdocvw: Fix module ref counting.
[wine] / dlls / shdocvw / factory.c
1 /*
2  * Implementation of class factory for IE Web Browser
3  *
4  * Copyright 2001 John R. Sheets (for CodeWeavers)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <string.h>
22 #include "wine/debug.h"
23 #include "shdocvw.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
26
27 /**********************************************************************
28  * Implement the WebBrowser class factory
29  *
30  * (Based on implementation in ddraw/main.c)
31  */
32
33 #define FACTORY(x) ((IClassFactory*) &(x)->lpClassFactoryVtbl)
34
35 typedef struct
36 {
37     /* IUnknown fields */
38     const IClassFactoryVtbl *lpClassFactoryVtbl;
39     HRESULT (*cf)(LPUNKNOWN, REFIID, LPVOID *);
40     LONG ref;
41 } IClassFactoryImpl;
42
43
44 /**********************************************************************
45  * WBCF_QueryInterface (IUnknown)
46  */
47 static HRESULT WINAPI WBCF_QueryInterface(LPCLASSFACTORY iface,
48                                           REFIID riid, LPVOID *ppobj)
49 {
50     TRACE("(%s %p)\n", debugstr_guid(riid), ppobj);
51
52     if (!ppobj)
53         return E_POINTER;
54
55     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
56         *ppobj = iface;
57         IClassFactory_AddRef(iface);
58         return S_OK;
59     }
60
61     WARN("Not supported interface %s\n", debugstr_guid(riid));
62
63     *ppobj = NULL;
64     return E_NOINTERFACE;
65 }
66
67 /************************************************************************
68  * WBCF_AddRef (IUnknown)
69  */
70 static ULONG WINAPI WBCF_AddRef(LPCLASSFACTORY iface)
71 {
72     SHDOCVW_LockModule();
73
74     return 2; /* non-heap based object */
75 }
76
77 /************************************************************************
78  * WBCF_Release (IUnknown)
79  */
80 static ULONG WINAPI WBCF_Release(LPCLASSFACTORY iface)
81 {
82     SHDOCVW_UnlockModule();
83
84     return 1; /* non-heap based object */
85 }
86
87 /************************************************************************
88  * WBCF_CreateInstance (IClassFactory)
89  */
90 static HRESULT WINAPI WBCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
91                                           REFIID riid, LPVOID *ppobj)
92 {
93     IClassFactoryImpl *This = (IClassFactoryImpl *) iface;
94     return This->cf(pOuter, riid, ppobj);
95 }
96
97 /************************************************************************
98  * WBCF_LockServer (IClassFactory)
99  */
100 static HRESULT WINAPI WBCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
101 {
102     TRACE("(%d)\n", dolock);
103
104     if (dolock)
105         SHDOCVW_LockModule();
106     else
107         SHDOCVW_UnlockModule();
108     
109     return S_OK;
110 }
111
112 static const IClassFactoryVtbl WBCF_Vtbl =
113 {
114     WBCF_QueryInterface,
115     WBCF_AddRef,
116     WBCF_Release,
117     WBCF_CreateInstance,
118     WBCF_LockServer
119 };
120
121 /*************************************************************************
122  *              DllGetClassObject (SHDOCVW.@)
123  */
124 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
125 {
126     static IClassFactoryImpl WB1ClassFactory = {&WBCF_Vtbl, WebBrowserV1_Create};
127     static IClassFactoryImpl WB2ClassFactory = {&WBCF_Vtbl, WebBrowserV2_Create};
128     static IClassFactoryImpl CUHClassFactory = {&WBCF_Vtbl, CUrlHistory_Create};
129
130     TRACE("\n");
131
132     if(IsEqualGUID(&CLSID_WebBrowser, rclsid))
133         return IClassFactory_QueryInterface(FACTORY(&WB2ClassFactory), riid, ppv);
134
135     if(IsEqualGUID(&CLSID_WebBrowser_V1, rclsid))
136         return IClassFactory_QueryInterface(FACTORY(&WB1ClassFactory), riid, ppv);
137
138     if(IsEqualGUID(&CLSID_CUrlHistory, rclsid))
139         return IClassFactory_QueryInterface(FACTORY(&CUHClassFactory), riid, ppv);
140
141     /* As a last resort, figure if the CLSID belongs to a 'Shell Instance Object' */
142     return SHDOCVW_GetShellInstanceObjectClassObject(rclsid, riid, ppv);
143 }
144
145 HRESULT register_class_object(BOOL do_reg)
146 {
147     HRESULT hres;
148
149     static DWORD cookie;
150     static IClassFactoryImpl IEClassFactory = {&WBCF_Vtbl, InternetExplorer_Create};
151
152     if(do_reg) {
153         hres = CoRegisterClassObject(&CLSID_InternetExplorer, (IUnknown*)FACTORY(&IEClassFactory),
154                                      CLSCTX_SERVER, REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED, &cookie);
155         if (FAILED(hres)) {
156             ERR("failed to register object %08x\n", hres);
157             return hres;
158         }
159
160         hres = CoResumeClassObjects();
161         if(SUCCEEDED(hres))
162             return hres;
163
164         ERR("failed to resume object %08x\n", hres);
165     }
166
167     return CoRevokeClassObject(cookie);
168 }