OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/cdm_result_promise.h" | 5 #include "content/renderer/media/cdm_result_promise.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" |
9 #include "third_party/WebKit/public/platform/WebString.h" | 9 #include "third_party/WebKit/public/platform/WebString.h" |
10 | 10 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 static blink::WebContentDecryptionModuleException ConvertException( | 13 static blink::WebContentDecryptionModuleException ConvertException( |
14 media::MediaKeys::Exception exception_code) { | 14 media::MediaKeys::Exception exception_code) { |
15 switch (exception_code) { | 15 switch (exception_code) { |
16 case media::MediaKeys::NOT_SUPPORTED_ERROR: | 16 case media::MediaKeys::NOT_SUPPORTED_ERROR: |
17 return blink::WebContentDecryptionModuleExceptionNotSupportedError; | 17 return blink::WebContentDecryptionModuleExceptionNotSupportedError; |
18 case media::MediaKeys::INVALID_STATE_ERROR: | 18 case media::MediaKeys::INVALID_STATE_ERROR: |
19 return blink::WebContentDecryptionModuleExceptionInvalidStateError; | 19 return blink::WebContentDecryptionModuleExceptionInvalidStateError; |
20 case media::MediaKeys::INVALID_ACCESS_ERROR: | 20 case media::MediaKeys::INVALID_ACCESS_ERROR: |
21 return blink::WebContentDecryptionModuleExceptionInvalidAccessError; | 21 return blink::WebContentDecryptionModuleExceptionInvalidAccessError; |
22 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: | 22 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: |
23 return blink::WebContentDecryptionModuleExceptionQuotaExceededError; | 23 return blink::WebContentDecryptionModuleExceptionQuotaExceededError; |
24 case media::MediaKeys::UNKNOWN_ERROR: | 24 case media::MediaKeys::UNKNOWN_ERROR: |
25 return blink::WebContentDecryptionModuleExceptionUnknownError; | 25 return blink::WebContentDecryptionModuleExceptionUnknownError; |
26 case media::MediaKeys::CLIENT_ERROR: | 26 case media::MediaKeys::CLIENT_ERROR: |
27 return blink::WebContentDecryptionModuleExceptionClientError; | 27 return blink::WebContentDecryptionModuleExceptionClientError; |
28 case media::MediaKeys::OUTPUT_ERROR: | 28 case media::MediaKeys::OUTPUT_ERROR: |
29 return blink::WebContentDecryptionModuleExceptionOutputError; | 29 return blink::WebContentDecryptionModuleExceptionOutputError; |
30 default: | |
31 NOTREACHED(); | |
32 return blink::WebContentDecryptionModuleExceptionUnknownError; | |
33 } | 30 } |
| 31 NOTREACHED(); |
| 32 return blink::WebContentDecryptionModuleExceptionUnknownError; |
34 } | 33 } |
35 | 34 |
36 template <typename T> | 35 static ResultCodeForUMA ConvertExceptionToUMAResult( |
37 CdmResultPromise<T>::CdmResultPromise( | 36 media::MediaKeys::Exception exception_code) { |
38 const blink::WebContentDecryptionModuleResult& result) | 37 switch (exception_code) { |
39 : media::CdmPromiseTemplate<T>( | 38 case media::MediaKeys::NOT_SUPPORTED_ERROR: |
40 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)), | 39 return NOT_SUPPORTED_ERROR; |
41 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this))), | 40 case media::MediaKeys::INVALID_STATE_ERROR: |
42 web_cdm_result_(result) { | 41 return INVALID_STATE_ERROR; |
| 42 case media::MediaKeys::INVALID_ACCESS_ERROR: |
| 43 return INVALID_ACCESS_ERROR; |
| 44 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: |
| 45 return QUOTA_EXCEEDED_ERROR; |
| 46 case media::MediaKeys::UNKNOWN_ERROR: |
| 47 return UNKNOWN_ERROR; |
| 48 case media::MediaKeys::CLIENT_ERROR: |
| 49 return CLIENT_ERROR; |
| 50 case media::MediaKeys::OUTPUT_ERROR: |
| 51 return OUTPUT_ERROR; |
| 52 } |
| 53 NOTREACHED(); |
| 54 return UNKNOWN_ERROR; |
43 } | 55 } |
44 | 56 |
45 template <typename T> | 57 static void ReportUMA(std::string uma_name, ResultCodeForUMA result) { |
46 CdmResultPromise<T>::CdmResultPromise( | 58 if (uma_name.empty()) |
| 59 return; |
| 60 |
| 61 base::LinearHistogram::FactoryGet( |
| 62 uma_name, |
| 63 1, |
| 64 NUM_RESULT_CODES, |
| 65 NUM_RESULT_CODES + 1, |
| 66 base::HistogramBase::kUmaTargetedHistogramFlag)->Add(result); |
| 67 } |
| 68 |
| 69 template <typename... T> |
| 70 CdmResultPromise<T...>::CdmResultPromise( |
47 const blink::WebContentDecryptionModuleResult& result, | 71 const blink::WebContentDecryptionModuleResult& result, |
48 const std::string& uma_name) | 72 const std::string& uma_name) |
49 : media::CdmPromiseTemplate<T>( | 73 : web_cdm_result_(result), uma_name_(uma_name) { |
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 } | 74 } |
55 | 75 |
56 template <typename T> | 76 template <typename... T> |
57 CdmResultPromise<T>::~CdmResultPromise() { | 77 CdmResultPromise<T...>::~CdmResultPromise() { |
58 } | 78 } |
59 | 79 |
60 template <> | 80 template <> |
61 void CdmResultPromise<std::string>::OnResolve(const std::string& result) { | 81 void CdmResultPromise<>::resolve() { |
62 // This must be overridden in a subclass. | 82 MarkPromiseSettled(); |
63 NOTREACHED(); | 83 ReportUMA(uma_name_, SUCCESS); |
| 84 web_cdm_result_.complete(); |
64 } | 85 } |
65 | 86 |
66 template <> | 87 template <> |
67 void CdmResultPromise<media::KeyIdsVector>::OnResolve( | 88 void CdmResultPromise<std::string>::resolve(const std::string& result) { |
| 89 // Since WebContentDecryptionModuleResult.completeWithSession() takes a |
| 90 // SessionStatus as the parameter and not the session ID, this must be |
| 91 // overridden in a subclass that determines the appropriate SessionStatus |
| 92 // to pass to blink. However, this method must be called from any overrides |
| 93 // to mark the promise as settled and report to UMA. |
| 94 MarkPromiseSettled(); |
| 95 ReportUMA(uma_name_, SUCCESS); |
| 96 } |
| 97 |
| 98 template <> |
| 99 void CdmResultPromise<media::KeyIdsVector>::resolve( |
68 const media::KeyIdsVector& result) { | 100 const media::KeyIdsVector& result) { |
69 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to | 101 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to |
70 // handle the set of keys. | 102 // handle the set of keys. |
71 OnReject(media::MediaKeys::NOT_SUPPORTED_ERROR, 0, "Not implemented."); | 103 reject(media::MediaKeys::NOT_SUPPORTED_ERROR, 0, "Not implemented."); |
72 } | 104 } |
73 | 105 |
74 template <typename T> | 106 template <typename... T> |
75 void CdmResultPromise<T>::OnReject(media::MediaKeys::Exception exception_code, | 107 void CdmResultPromise<T...>::reject(media::MediaKeys::Exception exception_code, |
76 uint32 system_code, | 108 uint32 system_code, |
77 const std::string& error_message) { | 109 const std::string& error_message) { |
78 web_cdm_result_.completeWithError(ConvertException(exception_code), | 110 MarkPromiseSettled(); |
79 system_code, | 111 ReportUMA(uma_name_, ConvertExceptionToUMAResult(exception_code)); |
80 blink::WebString::fromUTF8(error_message)); | |
81 } | |
82 | |
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 | |
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 | |
101 CdmResultPromise<void>::~CdmResultPromise() { | |
102 } | |
103 | |
104 void CdmResultPromise<void>::OnResolve() { | |
105 web_cdm_result_.complete(); | |
106 } | |
107 | |
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), | 112 web_cdm_result_.completeWithError(ConvertException(exception_code), |
113 system_code, | 113 system_code, |
114 blink::WebString::fromUTF8(error_message)); | 114 blink::WebString::fromUTF8(error_message)); |
115 } | 115 } |
116 | 116 |
117 // Explicit template instantiation for the templates needed. | 117 // Explicit template instantiation for the templates needed. |
| 118 template class CdmResultPromise<>; |
118 template class CdmResultPromise<std::string>; | 119 template class CdmResultPromise<std::string>; |
119 template class CdmResultPromise<media::KeyIdsVector>; | 120 template class CdmResultPromise<media::KeyIdsVector>; |
120 | 121 |
121 } // namespace content | 122 } // namespace content |
OLD | NEW |