Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / widevine_cdm_component_installer.cc
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/browser/component_updater/component_updater_service.h"
26 #include "chrome/browser/component_updater/default_component_installer.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_version_info.h"
29 #include "chrome/common/widevine_cdm_constants.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
38 using content::BrowserThread;
39 using content::PluginService;
40
41 namespace component_updater {
42
43 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
44
45 namespace {
46
47 // CRX hash. The extension id is: oimompecagnajdejgnnjijobebaeigek.
48 const 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.
54 const 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.
58 const char kCdmAdapterVersionName[] = "CdmAdapterVersion";
59
60 // Name of the Widevine CDM OS in the component manifest.
61 const 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.
71 const 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".
84 const char kCdmValueDelimiter = ',';
85 COMPILE_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.
93 const char kCdmModuleVersionsName[] = "x-cdm-module-versions";
94 //    Matches supported ContentDecryptionModule_* version(s).
95 const char kCdmInterfaceVersionsName[] = "x-cdm-interface-versions";
96 //    Matches supported Host_* version(s).
97 const 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.
100 const 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.
105 base::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
112 bool 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                              std::string(" (version: ") + version.GetString() +
131                              ")";
132   plugin_info->version = version.GetString();
133   content::WebPluginMimeType widevine_cdm_mime_type(
134       kWidevineCdmPluginMimeType,
135       kWidevineCdmPluginExtension,
136       kWidevineCdmPluginMimeTypeDescription);
137   widevine_cdm_mime_type.additional_param_names = additional_param_names;
138   widevine_cdm_mime_type.additional_param_values = additional_param_values;
139   plugin_info->mime_types.push_back(widevine_cdm_mime_type);
140   plugin_info->permissions = kWidevineCdmPluginPermissions;
141
142   return true;
143 }
144
145 typedef bool (*VersionCheckFunc)(int version);
146
147 bool CheckForCompatibleVersion(const base::DictionaryValue& manifest,
148                                const std::string version_name,
149                                VersionCheckFunc version_check_func) {
150   std::string versions_string;
151   if (!manifest.GetString(version_name, &versions_string)) {
152     DLOG(WARNING) << "Widevine CDM component manifest missing " << version_name;
153     return false;
154   }
155   DLOG_IF(WARNING, versions_string.empty())
156       << "Widevine CDM component manifest has empty " << version_name;
157
158   std::vector<std::string> versions;
159   base::SplitString(versions_string, kCdmValueDelimiter, &versions);
160
161   for (size_t i = 0; i < versions.size(); ++i) {
162     int version = 0;
163     if (base::StringToInt(versions[i], &version))
164       if (version_check_func(version))
165         return true;
166   }
167
168   DLOG(WARNING) << "Widevine CDM component manifest has no supported "
169                 << version_name << " in '" << versions_string << "'";
170   return false;
171 }
172
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);
188 }
189
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";
202   }
203 }
204
205 void RegisterWidevineCdmWithChrome(const base::Version& cdm_version,
206                                    const base::FilePath& adapter_install_path,
207                                    scoped_ptr<base::DictionaryValue> manifest) {
208   DCHECK(BrowserThread::CurrentlyOn(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;
220   }
221
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);
229 }
230
231 }  // namespace
232
233 class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits {
234  public:
235   WidevineCdmComponentInstallerTraits();
236   virtual ~WidevineCdmComponentInstallerTraits() {}
237
238  private:
239   // The following methods override ComponentInstallerTraits.
240   virtual bool CanAutoUpdate() const OVERRIDE;
241   virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
242                                const base::FilePath& install_dir) OVERRIDE;
243   virtual bool VerifyInstallation(
244       const base::FilePath& install_dir) const OVERRIDE;
245   virtual void ComponentReady(
246       const base::Version& version,
247       const base::FilePath& path,
248       scoped_ptr<base::DictionaryValue> manifest) OVERRIDE;
249   virtual base::FilePath GetBaseDirectory() const OVERRIDE;
250   virtual void GetHash(std::vector<uint8>* hash) const OVERRIDE;
251   virtual std::string GetName() const OVERRIDE;
252
253   // Checks and updates CDM adapter if necessary to make sure the latest CDM
254   // adapter is always used.
255   // Note: The component is ready when CDM is present, but the CDM won't be
256   // registered until the adapter is copied by this function (see
257   // VerifyInstallation).
258   void UpdateCdmAdapter(const base::Version& cdm_version,
259                         const base::FilePath& cdm_install_dir,
260                         scoped_ptr<base::DictionaryValue> manifest);
261
262   DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits);
263 };
264
265 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
266 }
267
268 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
269   return true;
270 }
271
272 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
273     const base::DictionaryValue& manifest,
274     const base::FilePath& install_dir) {
275   return true;
276 }
277
278 // Once the CDM is ready, check the CDM adapter.
279 void WidevineCdmComponentInstallerTraits::ComponentReady(
280     const base::Version& version,
281     const base::FilePath& path,
282     scoped_ptr<base::DictionaryValue> manifest) {
283   if (!IsCompatibleWithChrome(*manifest)) {
284     DLOG(WARNING) << "Installed Widevine CDM component is incompatible.";
285     return;
286   }
287
288   BrowserThread::PostBlockingPoolTask(
289       FROM_HERE,
290       base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter,
291                  base::Unretained(this),
292                  version,
293                  path,
294                  base::Passed(&manifest)));
295 }
296
297 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
298     const base::FilePath& install_dir) const {
299   return base::PathExists(
300       GetPlatformDirectory(install_dir).AppendASCII(kWidevineCdmFileName));
301 }
302
303 // The base directory on Windows looks like:
304 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
305 base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
306   base::FilePath result;
307   PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result);
308   return result;
309 }
310
311 void WidevineCdmComponentInstallerTraits::GetHash(
312     std::vector<uint8>* hash) const {
313   hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash));
314 }
315
316 std::string WidevineCdmComponentInstallerTraits::GetName() const {
317   return kWidevineCdmManifestName;
318 }
319
320 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
321     const base::Version& cdm_version,
322     const base::FilePath& cdm_install_dir,
323     scoped_ptr<base::DictionaryValue> manifest) {
324   const base::FilePath adapter_version_path =
325       GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName);
326   const base::FilePath adapter_install_path =
327       GetPlatformDirectory(cdm_install_dir)
328           .AppendASCII(kWidevineCdmAdapterFileName);
329
330   const std::string chrome_version = chrome::VersionInfo().Version();
331   DCHECK(!chrome_version.empty());
332   std::string adapter_version;
333   if (!base::ReadFileToString(adapter_version_path, &adapter_version) ||
334       adapter_version != chrome_version ||
335       !base::PathExists(adapter_install_path)) {
336     int bytes_written = base::WriteFile(
337         adapter_version_path, chrome_version.data(), chrome_version.size());
338     if (bytes_written < 0 ||
339         static_cast<size_t>(bytes_written) != chrome_version.size()) {
340       DLOG(WARNING) << "Failed to write Widevine CDM adapter version file.";
341       // Ignore version file writing failure and try to copy the CDM adapter.
342     }
343
344     base::FilePath adapter_source_path;
345     PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
346     if (!base::CopyFile(adapter_source_path, adapter_install_path)) {
347       DLOG(WARNING) << "Failed to copy Widevine CDM adapter.";
348       return;
349     }
350   }
351
352   BrowserThread::PostTask(content::BrowserThread::UI,
353                           FROM_HERE,
354                           base::Bind(&RegisterWidevineCdmWithChrome,
355                                      cdm_version,
356                                      adapter_install_path,
357                                      base::Passed(&manifest)));
358 }
359
360 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
361
362 void RegisterWidevineCdmComponent(ComponentUpdateService* cus) {
363 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
364   base::FilePath adapter_source_path;
365   PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
366   if (!base::PathExists(adapter_source_path))
367     return;
368   scoped_ptr<ComponentInstallerTraits> traits(
369       new WidevineCdmComponentInstallerTraits);
370   // |cus| will take ownership of |installer| during installer->Register(cus).
371   DefaultComponentInstaller* installer =
372       new DefaultComponentInstaller(traits.Pass());
373   installer->Register(cus);
374 #else
375   return;
376 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
377 }
378
379 }  // namespace component_updater