• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 "bindings/core/v8/ExceptionState.h"
33 
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/V8ThrowException.h"
36 #include "core/dom/ExceptionCode.h"
37 
38 namespace blink {
39 
clearException()40 void ExceptionState::clearException()
41 {
42     m_code = 0;
43     m_exception.clear();
44 }
45 
reject(ScriptState * scriptState)46 ScriptPromise ExceptionState::reject(ScriptState* scriptState)
47 {
48     ScriptPromise promise = ScriptPromise::reject(scriptState, m_exception.newLocal(scriptState->isolate()));
49     clearException();
50     return promise;
51 }
52 
throwDOMException(const ExceptionCode & ec,const String & message)53 void ExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
54 {
55     ASSERT(ec);
56     ASSERT(m_isolate);
57     ASSERT(!m_creationContext.IsEmpty());
58 
59     // SecurityError is thrown via ::throwSecurityError, and _careful_ consideration must be given to the data exposed to JavaScript via the 'sanitizedMessage'.
60     ASSERT(ec != SecurityError);
61 
62     m_code = ec;
63     String processedMessage = addExceptionContext(message);
64     m_message = processedMessage;
65     setException(V8ThrowException::createDOMException(ec, processedMessage, m_creationContext, m_isolate));
66 }
67 
throwSecurityError(const String & sanitizedMessage,const String & unsanitizedMessage)68 void ExceptionState::throwSecurityError(const String& sanitizedMessage, const String& unsanitizedMessage)
69 {
70     ASSERT(m_isolate);
71     ASSERT(!m_creationContext.IsEmpty());
72     m_code = SecurityError;
73     String finalSanitized = addExceptionContext(sanitizedMessage);
74     m_message = finalSanitized;
75     String finalUnsanitized = addExceptionContext(unsanitizedMessage);
76 
77     setException(V8ThrowException::createDOMException(SecurityError, finalSanitized, finalUnsanitized, m_creationContext, m_isolate));
78 }
79 
setException(v8::Handle<v8::Value> exception)80 void ExceptionState::setException(v8::Handle<v8::Value> exception)
81 {
82     // FIXME: Assert that exception is not empty?
83     if (exception.IsEmpty()) {
84         clearException();
85         return;
86     }
87 
88     m_exception.set(m_isolate, exception);
89 }
90 
throwException()91 void ExceptionState::throwException()
92 {
93     ASSERT(!m_exception.isEmpty());
94     V8ThrowException::throwException(m_exception.newLocal(m_isolate), m_isolate);
95 }
96 
throwTypeError(const String & message)97 void ExceptionState::throwTypeError(const String& message)
98 {
99     ASSERT(m_isolate);
100     m_code = V8TypeError;
101     m_message = message;
102     setException(V8ThrowException::createTypeError(addExceptionContext(message), m_isolate));
103 }
104 
throwRangeError(const String & message)105 void ExceptionState::throwRangeError(const String& message)
106 {
107     ASSERT(m_isolate);
108     m_code = V8RangeError;
109     m_message = message;
110     setException(V8ThrowException::createRangeError(addExceptionContext(message), m_isolate));
111 }
112 
throwDOMException(const ExceptionCode & ec,const String & message)113 void NonThrowableExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
114 {
115     ASSERT_NOT_REACHED();
116     m_code = ec;
117     m_message = message;
118 }
119 
throwTypeError(const String & message)120 void NonThrowableExceptionState::throwTypeError(const String& message)
121 {
122     ASSERT_NOT_REACHED();
123     m_code = V8TypeError;
124     m_message = message;
125 }
126 
throwSecurityError(const String & sanitizedMessage,const String &)127 void NonThrowableExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
128 {
129     ASSERT_NOT_REACHED();
130     m_code = SecurityError;
131     m_message = sanitizedMessage;
132 }
133 
throwRangeError(const String & message)134 void NonThrowableExceptionState::throwRangeError(const String& message)
135 {
136     ASSERT_NOT_REACHED();
137     m_code = V8RangeError;
138     m_message = message;
139 }
140 
throwDOMException(const ExceptionCode & ec,const String & message)141 void TrackExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
142 {
143     m_code = ec;
144     m_message = message;
145 }
146 
throwTypeError(const String & message)147 void TrackExceptionState::throwTypeError(const String& message)
148 {
149     m_code = V8TypeError;
150     m_message = message;
151 }
152 
throwSecurityError(const String & sanitizedMessage,const String &)153 void TrackExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
154 {
155     m_code = SecurityError;
156     m_message = sanitizedMessage;
157 }
158 
throwRangeError(const String & message)159 void TrackExceptionState::throwRangeError(const String& message)
160 {
161     m_code = V8RangeError;
162     m_message = message;
163 }
164 
addExceptionContext(const String & message) const165 String ExceptionState::addExceptionContext(const String& message) const
166 {
167     if (message.isEmpty())
168         return message;
169 
170     String processedMessage = message;
171     if (propertyName() && interfaceName() && m_context != UnknownContext) {
172         if (m_context == DeletionContext)
173             processedMessage = ExceptionMessages::failedToDelete(propertyName(), interfaceName(), message);
174         else if (m_context == ExecutionContext)
175             processedMessage = ExceptionMessages::failedToExecute(propertyName(), interfaceName(), message);
176         else if (m_context == GetterContext)
177             processedMessage = ExceptionMessages::failedToGet(propertyName(), interfaceName(), message);
178         else if (m_context == SetterContext)
179             processedMessage = ExceptionMessages::failedToSet(propertyName(), interfaceName(), message);
180     } else if (!propertyName() && interfaceName()) {
181         if (m_context == ConstructionContext)
182             processedMessage = ExceptionMessages::failedToConstruct(interfaceName(), message);
183         else if (m_context == EnumerationContext)
184             processedMessage = ExceptionMessages::failedToEnumerate(interfaceName(), message);
185         else if (m_context == IndexedDeletionContext)
186             processedMessage = ExceptionMessages::failedToDeleteIndexed(interfaceName(), message);
187         else if (m_context == IndexedGetterContext)
188             processedMessage = ExceptionMessages::failedToGetIndexed(interfaceName(), message);
189         else if (m_context == IndexedSetterContext)
190             processedMessage = ExceptionMessages::failedToSetIndexed(interfaceName(), message);
191     }
192     return processedMessage;
193 }
194 
195 } // namespace blink
196