1/*
2 * Copyright (C) 2010 Google, 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 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 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#ifndef PendingScript_h
27#define PendingScript_h
28
29#include "CachedResourceClient.h"
30#include "CachedResourceHandle.h"
31#include <wtf/text/TextPosition.h>
32#include <wtf/PassRefPtr.h>
33#include <wtf/RefPtr.h>
34
35namespace WebCore {
36
37class CachedScript;
38class Element;
39
40// A container for an external script which may be loaded and executed.
41//
42// A CachedResourceHandle alone does not prevent the underlying CachedResource
43// from purging its data buffer. This class holds a dummy client open for its
44// lifetime in order to guarantee that the data buffer will not be purged.
45class PendingScript : public CachedResourceClient {
46public:
47    PendingScript()
48        : m_watchingForLoad(false)
49        , m_startingPosition(TextPosition::belowRangePosition())
50    {
51    }
52
53    PendingScript(Element* element, CachedScript* cachedScript)
54        : m_watchingForLoad(false)
55        , m_element(element)
56    {
57        setCachedScript(cachedScript);
58    }
59
60    PendingScript(const PendingScript& other)
61        : CachedResourceClient(other)
62        , m_watchingForLoad(other.m_watchingForLoad)
63        , m_element(other.m_element)
64        , m_startingPosition(other.m_startingPosition)
65    {
66        setCachedScript(other.cachedScript());
67    }
68
69    ~PendingScript();
70
71    PendingScript& operator=(const PendingScript& other)
72    {
73        if (this == &other)
74            return *this;
75
76        m_watchingForLoad = other.m_watchingForLoad;
77        m_element = other.m_element;
78        m_startingPosition = other.m_startingPosition;
79        setCachedScript(other.cachedScript());
80
81        return *this;
82    }
83
84    TextPosition startingPosition() const { return m_startingPosition; }
85    void setStartingPosition(const TextPosition& position) { m_startingPosition = position; }
86
87    bool watchingForLoad() const { return m_watchingForLoad; }
88    void setWatchingForLoad(bool b) { m_watchingForLoad = b; }
89
90    Element* element() const { return m_element.get(); }
91    void setElement(Element* element) { m_element = element; }
92    PassRefPtr<Element> releaseElementAndClear();
93
94    CachedScript* cachedScript() const;
95    void setCachedScript(CachedScript*);
96
97    virtual void notifyFinished(CachedResource*);
98
99private:
100    bool m_watchingForLoad;
101    RefPtr<Element> m_element;
102    TextPosition m_startingPosition; // Only used for inline script tags.
103    CachedResourceHandle<CachedScript> m_cachedScript;
104};
105
106}
107
108#endif
109