webengine/osswebengine/WebKit/win/WebKitClassFactory.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 "WebCache.h"
       
    32 #include "WebDownload.h"
       
    33 #include "WebError.h"
       
    34 #include "WebFrame.h"
       
    35 #include "WebHistory.h"
       
    36 #include "WebHistoryItem.h"
       
    37 #include "WebIconDatabase.h"
       
    38 #include "WebJavaScriptCollector.h"
       
    39 #include "WebKit.h"
       
    40 #include "WebScrollBar.h"
       
    41 #include "WebKitStatistics.h"
       
    42 #include "WebMutableURLRequest.h"
       
    43 #include "WebNotificationCenter.h"
       
    44 #include "WebPreferences.h"
       
    45 #include "WebURLCredential.h"
       
    46 #include "WebURLProtectionSpace.h"
       
    47 #include "WebURLResponse.h"
       
    48 #include "WebDebugProgram.h"
       
    49 #include "WebView.h"
       
    50 #include <SafariTheme/SafariTheme.h>
       
    51 
       
    52 // WebKitClassFactory ---------------------------------------------------------
       
    53 
       
    54 typedef void (APIENTRY*STInitializePtr)();
       
    55 
       
    56 WebKitClassFactory::WebKitClassFactory(CLSID targetClass)
       
    57 : m_targetClass(targetClass)
       
    58 , m_refCount(0)
       
    59 {
       
    60     static bool didInitializeSafariTheme;
       
    61     if (!didInitializeSafariTheme) {
       
    62         if (HMODULE module = LoadLibrary(SAFARITHEMEDLL))
       
    63             if (STInitializePtr stInit = (STInitializePtr)GetProcAddress(module, "STInitialize"))
       
    64                 stInit();
       
    65         didInitializeSafariTheme = true;
       
    66     }
       
    67 
       
    68     gClassCount++;
       
    69 }
       
    70 
       
    71 WebKitClassFactory::~WebKitClassFactory()
       
    72 {
       
    73     gClassCount--;
       
    74 }
       
    75 
       
    76 // IUnknown -------------------------------------------------------------------
       
    77 
       
    78 HRESULT STDMETHODCALLTYPE WebKitClassFactory::QueryInterface(REFIID riid, void** ppvObject)
       
    79 {
       
    80     *ppvObject = 0;
       
    81     if (IsEqualGUID(riid, IID_IUnknown))
       
    82         *ppvObject = static_cast<IUnknown*>(this);
       
    83     else if (IsEqualGUID(riid, IID_IClassFactory))
       
    84         *ppvObject = static_cast<IClassFactory*>(this);
       
    85     else
       
    86         return E_NOINTERFACE;
       
    87 
       
    88     AddRef();
       
    89     return S_OK;
       
    90 }
       
    91 
       
    92 ULONG STDMETHODCALLTYPE WebKitClassFactory::AddRef(void)
       
    93 {
       
    94     return ++m_refCount;
       
    95 }
       
    96 
       
    97 ULONG STDMETHODCALLTYPE WebKitClassFactory::Release(void)
       
    98 {
       
    99     ULONG newRef = --m_refCount;
       
   100     if (!newRef && !gLockCount)
       
   101         delete(this);
       
   102 
       
   103     return newRef;
       
   104 }
       
   105 
       
   106 // IClassFactory --------------------------------------------------------------
       
   107 
       
   108 HRESULT STDMETHODCALLTYPE WebKitClassFactory::CreateInstance(IUnknown* pUnkOuter, REFIID riid, void** ppvObject)
       
   109 {
       
   110     IUnknown* unknown = 0;
       
   111     *ppvObject = 0;
       
   112     
       
   113     if (pUnkOuter)
       
   114         return CLASS_E_NOAGGREGATION;
       
   115 
       
   116     if (IsEqualGUID(m_targetClass, CLSID_WebView))
       
   117         unknown = static_cast<IWebView*>(WebView::createInstance());
       
   118     else if (IsEqualGUID(m_targetClass, CLSID_WebIconDatabase))
       
   119         unknown = static_cast<IWebIconDatabase*>(WebIconDatabase::createInstance());
       
   120     else if (IsEqualGUID(m_targetClass, CLSID_WebMutableURLRequest))
       
   121         unknown = static_cast<IWebMutableURLRequest*>(WebMutableURLRequest::createInstance());
       
   122     else if (IsEqualGUID(m_targetClass, CLSID_WebNotificationCenter))
       
   123         unknown = static_cast<IWebNotificationCenter*>(WebNotificationCenter::createInstance());
       
   124     else if (IsEqualGUID(m_targetClass, CLSID_WebHistory))
       
   125         unknown = static_cast<IWebHistory*>(WebHistory::createInstance());
       
   126     else if (IsEqualGUID(m_targetClass, CLSID_CFDictionaryPropertyBag))
       
   127         unknown = static_cast<IPropertyBag*>(CFDictionaryPropertyBag::createInstance());
       
   128     else if (IsEqualGUID(m_targetClass, CLSID_WebHistoryItem))
       
   129         unknown = static_cast<IWebHistoryItem*>(WebHistoryItem::createInstance());
       
   130     else if (IsEqualGUID(m_targetClass, CLSID_WebCache))
       
   131         unknown = static_cast<IWebCache*>(WebCache::createInstance());
       
   132     else if (IsEqualGUID(m_targetClass, CLSID_WebJavaScriptCollector))
       
   133         unknown = static_cast<IWebJavaScriptCollector*>(WebJavaScriptCollector::createInstance());
       
   134     else if (IsEqualGUID(m_targetClass, CLSID_WebPreferences))
       
   135         unknown = static_cast<IWebPreferences*>(WebPreferences::createInstance());
       
   136     else if (IsEqualGUID(m_targetClass, CLSID_WebScrollBar))
       
   137         unknown = static_cast<IWebScrollBarPrivate*>(WebScrollBar::createInstance());
       
   138     else if (IsEqualGUID(m_targetClass, CLSID_WebKitStatistics))
       
   139         unknown = static_cast<IWebKitStatistics*>(WebKitStatistics::createInstance());
       
   140     else if (IsEqualGUID(m_targetClass, CLSID_WebError))
       
   141         unknown = static_cast<IWebError*>(WebError::createInstance(ResourceError()));
       
   142     else if (IsEqualGUID(m_targetClass, CLSID_WebURLCredential))
       
   143         unknown = static_cast<IWebURLCredential*>(WebURLCredential::createInstance());
       
   144     else if (IsEqualGUID(m_targetClass, CLSID_WebDownload))
       
   145         unknown = static_cast<IWebDownload*>(WebDownload::createInstance());
       
   146     else if (IsEqualGUID(m_targetClass, CLSID_WebURLRequest))
       
   147         unknown = static_cast<IWebURLRequest*>(WebMutableURLRequest::createImmutableInstance());
       
   148     else if (IsEqualGUID(m_targetClass, CLSID_WebURLProtectionSpace))
       
   149         unknown = static_cast<IWebURLProtectionSpace*>(WebURLProtectionSpace::createInstance());
       
   150     else if (IsEqualGUID(m_targetClass, CLSID_WebDebugProgram))
       
   151         unknown = static_cast<IWebDebugProgram*>(WebDebugProgram::createInstance());
       
   152     else if (IsEqualGUID(m_targetClass, CLSID_WebURLResponse))
       
   153         unknown = static_cast<IWebURLResponse*>(WebURLResponse::createInstance());
       
   154     else
       
   155         return CLASS_E_CLASSNOTAVAILABLE;
       
   156 
       
   157     if (!unknown)
       
   158         return E_OUTOFMEMORY;
       
   159 
       
   160     HRESULT hr = unknown->QueryInterface(riid, ppvObject);
       
   161     if (FAILED(hr))
       
   162         delete unknown;
       
   163     else
       
   164         unknown->Release(); // both createInstance() and QueryInterface() added refs
       
   165 
       
   166     return hr;
       
   167 }
       
   168 
       
   169 HRESULT STDMETHODCALLTYPE WebKitClassFactory::LockServer(BOOL fLock)
       
   170 {
       
   171     if (fLock)
       
   172         gLockCount++;
       
   173     else
       
   174         gLockCount--;
       
   175 
       
   176     return S_OK;
       
   177 }