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"
10 #include "base/base_paths.h"
11 #include "base/bind.h"
12 #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 "content/public/browser/browser_thread.h"
24 #include "content/public/browser/gpu_data_manager.h"
25 #include "content/public/browser/gpu_data_manager_observer.h"
26 #include "gpu/config/gpu_feature_type.h"
28 using content::BrowserThread
;
29 using content::GpuDataManager
;
31 namespace component_updater
{
35 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
36 const uint8 kSha2Hash
[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
37 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
38 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
39 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
41 // File name of the internal SwiftShader plugin on different platforms.
42 const base::FilePath::CharType kSwiftShaderEglName
[] =
43 FILE_PATH_LITERAL("libegl.dll");
44 const base::FilePath::CharType kSwiftShaderGlesName
[] =
45 FILE_PATH_LITERAL("libglesv2.dll");
47 const char kSwiftShaderManifestName
[] = "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 if (!PathService::Get(DIR_SWIFT_SHADER
, &result
))
57 NOTREACHED() << "Couldn't get SwiftShader directory.";
61 // SwiftShader has version encoded in the path itself
62 // so we need to enumerate the directories to find the full path.
63 // On success it returns something like:
64 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
65 bool GetLatestSwiftShaderDirectory(base::FilePath
* result
,
67 std::vector
<base::FilePath
>* older_dirs
) {
68 base::FilePath base_dir
= GetSwiftShaderBaseDirectory();
70 base::FileEnumerator
file_enumerator(
71 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
72 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
73 path
= file_enumerator
.Next()) {
74 Version
version(path
.BaseName().MaybeAsASCII());
75 if (!version
.IsValid())
77 if (version
.CompareTo(*latest
) > 0 &&
78 base::PathExists(path
.Append(kSwiftShaderEglName
)) &&
79 base::PathExists(path
.Append(kSwiftShaderGlesName
))) {
80 if (found
&& older_dirs
)
81 older_dirs
->push_back(*result
);
87 older_dirs
->push_back(path
);
93 void RegisterSwiftShaderWithChrome(const base::FilePath
& path
) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
95 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path
);
98 class SwiftShaderComponentInstaller
: public ComponentInstaller
{
100 explicit SwiftShaderComponentInstaller(const Version
& version
);
102 virtual ~SwiftShaderComponentInstaller() {}
104 virtual void OnUpdateError(int error
) OVERRIDE
;
106 virtual bool Install(const base::DictionaryValue
& manifest
,
107 const base::FilePath
& unpack_path
) OVERRIDE
;
109 virtual bool GetInstalledFile(const std::string
& file
,
110 base::FilePath
* installed_file
) OVERRIDE
;
113 Version current_version_
;
116 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
117 const Version
& version
)
118 : 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
,
154 base::Bind(&RegisterSwiftShaderWithChrome
, path
));
158 bool SwiftShaderComponentInstaller::GetInstalledFile(
159 const std::string
& file
,
160 base::FilePath
* installed_file
) {
164 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService
* cus
,
165 const Version
& version
) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
168 CrxComponent swiftshader
;
169 swiftshader
.name
= "Swift Shader";
170 swiftshader
.installer
= new SwiftShaderComponentInstaller(version
);
171 swiftshader
.version
= version
;
172 swiftshader
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
173 if (cus
->RegisterComponent(swiftshader
) != ComponentUpdateService::kOk
) {
174 NOTREACHED() << "SwiftShader component registration fail";
178 class UpdateChecker
: public content::GpuDataManagerObserver
{
180 explicit UpdateChecker(ComponentUpdateService
* cus
);
182 virtual void OnGpuInfoUpdate() OVERRIDE
;
185 ComponentUpdateService
* cus_
;
188 UpdateChecker::UpdateChecker(ComponentUpdateService
* cus
) : cus_(cus
) {
191 void UpdateChecker::OnGpuInfoUpdate() {
192 GpuDataManager
* gpu_data_manager
= GpuDataManager::GetInstance();
194 if (!gpu_data_manager
->GpuAccessAllowed(NULL
) ||
195 gpu_data_manager
->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL
) ||
196 gpu_data_manager
->ShouldUseSwiftShader()) {
197 gpu_data_manager
->RemoveObserver(this);
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
199 base::FilePath path
= GetSwiftShaderBaseDirectory();
201 Version
version(kNullVersion
);
202 GetLatestSwiftShaderDirectory(&path
, &version
, NULL
);
204 BrowserThread::PostTask(
207 base::Bind(&FinishSwiftShaderUpdateRegistration
, cus_
, version
));
211 #if defined(ENABLE_SWIFTSHADER)
213 // Check if there already is a version of swiftshader installed,
214 // and if so register it.
215 void RegisterSwiftShaderPath(ComponentUpdateService
* cus
) {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
217 base::FilePath path
= GetSwiftShaderBaseDirectory();
218 if (!base::PathExists(path
)) {
219 if (!base::CreateDirectory(path
)) {
220 NOTREACHED() << "Could not create SwiftShader directory.";
225 Version
version(kNullVersion
);
226 std::vector
<base::FilePath
> older_dirs
;
227 if (GetLatestSwiftShaderDirectory(&path
, &version
, &older_dirs
))
228 BrowserThread::PostTask(BrowserThread::UI
,
230 base::Bind(&RegisterSwiftShaderWithChrome
, path
));
232 UpdateChecker
* update_checker
= new UpdateChecker(cus
);
233 GpuDataManager::GetInstance()->AddObserver(update_checker
);
234 update_checker
->OnGpuInfoUpdate();
235 // We leak update_checker here, because it has to stick around for the life
236 // of the GpuDataManager.
238 // Remove older versions of SwiftShader.
239 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
240 iter
!= older_dirs
.end();
242 base::DeleteFile(*iter
, true);
246 #endif // ENABLE_SWIFTSHADER
250 void RegisterSwiftShaderComponent(ComponentUpdateService
* cus
) {
251 #if defined(ENABLE_SWIFTSHADER)
256 BrowserThread::PostTask(BrowserThread::FILE,
258 base::Bind(&RegisterSwiftShaderPath
, cus
));
262 } // namespace component_updater