OLD | NEW |
(Empty) | |
| 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/webcontentdecryptionmoduleresult_helper.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "media/base/cdm_promise.h" |
| 10 #include "third_party/WebKit/public/platform/WebString.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 static blink::WebContentDecryptionModuleException ConvertException( |
| 15 media::MediaKeys::Exception exception_code) { |
| 16 switch (exception_code) { |
| 17 case media::MediaKeys::NOT_SUPPORTED_ERROR: |
| 18 return blink::WebContentDecryptionModuleExceptionNotSupportedError; |
| 19 case media::MediaKeys::INVALID_STATE_ERROR: |
| 20 return blink::WebContentDecryptionModuleExceptionInvalidStateError; |
| 21 case media::MediaKeys::INVALID_ACCESS_ERROR: |
| 22 return blink::WebContentDecryptionModuleExceptionInvalidAccessError; |
| 23 case media::MediaKeys::QUOTA_EXCEEDED_ERROR: |
| 24 return blink::WebContentDecryptionModuleExceptionQuotaExceededError; |
| 25 case media::MediaKeys::UNKNOWN_ERROR: |
| 26 return blink::WebContentDecryptionModuleExceptionUnknownError; |
| 27 case media::MediaKeys::CLIENT_ERROR: |
| 28 return blink::WebContentDecryptionModuleExceptionClientError; |
| 29 case media::MediaKeys::OUTPUT_ERROR: |
| 30 return blink::WebContentDecryptionModuleExceptionOutputError; |
| 31 default: |
| 32 NOTREACHED(); |
| 33 return blink::WebContentDecryptionModuleExceptionUnknownError; |
| 34 } |
| 35 } |
| 36 |
| 37 WebContentDecryptionModuleResultHelper::WebContentDecryptionModuleResultHelper() |
| 38 : next_available_result_index_(1), weak_ptr_factory_(this) { |
| 39 } |
| 40 |
| 41 WebContentDecryptionModuleResultHelper:: |
| 42 ~WebContentDecryptionModuleResultHelper() { |
| 43 // Release any WebContentDecryptionModuleResult objects that are left. Their |
| 44 // index will have been passed down via a CdmPromise, but it uses a WeakPtr. |
| 45 DLOG_IF(WARNING, outstanding_results_.size() > 0) |
| 46 << "Clearing " << outstanding_results_.size() << " results"; |
| 47 for (ResultMap::iterator it = outstanding_results_.begin(); |
| 48 it != outstanding_results_.end(); |
| 49 ++it) { |
| 50 it->second.completeWithError( |
| 51 blink::WebContentDecryptionModuleExceptionInvalidStateError, |
| 52 0, |
| 53 "Outstanding request being cancelled."); |
| 54 } |
| 55 outstanding_results_.clear(); |
| 56 } |
| 57 |
| 58 uint32 WebContentDecryptionModuleResultHelper::AddResult( |
| 59 blink::WebContentDecryptionModuleResult result) { |
| 60 uint32 result_index = next_available_result_index_++; |
| 61 outstanding_results_.insert(std::make_pair(result_index, result)); |
| 62 return result_index; |
| 63 } |
| 64 |
| 65 scoped_ptr<media::SimpleCdmPromise> |
| 66 WebContentDecryptionModuleResultHelper::CreateSimpleCdmPromise( |
| 67 blink::WebContentDecryptionModuleResult result) { |
| 68 uint32 result_index = AddResult(result); |
| 69 return make_scoped_ptr(new media::SimpleCdmPromise( |
| 70 base::Bind(&WebContentDecryptionModuleResultHelper::Complete, |
| 71 weak_ptr_factory_.GetWeakPtr(), |
| 72 result_index), |
| 73 base::Bind(&WebContentDecryptionModuleResultHelper::CompleteWithError, |
| 74 weak_ptr_factory_.GetWeakPtr(), |
| 75 result_index))); |
| 76 } |
| 77 |
| 78 scoped_ptr<media::KeyIdsPromise> |
| 79 WebContentDecryptionModuleResultHelper::CreateKeyIdsPromise( |
| 80 blink::WebContentDecryptionModuleResult result) { |
| 81 uint32 result_index = AddResult(result); |
| 82 return make_scoped_ptr(new media::KeyIdsPromise( |
| 83 base::Bind(&WebContentDecryptionModuleResultHelper::CompleteWithKeyIds, |
| 84 weak_ptr_factory_.GetWeakPtr(), |
| 85 result_index), |
| 86 base::Bind(&WebContentDecryptionModuleResultHelper::CompleteWithError, |
| 87 weak_ptr_factory_.GetWeakPtr(), |
| 88 result_index))); |
| 89 } |
| 90 |
| 91 void WebContentDecryptionModuleResultHelper::Complete(uint32 result_index) { |
| 92 ResultMap::iterator it = outstanding_results_.find(result_index); |
| 93 if (it == outstanding_results_.end()) |
| 94 return; |
| 95 |
| 96 blink::WebContentDecryptionModuleResult& result = it->second; |
| 97 result.complete(); |
| 98 outstanding_results_.erase(it); |
| 99 } |
| 100 |
| 101 void WebContentDecryptionModuleResultHelper::CompleteWithSession( |
| 102 uint32 result_index, |
| 103 blink::WebContentDecryptionModuleResult::SessionStatus status) { |
| 104 ResultMap::iterator it = outstanding_results_.find(result_index); |
| 105 if (it == outstanding_results_.end()) |
| 106 return; |
| 107 |
| 108 blink::WebContentDecryptionModuleResult& result = it->second; |
| 109 result.completeWithSession(status); |
| 110 outstanding_results_.erase(it); |
| 111 } |
| 112 |
| 113 void WebContentDecryptionModuleResultHelper::CompleteWithKeyIds( |
| 114 uint32 result_index, |
| 115 const media::KeyIdsVector& key_ids) { |
| 116 ResultMap::iterator it = outstanding_results_.find(result_index); |
| 117 if (it == outstanding_results_.end()) |
| 118 return; |
| 119 |
| 120 // TODO(jrummell): Update blink::WebContentDecryptionModuleResult to handle |
| 121 // the set of keys. |
| 122 blink::WebContentDecryptionModuleResult& result = it->second; |
| 123 result.completeWithError( |
| 124 blink::WebContentDecryptionModuleExceptionNotSupportedError, |
| 125 0, |
| 126 "Not implemented."); |
| 127 outstanding_results_.erase(it); |
| 128 } |
| 129 |
| 130 void WebContentDecryptionModuleResultHelper::CompleteWithError( |
| 131 uint32 result_index, |
| 132 media::MediaKeys::Exception exception_code, |
| 133 uint32 system_code, |
| 134 const std::string& error_message) { |
| 135 ResultMap::iterator it = outstanding_results_.find(result_index); |
| 136 if (it == outstanding_results_.end()) |
| 137 return; |
| 138 |
| 139 blink::WebContentDecryptionModuleResult& result = it->second; |
| 140 result.completeWithError(ConvertException(exception_code), |
| 141 system_code, |
| 142 blink::WebString::fromUTF8(error_message)); |
| 143 outstanding_results_.erase(it); |
| 144 } |
| 145 |
| 146 } // namespace content |
OLD | NEW |