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