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 "WebDocument.h"
33
34 #include "Document.h"
35 #include "DocumentLoader.h"
36 #include "Element.h"
37 #include "HTMLAllCollection.h"
38 #include "HTMLBodyElement.h"
39 #include "HTMLCollection.h"
40 #include "HTMLElement.h"
41 #include "HTMLHeadElement.h"
42 #include "NodeList.h"
43
44 #include "WebElement.h"
45 #include "WebFrameImpl.h"
46 #include "WebNodeCollection.h"
47 #include "WebURL.h"
48
49 #include <wtf/PassRefPtr.h>
50
51 using namespace WebCore;
52
53 namespace WebKit {
54
WebDocument(const PassRefPtr<Document> & elem)55 WebDocument::WebDocument(const PassRefPtr<Document>& elem)
56 : WebNode(elem.releaseRef())
57 {
58 }
59
operator =(const PassRefPtr<Document> & elem)60 WebDocument& WebDocument::operator=(const PassRefPtr<Document>& elem)
61 {
62 WebNode::assign(elem.releaseRef());
63 return *this;
64 }
65
operator PassRefPtr<Document>() const66 WebDocument::operator PassRefPtr<Document>() const
67 {
68 return PassRefPtr<Document>(static_cast<Document*>(m_private));
69 }
70
frame() const71 WebFrame* WebDocument::frame() const
72 {
73 return WebFrameImpl::fromFrame(constUnwrap<Document>()->frame());
74 }
75
isHTMLDocument() const76 bool WebDocument::isHTMLDocument() const
77 {
78 return constUnwrap<Document>()->isHTMLDocument();
79 }
80
baseURL() const81 WebURL WebDocument::baseURL() const
82 {
83 return constUnwrap<Document>()->baseURL();
84 }
85
documentElement() const86 WebElement WebDocument::documentElement() const
87 {
88 return WebElement(constUnwrap<Document>()->documentElement());
89 }
90
body() const91 WebElement WebDocument::body() const
92 {
93 return WebElement(constUnwrap<Document>()->body());
94 }
95
head()96 WebElement WebDocument::head()
97 {
98 return WebElement(unwrap<Document>()->head());
99 }
100
all()101 WebNodeCollection WebDocument::all()
102 {
103 return WebNodeCollection(unwrap<Document>()->all());
104 }
105
completeURL(const WebString & partialURL) const106 WebURL WebDocument::completeURL(const WebString& partialURL) const
107 {
108 return constUnwrap<Document>()->completeURL(partialURL);
109 }
110
getElementById(const WebString & id) const111 WebElement WebDocument::getElementById(const WebString& id) const
112 {
113 return WebElement(constUnwrap<Document>()->getElementById(id));
114 }
115
applicationID() const116 WebString WebDocument::applicationID() const
117 {
118 const char* kChromeApplicationHeader = "x-chrome-application";
119
120 // First check if the document's response included a header indicating the
121 // application it should go with.
122 const Document* document = constUnwrap<Document>();
123 Frame* frame = document->frame();
124 if (!frame)
125 return WebString();
126
127 DocumentLoader* loader = frame->loader()->documentLoader();
128 if (!loader)
129 return WebString();
130
131 WebString headerValue =
132 loader->response().httpHeaderField(kChromeApplicationHeader);
133 if (!headerValue.isEmpty())
134 return headerValue;
135
136 // Otherwise, fall back to looking for the meta tag.
137 RefPtr<NodeList> metaTags =
138 const_cast<Document*>(document)->getElementsByTagName("meta");
139 for (unsigned i = 0; i < metaTags->length(); ++i) {
140 Element* element = static_cast<Element*>(metaTags->item(i));
141 if (element->getAttribute("http-equiv").lower() ==
142 kChromeApplicationHeader) {
143 return element->getAttribute("value");
144 }
145 }
146
147 return WebString();
148 }
149
150 } // namespace WebKit
151