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 "core/fileapi/FileError.h"
33
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36
37 namespace blink {
38
39 const char FileError::abortErrorMessage[] = "An ongoing operation was aborted, typically with a call to abort().";
40 const char FileError::encodingErrorMessage[] = "A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs.";
41 const char FileError::invalidStateErrorMessage[] = "An operation that depends on state cached in an interface object was made but the state had changed since it was read from disk.";
42 const char FileError::noModificationAllowedErrorMessage[] = "An attempt was made to write to a file or directory which could not be modified due to the state of the underlying filesystem.";
43 const char FileError::notFoundErrorMessage[] = "A requested file or directory could not be found at the time an operation was processed.";
44 const char FileError::notReadableErrorMessage[] = "The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.";
45 const char FileError::pathExistsErrorMessage[] = "An attempt was made to create a file or directory where an element already exists.";
46 const char FileError::quotaExceededErrorMessage[] = "The operation failed because it would cause the application to exceed its storage quota.";
47 const char FileError::securityErrorMessage[] = "It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.";
48 const char FileError::syntaxErrorMessage[] = "An invalid or unsupported argument was given, like an invalid line ending specifier.";
49 const char FileError::typeMismatchErrorMessage[] = "The path supplied exists, but was not an entry of requested type.";
50
51 namespace {
52
errorCodeToExceptionCode(FileError::ErrorCode code)53 ExceptionCode errorCodeToExceptionCode(FileError::ErrorCode code)
54 {
55 switch (code) {
56 case FileError::OK:
57 return 0;
58 case FileError::NOT_FOUND_ERR:
59 return NotFoundError;
60 case FileError::SECURITY_ERR:
61 return SecurityError;
62 case FileError::ABORT_ERR:
63 return AbortError;
64 case FileError::NOT_READABLE_ERR:
65 return NotReadableError;
66 case FileError::ENCODING_ERR:
67 return EncodingError;
68 case FileError::NO_MODIFICATION_ALLOWED_ERR:
69 return NoModificationAllowedError;
70 case FileError::INVALID_STATE_ERR:
71 return InvalidStateError;
72 case FileError::SYNTAX_ERR:
73 return SyntaxError;
74 case FileError::INVALID_MODIFICATION_ERR:
75 return InvalidModificationError;
76 case FileError::QUOTA_EXCEEDED_ERR:
77 return QuotaExceededError;
78 case FileError::TYPE_MISMATCH_ERR:
79 return TypeMismatchError;
80 case FileError::PATH_EXISTS_ERR:
81 return PathExistsError;
82 default:
83 ASSERT_NOT_REACHED();
84 return code;
85 }
86 }
87
errorCodeToMessage(FileError::ErrorCode code)88 const char* errorCodeToMessage(FileError::ErrorCode code)
89 {
90 // Note that some of these do not set message. If message is 0 then the default message is used.
91 switch (code) {
92 case FileError::OK:
93 return 0;
94 case FileError::SECURITY_ERR:
95 return FileError::securityErrorMessage;
96 case FileError::NOT_FOUND_ERR:
97 return FileError::notFoundErrorMessage;
98 case FileError::ABORT_ERR:
99 return FileError::abortErrorMessage;
100 case FileError::NOT_READABLE_ERR:
101 return FileError::notReadableErrorMessage;
102 case FileError::ENCODING_ERR:
103 return FileError::encodingErrorMessage;
104 case FileError::NO_MODIFICATION_ALLOWED_ERR:
105 return FileError::noModificationAllowedErrorMessage;
106 case FileError::INVALID_STATE_ERR:
107 return FileError::invalidStateErrorMessage;
108 case FileError::SYNTAX_ERR:
109 return FileError::syntaxErrorMessage;
110 case FileError::INVALID_MODIFICATION_ERR:
111 return 0;
112 case FileError::QUOTA_EXCEEDED_ERR:
113 return FileError::quotaExceededErrorMessage;
114 case FileError::TYPE_MISMATCH_ERR:
115 return 0;
116 case FileError::PATH_EXISTS_ERR:
117 return FileError::pathExistsErrorMessage;
118 default:
119 ASSERT_NOT_REACHED();
120 return 0;
121 }
122 }
123
124 } // namespace
125
throwDOMException(ExceptionState & exceptionState,ErrorCode code)126 void FileError::throwDOMException(ExceptionState& exceptionState, ErrorCode code)
127 {
128 if (code == FileError::OK)
129 return;
130
131 // SecurityError is special-cased, as we want to route those exceptions through ExceptionState::throwSecurityError.
132 if (code == FileError::SECURITY_ERR) {
133 exceptionState.throwSecurityError(FileError::securityErrorMessage);
134 return;
135 }
136
137 exceptionState.throwDOMException(errorCodeToExceptionCode(code), errorCodeToMessage(code));
138 }
139
FileError(ErrorCode code)140 FileError::FileError(ErrorCode code)
141 : DOMError(DOMException::getErrorName(errorCodeToExceptionCode(code)), errorCodeToMessage(code))
142 , m_code(code)
143 {
144 }
145
146 } // namespace blink
147