Changeset 65350 in webkit


Ignore:
Timestamp:
Aug 13, 2010 8:09:36 PM (14 years ago)
Author:
tonyg@chromium.org
Message:

2010-08-13 Tony Gentilcore <tonyg@chromium.org>

Reviewed by Eric Seidel.

Refactor HTMLScriptRunner to allow deferred scripts to share code
https://bugs.webkit.org/show_bug.cgi?id=43736

No new tests because no functional change

  • html/HTMLScriptRunner.cpp: (WebCore::HTMLScriptRunner::sourceFromPendingScript): Make const to enforce the idea that it doesn't change m_parsingBlockingScript. (WebCore::HTMLScriptRunner::executeParsingBlockingScript): Rename to make it clear this deals with the parsing blocking script and refactor to use executePendingScriptAndDispatchEvent. (WebCore::HTMLScriptRunner::executePendingScriptAndDispatchEvent): Factored out of executePendingScript, this can be used for executing any external script. (WebCore::HTMLScriptRunner::executeScript): This can be used for executing any inline or external script. ASSERTs moved to the two calling points. (WebCore::HTMLScriptRunner::executeParsingBlockingScripts): Moved method. (WebCore::HTMLScriptRunner::requestParsingBlockingScript): Factored out requestPendingScript. (WebCore::HTMLScriptRunner::requestPendingScript): Factored out from reqeustParsingBlockingScript. (WebCore::HTMLScriptRunner::runScript):
  • html/HTMLScriptRunner.h:
