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