1/*
2 * Copyright (C) 2007, 2008, 2009, 2011 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 "NodeTraversal.h"
38#include "ScriptController.h"
39#include "TouchList.h"
40
41#if ENABLE(SVG)
42#include "JSSVGDocument.h"
43#include "SVGDocument.h"
44#endif
45
46#include <wtf/GetPtr.h>
47
48using namespace JSC;
49
50namespace WebCore {
51
52JSValue JSDocument::location(ExecState* exec) const
53{
54    RefPtr<Frame> frame = impl()->frame();
55
56    if (!frame)
57        return jsNull();
58
59    RefPtr<Location> location = frame->document()->domWindow()->location();
60    if (JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), location.get()))
61        return wrapper;
62
63    JSLocation* jsLocation = JSLocation::create(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location.get());
64    cacheWrapper(currentWorld(exec), location.get(), jsLocation);
65    return jsLocation;
66}
67
68void JSDocument::setLocation(ExecState* exec, JSValue value)
69{
70    String locationString = value.toString(exec)->value(exec);
71    if (exec->hadException())
72        return;
73
74    RefPtr<Frame> frame = impl()->frame();
75    if (!frame)
76        return;
77
78    if (RefPtr<Location> location = frame->document()->domWindow()->location())
79        location->setHref(locationString, activeDOMWindow(exec), firstDOMWindow(exec));
80}
81
82JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
83{
84    if (!document)
85        return jsNull();
86
87    JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), document);
88    if (wrapper)
89        return wrapper;
90
91    if (DOMWindow* domWindow = document->domWindow()) {
92        globalObject = toJSDOMWindow(toJS(exec, domWindow));
93        // Creating a wrapper for domWindow might have created a wrapper for document as well.
94        wrapper = getCachedWrapper(currentWorld(exec), document);
95        if (wrapper)
96            return wrapper;
97    }
98
99    if (document->isHTMLDocument())
100        wrapper = CREATE_DOM_WRAPPER(exec, globalObject, HTMLDocument, document);
101#if ENABLE(SVG)
102    else if (document->isSVGDocument())
103        wrapper = CREATE_DOM_WRAPPER(exec, globalObject, SVGDocument, document);
104#endif
105    else
106        wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Document, document);
107
108    // Make sure the document is kept around by the window object, and works right with the
109    // back/forward cache.
110    if (!document->frame()) {
111        size_t nodeCount = 0;
112        for (Node* n = document; n; n = NodeTraversal::next(n))
113            nodeCount++;
114
115        exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
116    }
117
118    return wrapper;
119}
120
121#if ENABLE(TOUCH_EVENTS)
122JSValue JSDocument::createTouchList(ExecState* exec)
123{
124    RefPtr<TouchList> touchList = TouchList::create();
125
126    for (size_t i = 0; i < exec->argumentCount(); i++)
127        touchList->append(toTouch(exec->argument(i)));
128
129    return toJS(exec, globalObject(), touchList.release());
130}
131#endif
132
133} // namespace WebCore
134