1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "config.h"
6#include "modules/encryptedmedia/SimpleContentDecryptionModuleResult.h"
7
8#include "bindings/core/v8/ScriptPromise.h"
9#include "bindings/core/v8/ScriptPromiseResolver.h"
10#include "bindings/core/v8/ScriptState.h"
11#include "bindings/core/v8/V8Binding.h"
12#include "core/dom/DOMException.h"
13#include "platform/Logging.h"
14#include "public/platform/WebString.h"
15#include "wtf/Assertions.h"
16
17namespace blink {
18
19ExceptionCode WebCdmExceptionToExceptionCode(WebContentDecryptionModuleException cdmException)
20{
21    switch (cdmException) {
22    case WebContentDecryptionModuleExceptionNotSupportedError:
23        return NotSupportedError;
24    case WebContentDecryptionModuleExceptionInvalidStateError:
25        return InvalidStateError;
26    case WebContentDecryptionModuleExceptionInvalidAccessError:
27        return InvalidAccessError;
28    case WebContentDecryptionModuleExceptionQuotaExceededError:
29        return QuotaExceededError;
30    case WebContentDecryptionModuleExceptionUnknownError:
31        return UnknownError;
32    case WebContentDecryptionModuleExceptionClientError:
33    case WebContentDecryptionModuleExceptionOutputError:
34        // Currently no matching DOMException for these 2 errors.
35        // FIXME: Update DOMException to handle these if actually added to
36        // the EME spec.
37        return UnknownError;
38    }
39
40    ASSERT_NOT_REACHED();
41    return UnknownError;
42}
43
44SimpleContentDecryptionModuleResult::SimpleContentDecryptionModuleResult(ScriptState* scriptState)
45    : m_resolver(ScriptPromiseResolver::create(scriptState))
46{
47}
48
49SimpleContentDecryptionModuleResult::~SimpleContentDecryptionModuleResult()
50{
51}
52
53void SimpleContentDecryptionModuleResult::complete()
54{
55    m_resolver->resolve(V8UndefinedType());
56    m_resolver.clear();
57}
58
59void SimpleContentDecryptionModuleResult::completeWithSession(WebContentDecryptionModuleResult::SessionStatus status)
60{
61    ASSERT_NOT_REACHED();
62    completeWithDOMException(InvalidStateError, "Unexpected completion.");
63}
64
65void SimpleContentDecryptionModuleResult::completeWithError(WebContentDecryptionModuleException exceptionCode, unsigned long systemCode, const WebString& errorMessage)
66{
67    completeWithDOMException(WebCdmExceptionToExceptionCode(exceptionCode), errorMessage);
68}
69
70ScriptPromise SimpleContentDecryptionModuleResult::promise()
71{
72    return m_resolver->promise();
73}
74
75void SimpleContentDecryptionModuleResult::completeWithDOMException(ExceptionCode code, const String& errorMessage)
76{
77    m_resolver->reject(DOMException::create(code, errorMessage));
78    m_resolver.clear();
79}
80
81} // namespace blink
82