2936f0f3d8ac158f674268d248f5c65d5b121c7c
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebDocument.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "public/web/WebDocument.h"
33
34 #include "bindings/v8/Dictionary.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/ScriptValue.h"
38 #include "core/accessibility/AXObjectCache.h"
39 #include "core/css/CSSParserMode.h"
40 #include "core/css/StyleSheetContents.h"
41 #include "core/dom/CSSSelectorWatch.h"
42 #include "core/dom/Document.h"
43 #include "core/dom/DocumentType.h"
44 #include "core/dom/Element.h"
45 #include "core/dom/FullscreenElementStack.h"
46 #include "core/dom/NodeList.h"
47 #include "core/dom/StyleEngine.h"
48 #include "core/html/HTMLAllCollection.h"
49 #include "core/html/HTMLBodyElement.h"
50 #include "core/html/HTMLCollection.h"
51 #include "core/html/HTMLElement.h"
52 #include "core/html/HTMLFormElement.h"
53 #include "core/html/HTMLHeadElement.h"
54 #include "core/loader/DocumentLoader.h"
55 #include "core/rendering/RenderObject.h"
56 #include "platform/weborigin/SecurityOrigin.h"
57 #include "public/platform/WebURL.h"
58 #include "public/web/WebAXObject.h"
59 #include "public/web/WebDOMEvent.h"
60 #include "public/web/WebDocumentType.h"
61 #include "public/web/WebElement.h"
62 #include "public/web/WebElementCollection.h"
63 #include "public/web/WebFormElement.h"
64 #include "public/web/WebNodeList.h"
65 #include "web/WebLocalFrameImpl.h"
66 #include "wtf/PassRefPtr.h"
67 #include <v8.h>
68
69 using namespace WebCore;
70
71 namespace blink {
72
73 WebURL WebDocument::url() const
74 {
75     return constUnwrap<Document>()->url();
76 }
77
78 WebSecurityOrigin WebDocument::securityOrigin() const
79 {
80     if (!constUnwrap<Document>())
81         return WebSecurityOrigin();
82     return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
83 }
84
85 WebString WebDocument::encoding() const
86 {
87     return constUnwrap<Document>()->encodingName();
88 }
89
90 WebString WebDocument::contentLanguage() const
91 {
92     return constUnwrap<Document>()->contentLanguage();
93 }
94
95 WebString WebDocument::referrer() const
96 {
97     return constUnwrap<Document>()->referrer();
98 }
99
100 WebURL WebDocument::openSearchDescriptionURL() const
101 {
102     return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
103 }
104
105 WebLocalFrame* WebDocument::frame() const
106 {
107     return WebLocalFrameImpl::fromFrame(constUnwrap<Document>()->frame());
108 }
109
110 bool WebDocument::isHTMLDocument() const
111 {
112     return constUnwrap<Document>()->isHTMLDocument();
113 }
114
115 bool WebDocument::isXHTMLDocument() const
116 {
117     return constUnwrap<Document>()->isXHTMLDocument();
118 }
119
120 bool WebDocument::isPluginDocument() const
121 {
122     return constUnwrap<Document>()->isPluginDocument();
123 }
124
125 WebURL WebDocument::baseURL() const
126 {
127     return constUnwrap<Document>()->baseURL();
128 }
129
130 WebURL WebDocument::firstPartyForCookies() const
131 {
132     return constUnwrap<Document>()->firstPartyForCookies();
133 }
134
135 WebElement WebDocument::documentElement() const
136 {
137     return WebElement(constUnwrap<Document>()->documentElement());
138 }
139
140 WebElement WebDocument::body() const
141 {
142     return WebElement(constUnwrap<Document>()->body());
143 }
144
145 WebElement WebDocument::head()
146 {
147     return WebElement(unwrap<Document>()->head());
148 }
149
150 WebString WebDocument::title() const
151 {
152     return WebString(constUnwrap<Document>()->title());
153 }
154
155 WebElementCollection WebDocument::all()
156 {
157     return WebElementCollection(unwrap<Document>()->all());
158 }
159
160 void WebDocument::images(WebVector<WebElement>& results)
161 {
162     RefPtr<HTMLCollection> images = unwrap<Document>()->images();
163     size_t sourceLength = images->length();
164     Vector<WebElement> temp;
165     temp.reserveCapacity(sourceLength);
166     for (size_t i = 0; i < sourceLength; ++i) {
167         Element* element = images->item(i);
168         if (element && element->isHTMLElement())
169             temp.append(WebElement(element));
170     }
171     results.assign(temp);
172 }
173
174 void WebDocument::forms(WebVector<WebFormElement>& results) const
175 {
176     RefPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
177     size_t sourceLength = forms->length();
178     Vector<WebFormElement> temp;
179     temp.reserveCapacity(sourceLength);
180     for (size_t i = 0; i < sourceLength; ++i) {
181         Element* element = forms->item(i);
182         // Strange but true, sometimes node can be 0.
183         if (element && element->isHTMLElement())
184             temp.append(WebFormElement(toHTMLFormElement(element)));
185     }
186     results.assign(temp);
187 }
188
189 WebURL WebDocument::completeURL(const WebString& partialURL) const
190 {
191     return constUnwrap<Document>()->completeURL(partialURL);
192 }
193
194 WebElement WebDocument::getElementById(const WebString& id) const
195 {
196     return WebElement(constUnwrap<Document>()->getElementById(id));
197 }
198
199 WebElement WebDocument::focusedElement() const
200 {
201     return WebElement(constUnwrap<Document>()->focusedElement());
202 }
203
204 WebDocumentType WebDocument::doctype() const
205 {
206     return WebDocumentType(constUnwrap<Document>()->doctype());
207 }
208
209 void WebDocument::insertStyleSheet(const WebString& sourceCode)
210 {
211     RefPtr<Document> document = unwrap<Document>();
212     ASSERT(document);
213     RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document.get(), 0));
214     parsedSheet->parseString(sourceCode);
215     document->styleEngine()->addAuthorSheet(parsedSheet);
216 }
217
218 void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
219 {
220     RefPtr<Document> document = unwrap<Document>();
221     Vector<String> selectors;
222     selectors.append(webSelectors.data(), webSelectors.size());
223     CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
224 }
225
226 void WebDocument::cancelFullScreen()
227 {
228     if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*unwrap<Document>()))
229         fullscreen->webkitCancelFullScreen();
230 }
231
232 WebElement WebDocument::fullScreenElement() const
233 {
234     Element* fullScreenElement = 0;
235     if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*const_cast<WebDocument*>(this)->unwrap<Document>()))
236         fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
237     return WebElement(fullScreenElement);
238 }
239
240 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
241 {
242     TrackExceptionState exceptionState;
243     WebDOMEvent event(unwrap<Document>()->createEvent(eventType, exceptionState));
244     if (exceptionState.hadException())
245         return WebDOMEvent();
246     return event;
247 }
248
249 WebReferrerPolicy WebDocument::referrerPolicy() const
250 {
251     return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
252 }
253
254 WebElement WebDocument::createElement(const WebString& tagName)
255 {
256     TrackExceptionState exceptionState;
257 #if ENABLE(OILPAN)
258     // FIXME: Document::createElement should return a raw pointer.
259     WebElement element(unwrap<Document>()->createElement(tagName, exceptionState).get());
260 #else
261     WebElement element(unwrap<Document>()->createElement(tagName, exceptionState));
262 #endif
263     if (exceptionState.hadException())
264         return WebElement();
265     return element;
266 }
267
268 WebAXObject WebDocument::accessibilityObject() const
269 {
270     const Document* document = constUnwrap<Document>();
271     return WebAXObject(document->axObjectCache()->getOrCreate(document->renderer()));
272 }
273
274 WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
275 {
276     const Document* document = constUnwrap<Document>();
277     return WebAXObject(document->axObjectCache()->objectFromAXID(axID));
278 }
279
280 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
281 {
282     WebVector<WebDraggableRegion> draggableRegions;
283     const Document* document = constUnwrap<Document>();
284     if (document->hasAnnotatedRegions()) {
285         const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
286         draggableRegions = WebVector<WebDraggableRegion>(regions.size());
287         for (size_t i = 0; i < regions.size(); i++) {
288             const AnnotatedRegionValue& value = regions[i];
289             draggableRegions[i].draggable = value.draggable;
290             draggableRegions[i].bounds = WebCore::IntRect(value.bounds);
291         }
292     }
293     return draggableRegions;
294 }
295
296 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
297 {
298     v8::Isolate* isolate = v8::Isolate::GetCurrent();
299     Document* document = unwrap<Document>();
300     Dictionary dictionary(options, isolate);
301     TrackExceptionState exceptionState;
302     ScriptValue constructor = document->registerElement(ScriptState::current(isolate), name, dictionary, exceptionState, CustomElement::EmbedderNames);
303     ec = exceptionState.code();
304     if (exceptionState.hadException())
305         return v8::Handle<v8::Value>();
306     return constructor.v8Value();
307 }
308
309 WebDocument::WebDocument(const PassRefPtrWillBeRawPtr<Document>& elem)
310     : WebNode(elem)
311 {
312 }
313
314 WebDocument& WebDocument::operator=(const PassRefPtrWillBeRawPtr<Document>& elem)
315 {
316     m_private = elem;
317     return *this;
318 }
319
320 WebDocument::operator PassRefPtrWillBeRawPtr<Document>() const
321 {
322     return toDocument(m_private.get());
323 }
324
325 } // namespace blink