1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebKitDLL.h"
28
29#include "ForEachCoClass.h"
30#include "resource.h"
31#include "WebKit.h"
32#include "WebKitClassFactory.h"
33#include <WebCore/COMPtr.h>
34#include <WebCore/IconDatabase.h>
35#include <WebCore/Page.h>
36#include <WebCore/PageGroup.h>
37#include <WebCore/RenderThemeWin.h>
38#include <WebCore/SharedBuffer.h>
39#include <WebCore/WebCoreInstanceHandle.h>
40#include <WebCore/Widget.h>
41#include <olectl.h>
42#include <wchar.h>
43#include <wtf/Vector.h>
44
45using namespace WebCore;
46
47ULONG gLockCount;
48ULONG gClassCount;
49HashCountedSet<String> gClassNameCount;
50HINSTANCE gInstance;
51
52#define CLSID_FOR_CLASS(cls) CLSID_##cls,
53CLSID gRegCLSIDs[] = {
54    FOR_EACH_COCLASS(CLSID_FOR_CLASS)
55};
56#undef CLSID_FOR_CLASS
57
58STDAPI_(BOOL) DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID /*lpReserved*/)
59{
60    switch (ul_reason_for_call) {
61        case DLL_PROCESS_ATTACH:
62            gLockCount = gClassCount = 0;
63            gInstance = hModule;
64            WebCore::setInstanceHandle(hModule);
65            return TRUE;
66
67        case DLL_PROCESS_DETACH:
68            WebCore::RenderThemeWin::setWebKitIsBeingUnloaded();
69            break;
70
71        case DLL_THREAD_ATTACH:
72        case DLL_THREAD_DETACH:
73            break;
74    }
75    return FALSE;
76}
77
78STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
79{
80    bool found = false;
81    for (size_t i = 0; i < WTF_ARRAY_LENGTH(gRegCLSIDs); ++i) {
82        if (IsEqualGUID(rclsid, gRegCLSIDs[i])) {
83            found = true;
84            break;
85        }
86    }
87    if (!found)
88        return E_FAIL;
89
90    if (!IsEqualGUID(riid, IID_IUnknown) && !IsEqualGUID(riid, IID_IClassFactory))
91        return E_NOINTERFACE;
92
93    WebKitClassFactory* factory = new WebKitClassFactory(rclsid);
94    *ppv = reinterpret_cast<LPVOID>(factory);
95    if (!factory)
96        return E_OUTOFMEMORY;
97
98    factory->AddRef();
99    return S_OK;
100}
101
102STDAPI DllCanUnloadNow(void)
103{
104    if (!gClassCount && !gLockCount)
105        return S_OK;
106
107    return S_FALSE;
108}
109
110// deprecated - do not use
111STDAPI DllUnregisterServer(void)
112{
113    return 0;
114}
115
116// deprecated - do not use
117STDAPI DllRegisterServer(void)
118{
119    return 0;
120}
121
122// deprecated - do not use
123STDAPI RunAsLocalServer()
124{
125    return 0;
126}
127
128// deprecated - do not use
129STDAPI LocalServerDidDie()
130{
131    return 0;
132}
133
134void shutDownWebKit()
135{
136    WebCore::iconDatabase().close();
137    WebCore::PageGroup::closeLocalStorage();
138}
139
140//FIXME: We should consider moving this to a new file for cross-project functionality
141PassRefPtr<WebCore::SharedBuffer> loadResourceIntoBuffer(const char* name)
142{
143    int idr;
144    // temporary hack to get resource id
145    if (!strcmp(name, "textAreaResizeCorner"))
146        idr = IDR_RESIZE_CORNER;
147    else if (!strcmp(name, "missingImage"))
148        idr = IDR_MISSING_IMAGE;
149    else if (!strcmp(name, "nullPlugin"))
150        idr = IDR_NULL_PLUGIN;
151    else if (!strcmp(name, "panIcon"))
152        idr = IDR_PAN_SCROLL_ICON;
153    else if (!strcmp(name, "panSouthCursor"))
154        idr = IDR_PAN_SOUTH_CURSOR;
155    else if (!strcmp(name, "panNorthCursor"))
156        idr = IDR_PAN_NORTH_CURSOR;
157    else if (!strcmp(name, "panEastCursor"))
158        idr = IDR_PAN_EAST_CURSOR;
159    else if (!strcmp(name, "panWestCursor"))
160        idr = IDR_PAN_WEST_CURSOR;
161    else if (!strcmp(name, "panSouthEastCursor"))
162        idr = IDR_PAN_SOUTH_EAST_CURSOR;
163    else if (!strcmp(name, "panSouthWestCursor"))
164        idr = IDR_PAN_SOUTH_WEST_CURSOR;
165    else if (!strcmp(name, "panNorthEastCursor"))
166        idr = IDR_PAN_NORTH_EAST_CURSOR;
167    else if (!strcmp(name, "panNorthWestCursor"))
168        idr = IDR_PAN_NORTH_WEST_CURSOR;
169    else if (!strcmp(name, "searchMagnifier"))
170        idr = IDR_SEARCH_MAGNIFIER;
171    else if (!strcmp(name, "searchMagnifierResults"))
172        idr = IDR_SEARCH_MAGNIFIER_RESULTS;
173    else if (!strcmp(name, "searchCancel"))
174        idr = IDR_SEARCH_CANCEL;
175    else if (!strcmp(name, "searchCancelPressed"))
176        idr = IDR_SEARCH_CANCEL_PRESSED;
177    else if (!strcmp(name, "zoomInCursor"))
178        idr = IDR_ZOOM_IN_CURSOR;
179    else if (!strcmp(name, "zoomOutCursor"))
180        idr = IDR_ZOOM_OUT_CURSOR;
181    else if (!strcmp(name, "verticalTextCursor"))
182        idr = IDR_VERTICAL_TEXT_CURSOR;
183    else if (!strcmp(name, "fsVideoAudioVolumeHigh"))
184        idr = IDR_FS_VIDEO_AUDIO_VOLUME_HIGH;
185    else if (!strcmp(name, "fsVideoAudioVolumeLow"))
186        idr = IDR_FS_VIDEO_AUDIO_VOLUME_LOW;
187    else if (!strcmp(name, "fsVideoExitFullscreen"))
188        idr = IDR_FS_VIDEO_EXIT_FULLSCREEN;
189    else if (!strcmp(name, "fsVideoPause"))
190        idr = IDR_FS_VIDEO_PAUSE;
191    else if (!strcmp(name, "fsVideoPlay"))
192        idr = IDR_FS_VIDEO_PLAY;
193    else
194        return 0;
195
196    HRSRC resInfo = FindResource(gInstance, MAKEINTRESOURCE(idr), L"PNG");
197    if (!resInfo)
198        return 0;
199    HANDLE res = LoadResource(gInstance, resInfo);
200    if (!res)
201        return 0;
202    void* resource = LockResource(res);
203    if (!resource)
204        return 0;
205    int size = SizeofResource(gInstance, resInfo);
206
207    return WebCore::SharedBuffer::create(reinterpret_cast<const char*>(resource), size);
208}
209