initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSDocumentCustom.cpp
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
51 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 = JSLocation::create(getDOMStructure<JSLocation>(exec, globalObject()), globalObject(), location);
62     cacheWrapper(currentWorld(exec), location, jsLocation);
63     return jsLocation;
64 }
65
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     bool lockHistory = !ScriptController::processingUserGesture();
82     frame->navigationScheduler()->scheduleLocationChange(lexicalFrame->document()->securityOrigin(),
83         str, activeFrame->loader()->outgoingReferrer(), lockHistory, false);
84 }
85
86 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Document* document)
87 {
88     if (!document)
89         return jsNull();
90
91     JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), document);
92     if (wrapper)
93         return wrapper;
94
95     if (document->isHTMLDocument())
96         wrapper = CREATE_DOM_WRAPPER(exec, globalObject, HTMLDocument, document);
97 #if ENABLE(SVG)
98     else if (document->isSVGDocument())
99         wrapper = CREATE_DOM_WRAPPER(exec, globalObject, SVGDocument, document);
100 #endif
101     else
102         wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Document, document);
103
104     // Make sure the document is kept around by the window object, and works right with the
105     // back/forward cache.
106     if (!document->frame()) {
107         size_t nodeCount = 0;
108         for (Node* n = document; n; n = n->traverseNextNode())
109             nodeCount++;
110         
111         exec->heap()->reportExtraMemoryCost(nodeCount * sizeof(Node));
112     }
113
114     return wrapper;
115 }
116
117 #if ENABLE(TOUCH_EVENTS)
118 JSValue JSDocument::createTouchList(ExecState* exec)
119 {
120     RefPtr<TouchList> touchList = TouchList::create();
121
122     for (int i = 0; i < exec->argumentCount(); i++)
123         touchList->append(toTouch(exec->argument(i)));
124
125     return toJS(exec, globalObject(), touchList.release());
126 }
127 #endif
128
129 } // namespace WebCore