Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / component_updater / widevine_cdm_component_installer.cc
blob28b67ab9c37cc8e0d53f44a02cb8709013f08cf2
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/chrome_version_info.h"
28 #include "chrome/common/widevine_cdm_constants.h"
29 #include "components/component_updater/component_updater_service.h"
30 #include "components/component_updater/default_component_installer.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 std::vector<std::string> versions;
160 base::SplitString(versions_string, kCdmValueDelimiter, &versions);
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;
169 DLOG(WARNING) << "Widevine CDM component manifest has no supported "
170 << version_name << " in '" << versions_string << "'";
171 return false;
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.
179 bool IsCompatibleWithChrome(const base::DictionaryValue& manifest) {
180 return CheckForCompatibleVersion(manifest,
181 kCdmModuleVersionsName,
182 media::IsSupportedCdmModuleVersion) &&
183 CheckForCompatibleVersion(manifest,
184 kCdmInterfaceVersionsName,
185 media::IsSupportedCdmInterfaceVersion) &&
186 CheckForCompatibleVersion(manifest,
187 kCdmHostVersionsName,
188 media::IsSupportedCdmHostVersion);
191 void GetAdditionalParams(const base::DictionaryValue& manifest,
192 std::vector<base::string16>* additional_param_names,
193 std::vector<base::string16>* additional_param_values) {
194 base::string16 codecs;
195 if (manifest.GetString(kCdmCodecsListName, &codecs)) {
196 DLOG_IF(WARNING, codecs.empty())
197 << "Widevine CDM component manifest has empty codecs list";
198 additional_param_names->push_back(
199 base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
200 additional_param_values->push_back(codecs);
201 } else {
202 DLOG(WARNING) << "Widevine CDM component manifest is missing codecs";
206 void RegisterWidevineCdmWithChrome(const base::Version& cdm_version,
207 const base::FilePath& adapter_install_path,
208 scoped_ptr<base::DictionaryValue> manifest) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 std::vector<base::string16> additional_param_names;
211 std::vector<base::string16> additional_param_values;
212 GetAdditionalParams(
213 *manifest, &additional_param_names, &additional_param_values);
214 content::PepperPluginInfo plugin_info;
215 if (!MakeWidevineCdmPluginInfo(cdm_version,
216 adapter_install_path,
217 additional_param_names,
218 additional_param_values,
219 &plugin_info)) {
220 return;
223 // true = Add to beginning of list to override any existing registrations.
224 PluginService::GetInstance()->RegisterInternalPlugin(
225 plugin_info.ToWebPluginInfo(), true);
226 // Tell the browser to refresh the plugin list. Then tell all renderers to
227 // update their plugin list caches.
228 PluginService::GetInstance()->RefreshPlugins();
229 PluginService::GetInstance()->PurgePluginListCache(NULL, false);
232 } // namespace
234 class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits {
235 public:
236 WidevineCdmComponentInstallerTraits();
237 ~WidevineCdmComponentInstallerTraits() override {}
239 private:
240 // The following methods override ComponentInstallerTraits.
241 bool CanAutoUpdate() const override;
242 bool OnCustomInstall(const base::DictionaryValue& manifest,
243 const base::FilePath& install_dir) override;
244 bool VerifyInstallation(
245 const base::DictionaryValue& manifest,
246 const base::FilePath& install_dir) const override;
247 void ComponentReady(
248 const base::Version& version,
249 const base::FilePath& path,
250 scoped_ptr<base::DictionaryValue> manifest) override;
251 base::FilePath GetBaseDirectory() const override;
252 void GetHash(std::vector<uint8_t>* hash) const override;
253 std::string GetName() const override;
255 // Checks and updates CDM adapter if necessary to make sure the latest CDM
256 // adapter is always used.
257 // Note: The component is ready when CDM is present, but the CDM won't be
258 // registered until the adapter is copied by this function (see
259 // VerifyInstallation).
260 void UpdateCdmAdapter(const base::Version& cdm_version,
261 const base::FilePath& cdm_install_dir,
262 scoped_ptr<base::DictionaryValue> manifest);
264 DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits);
267 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
270 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
271 return true;
274 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
275 const base::DictionaryValue& manifest,
276 const base::FilePath& install_dir) {
277 return true;
280 // Once the CDM is ready, check the CDM adapter.
281 void WidevineCdmComponentInstallerTraits::ComponentReady(
282 const base::Version& version,
283 const base::FilePath& path,
284 scoped_ptr<base::DictionaryValue> manifest) {
285 if (!IsCompatibleWithChrome(*manifest)) {
286 DLOG(WARNING) << "Installed Widevine CDM component is incompatible.";
287 return;
290 BrowserThread::PostBlockingPoolTask(
291 FROM_HERE,
292 base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter,
293 base::Unretained(this),
294 version,
295 path,
296 base::Passed(&manifest)));
299 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
300 const base::DictionaryValue& manifest,
301 const base::FilePath& install_dir) const {
302 return IsCompatibleWithChrome(manifest) &&
303 base::PathExists(GetPlatformDirectory(install_dir)
304 .AppendASCII(kWidevineCdmFileName));
307 // The base directory on Windows looks like:
308 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
309 base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
310 base::FilePath result;
311 PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result);
312 return result;
315 void WidevineCdmComponentInstallerTraits::GetHash(
316 std::vector<uint8_t>* hash) const {
317 hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash));
320 std::string WidevineCdmComponentInstallerTraits::GetName() const {
321 return kWidevineCdmManifestName;
324 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
325 const base::Version& cdm_version,
326 const base::FilePath& cdm_install_dir,
327 scoped_ptr<base::DictionaryValue> manifest) {
328 const base::FilePath adapter_version_path =
329 GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName);
330 const base::FilePath adapter_install_path =
331 GetPlatformDirectory(cdm_install_dir)
332 .AppendASCII(kWidevineCdmAdapterFileName);
334 const std::string chrome_version = chrome::VersionInfo().Version();
335 DCHECK(!chrome_version.empty());
336 std::string adapter_version;
337 if (!base::ReadFileToString(adapter_version_path, &adapter_version) ||
338 adapter_version != chrome_version ||
339 !base::PathExists(adapter_install_path)) {
340 int bytes_written = base::WriteFile(
341 adapter_version_path, chrome_version.data(), chrome_version.size());
342 if (bytes_written < 0 ||
343 static_cast<size_t>(bytes_written) != chrome_version.size()) {
344 DLOG(WARNING) << "Failed to write Widevine CDM adapter version file.";
345 // Ignore version file writing failure and try to copy the CDM adapter.
348 base::FilePath adapter_source_path;
349 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
350 if (!base::CopyFile(adapter_source_path, adapter_install_path)) {
351 DLOG(WARNING) << "Failed to copy Widevine CDM adapter.";
352 return;
356 BrowserThread::PostTask(content::BrowserThread::UI,
357 FROM_HERE,
358 base::Bind(&RegisterWidevineCdmWithChrome,
359 cdm_version,
360 adapter_install_path,
361 base::Passed(&manifest)));
364 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
366 void RegisterWidevineCdmComponent(ComponentUpdateService* cus) {
367 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
368 base::FilePath adapter_source_path;
369 PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
370 if (!base::PathExists(adapter_source_path))
371 return;
372 scoped_ptr<ComponentInstallerTraits> traits(
373 new WidevineCdmComponentInstallerTraits);
374 // |cus| will take ownership of |installer| during installer->Register(cus).
375 DefaultComponentInstaller* installer =
376 new DefaultComponentInstaller(traits.Pass());
377 installer->Register(cus);
378 #else
379 return;
380 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
383 } // namespace component_updater