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
;
28 namespace component_updater
{
32 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
33 const 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};
38 // File name of the internal SwiftShader plugin on different platforms.
39 const base::FilePath::CharType kSwiftShaderEglName
[] =
40 FILE_PATH_LITERAL("libegl.dll");
41 const base::FilePath::CharType kSwiftShaderGlesName
[] =
42 FILE_PATH_LITERAL("libglesv2.dll");
44 const char kSwiftShaderManifestName
[] = "SwiftShader";
46 const base::FilePath::CharType kSwiftShaderBaseDirectory
[] =
47 FILE_PATH_LITERAL("SwiftShader");
49 // If we don't have a SwiftShader component, this is the version we claim.
50 const char kNullVersion
[] = "0.0.0.0";
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 PathService::Get(chrome::DIR_USER_DATA
, &result
);
57 return result
.Append(kSwiftShaderBaseDirectory
);
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\.
64 bool GetLatestSwiftShaderDirectory(base::FilePath
* result
,
66 std::vector
<base::FilePath
>* older_dirs
) {
67 base::FilePath base_dir
= GetSwiftShaderBaseDirectory();
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())
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
);
86 older_dirs
->push_back(path
);
92 void RegisterSwiftShaderWithChrome(const base::FilePath
& path
) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
94 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path
);
99 class SwiftShaderComponentInstaller
: public ComponentInstaller
{
101 explicit SwiftShaderComponentInstaller(const Version
& version
);
103 virtual ~SwiftShaderComponentInstaller() {}
105 virtual void OnUpdateError(int error
) OVERRIDE
;
107 virtual bool Install(const base::DictionaryValue
& manifest
,
108 const base::FilePath
& unpack_path
) OVERRIDE
;
110 virtual bool GetInstalledFile(const std::string
& file
,
111 base::FilePath
* installed_file
) OVERRIDE
;
114 Version current_version_
;
117 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
118 const Version
& version
) : current_version_(version
) {
119 DCHECK(version
.IsValid());
122 void SwiftShaderComponentInstaller::OnUpdateError(int error
) {
123 NOTREACHED() << "SwiftShader update error: " << error
;
126 bool SwiftShaderComponentInstaller::Install(
127 const base::DictionaryValue
& manifest
,
128 const base::FilePath
& unpack_path
) {
130 manifest
.GetStringASCII("name", &name
);
131 if (name
!= kSwiftShaderManifestName
)
133 std::string proposed_version
;
134 manifest
.GetStringASCII("version", &proposed_version
);
135 Version
version(proposed_version
.c_str());
136 if (!version
.IsValid())
138 if (current_version_
.CompareTo(version
) >= 0)
140 if (!base::PathExists(unpack_path
.Append(kSwiftShaderEglName
)) ||
141 !base::PathExists(unpack_path
.Append(kSwiftShaderGlesName
)))
143 // Passed the basic tests. Time to install it.
144 base::FilePath path
=
145 GetSwiftShaderBaseDirectory().AppendASCII(version
.GetString());
146 if (base::PathExists(path
))
148 if (!base::Move(unpack_path
, path
))
150 // Installation is done. Now tell the rest of chrome.
151 current_version_
= version
;
152 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
153 base::Bind(&RegisterSwiftShaderWithChrome
, path
));
157 bool SwiftShaderComponentInstaller::GetInstalledFile(
158 const std::string
& file
, base::FilePath
* installed_file
) {
162 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService
* cus
,
163 const Version
& version
) {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
166 CrxComponent swiftshader
;
167 swiftshader
.name
= "Swift Shader";
168 swiftshader
.installer
= new SwiftShaderComponentInstaller(version
);
169 swiftshader
.version
= version
;
170 swiftshader
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
171 if (cus
->RegisterComponent(swiftshader
) != ComponentUpdateService::kOk
) {
172 NOTREACHED() << "SwiftShader component registration fail";
176 class UpdateChecker
: public content::GpuDataManagerObserver
{
178 explicit UpdateChecker(ComponentUpdateService
* cus
);
180 virtual void OnGpuInfoUpdate() OVERRIDE
;
183 ComponentUpdateService
* cus_
;
186 UpdateChecker::UpdateChecker(ComponentUpdateService
* cus
)
190 void UpdateChecker::OnGpuInfoUpdate() {
191 GpuDataManager
*gpu_data_manager
= GpuDataManager::GetInstance();
193 if (!gpu_data_manager
->GpuAccessAllowed(NULL
) ||
194 gpu_data_manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
) ||
195 gpu_data_manager
->ShouldUseSwiftShader()) {
196 gpu_data_manager
->RemoveObserver(this);
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
198 base::FilePath path
= GetSwiftShaderBaseDirectory();
200 Version
version(kNullVersion
);
201 GetLatestSwiftShaderDirectory(&path
, &version
, NULL
);
203 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
204 base::Bind(&FinishSwiftShaderUpdateRegistration
, cus_
, version
));
208 // Check if there already is a version of swiftshader installed,
209 // and if so register it.
210 void 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.";
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
));
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.
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);
239 void RegisterSwiftShaderComponent(ComponentUpdateService
* cus
) {
240 #if defined(ENABLE_SWIFTSHADER)
245 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
246 base::Bind(&RegisterSwiftShaderPath
, cus
));
250 } // namespace component_updater