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 { | |
12 | |
13 // A superset of media::MediaKeys::Exception for UMA reporting. These values | |
14 // should never be changed as it will affect existing reporting, and must match | |
15 // the values for CdmPromiseResult in tools/metrics/histograms/histograms.xml. | |
16 enum ResultCodeForUMA { | |
17 SUCCESS = 0, | |
18 NOT_SUPPORTED_ERROR = 1, | |
19 INVALID_STATE_ERROR = 2, | |
20 INVALID_ACCESS_ERROR = 3, | |
21 QUOTA_EXCEEDED_ERROR = 4, | |
22 UNKNOWN_ERROR = 5, | |
23 CLIENT_ERROR = 6, | |
24 OUTPUT_ERROR = 7, | |
25 NUM_RESULT_CODES | |
26 }; | |
27 | |
28 } // namespace | |
xhwang
2014/10/03 23:13:57
nit: Move this whole block into content namespace.
jrummell
2014/10/03 23:32:07
Done.
| |
29 | |
11 namespace content { | 30 namespace content { |
12 | 31 |
13 static blink::WebContentDecryptionModuleException ConvertException( | 32 static blink::WebContentDecryptionModuleException ConvertException( |
14 media::MediaKeys::Exception exception_code) { | 33 media::MediaKeys::Exception exception_code) { |
15 switch (exception_code) { | 34 switch (exception_code) { |
16 case media::MediaKeys::NOT_SUPPORTED_ERROR: | 35 case media::MediaKeys::NOT_SUPPORTED_ERROR: |
17 return blink::WebContentDecryptionModuleExceptionNotSupportedError; | 36 return blink::WebContentDecryptionModuleExceptionNotSupportedError; |
18 case media::MediaKeys::INVALID_STATE_ERROR: | 37 case media::MediaKeys::INVALID_STATE_ERROR: |
19 return blink::WebContentDecryptionModuleExceptionInvalidStateError; | 38 return blink::WebContentDecryptionModuleExceptionInvalidStateError; |
20 case media::MediaKeys::INVALID_ACCESS_ERROR: | 39 case media::MediaKeys::INVALID_ACCESS_ERROR: |
21 return blink::WebContentDecryptionModuleExceptionInvalidAccessError; | 40 return blink::WebContentDecryptionModuleExceptionInvalidAccessError; |
22 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: | 41 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: |
23 return blink::WebContentDecryptionModuleExceptionQuotaExceededError; | 42 return blink::WebContentDecryptionModuleExceptionQuotaExceededError; |
24 case media::MediaKeys::UNKNOWN_ERROR: | 43 case media::MediaKeys::UNKNOWN_ERROR: |
25 return blink::WebContentDecryptionModuleExceptionUnknownError; | 44 return blink::WebContentDecryptionModuleExceptionUnknownError; |
26 case media::MediaKeys::CLIENT_ERROR: | 45 case media::MediaKeys::CLIENT_ERROR: |
27 return blink::WebContentDecryptionModuleExceptionClientError; | 46 return blink::WebContentDecryptionModuleExceptionClientError; |
28 case media::MediaKeys::OUTPUT_ERROR: | 47 case media::MediaKeys::OUTPUT_ERROR: |
29 return blink::WebContentDecryptionModuleExceptionOutputError; | 48 return blink::WebContentDecryptionModuleExceptionOutputError; |
30 default: | |
31 NOTREACHED(); | |
32 return blink::WebContentDecryptionModuleExceptionUnknownError; | |
33 } | 49 } |
50 NOTREACHED(); | |
51 return blink::WebContentDecryptionModuleExceptionUnknownError; | |
34 } | 52 } |
35 | 53 |
36 template <typename T> | 54 static ResultCodeForUMA ConvertExceptionToUMAResult( |
37 CdmResultPromise<T>::CdmResultPromise( | 55 media::MediaKeys::Exception exception_code) { |
38 const blink::WebContentDecryptionModuleResult& result) | 56 switch (exception_code) { |
39 : media::CdmPromiseTemplate<T>( | 57 case media::MediaKeys::NOT_SUPPORTED_ERROR: |
40 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)), | 58 return NOT_SUPPORTED_ERROR; |
41 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this))), | 59 case media::MediaKeys::INVALID_STATE_ERROR: |
42 web_cdm_result_(result) { | 60 return INVALID_STATE_ERROR; |
61 case media::MediaKeys::INVALID_ACCESS_ERROR: | |
62 return INVALID_ACCESS_ERROR; | |
63 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: | |
64 return QUOTA_EXCEEDED_ERROR; | |
65 case media::MediaKeys::UNKNOWN_ERROR: | |
66 return UNKNOWN_ERROR; | |
67 case media::MediaKeys::CLIENT_ERROR: | |
68 return CLIENT_ERROR; | |
69 case media::MediaKeys::OUTPUT_ERROR: | |
70 return OUTPUT_ERROR; | |
71 } | |
72 NOTREACHED(); | |
73 return UNKNOWN_ERROR; | |
43 } | 74 } |
44 | 75 |
45 template <typename T> | 76 static void ReportUMA(std::string uma_name, ResultCodeForUMA result) { |
46 CdmResultPromise<T>::CdmResultPromise( | 77 if (uma_name.empty()) |
78 return; | |
79 | |
80 base::LinearHistogram::FactoryGet( | |
81 uma_name, | |
82 1, | |
83 NUM_RESULT_CODES, | |
84 NUM_RESULT_CODES + 1, | |
85 base::HistogramBase::kUmaTargetedHistogramFlag)->Add(result); | |
86 } | |
87 | |
88 template <typename... T> | |
89 CdmResultPromise<T...>::CdmResultPromise( | |
47 const blink::WebContentDecryptionModuleResult& result, | 90 const blink::WebContentDecryptionModuleResult& result, |
48 const std::string& uma_name) | 91 const std::string& uma_name) |
49 : media::CdmPromiseTemplate<T>( | 92 : 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 } | 93 } |
55 | 94 |
56 template <typename T> | 95 template <typename... T> |
57 CdmResultPromise<T>::~CdmResultPromise() { | 96 CdmResultPromise<T...>::~CdmResultPromise() { |
58 } | 97 } |
59 | 98 |
60 template <> | 99 template <> |
61 void CdmResultPromise<std::string>::OnResolve(const std::string& result) { | 100 void CdmResultPromise<>::resolve() { |
62 // This must be overridden in a subclass. | 101 MarkPromiseSettled(); |
63 NOTREACHED(); | 102 ReportUMA(uma_name_, SUCCESS); |
103 web_cdm_result_.complete(); | |
64 } | 104 } |
65 | 105 |
66 template <> | 106 template <> |
67 void CdmResultPromise<media::KeyIdsVector>::OnResolve( | 107 void CdmResultPromise<media::KeyIdsVector>::resolve( |
68 const media::KeyIdsVector& result) { | 108 const media::KeyIdsVector& result) { |
69 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to | 109 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to |
70 // handle the set of keys. | 110 // handle the set of keys. |
71 OnReject(media::MediaKeys::NOT_SUPPORTED_ERROR, 0, "Not implemented."); | 111 reject(media::MediaKeys::NOT_SUPPORTED_ERROR, 0, "Not implemented."); |
72 } | 112 } |
73 | 113 |
74 template <typename T> | 114 template <typename... T> |
75 void CdmResultPromise<T>::OnReject(media::MediaKeys::Exception exception_code, | 115 void CdmResultPromise<T...>::reject(media::MediaKeys::Exception exception_code, |
76 uint32 system_code, | 116 uint32 system_code, |
77 const std::string& error_message) { | 117 const std::string& error_message) { |
118 MarkPromiseSettled(); | |
119 ReportUMA(uma_name_, ConvertExceptionToUMAResult(exception_code)); | |
78 web_cdm_result_.completeWithError(ConvertException(exception_code), | 120 web_cdm_result_.completeWithError(ConvertException(exception_code), |
79 system_code, | 121 system_code, |
80 blink::WebString::fromUTF8(error_message)); | 122 blink::WebString::fromUTF8(error_message)); |
81 } | 123 } |
82 | 124 |
83 CdmResultPromise<void>::CdmResultPromise( | 125 NewSessionCdmResultPromise::NewSessionCdmResultPromise( |
84 const blink::WebContentDecryptionModuleResult& result) | 126 const blink::WebContentDecryptionModuleResult& result, |
85 : media::CdmPromiseTemplate<void>( | 127 const std::string& uma_name, |
86 base::Bind(&CdmResultPromise::OnResolve, base::Unretained(this)), | 128 const SessionInitializedCB& new_session_created_cb) |
87 base::Bind(&CdmResultPromise::OnReject, base::Unretained(this))), | 129 : web_cdm_result_(result), |
88 web_cdm_result_(result) { | 130 uma_name_(uma_name), |
131 new_session_created_cb_(new_session_created_cb) { | |
89 } | 132 } |
90 | 133 |
91 CdmResultPromise<void>::CdmResultPromise( | 134 NewSessionCdmResultPromise::~NewSessionCdmResultPromise() { |
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 } | 135 } |
100 | 136 |
101 CdmResultPromise<void>::~CdmResultPromise() { | 137 void NewSessionCdmResultPromise::resolve(const std::string& web_session_id) { |
138 MarkPromiseSettled(); | |
139 ReportUMA(uma_name_, SUCCESS); | |
140 blink::WebContentDecryptionModuleResult::SessionStatus status = | |
141 new_session_created_cb_.Run(web_session_id); | |
142 web_cdm_result_.completeWithSession(status); | |
102 } | 143 } |
103 | 144 |
104 void CdmResultPromise<void>::OnResolve() { | 145 void NewSessionCdmResultPromise::reject( |
105 web_cdm_result_.complete(); | |
106 } | |
107 | |
108 void CdmResultPromise<void>::OnReject( | |
109 media::MediaKeys::Exception exception_code, | 146 media::MediaKeys::Exception exception_code, |
110 uint32 system_code, | 147 uint32 system_code, |
111 const std::string& error_message) { | 148 const std::string& error_message) { |
149 MarkPromiseSettled(); | |
150 ReportUMA(uma_name_, ConvertExceptionToUMAResult(exception_code)); | |
112 web_cdm_result_.completeWithError(ConvertException(exception_code), | 151 web_cdm_result_.completeWithError(ConvertException(exception_code), |
113 system_code, | 152 system_code, |
114 blink::WebString::fromUTF8(error_message)); | 153 blink::WebString::fromUTF8(error_message)); |
115 } | 154 } |
116 | 155 |
117 // Explicit template instantiation for the templates needed. | 156 // Explicit template instantiation for the templates needed. |
118 template class CdmResultPromise<std::string>; | 157 template class CdmResultPromise<>; |
119 template class CdmResultPromise<media::KeyIdsVector>; | 158 template class CdmResultPromise<media::KeyIdsVector>; |
120 | 159 |
121 } // namespace content | 160 } // namespace content |
OLD | NEW |