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.
5 #include "chrome/browser/component_updater/widevine_cdm_component_installer.h"
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"
37 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
39 using content::BrowserThread
;
40 using content::PluginService
;
42 namespace component_updater
{
44 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
48 // CRX hash. The extension id is: oimompecagnajdejgnnjijobebaeigek.
49 const 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 };
54 // File name of the Widevine CDM component manifest on different platforms.
55 const char kWidevineCdmManifestName
[] = "WidevineCdm";
57 // File name of the Widevine CDM adapter version file. The CDM adapter shares
58 // the same version number with Chromium version.
59 const char kCdmAdapterVersionName
[] = "CdmAdapterVersion";
61 // Name of the Widevine CDM OS in the component manifest.
62 const char kWidevineCdmPlatform
[] =
63 #if defined(OS_MACOSX)
67 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
71 // Name of the Widevine CDM architecture in the component manifest.
72 const char kWidevineCdmArch
[] =
73 #if defined(ARCH_CPU_X86)
75 #elif defined(ARCH_CPU_X86_64)
77 #else // TODO(viettrungluu): Support an ARM check?
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".
85 const char kCdmValueDelimiter
= ',';
86 COMPILE_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
93 // Matches CDM_MODULE_VERSION.
94 const char kCdmModuleVersionsName
[] = "x-cdm-module-versions";
95 // Matches supported ContentDecryptionModule_* version(s).
96 const char kCdmInterfaceVersionsName
[] = "x-cdm-interface-versions";
97 // Matches supported Host_* version(s).
98 const 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.
101 const char kCdmCodecsListName
[] = "x-cdm-codecs";
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.
106 base::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
);
113 bool 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
)) {
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
;
144 typedef bool (*VersionCheckFunc
)(int version
);
146 bool 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
)) {
152 << "Widevine CDM component manifest is missing " << version_name
;
153 // TODO(ddorwin): Remove this once all users have been updated.
154 // The original manifests did not include this string, so add its version.
155 if (version_name
== kCdmModuleVersionsName
)
156 versions_string
= "4";
157 else if (version_name
== kCdmInterfaceVersionsName
)
158 versions_string
= "1";
159 else if (version_name
== kCdmHostVersionsName
)
160 versions_string
= "1";
162 DLOG_IF(WARNING
, versions_string
.empty())
163 << "Widevine CDM component manifest has empty " << version_name
;
165 std::vector
<std::string
> versions
;
166 base::SplitString(versions_string
,
170 for (size_t i
= 0; i
< versions
.size(); ++i
) {
172 if (base::StringToInt(versions
[i
], &version
))
173 if (version_check_func(version
))
177 DLOG(WARNING
) << "Widevine CDM component manifest has no supported "
178 << version_name
<< " in '" << versions_string
<< "'";
182 // Returns whether the CDM's API versions, as specified in the manifest, are
183 // compatible with this Chrome binary.
184 // Checks the module API, CDM interface API, and Host API.
185 // This should never fail except in rare cases where the component has not been
186 // updated recently or the user downgrades Chrome.
187 bool IsCompatibleWithChrome(const base::DictionaryValue
& manifest
) {
189 CheckForCompatibleVersion(manifest
,
190 kCdmModuleVersionsName
,
191 media::IsSupportedCdmModuleVersion
) &&
192 CheckForCompatibleVersion(manifest
,
193 kCdmInterfaceVersionsName
,
194 media::IsSupportedCdmInterfaceVersion
) &&
195 CheckForCompatibleVersion(manifest
,
196 kCdmHostVersionsName
,
197 media::IsSupportedCdmHostVersion
);
200 void GetAdditionalParams(const base::DictionaryValue
& manifest
,
201 std::vector
<base::string16
>* additional_param_names
,
202 std::vector
<base::string16
>* additional_param_values
) {
203 base::string16 codecs
;
204 if (manifest
.GetString(kCdmCodecsListName
, &codecs
)) {
205 DLOG_IF(WARNING
, codecs
.empty())
206 << "Widevine CDM component manifest has empty codecs list";
207 additional_param_names
->push_back(
208 base::ASCIIToUTF16(kCdmSupportedCodecsParamName
));
209 additional_param_values
->push_back(codecs
);
211 DLOG(WARNING
) << "Widevine CDM component manifest is missing codecs";
212 // TODO(ddorwin): Remove this once all users have been updated.
213 // The original manifests did not include this string, so add the base set.
214 additional_param_names
->push_back(
215 base::ASCIIToUTF16(kCdmSupportedCodecsParamName
));
216 additional_param_values
->push_back(base::ASCIIToUTF16("vp8,vorbis"));
220 void RegisterWidevineCdmWithChrome(const base::Version
& cdm_version
,
221 const base::FilePath
& adapter_install_path
,
222 scoped_ptr
<base::DictionaryValue
> manifest
) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
224 std::vector
<base::string16
> additional_param_names
;
225 std::vector
<base::string16
> additional_param_values
;
227 *manifest
, &additional_param_names
, &additional_param_values
);
228 content::PepperPluginInfo plugin_info
;
229 if (!MakeWidevineCdmPluginInfo(cdm_version
,
230 adapter_install_path
,
231 additional_param_names
,
232 additional_param_values
,
237 // true = Add to beginning of list to override any existing registrations.
238 PluginService::GetInstance()->RegisterInternalPlugin(
239 plugin_info
.ToWebPluginInfo(), true);
240 PluginService::GetInstance()->RefreshPlugins();
245 class WidevineCdmComponentInstallerTraits
: public ComponentInstallerTraits
{
247 WidevineCdmComponentInstallerTraits();
248 virtual ~WidevineCdmComponentInstallerTraits() {}
251 // The following methods override ComponentInstallerTraits.
252 virtual bool CanAutoUpdate() const OVERRIDE
;
253 virtual bool OnCustomInstall(const base::DictionaryValue
& manifest
,
254 const base::FilePath
& install_dir
) OVERRIDE
;
255 virtual bool VerifyInstallation(
256 const base::FilePath
& install_dir
) const OVERRIDE
;
257 virtual void ComponentReady(
258 const base::Version
& version
,
259 const base::FilePath
& path
,
260 scoped_ptr
<base::DictionaryValue
> manifest
) OVERRIDE
;
261 virtual base::FilePath
GetBaseDirectory() const OVERRIDE
;
262 virtual void GetHash(std::vector
<uint8
>* hash
) const OVERRIDE
;
263 virtual std::string
GetName() const OVERRIDE
;
265 // Checks and updates CDM adapter if necessary to make sure the latest CDM
266 // adapter is always used.
267 // Note: The component is ready when CDM is present, but the CDM won't be
268 // registered until the adapter is copied by this function (see
269 // VerifyInstallation).
270 void UpdateCdmAdapter(const base::Version
& cdm_version
,
271 const base::FilePath
& cdm_install_dir
,
272 scoped_ptr
<base::DictionaryValue
> manifest
);
274 DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits
);
277 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
280 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
284 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
285 const base::DictionaryValue
& manifest
,
286 const base::FilePath
& install_dir
) {
290 // Once the CDM is ready, check the CDM adapter.
291 void WidevineCdmComponentInstallerTraits::ComponentReady(
292 const base::Version
& version
,
293 const base::FilePath
& path
,
294 scoped_ptr
<base::DictionaryValue
> manifest
) {
295 if (!IsCompatibleWithChrome(*manifest
)) {
296 DLOG(WARNING
) << "Installed Widevine CDM component is incompatible.";
300 BrowserThread::PostBlockingPoolTask(
302 base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter
,
303 base::Unretained(this),
304 version
, path
, base::Passed(&manifest
)));
307 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
308 const base::FilePath
& install_dir
) const {
309 return base::PathExists(
310 GetPlatformDirectory(install_dir
).AppendASCII(kWidevineCdmFileName
));
313 // The base directory on Windows looks like:
314 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
315 base::FilePath
WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
316 base::FilePath result
;
317 PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM
, &result
);
321 void WidevineCdmComponentInstallerTraits::GetHash(
322 std::vector
<uint8
>* hash
) const {
323 hash
->assign(kSha2Hash
, kSha2Hash
+ arraysize(kSha2Hash
));
326 std::string
WidevineCdmComponentInstallerTraits::GetName() const {
327 return kWidevineCdmManifestName
;
330 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
331 const base::Version
& cdm_version
,
332 const base::FilePath
& cdm_install_dir
,
333 scoped_ptr
<base::DictionaryValue
> manifest
) {
334 const base::FilePath adapter_version_path
=
335 GetPlatformDirectory(cdm_install_dir
).AppendASCII(kCdmAdapterVersionName
);
336 const base::FilePath adapter_install_path
=
337 GetPlatformDirectory(cdm_install_dir
)
338 .AppendASCII(kWidevineCdmAdapterFileName
);
340 const std::string chrome_version
= chrome::VersionInfo().Version();
341 DCHECK(!chrome_version
.empty());
342 std::string adapter_version
;
343 if (!base::ReadFileToString(adapter_version_path
, &adapter_version
) ||
344 adapter_version
!= chrome_version
||
345 !base::PathExists(adapter_install_path
)) {
346 int bytes_written
= file_util::WriteFile(
347 adapter_version_path
, chrome_version
.data(), chrome_version
.size());
348 if (bytes_written
< 0 ||
349 static_cast<size_t>(bytes_written
) != chrome_version
.size()) {
350 DLOG(WARNING
) << "Failed to write Widevine CDM adapter version file.";
351 // Ignore version file writing failure and try to copy the CDM adapter.
354 base::FilePath adapter_source_path
;
355 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER
, &adapter_source_path
);
356 if (!base::CopyFile(adapter_source_path
, adapter_install_path
)) {
357 DLOG(WARNING
) << "Failed to copy Widevine CDM adapter.";
362 BrowserThread::PostTask(content::BrowserThread::UI
,
364 base::Bind(&RegisterWidevineCdmWithChrome
,
366 adapter_install_path
,
367 base::Passed(&manifest
)));
370 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
372 void RegisterWidevineCdmComponent(ComponentUpdateService
* cus
) {
373 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
374 base::FilePath adapter_source_path
;
375 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER
, &adapter_source_path
);
376 if (!base::PathExists(adapter_source_path
))
378 scoped_ptr
<ComponentInstallerTraits
> traits(
379 new WidevineCdmComponentInstallerTraits
);
380 // |cus| will take ownership of |installer| during installer->Register(cus).
381 DefaultComponentInstaller
* installer
382 = new DefaultComponentInstaller(traits
.Pass());
383 installer
->Register(cus
);
386 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
389 } // namespace component_updater