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