urlmon: Added implementation of protocol stream.
[wine] / dlls / urlmon / session.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "urlmon.h"
29 #include "urlmon_main.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
34
35 HRESULT get_protocol_iface(LPCWSTR url, IUnknown **ret)
36 {
37     WCHAR schema[64], str_clsid[64];
38     HKEY hkey = NULL;
39     DWORD res, type, size, schema_len;
40     CLSID clsid;
41     LPWSTR wszKey;
42     HRESULT hres;
43
44     static const WCHAR wszProtocolsKey[] =
45         {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
46     static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
47
48     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
49             &schema_len, 0);
50     if(FAILED(hres) || !schema_len)
51         return E_FAIL;
52
53     wszKey = HeapAlloc(GetProcessHeap(), 0, sizeof(wszProtocolsKey)+(schema_len+1)*sizeof(WCHAR));
54     memcpy(wszKey, wszProtocolsKey, sizeof(wszProtocolsKey));
55     memcpy(wszKey + sizeof(wszProtocolsKey)/sizeof(WCHAR), schema, (schema_len+1)*sizeof(WCHAR));
56
57     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
58     HeapFree(GetProcessHeap(), 0, wszKey);
59     if(res != ERROR_SUCCESS) {
60         TRACE("Could not open key %s\n", debugstr_w(wszKey));
61         return E_FAIL;
62     }
63     
64     size = sizeof(str_clsid);
65     res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
66     RegCloseKey(hkey);
67     if(res != ERROR_SUCCESS || type != REG_SZ) {
68         WARN("Could not get protocol CLSID res=%ld\n", res);
69         return E_FAIL;
70     }
71
72     hres = CLSIDFromString(str_clsid, &clsid);
73     if(FAILED(hres)) {
74         WARN("CLSIDFromString failed: %08lx\n", hres);
75         return hres;
76     }
77
78     return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)ret);
79 }
80
81 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
82         REFIID riid, void **ppv)
83 {
84     TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
85
86     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
87         *ppv = iface;
88         IInternetSession_AddRef(iface);
89         return S_OK;
90     }
91
92     *ppv = NULL;
93     return E_NOINTERFACE;
94 }
95
96 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
97 {
98     TRACE("()\n");
99     URLMON_LockModule();
100     return 2;
101 }
102
103 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
104 {
105     TRACE("()\n");
106     URLMON_UnlockModule();
107     return 1;
108 }
109
110 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
111         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
112         const LPCWSTR *ppwzPatterns, DWORD dwReserved)
113 {
114     FIXME("(%p %s %s %ld %p %ld)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
115             cPatterns, ppwzPatterns, dwReserved);
116     return E_NOTIMPL;
117 }
118
119 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
120         IClassFactory *pCF, LPCWSTR pszProtocol)
121 {
122     FIXME("(%p %s)\n", pCF, debugstr_w(pszProtocol));
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
127         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
128 {
129     FIXME("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
130     return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
134         IClassFactory *pCF, LPCWSTR pwzType)
135 {
136     FIXME("(%p %s)\n", pCF, debugstr_w(pwzType));
137     return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
141         LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
142         IInternetProtocol **ppOInetProt, DWORD dwOption)
143 {
144     FIXME("(%p %s %p %p %p %08lx)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
145             ppOInetProt, dwOption);
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
150         DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
151 {
152     FIXME("(%08lx %p %ld %ld)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
153     return E_NOTIMPL;
154 }
155
156 static const IInternetSessionVtbl InternetSessionVtbl = {
157     InternetSession_QueryInterface,
158     InternetSession_AddRef,
159     InternetSession_Release,
160     InternetSession_RegisterNameSpace,
161     InternetSession_UnregisterNameSpace,
162     InternetSession_RegisterMimeFilter,
163     InternetSession_UnregisterMimeFilter,
164     InternetSession_CreateBinding,
165     InternetSession_SetSessionOption
166 };
167
168 static IInternetSession InternetSession = { &InternetSessionVtbl };
169
170 /***********************************************************************
171  *           CoInternetGetSession (URLMON.@)
172  *
173  * Create a new internet session and return an IInternetSession interface
174  * representing it.
175  *
176  * PARAMS
177  *    dwSessionMode      [I] Mode for the internet session
178  *    ppIInternetSession [O] Destination for creates IInternetSession object
179  *    dwReserved         [I] Reserved, must be 0.
180  *
181  * RETURNS
182  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
183  *    Failure: E_INVALIDARG, if any argument is invalid, or
184  *             E_OUTOFMEMORY if memory allocation fails.
185  */
186 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
187         DWORD dwReserved)
188 {
189     TRACE("(%ld %p %ld)\n", dwSessionMode, ppIInternetSession, dwReserved);
190
191     if(dwSessionMode)
192         ERR("dwSessionMode=%ld\n", dwSessionMode);
193     if(dwReserved)
194         ERR("dwReserved=%ld\n", dwReserved);
195
196     *ppIInternetSession = &InternetSession;
197     return S_OK;
198 }