• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
7  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
9  * Copyright (C) 2013 Google Inc. All rights reserved.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  *
26  */
27 
28 #include "config.h"
29 #include "core/dom/DocumentInit.h"
30 
31 #include "RuntimeEnabledFeatures.h"
32 #include "core/dom/Document.h"
33 #include "core/dom/custom/CustomElementRegistrationContext.h"
34 #include "core/html/HTMLFrameOwnerElement.h"
35 #include "core/html/HTMLImportsController.h"
36 #include "core/frame/Frame.h"
37 
38 namespace WebCore {
39 
parentDocument(Frame * frame)40 static Document* parentDocument(Frame* frame)
41 {
42     if (!frame)
43         return 0;
44     Element* ownerElement = frame->ownerElement();
45     if (!ownerElement)
46         return 0;
47     return &ownerElement->document();
48 }
49 
50 
ownerDocument(Frame * frame)51 static Document* ownerDocument(Frame* frame)
52 {
53     if (!frame)
54         return 0;
55 
56     Frame* ownerFrame = frame->tree().parent();
57     if (!ownerFrame)
58         ownerFrame = frame->loader().opener();
59     if (!ownerFrame)
60         return 0;
61     return ownerFrame->document();
62 }
63 
DocumentInit(const KURL & url,Frame * frame,WeakPtr<Document> contextDocument,HTMLImport * import)64 DocumentInit::DocumentInit(const KURL& url, Frame* frame, WeakPtr<Document> contextDocument, HTMLImport* import)
65     : m_url(url)
66     , m_frame(frame)
67     , m_parent(parentDocument(frame))
68     , m_owner(ownerDocument(frame))
69     , m_contextDocument(contextDocument)
70     , m_import(import)
71 {
72 }
73 
DocumentInit(const DocumentInit & other)74 DocumentInit::DocumentInit(const DocumentInit& other)
75     : m_url(other.m_url)
76     , m_frame(other.m_frame)
77     , m_parent(other.m_parent)
78     , m_owner(other.m_owner)
79     , m_contextDocument(other.m_contextDocument)
80     , m_import(other.m_import)
81     , m_registrationContext(other.m_registrationContext)
82 {
83 }
84 
~DocumentInit()85 DocumentInit::~DocumentInit()
86 {
87 }
88 
shouldSetURL() const89 bool DocumentInit::shouldSetURL() const
90 {
91     Frame* frame = frameForSecurityContext();
92     return (frame && frame->ownerElement()) || !m_url.isEmpty();
93 }
94 
shouldTreatURLAsSrcdocDocument() const95 bool DocumentInit::shouldTreatURLAsSrcdocDocument() const
96 {
97     return m_parent && m_frame->loader().shouldTreatURLAsSrcdocDocument(m_url);
98 }
99 
isSeamlessAllowedFor(Document * child) const100 bool DocumentInit::isSeamlessAllowedFor(Document* child) const
101 {
102     if (!m_parent)
103         return false;
104     if (m_parent->isSandboxed(SandboxSeamlessIframes))
105         return false;
106     if (child->isSrcdocDocument())
107         return true;
108     if (m_parent->securityOrigin()->canAccess(child->securityOrigin()))
109         return true;
110     return m_parent->securityOrigin()->canRequest(child->url());
111 }
112 
frameForSecurityContext() const113 Frame* DocumentInit::frameForSecurityContext() const
114 {
115     if (m_frame)
116         return m_frame;
117     if (m_import)
118         return m_import->frame();
119     return 0;
120 }
121 
sandboxFlags() const122 SandboxFlags DocumentInit::sandboxFlags() const
123 {
124     ASSERT(frameForSecurityContext());
125     return frameForSecurityContext()->loader().effectiveSandboxFlags();
126 }
127 
settings() const128 Settings* DocumentInit::settings() const
129 {
130     ASSERT(frameForSecurityContext());
131     return frameForSecurityContext()->settings();
132 }
133 
parentBaseURL() const134 KURL DocumentInit::parentBaseURL() const
135 {
136     return m_parent->baseURL();
137 }
138 
withRegistrationContext(CustomElementRegistrationContext * registrationContext)139 DocumentInit& DocumentInit::withRegistrationContext(CustomElementRegistrationContext* registrationContext)
140 {
141     ASSERT(!m_registrationContext);
142     m_registrationContext = registrationContext;
143     return *this;
144 }
145 
registrationContext(Document * document) const146 PassRefPtr<CustomElementRegistrationContext> DocumentInit::registrationContext(Document* document) const
147 {
148     if (!RuntimeEnabledFeatures::customElementsEnabled() && !RuntimeEnabledFeatures::embedderCustomElementsEnabled())
149         return 0;
150 
151     if (!document->isHTMLDocument() && !document->isXHTMLDocument())
152         return 0;
153 
154     if (m_registrationContext)
155         return m_registrationContext.get();
156 
157     return CustomElementRegistrationContext::create();
158 }
159 
contextDocument() const160 WeakPtr<Document> DocumentInit::contextDocument() const
161 {
162     return m_contextDocument;
163 }
164 
fromContext(WeakPtr<Document> contextDocument,const KURL & url)165 DocumentInit DocumentInit::fromContext(WeakPtr<Document> contextDocument, const KURL& url)
166 {
167     return DocumentInit(url, 0, contextDocument, 0);
168 }
169 
170 } // namespace WebCore
171 
172