Changeset 62937 in webkit


Ignore:
Timestamp:
Jul 9, 2010 4:33:17 AM (14 years ago)
Author:
commit-queue@webkit.org
Message:

2010-07-09 Andreas Wictor <andreas.wictor@xcerion.com>

Reviewed by Alexey Proskuryakov.

Remove global variables from XSLTProcessorLibxslt.cpp
https://bugs.webkit.org/show_bug.cgi?id=41348

Remove the globalProcessor and globalDocLoader global variables
by using the _private field that exists on most libxml structs.

No new tests, existing tests covers this.

  • xml/XSLTProcessor.h: (WebCore::XSLTProcessor::sourceNode): (WebCore::XSLTProcessor::XSLTProcessor):
  • xml/XSLTProcessorLibxslt.cpp: (WebCore::registeredXSLTProcessors): (WebCore::registeredXSLStyleSheets): (WebCore::docLoaderFunc): (WebCore::clearSavedStyleSheetPointers): (WebCore::xsltStylesheetPointer): (WebCore::XSLTProcessor::transformToString):
Location:
trunk/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r62935 r62937  
     12010-07-09  Andreas Wictor  <andreas.wictor@xcerion.com>
     2
     3        Reviewed by Alexey Proskuryakov.
     4
     5        Remove global variables from XSLTProcessorLibxslt.cpp
     6        https://bugs.webkit.org/show_bug.cgi?id=41348
     7
     8        Remove the globalProcessor and globalDocLoader global variables
     9        by using the _private field that exists on most libxml structs.
     10
     11        No new tests, existing tests covers this.
     12
     13        * xml/XSLTProcessor.h:
     14        (WebCore::XSLTProcessor::sourceNode):
     15        (WebCore::XSLTProcessor::XSLTProcessor):
     16        * xml/XSLTProcessorLibxslt.cpp:
     17        (WebCore::registeredXSLTProcessors):
     18        (WebCore::registeredXSLStyleSheets):
     19        (WebCore::docLoaderFunc):
     20        (WebCore::clearSavedStyleSheetPointers):
     21        (WebCore::xsltStylesheetPointer):
     22        (WebCore::XSLTProcessor::transformToString):
     23
    1242010-07-09  Adam Barth  <abarth@webkit.org>
    225
  • trunk/WebCore/xml/XSLTProcessor.h

    r62566 r62937  
    6868    // Only for libXSLT callbacks
    6969    XSLStyleSheet* xslStylesheet() const { return m_stylesheet.get(); }
     70    Node* sourceNode() const { return m_sourceNode; }
    7071#endif
    7172
     
    7374
    7475private:
    75     XSLTProcessor() { }
     76    XSLTProcessor() : m_sourceNode(0) { }
    7677
    7778    RefPtr<XSLStyleSheet> m_stylesheet;
    7879    RefPtr<Node> m_stylesheetRootNode;
    7980    ParameterMap m_parameters;
     81
     82    Node* m_sourceNode; // Source node is only non-null in transformToString(), so this cannot become a dangling pointer.
    8083};
    8184
  • trunk/WebCore/xml/XSLTProcessorLibxslt.cpp

    r62566 r62937  
    4646#include <libxslt/xsltutils.h>
    4747#include <wtf/Assertions.h>
     48#include <wtf/HashSet.h>
    4849#include <wtf/text/CString.h>
    4950#include <wtf/Vector.h>
     
    9596}
    9697
    97 // FIXME: There seems to be no way to control the ctxt pointer for loading here, thus we have globals.
    98 static XSLTProcessor* globalProcessor = 0;
    99 static DocLoader* globalDocLoader = 0;
     98static HashSet<XSLTProcessor*>& registeredXSLTProcessors()
     99{
     100    DEFINE_STATIC_LOCAL(HashSet<XSLTProcessor*>, xsltProcessors, ());
     101    return xsltProcessors;
     102}
     103
     104static HashSet<XSLStyleSheet*>& registeredXSLStyleSheets()
     105{
     106    DEFINE_STATIC_LOCAL(HashSet<XSLStyleSheet*>, xslStyleSheets, ());
     107    return xslStyleSheets;
     108}
     109
    100110static xmlDocPtr docLoaderFunc(const xmlChar* uri,
    101111                                    xmlDictPtr,
     
    104114                                    xsltLoadType type)
    105115{
    106     if (!globalProcessor)
    107         return 0;
    108 
    109116    switch (type) {
    110117    case XSLT_LOAD_DOCUMENT: {
    111         xsltTransformContextPtr context = (xsltTransformContextPtr)ctxt;
     118        xsltTransformContextPtr context = static_cast<xsltTransformContextPtr>(ctxt);
     119        XSLTProcessor* processor = static_cast<XSLTProcessor*>(context->_private);
     120        if (!processor || HashTraits<XSLTProcessor*>::isDeletedValue(processor)
     121            || !registeredXSLTProcessors().contains(processor)) {
     122            static bool errorMessagePrinted = false;
     123            if (!errorMessagePrinted) {
     124                fprintf(stderr, "WebKit XSLT document loader was called with unknown transformation context.");
     125                errorMessagePrinted = true;
     126            }
     127            return 0;
     128        }
     129
     130        RefPtr<Document> ownerDocument = processor->sourceNode()->document();
     131        DocLoader* docLoader = ownerDocument->docLoader();
     132
    112133        xmlChar* base = xmlNodeGetBase(context->document->doc, context->node);
    113134        KURL url(KURL(ParsedURLString, reinterpret_cast<const char*>(base)), reinterpret_cast<const char*>(uri));
     
    118139        Vector<char> data;
    119140
    120         bool requestAllowed = globalDocLoader->frame() && globalDocLoader->doc()->securityOrigin()->canRequest(url);
     141        bool requestAllowed = docLoader->frame() && docLoader->doc()->securityOrigin()->canRequest(url);
    121142        if (requestAllowed) {
    122             globalDocLoader->frame()->loader()->loadResourceSynchronously(url, AllowStoredCredentials, error, response, data);
    123             requestAllowed = globalDocLoader->doc()->securityOrigin()->canRequest(response.url());
     143            docLoader->frame()->loader()->loadResourceSynchronously(url, AllowStoredCredentials, error, response, data);
     144            requestAllowed = docLoader->doc()->securityOrigin()->canRequest(response.url());
    124145        }
    125146        if (!requestAllowed) {
    126147            data.clear();
    127             globalDocLoader->printAccessDeniedMessage(url);
     148            docLoader->printAccessDeniedMessage(url);
    128149        }
    129150
    130151        Console* console = 0;
    131         if (Frame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame())
     152        if (Frame* frame = processor->xslStylesheet()->ownerDocument()->frame())
    132153            console = frame->domWindow()->console();
    133154        xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
     
    143164        return doc;
    144165    }
    145     case XSLT_LOAD_STYLESHEET:
    146         return globalProcessor->xslStylesheet()->locateStylesheetSubResource(((xsltStylesheetPtr)ctxt)->doc, uri);
     166    case XSLT_LOAD_STYLESHEET: {
     167        xsltStylesheetPtr style = static_cast<xsltStylesheetPtr>(ctxt);
     168        XSLStyleSheet* xslStyleSheet = static_cast<XSLStyleSheet*>(style->doc->_private);
     169        if (!xslStyleSheet || HashTraits<XSLStyleSheet*>::isDeletedValue(xslStyleSheet)
     170            || !registeredXSLStyleSheets().contains(xslStyleSheet)) {
     171            static bool errorMessagePrinted = false;
     172            if (!errorMessagePrinted) {
     173                fprintf(stderr, "WebKit XSLT document loader was called with unknown transformation context.");
     174                errorMessagePrinted = true;
     175            }
     176            return 0;
     177        }
     178
     179        xmlDocPtr result = xslStyleSheet->locateStylesheetSubResource(style->doc, uri);
     180        if (!result)
     181            return 0;
     182
     183        // Save a pointer to the root stylesheet so that we can access it again from
     184        // this callback function if this xsl document imports another xsl document.
     185        ASSERT(!result->_private);
     186        result->_private = xslStyleSheet;
     187        return result;
     188    }
    147189    default:
    148190        break;
     
    150192
    151193    return 0;
    152 }
    153 
    154 static inline void setXSLTLoadCallBack(xsltDocLoaderFunc func, XSLTProcessor* processor, DocLoader* loader)
    155 {
    156     xsltSetLoaderFunc(func);
    157     globalProcessor = processor;
    158     globalDocLoader = loader;
    159194}
    160195
     
    222257}
    223258
     259static void clearSavedStyleSheetPointers(xsltStylesheetPtr style)
     260{
     261    if (!style || !style->doc)
     262        return;
     263
     264    ASSERT(style->doc->_private);
     265    style->doc->_private = 0;
     266
     267    for (xsltStylesheetPtr import = style->imports; import; import = import->next)
     268        clearSavedStyleSheetPointers(import);
     269}
     270
    224271static xsltStylesheetPtr xsltStylesheetPointer(RefPtr<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
    225272{
     
    231278    }
    232279
    233     if (!cachedStylesheet || !cachedStylesheet->document())
     280    if (!cachedStylesheet)
    234281        return 0;
    235282
    236     return cachedStylesheet->compileStyleSheet();
     283    xmlDocPtr xslDocument = cachedStylesheet->document();
     284    if (!xslDocument)
     285        return 0;
     286
     287    // Save a pointer to the stylesheet so that we can access it from the libxslt loader callback.
     288    void* old = xslDocument->_private;
     289    xslDocument->_private = cachedStylesheet.get();
     290    registeredXSLStyleSheets().add(cachedStylesheet.get());
     291
     292    xsltStylesheetPtr result = cachedStylesheet->compileStyleSheet();
     293    registeredXSLStyleSheets().remove(cachedStylesheet.get());
     294    clearSavedStyleSheetPointers(result);
     295    xslDocument->_private = old;
     296    return result;
    237297}
    238298
     
    274334bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding)
    275335{
    276     RefPtr<Document> ownerDocument = sourceNode->document();
    277 
    278     setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->docLoader());
     336    xsltSetLoaderFunc(docLoaderFunc);
    279337    xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get());
    280338    if (!sheet) {
    281         setXSLTLoadCallBack(0, 0, 0);
     339        xsltSetLoaderFunc(0);
    282340        return false;
    283341    }
     
    298356        registerXSLTExtensions(transformContext);
    299357
     358        // Save a pointer to this XSLTProcessor so that we can access it from the libxslt loader callback.
     359        ASSERT(!transformContext->_private);
     360        transformContext->_private = this;
     361        registeredXSLTProcessors().add(this);
     362        m_sourceNode = sourceNode;
     363
    300364        // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor <xsl:sort> algorithm only compares by code point.
    301365        xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction);
     
    310374        xmlDocPtr resultDoc = xsltApplyStylesheetUser(sheet, sourceDoc, 0, 0, 0, transformContext);
    311375
     376        registeredXSLTProcessors().remove(this);
    312377        xsltFreeTransformContext(transformContext);
    313378        freeXsltParamArray(params);
     
    324389
    325390    sheet->method = origMethod;
    326     setXSLTLoadCallBack(0, 0, 0);
     391    xsltSetLoaderFunc(0);
    327392    xsltFreeStylesheet(sheet);
    328393    m_stylesheet = 0;
     394    m_sourceNode = 0;
    329395
    330396    return success;
Note: See TracChangeset for help on using the changeset viewer.