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