Home | History | Annotate | Download | only in win
      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 #include "WebKitClassFactory.h"
     29 
     30 #include "CFDictionaryPropertyBag.h"
     31 #include "ForEachCoClass.h"
     32 #include "WebArchive.h"
     33 #include "WebCache.h"
     34 #include "WebCookieManager.h"
     35 #include "WebCoreStatistics.h"
     36 #include "WebDatabaseManager.h"
     37 #include "WebDownload.h"
     38 #include "WebError.h"
     39 #include "WebFrame.h"
     40 #include "WebGeolocationPosition.h"
     41 #include "WebHistory.h"
     42 #include "WebHistoryItem.h"
     43 #include "WebIconDatabase.h"
     44 #include "WebJavaScriptCollector.h"
     45 #include "WebKit.h"
     46 #include "WebKitStatistics.h"
     47 #include "WebMutableURLRequest.h"
     48 #include "WebNotificationCenter.h"
     49 #include "WebPreferences.h"
     50 #include "WebScriptWorld.h"
     51 #include "WebScrollBar.h"
     52 #include "WebSerializedJSValue.h"
     53 #include "WebTextRenderer.h"
     54 #include "WebURLCredential.h"
     55 #include "WebURLProtectionSpace.h"
     56 #include "WebURLResponse.h"
     57 #include "WebView.h"
     58 #include "WebWorkersPrivate.h"
     59 #pragma warning(push, 0)
     60 #include <JavaScriptCore/InitializeThreading.h>
     61 #include <WebCore/FontDatabase.h>
     62 #include <WebCore/SoftLinking.h>
     63 #pragma warning(pop)
     64 
     65 // WebKitClassFactory ---------------------------------------------------------
     66 #if USE(SAFARI_THEME)
     67 #ifdef DEBUG_ALL
     68 SOFT_LINK_DEBUG_LIBRARY(SafariTheme)
     69 #else
     70 SOFT_LINK_LIBRARY(SafariTheme)
     71 #endif
     72 
     73 SOFT_LINK(SafariTheme, STInitialize, void, APIENTRY, (), ())
     74 #endif
     75 
     76 WebKitClassFactory::WebKitClassFactory(CLSID targetClass)
     77 : m_targetClass(targetClass)
     78 , m_refCount(0)
     79 {
     80 #if USE(SAFARI_THEME)
     81     static bool didInitializeSafariTheme;
     82     if (!didInitializeSafariTheme) {
     83         if (SafariThemeLibrary())
     84             STInitialize();
     85         didInitializeSafariTheme = true;
     86     }
     87 #endif
     88 
     89     JSC::initializeThreading();
     90     WebCore::populateFontDatabase();
     91 
     92     gClassCount++;
     93     gClassNameCount.add("WebKitClassFactory");
     94 }
     95 
     96 WebKitClassFactory::~WebKitClassFactory()
     97 {
     98     gClassCount--;
     99     gClassNameCount.remove("WebKitClassFactory");
    100 }
    101 
    102 // IUnknown -------------------------------------------------------------------
    103 
    104 HRESULT STDMETHODCALLTYPE WebKitClassFactory::QueryInterface(REFIID riid, void** ppvObject)
    105 {
    106     *ppvObject = 0;
    107     if (IsEqualGUID(riid, IID_IUnknown))
    108         *ppvObject = static_cast<IUnknown*>(this);
    109     else if (IsEqualGUID(riid, IID_IClassFactory))
    110         *ppvObject = static_cast<IClassFactory*>(this);
    111     else
    112         return E_NOINTERFACE;
    113 
    114     AddRef();
    115     return S_OK;
    116 }
    117 
    118 ULONG STDMETHODCALLTYPE WebKitClassFactory::AddRef(void)
    119 {
    120     return ++m_refCount;
    121 }
    122 
    123 ULONG STDMETHODCALLTYPE WebKitClassFactory::Release(void)
    124 {
    125     ULONG newRef = --m_refCount;
    126     if (!newRef && !gLockCount)
    127         delete(this);
    128 
    129     return newRef;
    130 }
    131 
    132 // FIXME: Remove these functions once all createInstance() functions return COMPtr.
    133 template <typename T>
    134 static T* releaseRefFromCreateInstance(T* object)
    135 {
    136     return object;
    137 }
    138 
    139 template <typename T>
    140 static T* releaseRefFromCreateInstance(COMPtr<T> object)
    141 {
    142     return object.releaseRef();
    143 }
    144 
    145 // IClassFactory --------------------------------------------------------------
    146 
    147 HRESULT STDMETHODCALLTYPE WebKitClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid, void** ppvObject)
    148 {
    149     IUnknown* unknown = 0;
    150     *ppvObject = 0;
    151 
    152     if (pUnkOuter)
    153         return CLASS_E_NOAGGREGATION;
    154 
    155 #define INITIALIZE_IF_CLASS(cls) \
    156     if (IsEqualGUID(m_targetClass, CLSID_##cls)) \
    157         unknown = static_cast<I##cls*>(releaseRefFromCreateInstance(cls::createInstance())); \
    158     else \
    159     // end of macro
    160 
    161     // These #defines are needed to appease the INITIALIZE_IF_CLASS macro.
    162     // There is no ICFDictionaryPropertyBag, we use IPropertyBag instead.
    163 #define ICFDictionaryPropertyBag IPropertyBag
    164     // There is no IWebScrollBar, we only have IWebScrollBarPrivate.
    165 #define IWebScrollBar IWebScrollBarPrivate
    166     // There is no class called WebURLRequest -- WebMutableURLRequest implements it for us.
    167 #define WebURLRequest WebMutableURLRequest
    168 
    169     FOR_EACH_COCLASS(INITIALIZE_IF_CLASS)
    170         // This is the final else case
    171         return CLASS_E_CLASSNOTAVAILABLE;
    172 
    173 #undef ICFDictionaryPropertyBag
    174 #undef IWebScrollBar
    175 #undef WebURLRequest
    176 #undef INITIALIZE_IF_CLASS
    177 
    178     if (!unknown)
    179         return E_OUTOFMEMORY;
    180 
    181     HRESULT hr = unknown->QueryInterface(riid, ppvObject);
    182     if (FAILED(hr))
    183         delete unknown;
    184     else
    185         unknown->Release(); // both createInstance() and QueryInterface() added refs
    186 
    187     return hr;
    188 }
    189 
    190 HRESULT STDMETHODCALLTYPE WebKitClassFactory::LockServer(BOOL fLock)
    191 {
    192     if (fLock)
    193         gLockCount++;
    194     else
    195         gLockCount--;
    196 
    197     return S_OK;
    198 }
    199