Home | History | Annotate | Download | only in web
      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/core/v8/Dictionary.h"
     35 #include "bindings/core/v8/ExceptionState.h"
     36 #include "bindings/core/v8/ScriptState.h"
     37 #include "bindings/core/v8/ScriptValue.h"
     38 #include "core/accessibility/AXObjectCache.h"
     39 #include "core/css/StyleSheetContents.h"
     40 #include "core/dom/CSSSelectorWatch.h"
     41 #include "core/dom/Document.h"
     42 #include "core/dom/DocumentType.h"
     43 #include "core/dom/Element.h"
     44 #include "core/dom/Fullscreen.h"
     45 #include "core/dom/StyleEngine.h"
     46 #include "core/html/HTMLAllCollection.h"
     47 #include "core/html/HTMLBodyElement.h"
     48 #include "core/html/HTMLCollection.h"
     49 #include "core/html/HTMLElement.h"
     50 #include "core/html/HTMLFormElement.h"
     51 #include "core/html/HTMLHeadElement.h"
     52 #include "core/html/HTMLLinkElement.h"
     53 #include "core/loader/DocumentLoader.h"
     54 #include "core/rendering/RenderObject.h"
     55 #include "core/rendering/RenderView.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 namespace blink {
     70 
     71 WebURL WebDocument::url() const
     72 {
     73     return constUnwrap<Document>()->url();
     74 }
     75 
     76 WebSecurityOrigin WebDocument::securityOrigin() const
     77 {
     78     if (!constUnwrap<Document>())
     79         return WebSecurityOrigin();
     80     return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
     81 }
     82 
     83 WebString WebDocument::encoding() const
     84 {
     85     return constUnwrap<Document>()->encodingName();
     86 }
     87 
     88 WebString WebDocument::contentLanguage() const
     89 {
     90     return constUnwrap<Document>()->contentLanguage();
     91 }
     92 
     93 WebString WebDocument::referrer() const
     94 {
     95     return constUnwrap<Document>()->referrer();
     96 }
     97 
     98 WebColor WebDocument::themeColor() const
     99 {
    100     return constUnwrap<Document>()->themeColor().rgb();
    101 }
    102 
    103 WebURL WebDocument::openSearchDescriptionURL() const
    104 {
    105     return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
    106 }
    107 
    108 WebLocalFrame* WebDocument::frame() const
    109 {
    110     return WebLocalFrameImpl::fromFrame(constUnwrap<Document>()->frame());
    111 }
    112 
    113 bool WebDocument::isHTMLDocument() const
    114 {
    115     return constUnwrap<Document>()->isHTMLDocument();
    116 }
    117 
    118 bool WebDocument::isXHTMLDocument() const
    119 {
    120     return constUnwrap<Document>()->isXHTMLDocument();
    121 }
    122 
    123 bool WebDocument::isPluginDocument() const
    124 {
    125     return constUnwrap<Document>()->isPluginDocument();
    126 }
    127 
    128 WebURL WebDocument::baseURL() const
    129 {
    130     return constUnwrap<Document>()->baseURL();
    131 }
    132 
    133 WebURL WebDocument::firstPartyForCookies() const
    134 {
    135     return constUnwrap<Document>()->firstPartyForCookies();
    136 }
    137 
    138 WebElement WebDocument::documentElement() const
    139 {
    140     return WebElement(constUnwrap<Document>()->documentElement());
    141 }
    142 
    143 WebElement WebDocument::body() const
    144 {
    145     return WebElement(constUnwrap<Document>()->body());
    146 }
    147 
    148 WebElement WebDocument::head()
    149 {
    150     return WebElement(unwrap<Document>()->head());
    151 }
    152 
    153 WebString WebDocument::title() const
    154 {
    155     return WebString(constUnwrap<Document>()->title());
    156 }
    157 
    158 WebElementCollection WebDocument::all()
    159 {
    160     return WebElementCollection(unwrap<Document>()->all());
    161 }
    162 
    163 void WebDocument::images(WebVector<WebElement>& results)
    164 {
    165     RefPtrWillBeRawPtr<HTMLCollection> images = unwrap<Document>()->images();
    166     size_t sourceLength = images->length();
    167     Vector<WebElement> temp;
    168     temp.reserveCapacity(sourceLength);
    169     for (size_t i = 0; i < sourceLength; ++i) {
    170         Element* element = images->item(i);
    171         if (element && element->isHTMLElement())
    172             temp.append(WebElement(element));
    173     }
    174     results.assign(temp);
    175 }
    176 
    177 void WebDocument::forms(WebVector<WebFormElement>& results) const
    178 {
    179     RefPtrWillBeRawPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
    180     size_t sourceLength = forms->length();
    181     Vector<WebFormElement> temp;
    182     temp.reserveCapacity(sourceLength);
    183     for (size_t i = 0; i < sourceLength; ++i) {
    184         Element* element = forms->item(i);
    185         // Strange but true, sometimes node can be 0.
    186         if (element && element->isHTMLElement())
    187             temp.append(WebFormElement(toHTMLFormElement(element)));
    188     }
    189     results.assign(temp);
    190 }
    191 
    192 WebURL WebDocument::completeURL(const WebString& partialURL) const
    193 {
    194     return constUnwrap<Document>()->completeURL(partialURL);
    195 }
    196 
    197 WebElement WebDocument::getElementById(const WebString& id) const
    198 {
    199     return WebElement(constUnwrap<Document>()->getElementById(id));
    200 }
    201 
    202 WebElement WebDocument::focusedElement() const
    203 {
    204     return WebElement(constUnwrap<Document>()->focusedElement());
    205 }
    206 
    207 WebDocumentType WebDocument::doctype() const
    208 {
    209     return WebDocumentType(constUnwrap<Document>()->doctype());
    210 }
    211 
    212 void WebDocument::insertStyleSheet(const WebString& sourceCode)
    213 {
    214     RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
    215     ASSERT(document);
    216     RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document, 0));
    217     parsedSheet->parseString(sourceCode);
    218     document->styleEngine()->addAuthorSheet(parsedSheet);
    219 }
    220 
    221 void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
    222 {
    223     RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
    224     Vector<String> selectors;
    225     selectors.append(webSelectors.data(), webSelectors.size());
    226     CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
    227 }
    228 
    229 void WebDocument::cancelFullScreen()
    230 {
    231     Fullscreen::fullyExitFullscreen(*unwrap<Document>());
    232 }
    233 
    234 WebElement WebDocument::fullScreenElement() const
    235 {
    236     Element* fullScreenElement = 0;
    237     if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*const_cast<WebDocument*>(this)->unwrap<Document>()))
    238         fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
    239     return WebElement(fullScreenElement);
    240 }
    241 
    242 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
    243 {
    244     TrackExceptionState exceptionState;
    245     WebDOMEvent event(unwrap<Document>()->createEvent(eventType, exceptionState));
    246     if (exceptionState.hadException())
    247         return WebDOMEvent();
    248     return event;
    249 }
    250 
    251 WebReferrerPolicy WebDocument::referrerPolicy() const
    252 {
    253     return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
    254 }
    255 
    256 WebElement WebDocument::createElement(const WebString& tagName)
    257 {
    258     TrackExceptionState exceptionState;
    259     WebElement element(unwrap<Document>()->createElement(tagName, exceptionState));
    260     if (exceptionState.hadException())
    261         return WebElement();
    262     return element;
    263 }
    264 
    265 WebSize WebDocument::scrollOffset() const
    266 {
    267     if (FrameView* view = constUnwrap<Document>()->view())
    268         return view->scrollOffset();
    269     return WebSize();
    270 }
    271 
    272 WebSize WebDocument::minimumScrollOffset() const
    273 {
    274     if (FrameView* view = constUnwrap<Document>()->view())
    275         return toIntSize(view->minimumScrollPosition());
    276     return WebSize();
    277 }
    278 
    279 WebSize WebDocument::maximumScrollOffset() const
    280 {
    281     if (FrameView* view = constUnwrap<Document>()->view())
    282         return toIntSize(view->maximumScrollPosition());
    283     return WebSize();
    284 }
    285 
    286 void WebDocument::setIsTransitionDocument()
    287 {
    288     // This ensures the transition UA stylesheet gets applied.
    289     unwrap<Document>()->setIsTransitionDocument();
    290 }
    291 
    292 void WebDocument::beginExitTransition(const WebString& cssSelector)
    293 {
    294     RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
    295     document->hideTransitionElements(cssSelector);
    296     document->styleEngine()->enableExitTransitionStylesheets();
    297 }
    298 
    299 WebAXObject WebDocument::accessibilityObject() const
    300 {
    301     const Document* document = constUnwrap<Document>();
    302     AXObjectCache* cache = document->axObjectCache();
    303     return cache ? WebAXObject(cache->getOrCreate(document->renderView())) : WebAXObject();
    304 }
    305 
    306 WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
    307 {
    308     const Document* document = constUnwrap<Document>();
    309     AXObjectCache* cache = document->axObjectCache();
    310     return cache ? WebAXObject(cache->objectFromAXID(axID)) : WebAXObject();
    311 }
    312 
    313 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
    314 {
    315     WebVector<WebDraggableRegion> draggableRegions;
    316     const Document* document = constUnwrap<Document>();
    317     if (document->hasAnnotatedRegions()) {
    318         const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
    319         draggableRegions = WebVector<WebDraggableRegion>(regions.size());
    320         for (size_t i = 0; i < regions.size(); i++) {
    321             const AnnotatedRegionValue& value = regions[i];
    322             draggableRegions[i].draggable = value.draggable;
    323             draggableRegions[i].bounds = IntRect(value.bounds);
    324         }
    325     }
    326     return draggableRegions;
    327 }
    328 
    329 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
    330 {
    331     v8::Isolate* isolate = v8::Isolate::GetCurrent();
    332     Document* document = unwrap<Document>();
    333     Dictionary dictionary(options, isolate);
    334     TrackExceptionState exceptionState;
    335     ScriptValue constructor = document->registerElement(ScriptState::current(isolate), name, dictionary, exceptionState, CustomElement::EmbedderNames);
    336     ec = exceptionState.code();
    337     if (exceptionState.hadException())
    338         return v8::Handle<v8::Value>();
    339     return constructor.v8Value();
    340 }
    341 
    342 WebURL WebDocument::manifestURL() const
    343 {
    344     const Document* document = constUnwrap<Document>();
    345     HTMLLinkElement* linkElement = document->linkManifest();
    346     if (!linkElement)
    347         return WebURL();
    348     return linkElement->href();
    349 }
    350 
    351 WebDocument::WebDocument(const PassRefPtrWillBeRawPtr<Document>& elem)
    352     : WebNode(elem)
    353 {
    354 }
    355 
    356 WebDocument& WebDocument::operator=(const PassRefPtrWillBeRawPtr<Document>& elem)
    357 {
    358     m_private = elem;
    359     return *this;
    360 }
    361 
    362 WebDocument::operator PassRefPtrWillBeRawPtr<Document>() const
    363 {
    364     return toDocument(m_private.get());
    365 }
    366 
    367 } // namespace blink
    368