blob: 9a24994b7bdf593203f0d25a0eb5cefa46a9d4b9 [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
[email protected]df353ab2011-12-16 23:57:577#include "base/base_paths.h"
[email protected]57999812013-02-24 05:40:528#include "base/bind.h"
[email protected]df353ab2011-12-16 23:57:579#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_util.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]df353ab2011-12-16 23:57:5713#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]9c2985a2013-04-02 18:11:5722#include "content/public/common/gpu_feature_type.h"
[email protected]df353ab2011-12-16 23:57:5723
24using content::BrowserThread;
[email protected]79078df2012-02-16 01:22:3225using content::GpuDataManager;
[email protected]df353ab2011-12-16 23:57:5726
27namespace {
28
29// CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
[email protected]d4261bea2013-04-22 05:29:3930const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
[email protected]df353ab2011-12-16 23:57:5731 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
32 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
33 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
34
35// File name of the internal SwiftShader plugin on different platforms.
[email protected]650b2d52013-02-10 03:41:4536const base::FilePath::CharType kSwiftShaderEglName[] =
[email protected]df353ab2011-12-16 23:57:5737 FILE_PATH_LITERAL("libegl.dll");
[email protected]650b2d52013-02-10 03:41:4538const base::FilePath::CharType kSwiftShaderGlesName[] =
[email protected]df353ab2011-12-16 23:57:5739 FILE_PATH_LITERAL("libglesv2.dll");
40
41const char kSwiftShaderManifestName[] = "SwiftShader";
42
[email protected]650b2d52013-02-10 03:41:4543const base::FilePath::CharType kSwiftShaderBaseDirectory[] =
[email protected]df353ab2011-12-16 23:57:5744 FILE_PATH_LITERAL("SwiftShader");
45
46// If we don't have a SwiftShader component, this is the version we claim.
47const char kNullVersion[] = "0.0.0.0";
48
49// The base directory on windows looks like:
50// <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
[email protected]650b2d52013-02-10 03:41:4551base::FilePath GetSwiftShaderBaseDirectory() {
52 base::FilePath result;
[email protected]df353ab2011-12-16 23:57:5753 PathService::Get(chrome::DIR_USER_DATA, &result);
54 return result.Append(kSwiftShaderBaseDirectory);
55}
56
57// SwiftShader has version encoded in the path itself
58// so we need to enumerate the directories to find the full path.
59// On success it returns something like:
60// <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
[email protected]650b2d52013-02-10 03:41:4561bool GetLatestSwiftShaderDirectory(base::FilePath* result,
[email protected]56177432012-06-06 00:37:4962 Version* latest,
[email protected]650b2d52013-02-10 03:41:4563 std::vector<base::FilePath>* older_dirs) {
64 base::FilePath base_dir = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:5765 bool found = false;
66 file_util::FileEnumerator
[email protected]56177432012-06-06 00:37:4967 file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES);
[email protected]650b2d52013-02-10 03:41:4568 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
[email protected]df353ab2011-12-16 23:57:5769 path = file_enumerator.Next()) {
70 Version version(path.BaseName().MaybeAsASCII());
71 if (!version.IsValid())
72 continue;
73 if (version.CompareTo(*latest) > 0 &&
74 file_util::PathExists(path.Append(kSwiftShaderEglName)) &&
75 file_util::PathExists(path.Append(kSwiftShaderGlesName))) {
[email protected]56177432012-06-06 00:37:4976 if (found && older_dirs)
77 older_dirs->push_back(*result);
[email protected]df353ab2011-12-16 23:57:5778 *latest = version;
79 *result = path;
80 found = true;
[email protected]56177432012-06-06 00:37:4981 } else {
82 if (older_dirs)
83 older_dirs->push_back(path);
[email protected]df353ab2011-12-16 23:57:5784 }
85 }
86 return found;
87}
88
[email protected]650b2d52013-02-10 03:41:4589void RegisterSwiftShaderWithChrome(const base::FilePath& path) {
[email protected]df353ab2011-12-16 23:57:5790 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
92}
93
94} // namespace
95
96class SwiftShaderComponentInstaller : public ComponentInstaller {
97 public:
98 explicit SwiftShaderComponentInstaller(const Version& version);
99
100 virtual ~SwiftShaderComponentInstaller() {}
101
102 virtual void OnUpdateError(int error) OVERRIDE;
103
104 virtual bool Install(base::DictionaryValue* manifest,
[email protected]650b2d52013-02-10 03:41:45105 const base::FilePath& unpack_path) OVERRIDE;
[email protected]df353ab2011-12-16 23:57:57106
107 private:
108 Version current_version_;
109};
110
111SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
112 const Version& version) : current_version_(version) {
113 DCHECK(version.IsValid());
114}
115
116void SwiftShaderComponentInstaller::OnUpdateError(int error) {
117 NOTREACHED() << "SwiftShader update error: " << error;
118}
119
120bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest,
[email protected]650b2d52013-02-10 03:41:45121 const base::FilePath& unpack_path) {
[email protected]df353ab2011-12-16 23:57:57122 std::string name;
123 manifest->GetStringASCII("name", &name);
124 if (name != kSwiftShaderManifestName)
125 return false;
126 std::string proposed_version;
127 manifest->GetStringASCII("version", &proposed_version);
128 Version version(proposed_version.c_str());
129 if (!version.IsValid())
130 return false;
131 if (current_version_.CompareTo(version) >= 0)
132 return false;
133 if (!file_util::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
134 !file_util::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
135 return false;
136 // Passed the basic tests. Time to install it.
[email protected]650b2d52013-02-10 03:41:45137 base::FilePath path =
[email protected]df353ab2011-12-16 23:57:57138 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
139 if (file_util::PathExists(path))
140 return false;
141 if (!file_util::Move(unpack_path, path))
142 return false;
143 // Installation is done. Now tell the rest of chrome.
144 current_version_ = version;
145 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
146 base::Bind(&RegisterSwiftShaderWithChrome, path));
147 return true;
148}
149
150void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
151 const Version& version) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153
154 CrxComponent swiftshader;
155 swiftshader.name = "Swift Shader";
156 swiftshader.installer = new SwiftShaderComponentInstaller(version);
157 swiftshader.version = version;
[email protected]d4261bea2013-04-22 05:29:39158 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
[email protected]df353ab2011-12-16 23:57:57159 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) {
160 NOTREACHED() << "SwiftShader component registration fail";
161 }
162}
163
[email protected]79078df2012-02-16 01:22:32164class UpdateChecker : public content::GpuDataManagerObserver {
[email protected]df353ab2011-12-16 23:57:57165 public:
166 explicit UpdateChecker(ComponentUpdateService* cus);
167
168 virtual void OnGpuInfoUpdate() OVERRIDE;
169
170 private:
171 ComponentUpdateService* cus_;
172};
173
174UpdateChecker::UpdateChecker(ComponentUpdateService* cus)
175 : cus_(cus) {
176}
177
178void UpdateChecker::OnGpuInfoUpdate() {
179 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance();
180
181 if (!gpu_data_manager->GpuAccessAllowed() ||
[email protected]9c2985a2013-04-02 18:11:57182 gpu_data_manager->IsFeatureBlacklisted(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));
[email protected]650b2d52013-02-10 03:41:45186 base::FilePath path = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:57187
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));
[email protected]650b2d52013-02-10 03:41:45200 base::FilePath path = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:57201 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]650b2d52013-02-10 03:41:45209 std::vector<base::FilePath> older_dirs;
[email protected]56177432012-06-06 00:37:49210 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.
[email protected]650b2d52013-02-10 03:41:45221 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
[email protected]56177432012-06-06 00:37:49222 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}