widevine_cdm_component_installer.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1// Copyright (c) 2013 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 "chrome/browser/component_updater/widevine_cdm_component_installer.h" 6 7#include <string.h> 8 9#include <string> 10#include <vector> 11 12#include "base/base_paths.h" 13#include "base/bind.h" 14#include "base/compiler_specific.h" 15#include "base/file_util.h" 16#include "base/files/file_path.h" 17#include "base/logging.h" 18#include "base/path_service.h" 19#include "base/strings/string16.h" 20#include "base/strings/string_number_conversions.h" 21#include "base/strings/string_split.h" 22#include "base/strings/utf_string_conversions.h" 23#include "base/values.h" 24#include "build/build_config.h" 25#include "chrome/common/chrome_paths.h" 26#include "chrome/common/chrome_version_info.h" 27#include "chrome/common/widevine_cdm_constants.h" 28#include "components/component_updater/component_updater_service.h" 29#include "components/component_updater/default_component_installer.h" 30#include "content/public/browser/browser_thread.h" 31#include "content/public/browser/plugin_service.h" 32#include "content/public/common/pepper_plugin_info.h" 33#include "media/cdm/ppapi/supported_cdm_versions.h" 34#include "third_party/widevine/cdm/widevine_cdm_common.h" 35 36#include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. NOLINT 37 38using content::BrowserThread; 39using content::PluginService; 40 41namespace component_updater { 42 43#if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT) 44 45namespace { 46 47// CRX hash. The extension id is: oimompecagnajdejgnnjijobebaeigek. 48const uint8 kSha2Hash[] = {0xe8, 0xce, 0xcf, 0x42, 0x06, 0xd0, 0x93, 0x49, 49 0x6d, 0xd9, 0x89, 0xe1, 0x41, 0x04, 0x86, 0x4a, 50 0x8f, 0xbd, 0x86, 0x12, 0xb9, 0x58, 0x9b, 0xfb, 51 0x4f, 0xbb, 0x1b, 0xa9, 0xd3, 0x85, 0x37, 0xef}; 52 53// File name of the Widevine CDM component manifest on different platforms. 54const char kWidevineCdmManifestName[] = "WidevineCdm"; 55 56// File name of the Widevine CDM adapter version file. The CDM adapter shares 57// the same version number with Chromium version. 58const char kCdmAdapterVersionName[] = "CdmAdapterVersion"; 59 60// Name of the Widevine CDM OS in the component manifest. 61const char kWidevineCdmPlatform[] = 62#if defined(OS_MACOSX) 63 "mac"; 64#elif defined(OS_WIN) 65 "win"; 66#else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android? 67 "linux"; 68#endif 69 70// Name of the Widevine CDM architecture in the component manifest. 71const char kWidevineCdmArch[] = 72#if defined(ARCH_CPU_X86) 73 "x86"; 74#elif defined(ARCH_CPU_X86_64) 75 "x64"; 76#else // TODO(viettrungluu): Support an ARM check? 77 "???"; 78#endif 79 80// The CDM manifest includes several custom values, all beginning with "x-cdm-". 81// All values are strings. 82// All values that are lists are delimited by commas. No trailing commas. 83// For example, "1,2,4". 84const char kCdmValueDelimiter = ','; 85COMPILE_ASSERT(kCdmValueDelimiter == kCdmSupportedCodecsValueDelimiter, 86 cdm_delimiters_do_not_match); 87// The following entries are required. 88// Interface versions are lists of integers (e.g. "1" or "1,2,4"). 89// These are checked in this file before registering the CDM. 90// All match the interface versions from content_decryption_module.h that the 91// CDM supports. 92// Matches CDM_MODULE_VERSION. 93const char kCdmModuleVersionsName[] = "x-cdm-module-versions"; 94// Matches supported ContentDecryptionModule_* version(s). 95const char kCdmInterfaceVersionsName[] = "x-cdm-interface-versions"; 96// Matches supported Host_* version(s). 97const char kCdmHostVersionsName[] = "x-cdm-host-versions"; 98// The codecs list is a list of simple codec names (e.g. "vp8,vorbis"). 99// The list is passed to other parts of Chrome. 100const char kCdmCodecsListName[] = "x-cdm-codecs"; 101 102// Widevine CDM is packaged as a multi-CRX. Widevine CDM binaries are located in 103// _platform_specific/<platform_arch> folder in the package. This function 104// returns the platform-specific subdirectory that is part of that multi-CRX. 105base::FilePath GetPlatformDirectory(const base::FilePath& base_path) { 106 std::string platform_arch = kWidevineCdmPlatform; 107 platform_arch += '_'; 108 platform_arch += kWidevineCdmArch; 109 return base_path.AppendASCII("_platform_specific").AppendASCII(platform_arch); 110} 111 112bool MakeWidevineCdmPluginInfo( 113 const base::Version& version, 114 const base::FilePath& path, 115 const std::vector<base::string16>& additional_param_names, 116 const std::vector<base::string16>& additional_param_values, 117 content::PepperPluginInfo* plugin_info) { 118 if (!version.IsValid() || 119 version.components().size() != 120 static_cast<size_t>(kWidevineCdmVersionNumComponents)) { 121 return false; 122 } 123 124 plugin_info->is_internal = false; 125 // Widevine CDM must run out of process. 126 plugin_info->is_out_of_process = true; 127 plugin_info->path = path; 128 plugin_info->name = kWidevineCdmDisplayName; 129 plugin_info->description = kWidevineCdmDescription; 130 plugin_info->version = version.GetString(); 131 content::WebPluginMimeType widevine_cdm_mime_type( 132 kWidevineCdmPluginMimeType, 133 kWidevineCdmPluginExtension, 134 kWidevineCdmPluginMimeTypeDescription); 135 widevine_cdm_mime_type.additional_param_names = additional_param_names; 136 widevine_cdm_mime_type.additional_param_values = additional_param_values; 137 plugin_info->mime_types.push_back(widevine_cdm_mime_type); 138 plugin_info->permissions = kWidevineCdmPluginPermissions; 139 140 return true; 141} 142 143typedef bool (*VersionCheckFunc)(int version); 144 145bool CheckForCompatibleVersion(const base::DictionaryValue& manifest, 146 const std::string version_name, 147 VersionCheckFunc version_check_func) { 148 std::string versions_string; 149 if (!manifest.GetString(version_name, &versions_string)) { 150 DLOG(WARNING) << "Widevine CDM component manifest missing " << version_name; 151 return false; 152 } 153 DLOG_IF(WARNING, versions_string.empty()) 154 << "Widevine CDM component manifest has empty " << version_name; 155 156 std::vector<std::string> versions; 157 base::SplitString(versions_string, kCdmValueDelimiter, &versions); 158 159 for (size_t i = 0; i < versions.size(); ++i) { 160 int version = 0; 161 if (base::StringToInt(versions[i], &version)) 162 if (version_check_func(version)) 163 return true; 164 } 165 166 DLOG(WARNING) << "Widevine CDM component manifest has no supported " 167 << version_name << " in '" << versions_string << "'"; 168 return false; 169} 170 171// Returns whether the CDM's API versions, as specified in the manifest, are 172// compatible with this Chrome binary. 173// Checks the module API, CDM interface API, and Host API. 174// This should never fail except in rare cases where the component has not been 175// updated recently or the user downgrades Chrome. 176bool IsCompatibleWithChrome(const base::DictionaryValue& manifest) { 177 return CheckForCompatibleVersion(manifest, 178 kCdmModuleVersionsName, 179 media::IsSupportedCdmModuleVersion) && 180 CheckForCompatibleVersion(manifest, 181 kCdmInterfaceVersionsName, 182 media::IsSupportedCdmInterfaceVersion) && 183 CheckForCompatibleVersion(manifest, 184 kCdmHostVersionsName, 185 media::IsSupportedCdmHostVersion); 186} 187 188void GetAdditionalParams(const base::DictionaryValue& manifest, 189 std::vector<base::string16>* additional_param_names, 190 std::vector<base::string16>* additional_param_values) { 191 base::string16 codecs; 192 if (manifest.GetString(kCdmCodecsListName, &codecs)) { 193 DLOG_IF(WARNING, codecs.empty()) 194 << "Widevine CDM component manifest has empty codecs list"; 195 additional_param_names->push_back( 196 base::ASCIIToUTF16(kCdmSupportedCodecsParamName)); 197 additional_param_values->push_back(codecs); 198 } else { 199 DLOG(WARNING) << "Widevine CDM component manifest is missing codecs"; 200 } 201} 202 203void RegisterWidevineCdmWithChrome(const base::Version& cdm_version, 204 const base::FilePath& adapter_install_path, 205 scoped_ptr<base::DictionaryValue> manifest) { 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 207 std::vector<base::string16> additional_param_names; 208 std::vector<base::string16> additional_param_values; 209 GetAdditionalParams( 210 *manifest, &additional_param_names, &additional_param_values); 211 content::PepperPluginInfo plugin_info; 212 if (!MakeWidevineCdmPluginInfo(cdm_version, 213 adapter_install_path, 214 additional_param_names, 215 additional_param_values, 216 &plugin_info)) { 217 return; 218 } 219 220 // true = Add to beginning of list to override any existing registrations. 221 PluginService::GetInstance()->RegisterInternalPlugin( 222 plugin_info.ToWebPluginInfo(), true); 223 // Tell the browser to refresh the plugin list. Then tell all renderers to 224 // update their plugin list caches. 225 PluginService::GetInstance()->RefreshPlugins(); 226 PluginService::GetInstance()->PurgePluginListCache(NULL, false); 227} 228 229} // namespace 230 231class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits { 232 public: 233 WidevineCdmComponentInstallerTraits(); 234 virtual ~WidevineCdmComponentInstallerTraits() {} 235 236 private: 237 // The following methods override ComponentInstallerTraits. 238 virtual bool CanAutoUpdate() const OVERRIDE; 239 virtual bool OnCustomInstall(const base::DictionaryValue& manifest, 240 const base::FilePath& install_dir) OVERRIDE; 241 virtual bool VerifyInstallation( 242 const base::FilePath& install_dir) const OVERRIDE; 243 virtual void ComponentReady( 244 const base::Version& version, 245 const base::FilePath& path, 246 scoped_ptr<base::DictionaryValue> manifest) OVERRIDE; 247 virtual base::FilePath GetBaseDirectory() const OVERRIDE; 248 virtual void GetHash(std::vector<uint8>* hash) const OVERRIDE; 249 virtual std::string GetName() const OVERRIDE; 250 251 // Checks and updates CDM adapter if necessary to make sure the latest CDM 252 // adapter is always used. 253 // Note: The component is ready when CDM is present, but the CDM won't be 254 // registered until the adapter is copied by this function (see 255 // VerifyInstallation). 256 void UpdateCdmAdapter(const base::Version& cdm_version, 257 const base::FilePath& cdm_install_dir, 258 scoped_ptr<base::DictionaryValue> manifest); 259 260 DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits); 261}; 262 263WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() { 264} 265 266bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const { 267 return true; 268} 269 270bool WidevineCdmComponentInstallerTraits::OnCustomInstall( 271 const base::DictionaryValue& manifest, 272 const base::FilePath& install_dir) { 273 return true; 274} 275 276// Once the CDM is ready, check the CDM adapter. 277void WidevineCdmComponentInstallerTraits::ComponentReady( 278 const base::Version& version, 279 const base::FilePath& path, 280 scoped_ptr<base::DictionaryValue> manifest) { 281 if (!IsCompatibleWithChrome(*manifest)) { 282 DLOG(WARNING) << "Installed Widevine CDM component is incompatible."; 283 return; 284 } 285 286 BrowserThread::PostBlockingPoolTask( 287 FROM_HERE, 288 base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter, 289 base::Unretained(this), 290 version, 291 path, 292 base::Passed(&manifest))); 293} 294 295bool WidevineCdmComponentInstallerTraits::VerifyInstallation( 296 const base::FilePath& install_dir) const { 297 return base::PathExists( 298 GetPlatformDirectory(install_dir).AppendASCII(kWidevineCdmFileName)); 299} 300 301// The base directory on Windows looks like: 302// <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\. 303base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const { 304 base::FilePath result; 305 PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result); 306 return result; 307} 308 309void WidevineCdmComponentInstallerTraits::GetHash( 310 std::vector<uint8>* hash) const { 311 hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash)); 312} 313 314std::string WidevineCdmComponentInstallerTraits::GetName() const { 315 return kWidevineCdmManifestName; 316} 317 318void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter( 319 const base::Version& cdm_version, 320 const base::FilePath& cdm_install_dir, 321 scoped_ptr<base::DictionaryValue> manifest) { 322 const base::FilePath adapter_version_path = 323 GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName); 324 const base::FilePath adapter_install_path = 325 GetPlatformDirectory(cdm_install_dir) 326 .AppendASCII(kWidevineCdmAdapterFileName); 327 328 const std::string chrome_version = chrome::VersionInfo().Version(); 329 DCHECK(!chrome_version.empty()); 330 std::string adapter_version; 331 if (!base::ReadFileToString(adapter_version_path, &adapter_version) || 332 adapter_version != chrome_version || 333 !base::PathExists(adapter_install_path)) { 334 int bytes_written = base::WriteFile( 335 adapter_version_path, chrome_version.data(), chrome_version.size()); 336 if (bytes_written < 0 || 337 static_cast<size_t>(bytes_written) != chrome_version.size()) { 338 DLOG(WARNING) << "Failed to write Widevine CDM adapter version file."; 339 // Ignore version file writing failure and try to copy the CDM adapter. 340 } 341 342 base::FilePath adapter_source_path; 343 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path); 344 if (!base::CopyFile(adapter_source_path, adapter_install_path)) { 345 DLOG(WARNING) << "Failed to copy Widevine CDM adapter."; 346 return; 347 } 348 } 349 350 BrowserThread::PostTask(content::BrowserThread::UI, 351 FROM_HERE, 352 base::Bind(&RegisterWidevineCdmWithChrome, 353 cdm_version, 354 adapter_install_path, 355 base::Passed(&manifest))); 356} 357 358#endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT) 359 360void RegisterWidevineCdmComponent(ComponentUpdateService* cus) { 361#if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT) 362 base::FilePath adapter_source_path; 363 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path); 364 if (!base::PathExists(adapter_source_path)) 365 return; 366 scoped_ptr<ComponentInstallerTraits> traits( 367 new WidevineCdmComponentInstallerTraits); 368 // |cus| will take ownership of |installer| during installer->Register(cus). 369 DefaultComponentInstaller* installer = 370 new DefaultComponentInstaller(traits.Pass()); 371 installer->Register(cus); 372#else 373 return; 374#endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT) 375} 376 377} // namespace component_updater 378