blob: f24828fc9dda61039e10a55e4ca6de3ca7c9b687 [file] [log] [blame]
[email protected]4d3c27f32012-03-31 09:11:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]df353ab2011-12-16 23:57:572// 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 "base/bind.h"
8#include "base/base_paths.h"
9#include "base/compiler_specific.h"
[email protected]4d3c27f32012-03-31 09:11:5910#include "base/cpu.h"
[email protected]df353ab2011-12-16 23:57:5711#include "base/file_path.h"
12#include "base/file_util.h"
13#include "base/logging.h"
14#include "base/path_service.h"
15#include "base/string_util.h"
16#include "base/values.h"
17#include "chrome/browser/component_updater/component_updater_service.h"
18#include "chrome/common/chrome_paths.h"
19#include "content/public/browser/browser_thread.h"
[email protected]79078df2012-02-16 01:22:3220#include "content/public/browser/gpu_data_manager.h"
21#include "content/public/browser/gpu_data_manager_observer.h"
[email protected]df353ab2011-12-16 23:57:5722
23using content::BrowserThread;
[email protected]79078df2012-02-16 01:22:3224using content::GpuDataManager;
[email protected]df353ab2011-12-16 23:57:5725
26namespace {
27
28// CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
29const uint8 sha2_hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
30 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
31 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
32 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
33
34// File name of the internal SwiftShader plugin on different platforms.
35const FilePath::CharType kSwiftShaderEglName[] =
36 FILE_PATH_LITERAL("libegl.dll");
37const FilePath::CharType kSwiftShaderGlesName[] =
38 FILE_PATH_LITERAL("libglesv2.dll");
39
40const char kSwiftShaderManifestName[] = "SwiftShader";
41
42const FilePath::CharType kSwiftShaderBaseDirectory[] =
43 FILE_PATH_LITERAL("SwiftShader");
44
45// If we don't have a SwiftShader component, this is the version we claim.
46const char kNullVersion[] = "0.0.0.0";
47
48// The base directory on windows looks like:
49// <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
50FilePath GetSwiftShaderBaseDirectory() {
51 FilePath result;
52 PathService::Get(chrome::DIR_USER_DATA, &result);
53 return result.Append(kSwiftShaderBaseDirectory);
54}
55
56// SwiftShader has version encoded in the path itself
57// so we need to enumerate the directories to find the full path.
58// On success it returns something like:
59// <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
[email protected]56177432012-06-06 00:37:4960bool GetLatestSwiftShaderDirectory(FilePath* result,
61 Version* latest,
62 std::vector<FilePath>* older_dirs) {
63 FilePath base_dir = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:5764 bool found = false;
65 file_util::FileEnumerator
[email protected]56177432012-06-06 00:37:4966 file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]df353ab2011-12-16 23:57:5767 for (FilePath path = file_enumerator.Next(); !path.value().empty();
68 path = file_enumerator.Next()) {
69 Version version(path.BaseName().MaybeAsASCII());
70 if (!version.IsValid())
71 continue;
72 if (version.CompareTo(*latest) > 0 &&
73 file_util::PathExists(path.Append(kSwiftShaderEglName)) &&
74 file_util::PathExists(path.Append(kSwiftShaderGlesName))) {
[email protected]56177432012-06-06 00:37:4975 if (found && older_dirs)
76 older_dirs->push_back(*result);
[email protected]df353ab2011-12-16 23:57:5777 *latest = version;
78 *result = path;
79 found = true;
[email protected]56177432012-06-06 00:37:4980 } else {
81 if (older_dirs)
82 older_dirs->push_back(path);
[email protected]df353ab2011-12-16 23:57:5783 }
84 }
85 return found;
86}
87
88void RegisterSwiftShaderWithChrome(const FilePath& path) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
91}
92
93} // namespace
94
95class SwiftShaderComponentInstaller : public ComponentInstaller {
96 public:
97 explicit SwiftShaderComponentInstaller(const Version& version);
98
99 virtual ~SwiftShaderComponentInstaller() {}
100
101 virtual void OnUpdateError(int error) OVERRIDE;
102
103 virtual bool Install(base::DictionaryValue* manifest,
104 const FilePath& unpack_path) OVERRIDE;
105
106 private:
107 Version current_version_;
108};
109
110SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
111 const Version& version) : current_version_(version) {
112 DCHECK(version.IsValid());
113}
114
115void SwiftShaderComponentInstaller::OnUpdateError(int error) {
116 NOTREACHED() << "SwiftShader update error: " << error;
117}
118
119bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest,
120 const FilePath& unpack_path) {
121 std::string name;
122 manifest->GetStringASCII("name", &name);
123 if (name != kSwiftShaderManifestName)
124 return false;
125 std::string proposed_version;
126 manifest->GetStringASCII("version", &proposed_version);
127 Version version(proposed_version.c_str());
128 if (!version.IsValid())
129 return false;
130 if (current_version_.CompareTo(version) >= 0)
131 return false;
132 if (!file_util::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
133 !file_util::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
134 return false;
135 // Passed the basic tests. Time to install it.
136 FilePath path =
137 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
138 if (file_util::PathExists(path))
139 return false;
140 if (!file_util::Move(unpack_path, path))
141 return false;
142 // Installation is done. Now tell the rest of chrome.
143 current_version_ = version;
144 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
145 base::Bind(&RegisterSwiftShaderWithChrome, path));
146 return true;
147}
148
149void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
150 const Version& version) {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
152
153 CrxComponent swiftshader;
154 swiftshader.name = "Swift Shader";
155 swiftshader.installer = new SwiftShaderComponentInstaller(version);
156 swiftshader.version = version;
157 swiftshader.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]);
158 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) {
159 NOTREACHED() << "SwiftShader component registration fail";
160 }
161}
162
[email protected]79078df2012-02-16 01:22:32163class UpdateChecker : public content::GpuDataManagerObserver {
[email protected]df353ab2011-12-16 23:57:57164 public:
165 explicit UpdateChecker(ComponentUpdateService* cus);
166
167 virtual void OnGpuInfoUpdate() OVERRIDE;
168
169 private:
170 ComponentUpdateService* cus_;
171};
172
173UpdateChecker::UpdateChecker(ComponentUpdateService* cus)
174 : cus_(cus) {
175}
176
177void UpdateChecker::OnGpuInfoUpdate() {
178 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance();
179
180 if (!gpu_data_manager->GpuAccessAllowed() ||
[email protected]d56ecf922012-02-15 16:03:11181 (gpu_data_manager->GetGpuFeatureType() &
182 content::GPU_FEATURE_TYPE_WEBGL) ||
[email protected]79078df2012-02-16 01:22:32183 gpu_data_manager->ShouldUseSoftwareRendering()) {
[email protected]df353ab2011-12-16 23:57:57184 gpu_data_manager->RemoveObserver(this);
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
186 FilePath path = GetSwiftShaderBaseDirectory();
187
188 Version version(kNullVersion);
[email protected]56177432012-06-06 00:37:49189 GetLatestSwiftShaderDirectory(&path, &version, NULL);
[email protected]df353ab2011-12-16 23:57:57190
191 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
192 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
193 }
194}
195
196// Check if there already is a version of swiftshader installed,
197// and if so register it.
198void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
200 FilePath path = GetSwiftShaderBaseDirectory();
201 if (!file_util::PathExists(path)) {
202 if (!file_util::CreateDirectory(path)) {
203 NOTREACHED() << "Could not create SwiftShader directory.";
204 return;
205 }
206 }
207
208 Version version(kNullVersion);
[email protected]56177432012-06-06 00:37:49209 std::vector<FilePath> older_dirs;
210 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
[email protected]df353ab2011-12-16 23:57:57211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
212 base::Bind(&RegisterSwiftShaderWithChrome, path));
213
214 UpdateChecker *update_checker = new UpdateChecker(cus);
215 GpuDataManager::GetInstance()->AddObserver(update_checker);
216 update_checker->OnGpuInfoUpdate();
217 // We leak update_checker here, because it has to stick around for the life
218 // of the GpuDataManager.
[email protected]56177432012-06-06 00:37:49219
220 // Remove older versions of SwiftShader.
221 for (std::vector<FilePath>::iterator iter = older_dirs.begin();
222 iter != older_dirs.end(); ++iter) {
223 file_util::Delete(*iter, true);
224 }
[email protected]df353ab2011-12-16 23:57:57225}
226
227void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
228#if defined(ENABLE_SWIFTSHADER)
[email protected]4d3c27f32012-03-31 09:11:59229 base::CPU cpu;
230
231 if (!cpu.has_sse2())
232 return;
[email protected]df353ab2011-12-16 23:57:57233 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
234 base::Bind(&RegisterSwiftShaderPath, cus));
235#endif
236}