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