Home | History | Annotate | Download | only in component_updater
      1 // Copyright (c) 2012 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/swiftshader_component_installer.h"
      6 
      7 #include <stdint.h>
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/base_paths.h"
     12 #include "base/bind.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/files/file_enumerator.h"
     15 #include "base/files/file_path.h"
     16 #include "base/files/file_util.h"
     17 #include "base/logging.h"
     18 #include "base/path_service.h"
     19 #include "base/strings/string_util.h"
     20 #include "base/values.h"
     21 #include "components/component_updater/component_updater_paths.h"
     22 #include "components/component_updater/component_updater_service.h"
     23 #include "content/public/browser/browser_thread.h"
     24 #include "content/public/browser/gpu_data_manager.h"
     25 #include "content/public/browser/gpu_data_manager_observer.h"
     26 #include "gpu/config/gpu_feature_type.h"
     27 
     28 using content::BrowserThread;
     29 using content::GpuDataManager;
     30 
     31 namespace component_updater {
     32 
     33 namespace {
     34 
     35 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
     36 const uint8_t kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
     37                              0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
     38                              0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
     39                              0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
     40 
     41 // File name of the internal SwiftShader plugin on different platforms.
     42 const base::FilePath::CharType kSwiftShaderEglName[] =
     43     FILE_PATH_LITERAL("libegl.dll");
     44 const base::FilePath::CharType kSwiftShaderGlesName[] =
     45     FILE_PATH_LITERAL("libglesv2.dll");
     46 
     47 const char kSwiftShaderManifestName[] = "SwiftShader";
     48 
     49 // If we don't have a SwiftShader component, this is the version we claim.
     50 const char kNullVersion[] = "0.0.0.0";
     51 
     52 // The base directory on windows looks like:
     53 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
     54 base::FilePath GetSwiftShaderBaseDirectory() {
     55   base::FilePath result;
     56   if (!PathService::Get(DIR_SWIFT_SHADER, &result))
     57     NOTREACHED() << "Couldn't get SwiftShader directory.";
     58   return result;
     59 }
     60 
     61 // SwiftShader has version encoded in the path itself
     62 // so we need to enumerate the directories to find the full path.
     63 // On success it returns something like:
     64 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
     65 bool GetLatestSwiftShaderDirectory(base::FilePath* result,
     66                                    Version* latest,
     67                                    std::vector<base::FilePath>* older_dirs) {
     68   base::FilePath base_dir = GetSwiftShaderBaseDirectory();
     69   bool found = false;
     70   base::FileEnumerator file_enumerator(
     71       base_dir, false, base::FileEnumerator::DIRECTORIES);
     72   for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
     73        path = file_enumerator.Next()) {
     74     Version version(path.BaseName().MaybeAsASCII());
     75     if (!version.IsValid())
     76       continue;
     77     if (version.CompareTo(*latest) > 0 &&
     78         base::PathExists(path.Append(kSwiftShaderEglName)) &&
     79         base::PathExists(path.Append(kSwiftShaderGlesName))) {
     80       if (found && older_dirs)
     81         older_dirs->push_back(*result);
     82       *latest = version;
     83       *result = path;
     84       found = true;
     85     } else {
     86       if (older_dirs)
     87         older_dirs->push_back(path);
     88     }
     89   }
     90   return found;
     91 }
     92 
     93 void RegisterSwiftShaderWithChrome(const base::FilePath& path) {
     94   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     95   GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
     96 }
     97 
     98 class SwiftShaderComponentInstaller : public ComponentInstaller {
     99  public:
    100   explicit SwiftShaderComponentInstaller(const Version& version);
    101 
    102   virtual ~SwiftShaderComponentInstaller() {}
    103 
    104   virtual void OnUpdateError(int error) OVERRIDE;
    105 
    106   virtual bool Install(const base::DictionaryValue& manifest,
    107                        const base::FilePath& unpack_path) OVERRIDE;
    108 
    109   virtual bool GetInstalledFile(const std::string& file,
    110                                 base::FilePath* installed_file) OVERRIDE;
    111 
    112  private:
    113   Version current_version_;
    114 };
    115 
    116 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
    117     const Version& version)
    118     : current_version_(version) {
    119   DCHECK(version.IsValid());
    120 }
    121 
    122 void SwiftShaderComponentInstaller::OnUpdateError(int error) {
    123   NOTREACHED() << "SwiftShader update error: " << error;
    124 }
    125 
    126 bool SwiftShaderComponentInstaller::Install(
    127     const base::DictionaryValue& manifest,
    128     const base::FilePath& unpack_path) {
    129   std::string name;
    130   manifest.GetStringASCII("name", &name);
    131   if (name != kSwiftShaderManifestName)
    132     return false;
    133   std::string proposed_version;
    134   manifest.GetStringASCII("version", &proposed_version);
    135   Version version(proposed_version.c_str());
    136   if (!version.IsValid())
    137     return false;
    138   if (current_version_.CompareTo(version) >= 0)
    139     return false;
    140   if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
    141       !base::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
    142     return false;
    143   // Passed the basic tests. Time to install it.
    144   base::FilePath path =
    145       GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
    146   if (base::PathExists(path))
    147     return false;
    148   if (!base::Move(unpack_path, path))
    149     return false;
    150   // Installation is done. Now tell the rest of chrome.
    151   current_version_ = version;
    152   BrowserThread::PostTask(BrowserThread::UI,
    153                           FROM_HERE,
    154                           base::Bind(&RegisterSwiftShaderWithChrome, path));
    155   return true;
    156 }
    157 
    158 bool SwiftShaderComponentInstaller::GetInstalledFile(
    159     const std::string& file,
    160     base::FilePath* installed_file) {
    161   return false;
    162 }
    163 
    164 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
    165                                          const Version& version) {
    166   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    167 
    168   CrxComponent swiftshader;
    169   swiftshader.name = "Swift Shader";
    170   swiftshader.installer = new SwiftShaderComponentInstaller(version);
    171   swiftshader.version = version;
    172   swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
    173   if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) {
    174     NOTREACHED() << "SwiftShader component registration fail";
    175   }
    176 }
    177 
    178 class UpdateChecker : public content::GpuDataManagerObserver {
    179  public:
    180   explicit UpdateChecker(ComponentUpdateService* cus);
    181 
    182   virtual void OnGpuInfoUpdate() OVERRIDE;
    183 
    184  private:
    185   ComponentUpdateService* cus_;
    186 };
    187 
    188 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) : cus_(cus) {
    189 }
    190 
    191 void UpdateChecker::OnGpuInfoUpdate() {
    192   GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance();
    193 
    194   if (!gpu_data_manager->GpuAccessAllowed(NULL) ||
    195       gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) ||
    196       gpu_data_manager->ShouldUseSwiftShader()) {
    197     gpu_data_manager->RemoveObserver(this);
    198     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    199     base::FilePath path = GetSwiftShaderBaseDirectory();
    200 
    201     Version version(kNullVersion);
    202     GetLatestSwiftShaderDirectory(&path, &version, NULL);
    203 
    204     BrowserThread::PostTask(
    205         BrowserThread::UI,
    206         FROM_HERE,
    207         base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
    208   }
    209 }
    210 
    211 #if defined(ENABLE_SWIFTSHADER)
    212 
    213 // Check if there already is a version of swiftshader installed,
    214 // and if so register it.
    215 void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
    216   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    217   base::FilePath path = GetSwiftShaderBaseDirectory();
    218   if (!base::PathExists(path)) {
    219     if (!base::CreateDirectory(path)) {
    220       NOTREACHED() << "Could not create SwiftShader directory.";
    221       return;
    222     }
    223   }
    224 
    225   Version version(kNullVersion);
    226   std::vector<base::FilePath> older_dirs;
    227   if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
    228     BrowserThread::PostTask(BrowserThread::UI,
    229                             FROM_HERE,
    230                             base::Bind(&RegisterSwiftShaderWithChrome, path));
    231 
    232   UpdateChecker* update_checker = new UpdateChecker(cus);
    233   GpuDataManager::GetInstance()->AddObserver(update_checker);
    234   update_checker->OnGpuInfoUpdate();
    235   // We leak update_checker here, because it has to stick around for the life
    236   // of the GpuDataManager.
    237 
    238   // Remove older versions of SwiftShader.
    239   for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
    240        iter != older_dirs.end();
    241        ++iter) {
    242     base::DeleteFile(*iter, true);
    243   }
    244 }
    245 
    246 #endif  // ENABLE_SWIFTSHADER
    247 
    248 }  // namespace
    249 
    250 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
    251 #if defined(ENABLE_SWIFTSHADER)
    252   BrowserThread::PostTask(BrowserThread::FILE,
    253                           FROM_HERE,
    254                           base::Bind(&RegisterSwiftShaderPath, cus));
    255 #endif
    256 }
    257 
    258 }  // namespace component_updater
    259