• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "JSDOMWindowCustom.h"
28 #include "JSHTMLDocument.h"
29 #include "JSLocation.h"
30 #include "Location.h"
31 #include "ScriptController.h"
32 
33 #if ENABLE(SVG)
34 #include "JSSVGDocument.h"
35 #include "SVGDocument.h"
36 #endif
37 
38 using namespace JSC;
39 
40 namespace WebCore {
41 
markChildren(MarkStack & markStack)42 void JSDocument::markChildren(MarkStack& markStack)
43 {
44     JSNode::markChildren(markStack);
45     markDOMNodesForDocument(markStack, impl());
46     markActiveObjectsForContext(markStack, *Heap::heap(this)->globalData(), impl());
47 }
48 
location(ExecState * exec) const49 JSValue JSDocument::location(ExecState* exec) const
50 {
51     Frame* frame = static_cast<Document*>(impl())->frame();
52     if (!frame)
53         return jsNull();
54 
55     Location* location = frame->domWindow()->location();
56     if (DOMObject* wrapper = getCachedDOMObjectWrapper(exec->globalData(), location))
57         return wrapper;
58 
59     JSLocation* jsLocation = new (exec) JSLocation(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location);
60     cacheDOMObjectWrapper(exec->globalData(), location, jsLocation);
61     return jsLocation;
62 }
63 
setLocation(ExecState * exec,JSValue value)64 void JSDocument::setLocation(ExecState* exec, JSValue value)
65 {
66     Frame* frame = static_cast<Document*>(impl())->frame();
67     if (!frame)
68         return;
69 
70     String str = value.toString(exec);
71 
72     // IE and Mozilla both resolve the URL relative to the source frame,
73     // not the target frame.
74     Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
75     if (activeFrame)
76         str = activeFrame->document()->completeURL(str).string();
77 
78     bool userGesture = activeFrame->script()->processingUserGesture();
79     frame->loader()->scheduleLocationChange(str, activeFrame->loader()->outgoingReferrer(), !activeFrame->script()->anyPageIsProcessingUserGesture(), false, userGesture);
80 }
81 
toJS(ExecState * exec,JSDOMGlobalObject * globalObject,Document * document)82 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
83 {
84     if (!document)
85         return jsNull();
86 
87     DOMObject* wrapper = getCachedDOMObjectWrapper(exec->globalData(), document);
88     if (wrapper)
89         return wrapper;
90 
91     if (document->isHTMLDocument())
92         wrapper = CREATE_DOM_OBJECT_WRAPPER(exec, globalObject, HTMLDocument, document);
93 #if ENABLE(SVG)
94     else if (document->isSVGDocument())
95         wrapper = CREATE_DOM_OBJECT_WRAPPER(exec, globalObject, SVGDocument, document);
96 #endif
97     else
98         wrapper = CREATE_DOM_OBJECT_WRAPPER(exec, globalObject, Document, document);
99 
100     // Make sure the document is kept around by the window object, and works right with the
101     // back/forward cache.
102     if (!document->frame()) {
103         size_t nodeCount = 0;
104         for (Node* n = document; n; n = n->traverseNextNode())
105             nodeCount++;
106 
107         exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
108     }
109 
110     return wrapper;
111 }
112 
113 } // namespace WebCore
114