OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / bindings / js / JSDocumentCustom.cpp
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(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
51 void JSDocument::markChildren(MarkStack& markStack)
52 {
53     JSNode::markChildren(markStack);
54
55     Document* document = impl();
56     JSGlobalData& globalData = *Heap::heap(this)->globalData();
57
58     markDOMNodesForDocument(markStack, document);
59     markActiveObjectsForContext(markStack, globalData, document);
60     markDOMObjectWrapper(markStack, globalData, document->implementation());
61     markDOMObjectWrapper(markStack, globalData, document->styleSheets());
62     document->markCachedNodeLists(markStack, globalData);
63 }
64
65 JSValue JSDocument::location(ExecState* exec) const
66 {
67     Frame* frame = static_cast<Document*>(impl())->frame();
68     if (!frame)
69         return jsNull();
70
71     Location* location = frame->domWindow()->location();
72     if (DOMObject* wrapper = getCachedDOMObjectWrapper(exec, location))
73         return wrapper;
74
75     JSLocation* jsLocation = new (exec) JSLocation(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location);
76     cacheDOMObjectWrapper(exec, location, jsLocation);
77     return jsLocation;
78 }
79
80 void JSDocument::setLocation(ExecState* exec, JSValue value)
81 {
82     Frame* frame = static_cast<Document*>(impl())->frame();
83     if (!frame)
84         return;
85
86     String str = ustringToString(value.toString(exec));
87
88     Frame* lexicalFrame = asJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame();
89
90     // IE and Mozilla both resolve the URL relative to the source frame,
91     // not the target frame.
92     Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
93     str = activeFrame->document()->completeURL(str).string();
94
95     frame->navigationScheduler()->scheduleLocationChange(lexicalFrame->document()->securityOrigin(),
96         str, activeFrame->loader()->outgoingReferrer(), !activeFrame->script()->anyPageIsProcessingUserGesture(), false);
97 }
98
99 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
100 {
101     if (!document)
102         return jsNull();
103
104     DOMObject* wrapper = getCachedDOMNodeWrapper(exec, document, document);
105     if (wrapper)
106         return wrapper;
107
108     if (document->isHTMLDocument())
109         wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, HTMLDocument, document);
110 #if ENABLE(SVG)
111     else if (document->isSVGDocument())
112         wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, SVGDocument, document);
113 #endif
114     else
115         wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Document, document);
116
117     // Make sure the document is kept around by the window object, and works right with the
118     // back/forward cache.
119     if (!document->frame()) {
120         size_t nodeCount = 0;
121         for (Node* n = document; n; n = n->traverseNextNode())
122             nodeCount++;
123         
124         exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
125     }
126
127     return wrapper;
128 }
129
130 #if ENABLE(TOUCH_EVENTS)
131 JSValue JSDocument::createTouchList(ExecState* exec)
132 {
133     RefPtr<TouchList> touchList = TouchList::create();
134
135     for (int i = 0; i < exec->argumentCount(); i++)
136         touchList->append(toTouch(exec->argument(i)));
137
138     return toJS(exec, touchList.release());
139 }
140 #endif
141
142 } // namespace WebCore