Location:
trunk/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r65349 r65350  
     12010-08-13  Tony Gentilcore  <tonyg@chromium.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        Refactor HTMLScriptRunner to allow deferred scripts to share code
     6        https://bugs.webkit.org/show_bug.cgi?id=43736
     7
     8        No new tests because no functional change
     9
     10        * html/HTMLScriptRunner.cpp:
     11        (WebCore::HTMLScriptRunner::sourceFromPendingScript): Make const to enforce the idea that it doesn't change m_parsingBlockingScript.
     12        (WebCore::HTMLScriptRunner::executeParsingBlockingScript): Rename to make it clear this deals with the parsing blocking script and refactor to use executePendingScriptAndDispatchEvent.
     13        (WebCore::HTMLScriptRunner::executePendingScriptAndDispatchEvent): Factored out of executePendingScript, this can be used for executing any external script.
     14        (WebCore::HTMLScriptRunner::executeScript): This can be used for executing any inline or external script. ASSERTs moved to the two calling points.
     15        (WebCore::HTMLScriptRunner::executeParsingBlockingScripts): Moved method.
     16        (WebCore::HTMLScriptRunner::requestParsingBlockingScript): Factored out requestPendingScript.
     17        (WebCore::HTMLScriptRunner::requestPendingScript): Factored out from reqeustParsingBlockingScript.
     18        (WebCore::HTMLScriptRunner::runScript):
     19        * html/HTMLScriptRunner.h:
     20
    1212010-08-13  Gavin Barraclough  <barraclough@apple.com>
    222
  • trunk/WebCore/html/HTMLScriptRunner.cpp

    r64857 r65350  
    4444using namespace HTMLNames;
    4545
    46 class NestScript : public Noncopyable {
     46// FIXME: Factor out to avoid duplication with HTMLDocumentParser.
     47class NestingLevelIncrementer : public Noncopyable {
    4748public:
    48     NestScript(unsigned& nestingLevel, HTMLInputStream& inputStream)
     49    explicit NestingLevelIncrementer(unsigned& nestingLevel)
    4950        : m_nestingLevel(&nestingLevel)
    50         , m_savedInsertionPoint(inputStream)
    5151    {
    5252        ++(*m_nestingLevel);
    5353    }
    5454
    55     ~NestScript()
     55    ~NestingLevelIncrementer()
    5656    {
    5757        --(*m_nestingLevel);
     
    6060private:
    6161    unsigned* m_nestingLevel;
    62     InsertionPointRecord m_savedInsertionPoint;
    6362};
    6463
     
    9897}
    9998
    100 ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred)
     99ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred) const
    101100{
    102101    if (script.cachedScript()) {
     
    119118}
    120119
    121 void HTMLScriptRunner::executePendingScript()
     120void HTMLScriptRunner::executeParsingBlockingScript()
    122121{
    123122    ASSERT(!m_scriptNestingLevel);
    124123    ASSERT(m_document->haveStylesheetsLoaded());
    125     bool errorOccurred = false;
    126124    ASSERT(isPendingScriptReady(m_parsingBlockingScript));
    127     ScriptSourceCode sourceCode = sourceFromPendingScript(m_parsingBlockingScript, errorOccurred);
    128125
    129126    // Stop watching loads before executeScript to prevent recursion if the script reloads itself.
     
    131128        stopWatchingForLoad(m_parsingBlockingScript);
    132129
     130    InsertionPointRecord insertionPointRecord(m_host->inputStream());
     131    executePendingScriptAndDispatchEvent(m_parsingBlockingScript);
     132}
     133
     134void HTMLScriptRunner::executePendingScriptAndDispatchEvent(PendingScript& pendingScript)
     135{
     136    bool errorOccurred = false;
     137    ScriptSourceCode sourceCode = sourceFromPendingScript(pendingScript, errorOccurred);
     138
    133139    // Clear the pending script before possible rentrancy from executeScript()
    134     RefPtr<Element> scriptElement = m_parsingBlockingScript.releaseElementAndClear();
    135     {
    136         NestScript nestingLevel(m_scriptNestingLevel, m_host->inputStream());
     140    RefPtr<Element> scriptElement = pendingScript.releaseElementAndClear();
     141    {
     142        NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
    137143        if (errorOccurred)
    138144            scriptElement->dispatchEvent(createScriptErrorEvent());
     
    145151}
    146152
    147 void HTMLScriptRunner::executeScript(Element* element, const ScriptSourceCode& sourceCode)
    148 {
    149     // FIXME: We do not block inline <script> tags on stylesheets for now.
    150     // When we do,  || !element->hasAttribute(srcAttr) should be removed from
    151     // the ASSERT below.  See https://bugs.webkit.org/show_bug.cgi?id=40047
    152     ASSERT(m_document->haveStylesheetsLoaded() || !element->hasAttribute(srcAttr));
     153void HTMLScriptRunner::executeScript(Element* element, const ScriptSourceCode& sourceCode) const
     154{
    153155    ScriptElement* scriptElement = toScriptElement(element);
    154156    ASSERT(scriptElement);
     
    205207        if (!isPendingScriptReady(m_parsingBlockingScript))
    206208            return false;
    207         executePendingScript();
     209        executeParsingBlockingScript();
    208210    }
    209211    return true;
     
    229231}
    230232
    231 void HTMLScriptRunner::requestScript(Element* script)
    232 {
    233     ASSERT(!m_parsingBlockingScript.element());
    234     AtomicString srcValue = script->getAttribute(srcAttr);
    235     // Allow the host to disllow script loads (using the XSSAuditor, etc.)
    236     if (!m_host->shouldLoadExternalScriptFromSrc(srcValue))
     233void HTMLScriptRunner::requestParsingBlockingScript(Element* element)
     234{
     235    if (!requestPendingScript(m_parsingBlockingScript, element))
    237236        return;
    238     // FIXME: We need to resolve the url relative to the element.
    239     if (!script->dispatchBeforeLoadEvent(srcValue))
    240         return;
    241     m_parsingBlockingScript.adoptElement(script);
    242     // This should correctly return 0 for empty or invalid srcValues.
    243     CachedScript* cachedScript = m_document->docLoader()->requestScript(srcValue, toScriptElement(script)->scriptCharset());
    244     if (!cachedScript) {
    245         notImplemented(); // Dispatch error event.
    246         return;
    247     }
    248 
    249     m_parsingBlockingScript.setCachedScript(cachedScript);
     237
     238    ASSERT(m_parsingBlockingScript.cachedScript());
    250239
    251240    // We only care about a load callback if cachedScript is not already
     
    256245}
    257246
     247bool HTMLScriptRunner::requestPendingScript(PendingScript& pendingScript, Element* script) const
     248{
     249    ASSERT(!pendingScript.element());
     250    const AtomicString& srcValue = script->getAttribute(srcAttr);
     251    // Allow the host to disllow script loads (using the XSSAuditor, etc.)
     252    if (!m_host->shouldLoadExternalScriptFromSrc(srcValue))
     253        return false;
     254    // FIXME: We need to resolve the url relative to the element.
     255    if (!script->dispatchBeforeLoadEvent(srcValue))
     256        return false;
     257    pendingScript.adoptElement(script);
     258    // This should correctly return 0 for empty or invalid srcValues.
     259    CachedScript* cachedScript = m_document->docLoader()->requestScript(srcValue, toScriptElement(script)->scriptCharset());
     260    if (!cachedScript) {
     261        notImplemented(); // Dispatch error event.
     262        return false;
     263    }
     264    pendingScript.setCachedScript(cachedScript);
     265    return true;
     266}
     267
    258268// This method is meant to match the HTML5 definition of "running a script"
    259269// http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#running-a-script
     
    262272    ASSERT(!haveParsingBlockingScript());
    263273    {
    264         NestScript nestingLevel(m_scriptNestingLevel, m_host->inputStream());
     274        InsertionPointRecord insertionPointRecord(m_host->inputStream());
     275        NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
    265276
    266277        // Check script type and language, current code uses ScriptElement::shouldExecuteAsJavaScript(), but that may not be HTML5 compliant.
     
    269280        if (script->hasAttribute(srcAttr)) {
    270281            // FIXME: Handle defer and async
    271             requestScript(script);
     282            requestParsingBlockingScript(script);
    272283        } else {
    273284            // FIXME: We do not block inline <script> tags on stylesheets to match the
    274             // old parser for now.  See https://bugs.webkit.org/show_bug.cgi?id=40047
     285            // old parser for now.  When we do, the ASSERT below should be added.
     286            // See https://bugs.webkit.org/show_bug.cgi?id=40047
     287            // ASSERT(document()->haveStylesheetsLoaded());
     288            ASSERT(isExecutingScript());
    275289            ScriptSourceCode sourceCode(script->textContent(), documentURLForScriptExecution(m_document), startingLineNumber);
    276290            executeScript(script, sourceCode);
  • trunk/WebCore/html/HTMLScriptRunner.h

    r64857 r65350  
    5353    bool executeScriptsWaitingForStylesheets();
    5454
    55     bool isExecutingScript() { return !!m_scriptNestingLevel; }
     55    bool isExecutingScript() const { return !!m_scriptNestingLevel; }
    5656
    5757private:
    5858    Frame* frame() const;
    5959
     60    void executeParsingBlockingScript();
     61    void executePendingScriptAndDispatchEvent(PendingScript&);
     62    void executeScript(Element*, const ScriptSourceCode&) const;
    6063    bool haveParsingBlockingScript() const;
    6164    bool executeParsingBlockingScripts();
    62     void executePendingScript();
    6365
    64     void requestScript(Element*);
     66    void requestParsingBlockingScript(Element*);
     67    bool requestPendingScript(PendingScript&, Element*) const;
     68
    6569    void runScript(Element*, int startingLineNumber);
    6670
     
    6872    void watchForLoad(PendingScript&);
    6973    void stopWatchingForLoad(PendingScript&);
    70     void executeScript(Element*, const ScriptSourceCode&);
    71 
    7274    bool isPendingScriptReady(const PendingScript&);
    73     ScriptSourceCode sourceFromPendingScript(const PendingScript&, bool& errorOccurred);
     75    ScriptSourceCode sourceFromPendingScript(const PendingScript&, bool& errorOccurred) const;
    7476
    7577    Document* m_document;
Note: See TracChangeset for help on using the changeset viewer.