1/*
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "JSDocument.h"
22
23#include "ExceptionCode.h"
24#include "Frame.h"
25#include "FrameLoader.h"
26#include "HTMLDocument.h"
27#include "JSCanvasRenderingContext2D.h"
28#if ENABLE(WEBGL)
29#include "JSWebGLRenderingContext.h"
30#endif
31#include "JSDOMWindowCustom.h"
32#include "JSHTMLDocument.h"
33#include "JSLocation.h"
34#include "JSTouch.h"
35#include "JSTouchList.h"
36#include "Location.h"
37#include "ScriptController.h"
38#include "TouchList.h"
39
40#if ENABLE(SVG)
41#include "JSSVGDocument.h"
42#include "SVGDocument.h"
43#endif
44
45#include <wtf/GetPtr.h>
46
47using namespace JSC;
48
49namespace WebCore {
50
51void JSDocument::markChildren(MarkStack& markStack)
52{
53    JSNode::markChildren(markStack);
54
55    Document* document = impl();
56    JSGlobalData& globalData = *Heap::heap(this)->globalData();
57
58    markActiveObjectsForContext(markStack, globalData, document);
59    markDOMObjectWrapper(markStack, globalData, document->implementation());
60    markDOMObjectWrapper(markStack, globalData, document->styleSheets());
61}
62
63JSValue JSDocument::location(ExecState* exec) const
64{
65    Frame* frame = static_cast<Document*>(impl())->frame();
66    if (!frame)
67        return jsNull();
68
69    Location* location = frame->domWindow()->location();
70    if (JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), location))
71        return wrapper;
72
73    JSLocation* jsLocation = new (exec) JSLocation(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location);
74    cacheWrapper(currentWorld(exec), location, jsLocation);
75    return jsLocation;
76}
77
78void JSDocument::setLocation(ExecState* exec, JSValue value)
79{
80    Frame* frame = static_cast<Document*>(impl())->frame();
81    if (!frame)
82        return;
83
84    String str = ustringToString(value.toString(exec));
85
86    Frame* lexicalFrame = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame();
87
88    // IE and Mozilla both resolve the URL relative to the source frame,
89    // not the target frame.
90    Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
91    str = activeFrame->document()->completeURL(str).string();
92
93    frame->navigationScheduler()->scheduleLocationChange(lexicalFrame->document()->securityOrigin(),
94        str, activeFrame->loader()->outgoingReferrer(), !activeFrame->script()->anyPageIsProcessingUserGesture(), false);
95}
96
97JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
98{
99    if (!document)
100        return jsNull();
101
102    JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), document);
103    if (wrapper)
104        return wrapper;
105
106    if (document->isHTMLDocument())
107        wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, HTMLDocument, document);
108#if ENABLE(SVG)
109    else if (document->isSVGDocument())
110        wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, SVGDocument, document);
111#endif
112    else
113        wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Document, document);
114
115    // Make sure the document is kept around by the window object, and works right with the
116    // back/forward cache.
117    if (!document->frame()) {
118        size_t nodeCount = 0;
119        for (Node* n = document; n; n = n->traverseNextNode())
120            nodeCount++;
121
122        exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
123    }
124
125    return wrapper;
126}
127
128#if ENABLE(TOUCH_EVENTS)
129JSValue JSDocument::createTouchList(ExecState* exec)
130{
131    RefPtr<TouchList> touchList = TouchList::create();
132
133    for (int i = 0; i < exec->argumentCount(); i++)
134        touchList->append(toTouch(exec->argument(i)));
135
136    return toJS(exec, touchList.release());
137}
138#endif
139
140} // namespace WebCore
141