1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * (C) 2006 Alexey Proskuryakov (ap (at) 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 "core/dom/Document.h" 32 #include "core/dom/custom/CustomElementRegistrationContext.h" 33 #include "core/frame/LocalFrame.h" 34 #include "core/html/HTMLFrameOwnerElement.h" 35 #include "core/html/imports/HTMLImportsController.h" 36 #include "platform/RuntimeEnabledFeatures.h" 37 38 namespace WebCore { 39 40 // FIXME: Broken with OOPI. 41 static Document* parentDocument(LocalFrame* frame) 42 { 43 if (!frame) 44 return 0; 45 Element* ownerElement = frame->deprecatedLocalOwner(); 46 if (!ownerElement) 47 return 0; 48 return &ownerElement->document(); 49 } 50 51 52 static Document* ownerDocument(LocalFrame* frame) 53 { 54 if (!frame) 55 return 0; 56 57 Frame* ownerFrame = frame->tree().parent(); 58 if (!ownerFrame) 59 ownerFrame = frame->loader().opener(); 60 if (!ownerFrame || !ownerFrame->isLocalFrame()) 61 return 0; 62 return toLocalFrame(ownerFrame)->document(); 63 } 64 65 DocumentInit::DocumentInit(const KURL& url, LocalFrame* frame, WeakPtrWillBeRawPtr<Document> contextDocument, HTMLImportsController* importsController) 66 : m_url(url) 67 , m_frame(frame) 68 , m_parent(parentDocument(frame)) 69 , m_owner(ownerDocument(frame)) 70 , m_contextDocument(contextDocument) 71 , m_importsController(importsController) 72 , m_createNewRegistrationContext(false) 73 { 74 } 75 76 DocumentInit::DocumentInit(const DocumentInit& other) 77 : m_url(other.m_url) 78 , m_frame(other.m_frame) 79 , m_parent(other.m_parent) 80 , m_owner(other.m_owner) 81 , m_contextDocument(other.m_contextDocument) 82 , m_importsController(other.m_importsController) 83 , m_registrationContext(other.m_registrationContext) 84 , m_createNewRegistrationContext(other.m_createNewRegistrationContext) 85 { 86 } 87 88 DocumentInit::~DocumentInit() 89 { 90 } 91 92 bool DocumentInit::shouldSetURL() const 93 { 94 LocalFrame* frame = frameForSecurityContext(); 95 return (frame && frame->owner()) || !m_url.isEmpty(); 96 } 97 98 bool DocumentInit::shouldTreatURLAsSrcdocDocument() const 99 { 100 return m_parent && m_frame->loader().shouldTreatURLAsSrcdocDocument(m_url); 101 } 102 103 LocalFrame* DocumentInit::frameForSecurityContext() const 104 { 105 if (m_frame) 106 return m_frame; 107 if (m_importsController) 108 return m_importsController->master()->frame(); 109 return 0; 110 } 111 112 SandboxFlags DocumentInit::sandboxFlags() const 113 { 114 ASSERT(frameForSecurityContext()); 115 return frameForSecurityContext()->loader().effectiveSandboxFlags(); 116 } 117 118 Settings* DocumentInit::settings() const 119 { 120 ASSERT(frameForSecurityContext()); 121 return frameForSecurityContext()->settings(); 122 } 123 124 KURL DocumentInit::parentBaseURL() const 125 { 126 return m_parent->baseURL(); 127 } 128 129 DocumentInit& DocumentInit::withRegistrationContext(CustomElementRegistrationContext* registrationContext) 130 { 131 ASSERT(!m_createNewRegistrationContext && !m_registrationContext); 132 m_registrationContext = registrationContext; 133 return *this; 134 } 135 136 DocumentInit& DocumentInit::withNewRegistrationContext() 137 { 138 ASSERT(!m_createNewRegistrationContext && !m_registrationContext); 139 m_createNewRegistrationContext = true; 140 return *this; 141 } 142 143 PassRefPtrWillBeRawPtr<CustomElementRegistrationContext> DocumentInit::registrationContext(Document* document) const 144 { 145 if (!document->isHTMLDocument() && !document->isXHTMLDocument()) 146 return nullptr; 147 148 if (m_createNewRegistrationContext) 149 return CustomElementRegistrationContext::create(); 150 151 return m_registrationContext.get(); 152 } 153 154 WeakPtrWillBeRawPtr<Document> DocumentInit::contextDocument() const 155 { 156 return m_contextDocument; 157 } 158 159 DocumentInit DocumentInit::fromContext(WeakPtrWillBeRawPtr<Document> contextDocument, const KURL& url) 160 { 161 return DocumentInit(url, 0, contextDocument, 0); 162 } 163 164 } // namespace WebCore 165 166