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.
5 #include "chrome/browser/component_updater/swiftshader_component_installer.h"
7 #include "base/base_paths.h"
9 #include "base/compiler_specific.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"
25 using content::BrowserThread;
26 using content::GpuDataManager;
30 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
31 const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
32 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
33 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
34 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
36 // File name of the internal SwiftShader plugin on different platforms.
37 const base::FilePath::CharType kSwiftShaderEglName[] =
38 FILE_PATH_LITERAL("libegl.dll");
39 const base::FilePath::CharType kSwiftShaderGlesName[] =
40 FILE_PATH_LITERAL("libglesv2.dll");
42 const char kSwiftShaderManifestName[] = "SwiftShader";
44 const base::FilePath::CharType kSwiftShaderBaseDirectory[] =
45 FILE_PATH_LITERAL("SwiftShader");
47 // If we don't have a SwiftShader component, this is the version we claim.
48 const char kNullVersion[] = "0.0.0.0";
50 // The base directory on windows looks like:
51 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
52 base::FilePath GetSwiftShaderBaseDirectory() {
53 base::FilePath result;
54 PathService::Get(chrome::DIR_USER_DATA, &result);
55 return result.Append(kSwiftShaderBaseDirectory);
58 // SwiftShader has version encoded in the path itself
59 // so we need to enumerate the directories to find the full path.
60 // On success it returns something like:
61 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
62 bool GetLatestSwiftShaderDirectory(base::FilePath* result,
64 std::vector<base::FilePath>* older_dirs) {
65 base::FilePath base_dir = GetSwiftShaderBaseDirectory();
68 file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES);
69 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
70 path = file_enumerator.Next()) {
71 Version version(path.BaseName().MaybeAsASCII());
72 if (!version.IsValid())
74 if (version.CompareTo(*latest) > 0 &&
75 base::PathExists(path.Append(kSwiftShaderEglName)) &&
76 base::PathExists(path.Append(kSwiftShaderGlesName))) {
77 if (found && older_dirs)
78 older_dirs->push_back(*result);
84 older_dirs->push_back(path);
90 void RegisterSwiftShaderWithChrome(const base::FilePath& path) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
97 class SwiftShaderComponentInstaller : public ComponentInstaller {
99 explicit SwiftShaderComponentInstaller(const Version& version);
101 virtual ~SwiftShaderComponentInstaller() {}
103 virtual void OnUpdateError(int error) OVERRIDE;
105 virtual bool Install(const base::DictionaryValue& manifest,
106 const base::FilePath& unpack_path) OVERRIDE;
108 virtual bool GetInstalledFile(const std::string& file,
109 base::FilePath* installed_file) OVERRIDE;
112 Version current_version_;
115 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
116 const Version& version) : current_version_(version) {
117 DCHECK(version.IsValid());
120 void SwiftShaderComponentInstaller::OnUpdateError(int error) {
121 NOTREACHED() << "SwiftShader update error: " << error;
124 bool SwiftShaderComponentInstaller::Install(
125 const base::DictionaryValue& manifest,
126 const base::FilePath& unpack_path) {
128 manifest.GetStringASCII("name", &name);
129 if (name != kSwiftShaderManifestName)
131 std::string proposed_version;
132 manifest.GetStringASCII("version", &proposed_version);
133 Version version(proposed_version.c_str());
134 if (!version.IsValid())
136 if (current_version_.CompareTo(version) >= 0)
138 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
139 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
141 // Passed the basic tests. Time to install it.
142 base::FilePath path =
143 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
144 if (base::PathExists(path))
146 if (!base::Move(unpack_path, path))
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));
155 bool SwiftShaderComponentInstaller::GetInstalledFile(
156 const std::string& file, base::FilePath* installed_file) {
160 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
161 const Version& version) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
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";
174 class UpdateChecker : public content::GpuDataManagerObserver {
176 explicit UpdateChecker(ComponentUpdateService* cus);
178 virtual void OnGpuInfoUpdate() OVERRIDE;
181 ComponentUpdateService* cus_;
184 UpdateChecker::UpdateChecker(ComponentUpdateService* cus)
188 void UpdateChecker::OnGpuInfoUpdate() {
189 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance();
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();
198 Version version(kNullVersion);
199 GetLatestSwiftShaderDirectory(&path, &version, NULL);
201 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
202 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
206 // Check if there already is a version of swiftshader installed,
207 // and if so register it.
208 void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
210 base::FilePath path = GetSwiftShaderBaseDirectory();
211 if (!base::PathExists(path)) {
212 if (!file_util::CreateDirectory(path)) {
213 NOTREACHED() << "Could not create SwiftShader directory.";
218 Version version(kNullVersion);
219 std::vector<base::FilePath> older_dirs;
220 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
222 base::Bind(&RegisterSwiftShaderWithChrome, path));
224 UpdateChecker *update_checker = new UpdateChecker(cus);
225 GpuDataManager::GetInstance()->AddObserver(update_checker);
226 update_checker->OnGpuInfoUpdate();
227 // We leak update_checker here, because it has to stick around for the life
228 // of the GpuDataManager.
230 // Remove older versions of SwiftShader.
231 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
232 iter != older_dirs.end(); ++iter) {
233 base::DeleteFile(*iter, true);
237 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
238 #if defined(ENABLE_SWIFTSHADER)
243 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
244 base::Bind(&RegisterSwiftShaderPath, cus));