| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/crypto/key_systems.h" | 5 #include "content/renderer/media/crypto/key_systems.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "content/public/common/content_client.h" |
| 13 #include "content/public/renderer/content_renderer_client.h" |
| 14 #include "content/public/renderer/key_system_info.h" |
| 12 #include "content/renderer/media/crypto/key_systems_info.h" | 15 #include "content/renderer/media/crypto/key_systems_info.h" |
| 13 #include "net/base/mime_util.h" | 16 #include "net/base/mime_util.h" |
| 14 #include "third_party/WebKit/public/platform/WebCString.h" | 17 #include "third_party/WebKit/public/platform/WebCString.h" |
| 15 #include "third_party/WebKit/public/platform/WebString.h" | 18 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 | 19 |
| 17 namespace content { | 20 namespace content { |
| 18 | 21 |
| 19 // Convert a WebString to ASCII, falling back on an empty string in the case | 22 // Convert a WebString to ASCII, falling back on an empty string in the case |
| 20 // of a non-ASCII string. | 23 // of a non-ASCII string. |
| 21 static std::string ToASCIIOrEmpty(const WebKit::WebString& string) { | 24 static std::string ToASCIIOrEmpty(const WebKit::WebString& string) { |
| 22 return IsStringASCII(string) ? UTF16ToASCII(string) : std::string(); | 25 return IsStringASCII(string) ? UTF16ToASCII(string) : std::string(); |
| 23 } | 26 } |
| 24 | 27 |
| 25 class KeySystems { | 28 class KeySystems { |
| 26 public: | 29 public: |
| 27 static KeySystems* GetInstance(); | 30 static KeySystems& GetInstance(); |
| 28 | |
| 29 void AddConcreteSupportedKeySystem( | |
| 30 const std::string& key_system, | |
| 31 bool use_aes_decryptor, | |
| 32 #if defined(ENABLE_PEPPER_CDMS) | |
| 33 const std::string& pepper_type, | |
| 34 #elif defined(OS_ANDROID) | |
| 35 const uint8 uuid[16], | |
| 36 #endif | |
| 37 const std::string& parent_key_system); | |
| 38 | |
| 39 void AddSupportedType(const std::string& key_system, | |
| 40 const std::string& mime_type, | |
| 41 const std::string& codecs_list); | |
| 42 | 31 |
| 43 bool IsConcreteSupportedKeySystem(const std::string& key_system); | 32 bool IsConcreteSupportedKeySystem(const std::string& key_system); |
| 44 | 33 |
| 45 bool IsSupportedKeySystemWithMediaMimeType( | 34 bool IsSupportedKeySystemWithMediaMimeType( |
| 46 const std::string& mime_type, | 35 const std::string& mime_type, |
| 47 const std::vector<std::string>& codecs, | 36 const std::vector<std::string>& codecs, |
| 48 const std::string& key_system); | 37 const std::string& key_system); |
| 49 | 38 |
| 50 bool UseAesDecryptor(const std::string& concrete_key_system); | 39 bool UseAesDecryptor(const std::string& concrete_key_system); |
| 51 | 40 |
| 52 #if defined(ENABLE_PEPPER_CDMS) | 41 #if defined(ENABLE_PEPPER_CDMS) |
| 53 std::string GetPepperType(const std::string& concrete_key_system); | 42 std::string GetPepperType(const std::string& concrete_key_system); |
| 54 #elif defined(OS_ANDROID) | 43 #elif defined(OS_ANDROID) |
| 55 std::vector<uint8> GetUUID(const std::string& concrete_key_system); | 44 std::vector<uint8> GetUUID(const std::string& concrete_key_system); |
| 56 #endif | 45 #endif |
| 57 | 46 |
| 58 private: | 47 private: |
| 48 void AddConcreteSupportedKeySystems( |
| 49 const std::vector<KeySystemInfo>& concrete_key_systems); |
| 50 |
| 51 void AddConcreteSupportedKeySystem( |
| 52 const std::string& key_system, |
| 53 bool use_aes_decryptor, |
| 54 #if defined(ENABLE_PEPPER_CDMS) |
| 55 const std::string& pepper_type, |
| 56 #elif defined(OS_ANDROID) |
| 57 const std::vector<uint8>& uuid, |
| 58 #endif |
| 59 const std::vector<KeySystemInfo::ContainerCodecsPair>& supported_types, |
| 60 const std::string& parent_key_system); |
| 61 |
| 62 |
| 59 friend struct base::DefaultLazyInstanceTraits<KeySystems>; | 63 friend struct base::DefaultLazyInstanceTraits<KeySystems>; |
| 60 | 64 |
| 61 typedef base::hash_set<std::string> CodecSet; | 65 typedef base::hash_set<std::string> CodecSet; |
| 62 typedef std::map<std::string, CodecSet> MimeTypeMap; | 66 typedef std::map<std::string, CodecSet> MimeTypeMap; |
| 63 | 67 |
| 64 struct KeySystemProperties { | 68 struct KeySystemProperties { |
| 65 KeySystemProperties() : use_aes_decryptor(false) {} | 69 KeySystemProperties() : use_aes_decryptor(false) {} |
| 66 | 70 |
| 67 bool use_aes_decryptor; | 71 bool use_aes_decryptor; |
| 68 #if defined(ENABLE_PEPPER_CDMS) | 72 #if defined(ENABLE_PEPPER_CDMS) |
| 69 std::string pepper_type; | 73 std::string pepper_type; |
| 70 #elif defined(OS_ANDROID) | 74 #elif defined(OS_ANDROID) |
| 71 std::vector<uint8> uuid; | 75 std::vector<uint8> uuid; |
| 72 #endif | 76 #endif |
| 73 MimeTypeMap types; | 77 MimeTypeMap types; |
| 74 }; | 78 }; |
| 75 | 79 |
| 76 typedef std::map<std::string, KeySystemProperties> KeySystemPropertiesMap; | 80 typedef std::map<std::string, KeySystemProperties> KeySystemPropertiesMap; |
| 77 | 81 |
| 78 typedef std::map<std::string, std::string> ParentKeySystemMap; | 82 typedef std::map<std::string, std::string> ParentKeySystemMap; |
| 79 | 83 |
| 80 KeySystems() {} | 84 KeySystems(); |
| 85 ~KeySystems() {} |
| 86 |
| 87 void AddSupportedType(const std::string& mime_type, |
| 88 const std::string& codecs_list, |
| 89 KeySystemProperties* properties); |
| 81 | 90 |
| 82 bool IsSupportedKeySystemWithContainerAndCodec( | 91 bool IsSupportedKeySystemWithContainerAndCodec( |
| 83 const std::string& mime_type, | 92 const std::string& mime_type, |
| 84 const std::string& codec, | 93 const std::string& codec, |
| 85 const std::string& key_system); | 94 const std::string& key_system); |
| 86 | 95 |
| 87 // Map from key system string to capabilities. | 96 // Map from key system string to capabilities. |
| 88 KeySystemPropertiesMap concrete_key_system_map_; | 97 KeySystemPropertiesMap concrete_key_system_map_; |
| 89 | 98 |
| 90 // Map from parent key system to the concrete key system that should be used | 99 // Map from parent key system to the concrete key system that should be used |
| 91 // to represent its capabilities. | 100 // to represent its capabilities. |
| 92 ParentKeySystemMap parent_key_system_map_; | 101 ParentKeySystemMap parent_key_system_map_; |
| 93 | 102 |
| 94 DISALLOW_COPY_AND_ASSIGN(KeySystems); | 103 DISALLOW_COPY_AND_ASSIGN(KeySystems); |
| 95 }; | 104 }; |
| 96 | 105 |
| 97 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER; | 106 static base::LazyInstance<KeySystems> g_key_systems = LAZY_INSTANCE_INITIALIZER; |
| 98 | 107 |
| 99 KeySystems* KeySystems::GetInstance() { | 108 KeySystems& KeySystems::GetInstance() { |
| 100 KeySystems* key_systems = &g_key_systems.Get(); | 109 return g_key_systems.Get(); |
| 101 // TODO(ddorwin): Call out to ContentClient to register key systems. | 110 } |
| 102 static bool is_registered = false; | 111 |
| 103 if (!is_registered) { | 112 // Because we use a LazyInstance, the key systems info must be populated when |
| 104 is_registered = true; // Prevent reentrancy when Add*() is called. | 113 // the instance is lazily initiated. |
| 105 RegisterKeySystems(); | 114 KeySystems::KeySystems() { |
| 115 std::vector<KeySystemInfo> key_systems_info; |
| 116 GetContentClient()->renderer()->AddKeySystems(&key_systems_info); |
| 117 // TODO(ddorwin): Remove in next CL after moving info to |
| 118 // ChromeContentRendererClient. |
| 119 AddKeySystems(&key_systems_info); |
| 120 AddConcreteSupportedKeySystems(key_systems_info); |
| 121 } |
| 122 |
| 123 void KeySystems::AddConcreteSupportedKeySystems( |
| 124 const std::vector<KeySystemInfo>& concrete_key_systems) { |
| 125 for (size_t i = 0; i < concrete_key_systems.size(); ++i) { |
| 126 const KeySystemInfo& key_system_info = concrete_key_systems[i]; |
| 127 AddConcreteSupportedKeySystem(key_system_info.key_system, |
| 128 key_system_info.use_aes_decryptor, |
| 129 #if defined(ENABLE_PEPPER_CDMS) |
| 130 key_system_info.pepper_type, |
| 131 #elif defined(OS_ANDROID) |
| 132 key_system_info.uuid, |
| 133 #endif |
| 134 key_system_info.supported_types, |
| 135 key_system_info.parent_key_system); |
| 106 } | 136 } |
| 107 return key_systems; | |
| 108 } | 137 } |
| 109 | 138 |
| 110 void KeySystems::AddConcreteSupportedKeySystem( | 139 void KeySystems::AddConcreteSupportedKeySystem( |
| 111 const std::string& concrete_key_system, | 140 const std::string& concrete_key_system, |
| 112 bool use_aes_decryptor, | 141 bool use_aes_decryptor, |
| 113 #if defined(ENABLE_PEPPER_CDMS) | 142 #if defined(ENABLE_PEPPER_CDMS) |
| 114 const std::string& pepper_type, | 143 const std::string& pepper_type, |
| 115 #elif defined(OS_ANDROID) | 144 #elif defined(OS_ANDROID) |
| 116 const uint8 uuid[16], | 145 const std::vector<uint8>& uuid, |
| 117 #endif | 146 #endif |
| 147 const std::vector<KeySystemInfo::ContainerCodecsPair>& supported_types, |
| 118 const std::string& parent_key_system) { | 148 const std::string& parent_key_system) { |
| 119 DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system)) | 149 DCHECK(!IsConcreteSupportedKeySystem(concrete_key_system)) |
| 120 << "Key system '" << concrete_key_system << "' already registered"; | 150 << "Key system '" << concrete_key_system << "' already registered"; |
| 151 DCHECK(parent_key_system_map_.find(concrete_key_system) == |
| 152 parent_key_system_map_.end()) |
| 153 << "'" << concrete_key_system << " is already registered as a parent"; |
| 121 | 154 |
| 122 KeySystemProperties properties; | 155 KeySystemProperties properties; |
| 123 properties.use_aes_decryptor = use_aes_decryptor; | 156 properties.use_aes_decryptor = use_aes_decryptor; |
| 124 #if defined(ENABLE_PEPPER_CDMS) | 157 #if defined(ENABLE_PEPPER_CDMS) |
| 125 DCHECK_EQ(use_aes_decryptor, pepper_type.empty()); | 158 DCHECK_EQ(use_aes_decryptor, pepper_type.empty()); |
| 126 properties.pepper_type = pepper_type; | 159 properties.pepper_type = pepper_type; |
| 127 #elif defined(OS_ANDROID) | 160 #elif defined(OS_ANDROID) |
| 128 // Since |uuid| can't be empty, use |use_aes_decryptor|. | 161 DCHECK_EQ(use_aes_decryptor, uuid.empty()); |
| 129 if (!use_aes_decryptor) | 162 DCHECK(use_aes_decryptor || uuid.size() == 16); |
| 130 properties.uuid.assign(uuid, uuid + 16); | 163 properties.uuid = uuid; |
| 131 #endif | 164 #endif |
| 165 |
| 166 for (size_t i = 0; i < supported_types.size(); ++i) { |
| 167 const KeySystemInfo::ContainerCodecsPair& pair = supported_types[i]; |
| 168 const std::string& mime_type = pair.first; |
| 169 const std::string& codecs_list = pair.second; |
| 170 AddSupportedType(mime_type, codecs_list, &properties); |
| 171 } |
| 172 |
| 132 concrete_key_system_map_[concrete_key_system] = properties; | 173 concrete_key_system_map_[concrete_key_system] = properties; |
| 133 | 174 |
| 134 if (!parent_key_system.empty()) { | 175 if (!parent_key_system.empty()) { |
| 176 DCHECK(!IsConcreteSupportedKeySystem(parent_key_system)) |
| 177 << "Parent '" << parent_key_system << "' already registered concrete"; |
| 135 DCHECK(parent_key_system_map_.find(parent_key_system) == | 178 DCHECK(parent_key_system_map_.find(parent_key_system) == |
| 136 parent_key_system_map_.end()) | 179 parent_key_system_map_.end()) |
| 137 << "Parent '" << parent_key_system.c_str() << "' already registered."; | 180 << "Parent '" << parent_key_system << "' already registered"; |
| 138 parent_key_system_map_[parent_key_system] = concrete_key_system; | 181 parent_key_system_map_[parent_key_system] = concrete_key_system; |
| 139 } | 182 } |
| 140 } | 183 } |
| 141 | 184 |
| 142 void KeySystems::AddSupportedType(const std::string& concrete_key_system, | 185 void KeySystems::AddSupportedType(const std::string& mime_type, |
| 143 const std::string& mime_type, | 186 const std::string& codecs_list, |
| 144 const std::string& codecs_list) { | 187 KeySystemProperties* properties) { |
| 145 std::vector<std::string> mime_type_codecs; | 188 std::vector<std::string> mime_type_codecs; |
| 146 net::ParseCodecString(codecs_list, &mime_type_codecs, false); | 189 net::ParseCodecString(codecs_list, &mime_type_codecs, false); |
| 147 | 190 |
| 148 CodecSet codecs(mime_type_codecs.begin(), mime_type_codecs.end()); | 191 CodecSet codecs(mime_type_codecs.begin(), mime_type_codecs.end()); |
| 149 // Support the MIME type string alone, without codec(s) specified. | 192 // Support the MIME type string alone, without codec(s) specified. |
| 150 codecs.insert(std::string()); | 193 codecs.insert(std::string()); |
| 151 | 194 |
| 152 KeySystemPropertiesMap::iterator key_system_iter = | 195 MimeTypeMap& mime_types_map = properties->types; |
| 153 concrete_key_system_map_.find(concrete_key_system); | |
| 154 DCHECK(key_system_iter != concrete_key_system_map_.end()); | |
| 155 MimeTypeMap& mime_types_map = key_system_iter->second.types; | |
| 156 // mime_types_map must not be repeated for a given key system. | 196 // mime_types_map must not be repeated for a given key system. |
| 157 DCHECK(mime_types_map.find(mime_type) == mime_types_map.end()); | 197 DCHECK(mime_types_map.find(mime_type) == mime_types_map.end()); |
| 158 mime_types_map[mime_type] = codecs; | 198 mime_types_map[mime_type] = codecs; |
| 159 } | 199 } |
| 160 | 200 |
| 161 bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) { | 201 bool KeySystems::IsConcreteSupportedKeySystem(const std::string& key_system) { |
| 162 return concrete_key_system_map_.find(key_system) != | 202 return concrete_key_system_map_.find(key_system) != |
| 163 concrete_key_system_map_.end(); | 203 concrete_key_system_map_.end(); |
| 164 } | 204 } |
| 165 | 205 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 DLOG(FATAL) << concrete_key_system << " is not a known concrete system"; | 287 DLOG(FATAL) << concrete_key_system << " is not a known concrete system"; |
| 248 return std::vector<uint8>(); | 288 return std::vector<uint8>(); |
| 249 } | 289 } |
| 250 | 290 |
| 251 return key_system_iter->second.uuid; | 291 return key_system_iter->second.uuid; |
| 252 } | 292 } |
| 253 #endif | 293 #endif |
| 254 | 294 |
| 255 //------------------------------------------------------------------------------ | 295 //------------------------------------------------------------------------------ |
| 256 | 296 |
| 257 void AddConcreteSupportedKeySystem( | |
| 258 const std::string& key_system, | |
| 259 bool use_aes_decryptor, | |
| 260 #if defined(ENABLE_PEPPER_CDMS) | |
| 261 const std::string& pepper_type, | |
| 262 #elif defined(OS_ANDROID) | |
| 263 const uint8 uuid[16], | |
| 264 #endif | |
| 265 const std::string& parent_key_system) { | |
| 266 KeySystems::GetInstance()->AddConcreteSupportedKeySystem(key_system, | |
| 267 use_aes_decryptor, | |
| 268 #if defined(ENABLE_PEPPER_CDMS) | |
| 269 pepper_type, | |
| 270 #elif defined(OS_ANDROID) | |
| 271 uuid, | |
| 272 #endif | |
| 273 parent_key_system); | |
| 274 } | |
| 275 | |
| 276 void AddSupportedType(const std::string& key_system, | |
| 277 const std::string& mime_type, | |
| 278 const std::string& codecs_list) { | |
| 279 KeySystems::GetInstance()->AddSupportedType(key_system, | |
| 280 mime_type, codecs_list); | |
| 281 } | |
| 282 | |
| 283 bool IsConcreteSupportedKeySystem(const WebKit::WebString& key_system) { | 297 bool IsConcreteSupportedKeySystem(const WebKit::WebString& key_system) { |
| 284 return KeySystems::GetInstance()->IsConcreteSupportedKeySystem( | 298 return KeySystems::GetInstance().IsConcreteSupportedKeySystem( |
| 285 ToASCIIOrEmpty(key_system)); | 299 ToASCIIOrEmpty(key_system)); |
| 286 } | 300 } |
| 287 | 301 |
| 288 bool IsSupportedKeySystemWithMediaMimeType( | 302 bool IsSupportedKeySystemWithMediaMimeType( |
| 289 const std::string& mime_type, | 303 const std::string& mime_type, |
| 290 const std::vector<std::string>& codecs, | 304 const std::vector<std::string>& codecs, |
| 291 const std::string& key_system) { | 305 const std::string& key_system) { |
| 292 return KeySystems::GetInstance()->IsSupportedKeySystemWithMediaMimeType( | 306 return KeySystems::GetInstance().IsSupportedKeySystemWithMediaMimeType( |
| 293 mime_type, codecs, key_system); | 307 mime_type, codecs, key_system); |
| 294 } | 308 } |
| 295 | 309 |
| 296 std::string KeySystemNameForUMA(const WebKit::WebString& key_system) { | 310 std::string KeySystemNameForUMA(const WebKit::WebString& key_system) { |
| 297 return KeySystemNameForUMAInternal(key_system); | 311 return KeySystemNameForUMAInternal(key_system); |
| 298 } | 312 } |
| 299 | 313 |
| 300 bool CanUseAesDecryptor(const std::string& concrete_key_system) { | 314 bool CanUseAesDecryptor(const std::string& concrete_key_system) { |
| 301 return KeySystems::GetInstance()->UseAesDecryptor(concrete_key_system); | 315 return KeySystems::GetInstance().UseAesDecryptor(concrete_key_system); |
| 302 } | 316 } |
| 303 | 317 |
| 304 #if defined(ENABLE_PEPPER_CDMS) | 318 #if defined(ENABLE_PEPPER_CDMS) |
| 305 std::string GetPepperType(const std::string& concrete_key_system) { | 319 std::string GetPepperType(const std::string& concrete_key_system) { |
| 306 return KeySystems::GetInstance()->GetPepperType(concrete_key_system); | 320 return KeySystems::GetInstance().GetPepperType(concrete_key_system); |
| 307 } | 321 } |
| 308 #elif defined(OS_ANDROID) | 322 #elif defined(OS_ANDROID) |
| 309 std::vector<uint8> GetUUID(const std::string& concrete_key_system) { | 323 std::vector<uint8> GetUUID(const std::string& concrete_key_system) { |
| 310 return KeySystems::GetInstance()->GetUUID(concrete_key_system); | 324 return KeySystems::GetInstance().GetUUID(concrete_key_system); |
| 311 } | 325 } |
| 312 #endif | 326 #endif |
| 313 | 327 |
| 314 } // namespace content | 328 } // namespace content |
| OLD | NEW |