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 "content/renderer/media/cdm_result_promise.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "third_party/WebKit/public/platform/WebString.h"
10
11 namespace content {
12
ConvertException(media::MediaKeys::Exception exception_code)13 static blink::WebContentDecryptionModuleException ConvertException(
14 media::MediaKeys::Exception exception_code) {
15 switch (exception_code) {
16 case media::MediaKeys::NOT_SUPPORTED_ERROR:
17 return blink::WebContentDecryptionModuleExceptionNotSupportedError;
18 case media::MediaKeys::INVALID_STATE_ERROR:
19 return blink::WebContentDecryptionModuleExceptionInvalidStateError;
20 case media::MediaKeys::INVALID_ACCESS_ERROR:
21 return blink::WebContentDecryptionModuleExceptionInvalidAccessError;
22 case media::MediaKeys::QUOTA_EXCEEDED_ERROR:
23 return blink::WebContentDecryptionModuleExceptionQuotaExceededError;
24 case media::MediaKeys::UNKNOWN_ERROR:
25 return blink::WebContentDecryptionModuleExceptionUnknownError;
26 case media::MediaKeys::CLIENT_ERROR:
27 return blink::WebContentDecryptionModuleExceptionClientError;
28 case media::MediaKeys::OUTPUT_ERROR:
29 return blink::WebContentDecryptionModuleExceptionOutputError;
30 default:
31 NOTREACHED();
32 return blink::WebContentDecryptionModuleExceptionUnknownError;
33 }
34 }
35
36 template <typename T>
CdmResultPromise(const blink::WebContentDecryptionModuleResult & result)37 CdmResultPromise<T>::CdmResultPromise(
38 const blink::WebContentDecryptionModuleResult& result)
39 : media::CdmPromiseTemplate<T>(
40 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)),
41 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this))),
42 web_cdm_result_(result) {
43 }
44
45 template <typename T>
CdmResultPromise(const blink::WebContentDecryptionModuleResult & result,const std::string & uma_name)46 CdmResultPromise<T>::CdmResultPromise(
47 const blink::WebContentDecryptionModuleResult& result,
48 const std::string& uma_name)
49 : media::CdmPromiseTemplate<T>(
50 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)),
51 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this)),
52 uma_name),
53 web_cdm_result_(result) {
54 }
55
56 template <typename T>
~CdmResultPromise()57 CdmResultPromise<T>::~CdmResultPromise() {
58 }
59
60 template <>
OnResolve(const std::string & result)61 void CdmResultPromise<std::string>::OnResolve(const std::string& result) {
62 // This must be overridden in a subclass.
63 NOTREACHED();
64 }
65
66 template <>
OnResolve(const media::KeyIdsVector & result)67 void CdmResultPromise<media::KeyIdsVector>::OnResolve(
68 const media::KeyIdsVector& result) {
69 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to
70 // handle the set of keys.
71 OnReject(media::MediaKeys::NOT_SUPPORTED_ERROR, 0, "Not implemented.");
72 }
73
74 template <typename T>
OnReject(media::MediaKeys::Exception exception_code,uint32 system_code,const std::string & error_message)75 void CdmResultPromise<T>::OnReject(media::MediaKeys::Exception exception_code,
76 uint32 system_code,
77 const std::string& error_message) {
78 web_cdm_result_.completeWithError(ConvertException(exception_code),
79 system_code,
80 blink::WebString::fromUTF8(error_message));
81 }
82
CdmResultPromise(const blink::WebContentDecryptionModuleResult & result)83 CdmResultPromise<void>::CdmResultPromise(
84 const blink::WebContentDecryptionModuleResult& result)
85 : media::CdmPromiseTemplate<void>(
86 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)),
87 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this))),
88 web_cdm_result_(result) {
89 }
90
CdmResultPromise(const blink::WebContentDecryptionModuleResult & result,const std::string & uma_name)91 CdmResultPromise<void>::CdmResultPromise(
92 const blink::WebContentDecryptionModuleResult& result,
93 const std::string& uma_name)
94 : media::CdmPromiseTemplate<void>(
95 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)),
96 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this)),
97 uma_name),
98 web_cdm_result_(result) {
99 }
100
~CdmResultPromise()101 CdmResultPromise<void>::~CdmResultPromise() {
102 }
103
OnResolve()104 void CdmResultPromise<void>::OnResolve() {
105 web_cdm_result_.complete();
106 }
107
OnReject(media::MediaKeys::Exception exception_code,uint32 system_code,const std::string & error_message)108 void CdmResultPromise<void>::OnReject(
109 media::MediaKeys::Exception exception_code,
110 uint32 system_code,
111 const std::string& error_message) {
112 web_cdm_result_.completeWithError(ConvertException(exception_code),
113 system_code,
114 blink::WebString::fromUTF8(error_message));
115 }
116
117 // Explicit template instantiation for the templates needed.
118 template class CdmResultPromise<std::string>;
119 template class CdmResultPromise<media::KeyIdsVector>;
120
121 } // namespace content
122