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 <string.h>
      8 
      9 #include <vector>
     10 
     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"
     36 
     37 #include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR.
     38 
     39 using content::BrowserThread;
     40 using content::PluginService;
     41 
     42 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
     43 
     44 namespace {
     45 
     46 // CRX hash. The extension id is: oimompecagnajdejgnnjijobebaeigek.
     47 const uint8 kSha2Hash[] = { 0xe8, 0xce, 0xcf, 0x42, 0x06, 0xd0, 0x93, 0x49,
     48                             0x6d, 0xd9, 0x89, 0xe1, 0x41, 0x04, 0x86, 0x4a,
     49                             0x8f, 0xbd, 0x86, 0x12, 0xb9, 0x58, 0x9b, 0xfb,
     50                             0x4f, 0xbb, 0x1b, 0xa9, 0xd3, 0x85, 0x37, 0xef };
     51 
     52 // File name of the Widevine CDM component manifest on different platforms.
     53 const char kWidevineCdmManifestName[] = "WidevineCdm";
     54 
     55 // File name of the Widevine CDM adapter version file. The CDM adapter shares
     56 // the same version number with Chromium version.
     57 const char kCdmAdapterVersionName[] = "CdmAdapterVersion";
     58 
     59 // Name of the Widevine CDM OS in the component manifest.
     60 const char kWidevineCdmPlatform[] =
     61 #if defined(OS_MACOSX)
     62     "mac";
     63 #elif defined(OS_WIN)
     64     "win";
     65 #else  // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
     66     "linux";
     67 #endif
     68 
     69 // Name of the Widevine CDM architecture in the component manifest.
     70 const char kWidevineCdmArch[] =
     71 #if defined(ARCH_CPU_X86)
     72     "x86";
     73 #elif defined(ARCH_CPU_X86_64)
     74     "x64";
     75 #else  // TODO(viettrungluu): Support an ARM check?
     76     "???";
     77 #endif
     78 
     79 // The CDM manifest includes several custom values, all beginning with "x-cdm-".
     80 // All values are strings.
     81 // All values that are lists are delimited by commas. No trailing commas.
     82 // For example, "1,2,4".
     83 const char kCdmValueDelimiter = ',';
     84 COMPILE_ASSERT(kCdmValueDelimiter == kCdmSupportedCodecsValueDelimiter,
     85                cdm_delimiters_do_not_match);
     86 // The following entries are required.
     87 //  Interface versions are lists of integers (e.g. "1" or "1,2,4").
     88 //  These are checked in this file before registering the CDM.
     89 //  All match the interface versions from content_decryption_module.h that the
     90 //  CDM supports.
     91 //    Matches CDM_MODULE_VERSION.
     92 const char kCdmModuleVersionsName[] = "x-cdm-module-versions";
     93 //    Matches supported ContentDecryptionModule_* version(s).
     94 const char kCdmInterfaceVersionsName[] = "x-cdm-interface-versions";
     95 //    Matches supported Host_* version(s).
     96 const char kCdmHostVersionsName[] = "x-cdm-host-versions";
     97 //  The codecs list is a list of simple codec names (e.g. "vp8,vorbis").
     98 //  The list is passed to other parts of Chrome.
     99 const char kCdmCodecsListName[] = "x-cdm-codecs";
    100 
    101 // Widevine CDM is packaged as a multi-CRX. Widevine CDM binaries are located in
    102 // _platform_specific/<platform_arch> folder in the package. This function
    103 // returns the platform-specific subdirectory that is part of that multi-CRX.
    104 base::FilePath GetPlatformDirectory(const base::FilePath& base_path) {
    105   std::string platform_arch = kWidevineCdmPlatform;
    106   platform_arch += '_';
    107   platform_arch += kWidevineCdmArch;
    108   return base_path.AppendASCII("_platform_specific").AppendASCII(platform_arch);
    109 }
    110 
    111 bool MakeWidevineCdmPluginInfo(
    112     const base::Version& version,
    113     const base::FilePath& path,
    114     const std::vector<base::string16>& additional_param_names,
    115     const std::vector<base::string16>& additional_param_values,
    116     content::PepperPluginInfo* plugin_info) {
    117   if (!version.IsValid() ||
    118       version.components().size() !=
    119           static_cast<size_t>(kWidevineCdmVersionNumComponents)) {
    120     return false;
    121   }
    122 
    123   plugin_info->is_internal = false;
    124   // Widevine CDM must run out of process.
    125   plugin_info->is_out_of_process = true;
    126   plugin_info->path = path;
    127   plugin_info->name = kWidevineCdmDisplayName;
    128   plugin_info->description = kWidevineCdmDescription;
    129   plugin_info->version = version.GetString();
    130   content::WebPluginMimeType widevine_cdm_mime_type(
    131       kWidevineCdmPluginMimeType,
    132       kWidevineCdmPluginExtension,
    133       kWidevineCdmPluginMimeTypeDescription);
    134   widevine_cdm_mime_type.additional_param_names = additional_param_names;
    135   widevine_cdm_mime_type.additional_param_values = additional_param_values;
    136   plugin_info->mime_types.push_back(widevine_cdm_mime_type);
    137   plugin_info->permissions = kWidevineCdmPluginPermissions;
    138 
    139   return true;
    140 }
    141 
    142 typedef bool (*VersionCheckFunc)(int version);
    143 
    144 bool CheckForCompatibleVersion(const base::DictionaryValue& manifest,
    145                                const std::string version_name,
    146                                VersionCheckFunc version_check_func) {
    147   std::string versions_string;
    148   if (!manifest.GetString(version_name, &versions_string)) {
    149     DLOG(WARNING)
    150         << "Widevine CDM component manifest is missing " << version_name;
    151     // TODO(ddorwin): Remove this once all users have been updated.
    152     // The original manifests did not include this string, so add its version.
    153     if (version_name == kCdmModuleVersionsName)
    154       versions_string = "4";
    155     else if (version_name == kCdmInterfaceVersionsName)
    156       versions_string = "1";
    157     else if (version_name == kCdmHostVersionsName)
    158       versions_string = "1";
    159   }
    160   DLOG_IF(WARNING, versions_string.empty())
    161       << "Widevine CDM component manifest has empty " << version_name;
    162 
    163   std::vector<std::string> versions;
    164   base::SplitString(versions_string,
    165                     kCdmValueDelimiter,
    166                     &versions);
    167 
    168   for (size_t i = 0; i < versions.size(); ++i) {
    169     int version = 0;
    170     if (base::StringToInt(versions[i], &version))
    171       if (version_check_func(version))
    172         return true;
    173   }
    174 
    175   DLOG(WARNING) << "Widevine CDM component manifest has no supported "
    176                 << version_name << " in '" << versions_string << "'";
    177   return false;
    178 }
    179 
    180 // Returns whether the CDM's API versions, as specified in the manifest, are
    181 // compatible with this Chrome binary.
    182 // Checks the module API, CDM interface API, and Host API.
    183 // This should never fail except in rare cases where the component has not been
    184 // updated recently or the user downgrades Chrome.
    185 bool IsCompatibleWithChrome(const base::DictionaryValue& manifest) {
    186   return
    187       CheckForCompatibleVersion(manifest,
    188                                 kCdmModuleVersionsName,
    189                                 media::IsSupportedCdmModuleVersion) &&
    190       CheckForCompatibleVersion(manifest,
    191                                 kCdmInterfaceVersionsName,
    192                                 media::IsSupportedCdmInterfaceVersion) &&
    193       CheckForCompatibleVersion(manifest,
    194                                 kCdmHostVersionsName,
    195                                 media::IsSupportedCdmHostVersion);
    196 }
    197 
    198 void GetAdditionalParams(const base::DictionaryValue& manifest,
    199                          std::vector<base::string16>* additional_param_names,
    200                          std::vector<base::string16>* additional_param_values) {
    201   base::string16 codecs;
    202   if (manifest.GetString(kCdmCodecsListName, &codecs)) {
    203     DLOG_IF(WARNING, codecs.empty())
    204         << "Widevine CDM component manifest has empty codecs list";
    205     additional_param_names->push_back(
    206         base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
    207     additional_param_values->push_back(codecs);
    208   } else {
    209     DLOG(WARNING) << "Widevine CDM component manifest is missing codecs";
    210     // TODO(ddorwin): Remove this once all users have been updated.
    211     // The original manifests did not include this string, so add the base set.
    212     additional_param_names->push_back(
    213         base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
    214     additional_param_values->push_back(base::ASCIIToUTF16("vp8,vorbis"));
    215   }
    216 }
    217 
    218 void RegisterWidevineCdmWithChrome(const base::Version& cdm_version,
    219                                    const base::FilePath& adapter_install_path,
    220                                    scoped_ptr<base::DictionaryValue> manifest) {
    221   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    222   std::vector<base::string16> additional_param_names;
    223   std::vector<base::string16> additional_param_values;
    224   GetAdditionalParams(
    225       *manifest, &additional_param_names, &additional_param_values);
    226   content::PepperPluginInfo plugin_info;
    227   if (!MakeWidevineCdmPluginInfo(cdm_version,
    228                                  adapter_install_path,
    229                                  additional_param_names,
    230                                  additional_param_values,
    231                                  &plugin_info)) {
    232     return;
    233   }
    234 
    235   // true = Add to beginning of list to override any existing registrations.
    236   PluginService::GetInstance()->RegisterInternalPlugin(
    237       plugin_info.ToWebPluginInfo(), true);
    238   PluginService::GetInstance()->RefreshPlugins();
    239 }
    240 
    241 }  // namespace
    242 
    243 class WidevineCdmComponentInstallerTraits : public ComponentInstallerTraits {
    244  public:
    245   WidevineCdmComponentInstallerTraits();
    246   virtual ~WidevineCdmComponentInstallerTraits() {}
    247 
    248  private:
    249   // The following methods override ComponentInstallerTraits.
    250   virtual bool CanAutoUpdate() const OVERRIDE;
    251   virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
    252                                const base::FilePath& install_dir) OVERRIDE;
    253   virtual bool VerifyInstallation(
    254       const base::FilePath& install_dir) const OVERRIDE;
    255   virtual void ComponentReady(
    256       const base::Version& version,
    257       const base::FilePath& path,
    258       scoped_ptr<base::DictionaryValue> manifest) OVERRIDE;
    259   virtual base::FilePath GetBaseDirectory() const OVERRIDE;
    260   virtual void GetHash(std::vector<uint8>* hash) const OVERRIDE;
    261   virtual std::string GetName() const OVERRIDE;
    262 
    263   // Checks and updates CDM adapter if necessary to make sure the latest CDM
    264   // adapter is always used.
    265   // Note: The component is ready when CDM is present, but the CDM won't be
    266   // registered until the adapter is copied by this function (see
    267   // VerifyInstallation).
    268   void UpdateCdmAdapter(const base::Version& cdm_version,
    269                         const base::FilePath& cdm_install_dir,
    270                         scoped_ptr<base::DictionaryValue> manifest);
    271 
    272   DISALLOW_COPY_AND_ASSIGN(WidevineCdmComponentInstallerTraits);
    273 };
    274 
    275 WidevineCdmComponentInstallerTraits::WidevineCdmComponentInstallerTraits() {
    276 }
    277 
    278 bool WidevineCdmComponentInstallerTraits::CanAutoUpdate() const {
    279   return true;
    280 }
    281 
    282 bool WidevineCdmComponentInstallerTraits::OnCustomInstall(
    283     const base::DictionaryValue& manifest,
    284     const base::FilePath& install_dir) {
    285   return true;
    286 }
    287 
    288 // Once the CDM is ready, check the CDM adapter.
    289 void WidevineCdmComponentInstallerTraits::ComponentReady(
    290     const base::Version& version,
    291     const base::FilePath& path,
    292     scoped_ptr<base::DictionaryValue> manifest) {
    293   if (!IsCompatibleWithChrome(*manifest)) {
    294     DLOG(WARNING) << "Installed Widevine CDM component is incompatible.";
    295     return;
    296   }
    297 
    298   BrowserThread::PostBlockingPoolTask(
    299       FROM_HERE,
    300       base::Bind(&WidevineCdmComponentInstallerTraits::UpdateCdmAdapter,
    301                  base::Unretained(this),
    302                  version, path, base::Passed(&manifest)));
    303 }
    304 
    305 bool WidevineCdmComponentInstallerTraits::VerifyInstallation(
    306     const base::FilePath& install_dir) const {
    307   return base::PathExists(
    308       GetPlatformDirectory(install_dir).AppendASCII(kWidevineCdmFileName));
    309 }
    310 
    311 // The base directory on Windows looks like:
    312 // <profile>\AppData\Local\Google\Chrome\User Data\WidevineCdm\.
    313 base::FilePath WidevineCdmComponentInstallerTraits::GetBaseDirectory() const {
    314   base::FilePath result;
    315   PathService::Get(chrome::DIR_COMPONENT_WIDEVINE_CDM, &result);
    316   return result;
    317 }
    318 
    319 void WidevineCdmComponentInstallerTraits::GetHash(
    320     std::vector<uint8>* hash) const {
    321   hash->assign(kSha2Hash, kSha2Hash + arraysize(kSha2Hash));
    322 }
    323 
    324 std::string WidevineCdmComponentInstallerTraits::GetName() const {
    325   return kWidevineCdmManifestName;
    326 }
    327 
    328 void WidevineCdmComponentInstallerTraits::UpdateCdmAdapter(
    329     const base::Version& cdm_version,
    330     const base::FilePath& cdm_install_dir,
    331     scoped_ptr<base::DictionaryValue> manifest) {
    332   const base::FilePath adapter_version_path =
    333       GetPlatformDirectory(cdm_install_dir).AppendASCII(kCdmAdapterVersionName);
    334   const base::FilePath adapter_install_path =
    335       GetPlatformDirectory(cdm_install_dir)
    336           .AppendASCII(kWidevineCdmAdapterFileName);
    337 
    338   const std::string chrome_version = chrome::VersionInfo().Version();
    339   DCHECK(!chrome_version.empty());
    340   std::string adapter_version;
    341   if (!base::ReadFileToString(adapter_version_path, &adapter_version) ||
    342       adapter_version != chrome_version ||
    343       !base::PathExists(adapter_install_path)) {
    344     int bytes_written = file_util::WriteFile(
    345         adapter_version_path, chrome_version.data(), chrome_version.size());
    346     if (bytes_written < 0 ||
    347         static_cast<size_t>(bytes_written) != chrome_version.size()) {
    348       DLOG(WARNING) << "Failed to write Widevine CDM adapter version file.";
    349       // Ignore version file writing failure and try to copy the CDM adapter.
    350     }
    351 
    352     base::FilePath adapter_source_path;
    353     PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
    354     if (!base::CopyFile(adapter_source_path, adapter_install_path)) {
    355       DLOG(WARNING) << "Failed to copy Widevine CDM adapter.";
    356       return;
    357     }
    358   }
    359 
    360   BrowserThread::PostTask(content::BrowserThread::UI,
    361                           FROM_HERE,
    362                           base::Bind(&RegisterWidevineCdmWithChrome,
    363                                      cdm_version,
    364                                      adapter_install_path,
    365                                      base::Passed(&manifest)));
    366 }
    367 
    368 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    369 
    370 void RegisterWidevineCdmComponent(ComponentUpdateService* cus) {
    371 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    372   base::FilePath adapter_source_path;
    373   PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &adapter_source_path);
    374   if (!base::PathExists(adapter_source_path))
    375     return;
    376   scoped_ptr<ComponentInstallerTraits> traits(
    377       new WidevineCdmComponentInstallerTraits);
    378   // |cus| will take ownership of |installer| during installer->Register(cus).
    379   DefaultComponentInstaller* installer
    380       = new DefaultComponentInstaller(traits.Pass());
    381   installer->Register(cus);
    382 #else
    383   return;
    384 #endif  // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
    385 }
    386