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