Home | History | Annotate | Download | only in component_updater
      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 <stdint.h>
      8 #include <string.h>
      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/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"
     36 
     37 #include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR. NOLINT
     38 
     39 using content::BrowserThread;
     40 using content::PluginService;
     41 
     42 namespace component_updater {
     43 
     44 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
     45 
     46 namespace {
     47 
     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};
     53 
     54 // File name of the Widevine CDM component manifest on different platforms.
     55 const 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.
     59 const char kCdmAdapterVersionName[] = "CdmAdapterVersion";
     60 
     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
     70 
     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
     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".
     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
     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";
    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.
    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);
    111 }
    112 
    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;
    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                              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;
    142 
    143   return true;
    144 }
    145 
    146 typedef bool (*VersionCheckFunc)(int version);
    147 
    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;
    155   }
    156   DLOG_IF(WARNING, versions_string.empty())
    157       << "Widevine CDM component manifest has empty " << version_name;
    158 
    159   std::vector<std::string> versions;
    160   base::SplitString(versions_string, kCdmValueDelimiter, &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.
    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);
    189 }
    190 
    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";
    203   }
    204 }
    205 
    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;
    221   }
    222 
    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);
    230 }
    231 
    232 }  // namespace
    233 
    234 class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits {
    235  public:
    236   WidevineCdmComponentInstallerTraits();
    237   virtual ~WidevineCdmComponentInstallerTraits() {}
    238 
    239  private:
    240   // The following methods override ComponentInstallerTraits.
    241   virtual bool CanAutoUpdate() const OVERRIDE;
    242   virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
    243                                const base::FilePath& install_dir) OVERRIDE;
    244   virtual bool VerifyInstallation(
    245       const base::FilePath& install_dir) const OVERRIDE;
    246   virtual void ComponentReady(
    247       const base::Version& version,
    248       const base::FilePath& path,
    249       scoped_ptr<base::DictionaryValue> manifest) OVERRIDE;
    250   virtual base::FilePath GetBaseDirectory() const OVERRIDE;
    251   virtual void GetHash(std::vector<uint8_t>* hash) const OVERRIDE;
    252   virtual std::string GetName() const OVERRIDE;
    253 
    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);
    262 
    263   DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits);
    264 };
    265 
    266 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
    267 }
    268 
    269 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
    270   return true;
    271 }
    272 
    273 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
    274     const base::DictionaryValue& manifest,
    275     const base::FilePath& install_dir) {
    276   return true;
    277 }
    278 
    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;
    287   }
    288 
    289   BrowserThread::PostBlockingPoolTask(
    290       FROM_HERE,
    291       base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter,
    292                  base::Unretained(this),
    293                  version,
    294                  path,
    295                  base::Passed(&manifest)));
    296 }
    297 
    298 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
    299     const base::FilePath& install_dir) const {
    300   return base::PathExists(
    301       GetPlatformDirectory(install_dir).AppendASCII(kWidevineCdmFileName));
    302 }
    303 
    304 // The base directory on Windows looks like:
    305 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
    306 base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
    307   base::FilePath result;
    308   PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result);
    309   return result;
    310 }
    311 
    312 void WidevineCdmComponentInstallerTraits::GetHash(
    313     std::vector<uint8_t>* hash) const {
    314   hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash));
    315 }
    316 
    317 std::string WidevineCdmComponentInstallerTraits::GetName() const {
    318   return kWidevineCdmManifestName;
    319 }
    320 
    321 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
    322     const base::Version& cdm_version,
    323     const base::FilePath& cdm_install_dir,
    324     scoped_ptr<base::DictionaryValue> manifest) {
    325   const base::FilePath adapter_version_path =
    326       GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName);
    327   const base::FilePath adapter_install_path =
    328       GetPlatformDirectory(cdm_install_dir)
    329           .AppendASCII(kWidevineCdmAdapterFileName);
    330 
    331   const std::string chrome_version = chrome::VersionInfo().Version();
    332   DCHECK(!chrome_version.empty());
    333   std::string adapter_version;
    334   if (!base::ReadFileToString(adapter_version_path, &adapter_version) ||
    335       adapter_version != chrome_version ||
    336       !base::PathExists(adapter_install_path)) {
    337     int bytes_written = base::WriteFile(
    338         adapter_version_path, chrome_version.data(), chrome_version.size());
    339     if (bytes_written < 0 ||
    340         static_cast<size_t>(bytes_written) != chrome_version.size()) {
    341       DLOG(WARNING) << "Failed to write Widevine CDM adapter version file.";
    342       // Ignore version file writing failure and try to copy the CDM adapter.
    343     }
    344 
    345     base::FilePath adapter_source_path;
    346     PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
    347     if (!base::CopyFile(adapter_source_path, adapter_install_path)) {
    348       DLOG(WARNING) << "Failed to copy Widevine CDM adapter.";
    349       return;
    350     }
    351   }
    352 
    353   BrowserThread::PostTask(content::BrowserThread::UI,
    354                           FROM_HERE,
    355                           base::Bind(&RegisterWidevineCdmWithChrome,
    356                                      cdm_version,
    357                                      adapter_install_path,
    358                                      base::Passed(&manifest)));
    359 }
    360 
    361 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    362 
    363 void RegisterWidevineCdmComponent(ComponentUpdateService* cus) {
    364 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    365   base::FilePath adapter_source_path;
    366   PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
    367   if (!base::PathExists(adapter_source_path))
    368     return;
    369   scoped_ptr<ComponentInstallerTraits> traits(
    370       new WidevineCdmComponentInstallerTraits);
    371   // |cus| will take ownership of |installer| during installer->Register(cus).
    372   DefaultComponentInstaller* installer =
    373       new DefaultComponentInstaller(traits.Pass());
    374   installer->Register(cus);
    375 #else
    376   return;
    377 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    378 }
    379 
    380 }  // namespace component_updater
    381