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 "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
38namespace blink {
39
40// FIXME: Broken with OOPI.
41static 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
52static 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
65DocumentInit::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    , m_shouldReuseDefaultView(frame && frame->shouldReuseDefaultView(url))
74{
75}
76
77DocumentInit::DocumentInit(const DocumentInit& other)
78    : m_url(other.m_url)
79    , m_frame(other.m_frame)
80    , m_parent(other.m_parent)
81    , m_owner(other.m_owner)
82    , m_contextDocument(other.m_contextDocument)
83    , m_importsController(other.m_importsController)
84    , m_registrationContext(other.m_registrationContext)
85    , m_createNewRegistrationContext(other.m_createNewRegistrationContext)
86    , m_shouldReuseDefaultView(other.m_shouldReuseDefaultView)
87{
88}
89
90DocumentInit::~DocumentInit()
91{
92}
93
94bool DocumentInit::shouldSetURL() const
95{
96    LocalFrame* frame = frameForSecurityContext();
97    return (frame && frame->owner()) || !m_url.isEmpty();
98}
99
100bool DocumentInit::shouldTreatURLAsSrcdocDocument() const
101{
102    return m_parent && m_frame->loader().shouldTreatURLAsSrcdocDocument(m_url);
103}
104
105LocalFrame* DocumentInit::frameForSecurityContext() const
106{
107    if (m_frame)
108        return m_frame;
109    if (m_importsController)
110        return m_importsController->master()->frame();
111    return 0;
112}
113
114SandboxFlags DocumentInit::sandboxFlags() const
115{
116    ASSERT(frameForSecurityContext());
117    return frameForSecurityContext()->loader().effectiveSandboxFlags();
118}
119
120Settings* DocumentInit::settings() const
121{
122    ASSERT(frameForSecurityContext());
123    return frameForSecurityContext()->settings();
124}
125
126KURL DocumentInit::parentBaseURL() const
127{
128    return m_parent->baseURL();
129}
130
131DocumentInit& DocumentInit::withRegistrationContext(CustomElementRegistrationContext* registrationContext)
132{
133    ASSERT(!m_createNewRegistrationContext && !m_registrationContext);
134    m_registrationContext = registrationContext;
135    return *this;
136}
137
138DocumentInit& DocumentInit::withNewRegistrationContext()
139{
140    ASSERT(!m_createNewRegistrationContext && !m_registrationContext);
141    m_createNewRegistrationContext = true;
142    return *this;
143}
144
145PassRefPtrWillBeRawPtr<CustomElementRegistrationContext> DocumentInit::registrationContext(Document* document) const
146{
147    if (!document->isHTMLDocument() && !document->isXHTMLDocument())
148        return nullptr;
149
150    if (m_createNewRegistrationContext)
151        return CustomElementRegistrationContext::create();
152
153    return m_registrationContext.get();
154}
155
156WeakPtrWillBeRawPtr<Document> DocumentInit::contextDocument() const
157{
158    return m_contextDocument;
159}
160
161DocumentInit DocumentInit::fromContext(WeakPtrWillBeRawPtr<Document> contextDocument, const KURL& url)
162{
163    return DocumentInit(url, 0, contextDocument, 0);
164}
165
166} // namespace blink
167
168