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"
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 "components/update_client/update_client.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/gpu_data_manager.h"
26 #include "content/public/browser/gpu_data_manager_observer.h"
27 #include "gpu/config/gpu_feature_type.h"
29 using content::BrowserThread
;
30 using content::GpuDataManager
;
32 namespace component_updater
{
36 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
37 const uint8_t kSha2Hash
[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
38 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
39 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
40 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
42 // File name of the internal SwiftShader plugin on different platforms.
43 const base::FilePath::CharType kSwiftShaderEglName
[] =
44 FILE_PATH_LITERAL("libegl.dll");
45 const base::FilePath::CharType kSwiftShaderGlesName
[] =
46 FILE_PATH_LITERAL("libglesv2.dll");
48 const char kSwiftShaderManifestName
[] = "SwiftShader";
50 // If we don't have a SwiftShader component, this is the version we claim.
51 const char kNullVersion
[] = "0.0.0.0";
53 // The base directory on windows looks like:
54 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
55 base::FilePath
GetSwiftShaderBaseDirectory() {
56 base::FilePath result
;
57 if (!PathService::Get(DIR_SWIFT_SHADER
, &result
))
58 NOTREACHED() << "Couldn't get SwiftShader directory.";
62 // SwiftShader has version encoded in the path itself
63 // so we need to enumerate the directories to find the full path.
64 // On success it returns something like:
65 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
66 bool GetLatestSwiftShaderDirectory(base::FilePath
* result
,
68 std::vector
<base::FilePath
>* older_dirs
) {
69 base::FilePath base_dir
= GetSwiftShaderBaseDirectory();
71 base::FileEnumerator
file_enumerator(
72 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
73 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
74 path
= file_enumerator
.Next()) {
75 Version
version(path
.BaseName().MaybeAsASCII());
76 if (!version
.IsValid())
78 if (version
.CompareTo(*latest
) > 0 &&
79 base::PathExists(path
.Append(kSwiftShaderEglName
)) &&
80 base::PathExists(path
.Append(kSwiftShaderGlesName
))) {
81 if (found
&& older_dirs
)
82 older_dirs
->push_back(*result
);
88 older_dirs
->push_back(path
);
94 void RegisterSwiftShaderWithChrome(const base::FilePath
& path
) {
95 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
96 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path
);
99 class SwiftShaderComponentInstaller
: public update_client::CrxInstaller
{
101 explicit SwiftShaderComponentInstaller(const Version
& version
);
103 // ComponentInstaller implementation:
104 void OnUpdateError(int error
) override
;
106 bool Install(const base::DictionaryValue
& manifest
,
107 const base::FilePath
& unpack_path
) override
;
109 bool GetInstalledFile(const std::string
& file
,
110 base::FilePath
* installed_file
) override
;
112 bool Uninstall() override
;
115 ~SwiftShaderComponentInstaller() override
{}
117 Version current_version_
;
120 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
121 const Version
& version
)
122 : current_version_(version
) {
123 DCHECK(version
.IsValid());
126 void SwiftShaderComponentInstaller::OnUpdateError(int error
) {
127 NOTREACHED() << "SwiftShader update error: " << error
;
130 bool SwiftShaderComponentInstaller::Install(
131 const base::DictionaryValue
& manifest
,
132 const base::FilePath
& unpack_path
) {
134 manifest
.GetStringASCII("name", &name
);
135 if (name
!= kSwiftShaderManifestName
)
137 std::string proposed_version
;
138 manifest
.GetStringASCII("version", &proposed_version
);
139 Version
version(proposed_version
.c_str());
140 if (!version
.IsValid())
142 if (current_version_
.CompareTo(version
) >= 0)
144 if (!base::PathExists(unpack_path
.Append(kSwiftShaderEglName
)) ||
145 !base::PathExists(unpack_path
.Append(kSwiftShaderGlesName
)))
147 // Passed the basic tests. Time to install it.
148 base::FilePath path
=
149 GetSwiftShaderBaseDirectory().AppendASCII(version
.GetString());
150 if (base::PathExists(path
))
152 if (!base::Move(unpack_path
, path
))
154 // Installation is done. Now tell the rest of chrome.
155 current_version_
= version
;
156 BrowserThread::PostTask(BrowserThread::UI
,
158 base::Bind(&RegisterSwiftShaderWithChrome
, path
));
162 bool SwiftShaderComponentInstaller::GetInstalledFile(
163 const std::string
& file
,
164 base::FilePath
* installed_file
) {
168 bool SwiftShaderComponentInstaller::Uninstall() {
172 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService
* cus
,
173 const Version
& version
) {
174 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
176 update_client::CrxComponent swiftshader
;
177 swiftshader
.name
= "Swift Shader";
178 swiftshader
.installer
= new SwiftShaderComponentInstaller(version
);
179 swiftshader
.version
= version
;
180 swiftshader
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
181 if (!cus
->RegisterComponent(swiftshader
)) {
182 NOTREACHED() << "SwiftShader component registration fail";
186 class UpdateChecker
: public content::GpuDataManagerObserver
{
188 explicit UpdateChecker(ComponentUpdateService
* cus
);
190 void OnGpuInfoUpdate() override
;
193 ComponentUpdateService
* cus_
;
196 UpdateChecker::UpdateChecker(ComponentUpdateService
* cus
) : cus_(cus
) {
199 void UpdateChecker::OnGpuInfoUpdate() {
200 GpuDataManager
* gpu_data_manager
= GpuDataManager::GetInstance();
202 if (!gpu_data_manager
->GpuAccessAllowed(NULL
) ||
203 gpu_data_manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
) ||
204 gpu_data_manager
->ShouldUseSwiftShader()) {
205 gpu_data_manager
->RemoveObserver(this);
206 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
207 base::FilePath path
= GetSwiftShaderBaseDirectory();
209 Version
version(kNullVersion
);
210 GetLatestSwiftShaderDirectory(&path
, &version
, NULL
);
212 BrowserThread::PostTask(
215 base::Bind(&FinishSwiftShaderUpdateRegistration
, cus_
, version
));
219 #if defined(ENABLE_SWIFTSHADER)
221 // Check if there already is a version of swiftshader installed,
222 // and if so register it.
223 void RegisterSwiftShaderPath(ComponentUpdateService
* cus
) {
224 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
225 base::FilePath path
= GetSwiftShaderBaseDirectory();
226 if (!base::PathExists(path
)) {
227 if (!base::CreateDirectory(path
)) {
228 NOTREACHED() << "Could not create SwiftShader directory.";
233 Version
version(kNullVersion
);
234 std::vector
<base::FilePath
> older_dirs
;
235 if (GetLatestSwiftShaderDirectory(&path
, &version
, &older_dirs
))
236 BrowserThread::PostTask(BrowserThread::UI
,
238 base::Bind(&RegisterSwiftShaderWithChrome
, path
));
240 UpdateChecker
* update_checker
= new UpdateChecker(cus
);
241 GpuDataManager::GetInstance()->AddObserver(update_checker
);
242 update_checker
->OnGpuInfoUpdate();
243 // We leak update_checker here, because it has to stick around for the life
244 // of the GpuDataManager.
246 // Remove older versions of SwiftShader.
247 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
248 iter
!= older_dirs
.end();
250 base::DeleteFile(*iter
, true);
254 #endif // ENABLE_SWIFTSHADER
258 void RegisterSwiftShaderComponent(ComponentUpdateService
* cus
) {
259 #if defined(ENABLE_SWIFTSHADER)
260 BrowserThread::PostTask(BrowserThread::FILE,
262 base::Bind(&RegisterSwiftShaderPath
, cus
));
266 } // namespace component_updater