| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "modules/serviceworkers/ServiceWorkerError.h" | 31 #include "modules/serviceworkers/ServiceWorkerError.h" |
| 32 | 32 |
| 33 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 34 #include "bindings/core/v8/ToV8.h" |
| 33 #include "core/dom/DOMException.h" | 35 #include "core/dom/DOMException.h" |
| 34 #include "core/dom/ExceptionCode.h" | 36 #include "core/dom/ExceptionCode.h" |
| 35 | 37 |
| 36 using blink::WebServiceWorkerError; | 38 using blink::WebServiceWorkerError; |
| 37 | 39 |
| 38 namespace blink { | 40 namespace blink { |
| 39 | 41 |
| 40 static DOMException* createException(ExceptionCode code, const String& defaultMe
ssage, const String& message) | 42 namespace { |
| 43 |
| 44 struct ExceptionParams { |
| 45 ExceptionParams(ExceptionCode code, const String& defaultMessage = String(),
const String& message = String()) |
| 46 : code(code), message(message.isEmpty() ? defaultMessage : message) {} |
| 47 |
| 48 ExceptionCode code; |
| 49 String message; |
| 50 }; |
| 51 |
| 52 ExceptionParams getExceptionParams(const WebServiceWorkerError& webError) |
| 41 { | 53 { |
| 42 return DOMException::create(code, message.isEmpty() ? defaultMessage : messa
ge); | 54 switch (webError.errorType) { |
| 55 case WebServiceWorkerError::ErrorTypeAbort: |
| 56 return ExceptionParams(AbortError, "The Service Worker operation was abo
rted.", webError.message); |
| 57 case WebServiceWorkerError::ErrorTypeActivate: |
| 58 // Not currently returned as a promise rejection. |
| 59 // TODO: Introduce new ActivateError type to ExceptionCodes? |
| 60 return ExceptionParams(AbortError, "The Service Worker activation failed
.", webError.message); |
| 61 case WebServiceWorkerError::ErrorTypeDisabled: |
| 62 return ExceptionParams(NotSupportedError, "Service Worker support is dis
abled.", webError.message); |
| 63 case WebServiceWorkerError::ErrorTypeInstall: |
| 64 // TODO: Introduce new InstallError type to ExceptionCodes? |
| 65 return ExceptionParams(AbortError, "The Service Worker installation fail
ed.", webError.message); |
| 66 case WebServiceWorkerError::ErrorTypeScriptEvaluateFailed: |
| 67 return ExceptionParams(AbortError, "The Service Worker script failed to
evaluate.", webError.message); |
| 68 case WebServiceWorkerError::ErrorTypeNavigation: |
| 69 // ErrorTypeNavigation should have bailed out before calling this. |
| 70 ASSERT_NOT_REACHED(); |
| 71 return ExceptionParams(UnknownError); |
| 72 case WebServiceWorkerError::ErrorTypeNetwork: |
| 73 return ExceptionParams(NetworkError, "The Service Worker failed by netwo
rk.", webError.message); |
| 74 case WebServiceWorkerError::ErrorTypeNotFound: |
| 75 return ExceptionParams(NotFoundError, "The specified Service Worker reso
urce was not found.", webError.message); |
| 76 case WebServiceWorkerError::ErrorTypeSecurity: |
| 77 return ExceptionParams(SecurityError, "The Service Worker security polic
y prevented an action.", webError.message); |
| 78 case WebServiceWorkerError::ErrorTypeState: |
| 79 return ExceptionParams(InvalidStateError, "The Service Worker state was
not valid.", webError.message); |
| 80 case WebServiceWorkerError::ErrorTypeTimeout: |
| 81 return ExceptionParams(AbortError, "The Service Worker operation timed o
ut.", webError.message); |
| 82 case WebServiceWorkerError::ErrorTypeUnknown: |
| 83 return ExceptionParams(UnknownError, "An unknown error occurred within S
ervice Worker.", webError.message); |
| 84 case WebServiceWorkerError::ErrorTypeType: |
| 85 // ErrorTypeType should have been handled before reaching this point. |
| 86 ASSERT_NOT_REACHED(); |
| 87 return ExceptionParams(UnknownError); |
| 88 } |
| 89 ASSERT_NOT_REACHED(); |
| 90 return ExceptionParams(UnknownError); |
| 43 } | 91 } |
| 44 | 92 |
| 93 } // namespace |
| 94 |
| 45 // static | 95 // static |
| 46 DOMException* ServiceWorkerError::take(ScriptPromiseResolver*, const WebServiceW
orkerError& webError) | 96 DOMException* ServiceWorkerError::take(ScriptPromiseResolver*, const WebServiceW
orkerError& webError) |
| 47 { | 97 { |
| 48 switch (webError.errorType) { | 98 ExceptionParams params = getExceptionParams(webError); |
| 49 case WebServiceWorkerError::ErrorTypeAbort: | 99 ASSERT(params.code != UnknownError); |
| 50 return createException(AbortError, "The Service Worker operation was abo
rted.", webError.message); | 100 return DOMException::create(params.code, params.message); |
| 51 case WebServiceWorkerError::ErrorTypeActivate: | 101 } |
| 52 // Not currently returned as a promise rejection. | 102 |
| 53 // FIXME: Introduce new ActivateError type to ExceptionCodes? | 103 // static |
| 54 return createException(AbortError, "The Service Worker activation failed
.", webError.message); | 104 v8::Local<v8::Value> ServiceWorkerErrorForUpdate::take(ScriptPromiseResolver* re
solver, const WebServiceWorkerError& webError) |
| 55 case WebServiceWorkerError::ErrorTypeDisabled: | 105 { |
| 56 return createException(NotSupportedError, "Service Worker support is dis
abled.", webError.message); | 106 ScriptState* scriptState = resolver->getScriptState(); |
| 57 case WebServiceWorkerError::ErrorTypeInstall: | 107 switch(webError.errorType) { |
| 58 // FIXME: Introduce new InstallError type to ExceptionCodes? | |
| 59 return createException(AbortError, "The Service Worker installation fail
ed.", webError.message); | |
| 60 case WebServiceWorkerError::ErrorTypeNavigation: | |
| 61 // ErrorTypeNavigation should have bailed out before calling this. | |
| 62 ASSERT_NOT_REACHED(); | |
| 63 return DOMException::create(UnknownError); | |
| 64 case WebServiceWorkerError::ErrorTypeNetwork: | 108 case WebServiceWorkerError::ErrorTypeNetwork: |
| 65 return createException(NetworkError, "The Service Worker failed by netwo
rk.", webError.message); | |
| 66 case WebServiceWorkerError::ErrorTypeNotFound: | 109 case WebServiceWorkerError::ErrorTypeNotFound: |
| 67 return createException(NotFoundError, "The specified Service Worker reso
urce was not found.", webError.message); | 110 case WebServiceWorkerError::ErrorTypeScriptEvaluateFailed: |
| 68 case WebServiceWorkerError::ErrorTypeSecurity: | 111 // According to the spec, these errors during update should result in |
| 69 return createException(SecurityError, "The Service Worker security polic
y prevented an action.", webError.message); | 112 // a TypeError. |
| 70 case WebServiceWorkerError::ErrorTypeState: | 113 return V8ThrowException::createTypeError(scriptState->isolate(), getExce
ptionParams(webError).message); |
| 71 return createException(InvalidStateError, "The Service Worker state was
not valid.", webError.message); | 114 default: |
| 72 case WebServiceWorkerError::ErrorTypeTimeout: | 115 return toV8(ServiceWorkerError::take(resolver, webError), scriptState->c
ontext()->Global(), scriptState->isolate()); |
| 73 return createException(AbortError, "The Service Worker operation timed o
ut.", webError.message); | |
| 74 case WebServiceWorkerError::ErrorTypeUnknown: | |
| 75 return createException(UnknownError, "An unknown error occurred within S
ervice Worker.", webError.message); | |
| 76 case WebServiceWorkerError::ErrorTypeType: | |
| 77 // ErrorTypeType should have been handled before reaching this point. | |
| 78 ASSERT_NOT_REACHED(); | |
| 79 return DOMException::create(UnknownError); | |
| 80 } | 116 } |
| 81 ASSERT_NOT_REACHED(); | |
| 82 return DOMException::create(UnknownError); | |
| 83 } | 117 } |
| 84 | 118 |
| 85 } // namespace blink | 119 } // namespace blink |
| OLD | NEW |