Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / component_updater / widevine_cdm_component_installer.cc
blob541fe18223480831245a0f0688ee2e8cb9bf899b
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"
7 #include <stdint.h>
8 #include <string.h>
9 #include <string>
10 #include <vector>
12 #include "base/base_paths.h"
13 #include "base/bind.h"
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/logging.h"
18 #include "base/macros.h"
19 #include "base/path_service.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/values.h"
25 #include "build/build_config.h"
26 #include "chrome/common/chrome_paths.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 "components/version_info/version_info.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. NOLINT
39 using content::BrowserThread;
40 using content::PluginService;
42 namespace component_updater {
44 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
46 namespace {
48 // CRX hash. The extension id is: oimompecagnajdejgnnjijobebaeigek.
49 const uint8_t 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)
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
71 // Name of the Widevine CDM architecture in the component manifest.
72 const 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
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 static_assert(kCdmValueDelimiter == kCdmSupportedCodecsValueDelimiter,
87 "cdm delimiters must 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.
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)) {
122 return false;
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 std::string(" (version: ") + version.GetString() +
132 ")";
133 plugin_info->version = version.GetString();
134 content::WebPluginMimeType widevine_cdm_mime_type(
135 kWidevineCdmPluginMimeType,
136 kWidevineCdmPluginExtension,
137 kWidevineCdmPluginMimeTypeDescription);
138 widevine_cdm_mime_type.additional_param_names = additional_param_names;
139 widevine_cdm_mime_type.additional_param_values = additional_param_values;
140 plugin_info->mime_types.push_back(widevine_cdm_mime_type);
141 plugin_info->permissions = kWidevineCdmPluginPermissions;
143 return true;
146 typedef bool (*VersionCheckFunc)(int version);
148 bool CheckForCompatibleVersion(const base::DictionaryValue& manifest,
149 const std::string version_name,
150 VersionCheckFunc version_check_func) {
151 std::string versions_string;
152 if (!manifest.GetString(version_name, &versions_string)) {
153 DLOG(WARNING) << "Widevine CDM component manifest missing " << version_name;
154 return false;
156 DLOG_IF(WARNING, versions_string.empty())
157 << "Widevine CDM component manifest has empty " << version_name;
159 for (const base::StringPiece& ver_str : base::SplitStringPiece(
160 versions_string, std::string(1, kCdmValueDelimiter),
161 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
162 int version = 0;
163 if (base::StringToInt(ver_str, &version))
164 if (version_check_func(version))
165 return true;
168 DLOG(WARNING) << "Widevine CDM component manifest has no supported "
169 << version_name << " in '" << versions_string << "'";
170 return false;
173 // Returns whether the CDM's API versions, as specified in the manifest, are
174 // compatible with this Chrome binary.
175 // Checks the module API, CDM interface API, and Host API.
176 // This should never fail except in rare cases where the component has not been
177 // updated recently or the user downgrades Chrome.
178 bool IsCompatibleWithChrome(const base::DictionaryValue& manifest) {
179 return CheckForCompatibleVersion(manifest,
180 kCdmModuleVersionsName,
181 media::IsSupportedCdmModuleVersion) &&
182 CheckForCompatibleVersion(manifest,
183 kCdmInterfaceVersionsName,
184 media::IsSupportedCdmInterfaceVersion) &&
185 CheckForCompatibleVersion(manifest,
186 kCdmHostVersionsName,
187 media::IsSupportedCdmHostVersion);
190 void GetAdditionalParams(const base::DictionaryValue& manifest,
191 std::vector<base::string16>* additional_param_names,
192 std::vector<base::string16>* additional_param_values) {
193 base::string16 codecs;
194 if (manifest.GetString(kCdmCodecsListName, &codecs)) {
195 DLOG_IF(WARNING, codecs.empty())
196 << "Widevine CDM component manifest has empty codecs list";
197 additional_param_names->push_back(
198 base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
199 additional_param_values->push_back(codecs);
200 } else {
201 DLOG(WARNING) << "Widevine CDM component manifest is missing codecs";
205 void RegisterWidevineCdmWithChrome(const base::Version& cdm_version,
206 const base::FilePath& adapter_install_path,
207 scoped_ptr<base::DictionaryValue> manifest) {
208 DCHECK_CURRENTLY_ON(BrowserThread::UI);
209 std::vector<base::string16> additional_param_names;
210 std::vector<base::string16> additional_param_values;
211 GetAdditionalParams(
212 *manifest, &additional_param_names, &additional_param_values);
213 content::PepperPluginInfo plugin_info;
214 if (!MakeWidevineCdmPluginInfo(cdm_version,
215 adapter_install_path,
216 additional_param_names,
217 additional_param_values,
218 &plugin_info)) {
219 return;
222 // true = Add to beginning of list to override any existing registrations.
223 PluginService::GetInstance()->RegisterInternalPlugin(
224 plugin_info.ToWebPluginInfo(), true);
225 // Tell the browser to refresh the plugin list. Then tell all renderers to
226 // update their plugin list caches.
227 PluginService::GetInstance()->RefreshPlugins();
228 PluginService::GetInstance()->PurgePluginListCache(NULL, false);
231 } // namespace
233 class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits {
234 public:
235 WidevineCdmComponentInstallerTraits();
236 ~WidevineCdmComponentInstallerTraits() override {}
238 private:
239 // The following methods override ComponentInstallerTraits.
240 bool CanAutoUpdate() const override;
241 bool OnCustomInstall(const base::DictionaryValue& manifest,
242 const base::FilePath& install_dir) override;
243 bool VerifyInstallation(
244 const base::DictionaryValue& manifest,
245 const base::FilePath& install_dir) const override;
246 void ComponentReady(
247 const base::Version& version,
248 const base::FilePath& path,
249 scoped_ptr<base::DictionaryValue> manifest) override;
250 base::FilePath GetBaseDirectory() const override;
251 void GetHash(std::vector<uint8_t>* hash) const override;
252 std::string GetName() const override;
254 // Checks and updates CDM adapter if necessary to make sure the latest CDM
255 // adapter is always used.
256 // Note: The component is ready when CDM is present, but the CDM won't be
257 // registered until the adapter is copied by this function (see
258 // VerifyInstallation).
259 void UpdateCdmAdapter(const base::Version& cdm_version,
260 const base::FilePath& cdm_install_dir,
261 scoped_ptr<base::DictionaryValue> manifest);
263 DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits);
266 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
269 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
270 return true;
273 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
274 const base::DictionaryValue& manifest,
275 const base::FilePath& install_dir) {
276 return true;
279 // Once the CDM is ready, check the CDM adapter.
280 void WidevineCdmComponentInstallerTraits::ComponentReady(
281 const base::Version& version,
282 const base::FilePath& path,
283 scoped_ptr<base::DictionaryValue> manifest) {
284 if (!IsCompatibleWithChrome(*manifest)) {
285 DLOG(WARNING) << "Installed Widevine CDM component is incompatible.";
286 return;
289 BrowserThread::PostBlockingPoolTask(
290 FROM_HERE,
291 base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter,
292 base::Unretained(this),
293 version,
294 path,
295 base::Passed(&manifest)));
298 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
299 const base::DictionaryValue& manifest,
300 const base::FilePath& install_dir) const {
301 return IsCompatibleWithChrome(manifest) &&
302 base::PathExists(GetPlatformDirectory(install_dir)
303 .AppendASCII(kWidevineCdmFileName));
306 // The base directory on Windows looks like:
307 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
308 base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
309 base::FilePath result;
310 PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result);
311 return result;
314 void WidevineCdmComponentInstallerTraits::GetHash(
315 std::vector<uint8_t>* hash) const {
316 hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash));
319 std::string WidevineCdmComponentInstallerTraits::GetName() const {
320 return kWidevineCdmManifestName;
323 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
324 const base::Version& cdm_version,
325 const base::FilePath& cdm_install_dir,
326 scoped_ptr<base::DictionaryValue> manifest) {
327 const base::FilePath adapter_version_path =
328 GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName);
329 const base::FilePath adapter_install_path =
330 GetPlatformDirectory(cdm_install_dir)
331 .AppendASCII(kWidevineCdmAdapterFileName);
333 const std::string chrome_version = version_info::GetVersionNumber();
334 DCHECK(!chrome_version.empty());
335 std::string adapter_version;
336 if (!base::ReadFileToString(adapter_version_path, &adapter_version) ||
337 adapter_version != chrome_version ||
338 !base::PathExists(adapter_install_path)) {
339 int bytes_written = base::WriteFile(
340 adapter_version_path, chrome_version.data(), chrome_version.size());
341 if (bytes_written < 0 ||
342 static_cast<size_t>(bytes_written) != chrome_version.size()) {
343 DLOG(WARNING) << "Failed to write Widevine CDM adapter version file.";
344 // Ignore version file writing failure and try to copy the CDM adapter.
347 base::FilePath adapter_source_path;
348 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
349 if (!base::CopyFile(adapter_source_path, adapter_install_path)) {
350 DLOG(WARNING) << "Failed to copy Widevine CDM adapter.";
351 return;
355 BrowserThread::PostTask(content::BrowserThread::UI,
356 FROM_HERE,
357 base::Bind(&RegisterWidevineCdmWithChrome,
358 cdm_version,
359 adapter_install_path,
360 base::Passed(&manifest)));
363 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
365 void RegisterWidevineCdmComponent(ComponentUpdateService* cus) {
366 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
367 base::FilePath adapter_source_path;
368 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
369 if (!base::PathExists(adapter_source_path))
370 return;
371 scoped_ptr<ComponentInstallerTraits> traits(
372 new WidevineCdmComponentInstallerTraits);
373 // |cus| will take ownership of |installer| during installer->Register(cus).
374 DefaultComponentInstaller* installer =
375 new DefaultComponentInstaller(traits.Pass());
376 installer->Register(cus, base::Closure());
377 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
380 } // namespace component_updater