Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / DOMSecurityPolicy.cpp
1 /*
2  * Copyright (C) 2012 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "DOMSecurityPolicy.h"
28
29 #include "ContentSecurityPolicy.h"
30 #include "ContextDestructionObserver.h"
31 #include "DOMStringList.h"
32 #include "Frame.h"
33 #include "ScriptExecutionContext.h"
34 #include <wtf/text/TextPosition.h>
35 #include <wtf/text/WTFString.h>
36
37 namespace WebCore {
38
39 namespace {
40
41 bool isPolicyActiveInContext(ScriptExecutionContext* context)
42 {
43     // If the ScriptExecutionContext has been destroyed, there's no active policy.
44     if (!context)
45         return false;
46
47     return context->contentSecurityPolicy()->isActive();
48 }
49
50 template<bool (ContentSecurityPolicy::*allowWithURL)(const KURL&, ContentSecurityPolicy::ReportingStatus) const>
51 bool isAllowedWithURL(ScriptExecutionContext* context, const String& url)
52 {
53     if (!isPolicyActiveInContext(context))
54         return true;
55
56     KURL parsedURL = context->completeURL(url);
57     if (!parsedURL.isValid())
58         return false; // FIXME: Figure out how to throw a JavaScript error.
59
60     return (context->contentSecurityPolicy()->*allowWithURL)(parsedURL, ContentSecurityPolicy::SuppressReport);
61 }
62
63 template<bool (ContentSecurityPolicy::*allowWithContext)(const String&, const WTF::OrdinalNumber&, ContentSecurityPolicy::ReportingStatus) const>
64 bool isAllowed(ScriptExecutionContext* context)
65 {
66     if (!isPolicyActiveInContext(context))
67         return true;
68
69     return (context->contentSecurityPolicy()->*allowWithContext)(KURL(), WTF::OrdinalNumber::beforeFirst(), ContentSecurityPolicy::SuppressReport);
70 }
71
72 } // namespace
73
74 DOMSecurityPolicy::DOMSecurityPolicy(ScriptExecutionContext* context)
75     : ContextDestructionObserver(context)
76 {
77 }
78
79 DOMSecurityPolicy::~DOMSecurityPolicy()
80 {
81 }
82
83 bool DOMSecurityPolicy::isActive() const
84 {
85     return isPolicyActiveInContext(scriptExecutionContext());
86 }
87
88 PassRefPtr<DOMStringList> DOMSecurityPolicy::reportURIs() const
89 {
90     RefPtr<DOMStringList> result = DOMStringList::create();
91
92     if (isActive())
93         scriptExecutionContext()->contentSecurityPolicy()->gatherReportURIs(*result.get());
94
95     return result.release();
96 }
97
98 bool DOMSecurityPolicy::allowsInlineScript() const
99 {
100     return isAllowed<&ContentSecurityPolicy::allowInlineScript>(scriptExecutionContext());
101 }
102
103 bool DOMSecurityPolicy::allowsInlineStyle() const
104 {
105     return isAllowed<&ContentSecurityPolicy::allowInlineStyle>(scriptExecutionContext());
106 }
107
108 bool DOMSecurityPolicy::allowsEval() const
109 {
110     if (!isActive())
111         return true;
112
113     return scriptExecutionContext()->contentSecurityPolicy()->allowEval(0, ContentSecurityPolicy::SuppressReport);
114 }
115
116
117 bool DOMSecurityPolicy::allowsConnectionTo(const String& url) const
118 {
119     return isAllowedWithURL<&ContentSecurityPolicy::allowConnectToSource>(scriptExecutionContext(), url);
120 }
121
122 bool DOMSecurityPolicy::allowsFontFrom(const String& url) const
123 {
124     return isAllowedWithURL<&ContentSecurityPolicy::allowFontFromSource>(scriptExecutionContext(), url);
125 }
126
127 bool DOMSecurityPolicy::allowsFrameFrom(const String& url) const
128 {
129     return isAllowedWithURL<&ContentSecurityPolicy::allowChildFrameFromSource>(scriptExecutionContext(), url);
130 }
131
132 bool DOMSecurityPolicy::allowsImageFrom(const String& url) const
133 {
134     return isAllowedWithURL<&ContentSecurityPolicy::allowImageFromSource>(scriptExecutionContext(), url);
135 }
136
137 bool DOMSecurityPolicy::allowsMediaFrom(const String& url) const
138 {
139     return isAllowedWithURL<&ContentSecurityPolicy::allowMediaFromSource>(scriptExecutionContext(), url);
140 }
141
142 bool DOMSecurityPolicy::allowsObjectFrom(const String& url) const
143 {
144     return isAllowedWithURL<&ContentSecurityPolicy::allowObjectFromSource>(scriptExecutionContext(), url);
145 }
146
147 bool DOMSecurityPolicy::allowsScriptFrom(const String& url) const
148 {
149     return isAllowedWithURL<&ContentSecurityPolicy::allowScriptFromSource>(scriptExecutionContext(), url);
150 }
151
152 bool DOMSecurityPolicy::allowsStyleFrom(const String& url) const
153 {
154     return isAllowedWithURL<&ContentSecurityPolicy::allowStyleFromSource>(scriptExecutionContext(), url);
155 }
156
157 } // namespace WebCore