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 "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
47 using namespace JSC;
48
49 namespace WebCore {
50
location(ExecState * exec) const51 JSValue JSDocument::location(ExecState* exec) const
52 {
53 Frame* frame = static_cast<Document*>(impl())->frame();
54 if (!frame)
55 return jsNull();
56
57 Location* location = frame->domWindow()->location();
58 if (JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), location))
59 return wrapper;
60
61 JSLocation* jsLocation = new (exec) JSLocation(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location);
62 cacheWrapper(currentWorld(exec), location, jsLocation);
63 return jsLocation;
64 }
65
setLocation(ExecState * exec,JSValue value)66 void JSDocument::setLocation(ExecState* exec, JSValue value)
67 {
68 Frame* frame = static_cast<Document*>(impl())->frame();
69 if (!frame)
70 return;
71
72 String str = ustringToString(value.toString(exec));
73
74 Frame* lexicalFrame = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame();
75
76 // IE and Mozilla both resolve the URL relative to the source frame,
77 // not the target frame.
78 Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
79 str = activeFrame->document()->completeURL(str).string();
80
81 frame->navigationScheduler()->scheduleLocationChange(lexicalFrame->document()->securityOrigin(),
82 str, activeFrame->loader()->outgoingReferrer(), !activeFrame->script()->anyPageIsProcessingUserGesture(), false);
83 }
84
toJS(ExecState * exec,JSDOMGlobalObject * globalObject,Document * document)85 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
86 {
87 if (!document)
88 return jsNull();
89
90 JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), document);
91 if (wrapper)
92 return wrapper;
93
94 if (document->isHTMLDocument())
95 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, HTMLDocument, document);
96 #if ENABLE(SVG)
97 else if (document->isSVGDocument())
98 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, SVGDocument, document);
99 #endif
100 else
101 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Document, document);
102
103 // Make sure the document is kept around by the window object, and works right with the
104 // back/forward cache.
105 if (!document->frame()) {
106 size_t nodeCount = 0;
107 for (Node* n = document; n; n = n->traverseNextNode())
108 nodeCount++;
109
110 exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
111 }
112
113 return wrapper;
114 }
115
116 #if ENABLE(TOUCH_EVENTS)
createTouchList(ExecState * exec)117 JSValue JSDocument::createTouchList(ExecState* exec)
118 {
119 RefPtr<TouchList> touchList = TouchList::create();
120
121 for (int i = 0; i < exec->argumentCount(); i++)
122 touchList->append(toTouch(exec->argument(i)));
123
124 return toJS(exec, globalObject(), touchList.release());
125 }
126 #endif
127
128 } // namespace WebCore
129