blob: 9e9020fa410eec1903b78473d95979fe0c4b2daa [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]25a4c1c2013-06-08 04:53:3612#include "base/files/file_enumerator.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]df353ab2011-12-16 23:57:5714#include "base/logging.h"
15#include "base/path_service.h"
[email protected]e7463412013-06-10 22:53:4616#include "base/strings/string_util.h"
[email protected]df353ab2011-12-16 23:57:5717#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"
[email protected]79078df2012-02-16 01:22:3221#include "content/public/browser/gpu_data_manager.h"
22#include "content/public/browser/gpu_data_manager_observer.h"
[email protected]d7b5cc72013-05-23 20:05:0023#include "gpu/config/gpu_feature_type.h"
[email protected]df353ab2011-12-16 23:57:5724
25using content::BrowserThread;
[email protected]79078df2012-02-16 01:22:3226using content::GpuDataManager;
[email protected]df353ab2011-12-16 23:57:5727
[email protected]055981f2014-01-17 20:22:3228namespace component_updater {
29
[email protected]df353ab2011-12-16 23:57:5730namespace {
31
32// CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
[email protected]d4261bea2013-04-22 05:29:3933const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
[email protected]df353ab2011-12-16 23:57:5734 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};
37
38// File name of the internal SwiftShader plugin on different platforms.
[email protected]650b2d52013-02-10 03:41:4539const base::FilePath::CharType kSwiftShaderEglName[] =
[email protected]df353ab2011-12-16 23:57:5740 FILE_PATH_LITERAL("libegl.dll");
[email protected]650b2d52013-02-10 03:41:4541const base::FilePath::CharType kSwiftShaderGlesName[] =
[email protected]df353ab2011-12-16 23:57:5742 FILE_PATH_LITERAL("libglesv2.dll");
43
44const char kSwiftShaderManifestName[] = "SwiftShader";
45
[email protected]650b2d52013-02-10 03:41:4546const base::FilePath::CharType kSwiftShaderBaseDirectory[] =
[email protected]df353ab2011-12-16 23:57:5747 FILE_PATH_LITERAL("SwiftShader");
48
49// If we don't have a SwiftShader component, this is the version we claim.
50const char kNullVersion[] = "0.0.0.0";
51
52// The base directory on windows looks like:
53// <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
[email protected]650b2d52013-02-10 03:41:4554base::FilePath GetSwiftShaderBaseDirectory() {
55 base::FilePath result;
[email protected]df353ab2011-12-16 23:57:5756 PathService::Get(chrome::DIR_USER_DATA, &result);
57 return result.Append(kSwiftShaderBaseDirectory);
58}
59
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\.
[email protected]650b2d52013-02-10 03:41:4564bool GetLatestSwiftShaderDirectory(base::FilePath* result,
[email protected]c5e4a2222014-01-03 16:06:1365 Version* latest,
[email protected]650b2d52013-02-10 03:41:4566 std::vector<base::FilePath>* older_dirs) {
67 base::FilePath base_dir = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:5768 bool found = false;
[email protected]25a4c1c2013-06-08 04:53:3669 base::FileEnumerator
70 file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES);
[email protected]650b2d52013-02-10 03:41:4571 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
[email protected]df353ab2011-12-16 23:57:5772 path = file_enumerator.Next()) {
[email protected]c5e4a2222014-01-03 16:06:1373 Version version(path.BaseName().MaybeAsASCII());
[email protected]df353ab2011-12-16 23:57:5774 if (!version.IsValid())
75 continue;
76 if (version.CompareTo(*latest) > 0 &&
[email protected]7567484142013-07-11 17:36:0777 base::PathExists(path.Append(kSwiftShaderEglName)) &&
78 base::PathExists(path.Append(kSwiftShaderGlesName))) {
[email protected]56177432012-06-06 00:37:4979 if (found && older_dirs)
80 older_dirs->push_back(*result);
[email protected]df353ab2011-12-16 23:57:5781 *latest = version;
82 *result = path;
83 found = true;
[email protected]56177432012-06-06 00:37:4984 } else {
85 if (older_dirs)
86 older_dirs->push_back(path);
[email protected]df353ab2011-12-16 23:57:5787 }
88 }
89 return found;
90}
91
[email protected]650b2d52013-02-10 03:41:4592void RegisterSwiftShaderWithChrome(const base::FilePath& path) {
[email protected]df353ab2011-12-16 23:57:5793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
95}
96
97} // namespace
98
99class SwiftShaderComponentInstaller : public ComponentInstaller {
100 public:
[email protected]c5e4a2222014-01-03 16:06:13101 explicit SwiftShaderComponentInstaller(const Version& version);
[email protected]df353ab2011-12-16 23:57:57102
103 virtual ~SwiftShaderComponentInstaller() {}
104
105 virtual void OnUpdateError(int error) OVERRIDE;
106
[email protected]14ca8872013-05-02 01:51:06107 virtual bool Install(const base::DictionaryValue& manifest,
[email protected]650b2d52013-02-10 03:41:45108 const base::FilePath& unpack_path) OVERRIDE;
[email protected]df353ab2011-12-16 23:57:57109
[email protected]e3e696d32013-06-21 20:41:36110 virtual bool GetInstalledFile(const std::string& file,
111 base::FilePath* installed_file) OVERRIDE;
112
[email protected]df353ab2011-12-16 23:57:57113 private:
[email protected]c5e4a2222014-01-03 16:06:13114 Version current_version_;
[email protected]df353ab2011-12-16 23:57:57115};
116
117SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
[email protected]c5e4a2222014-01-03 16:06:13118 const Version& version) : current_version_(version) {
[email protected]df353ab2011-12-16 23:57:57119 DCHECK(version.IsValid());
120}
121
122void SwiftShaderComponentInstaller::OnUpdateError(int error) {
123 NOTREACHED() << "SwiftShader update error: " << error;
124}
125
[email protected]14ca8872013-05-02 01:51:06126bool SwiftShaderComponentInstaller::Install(
127 const base::DictionaryValue& manifest,
128 const base::FilePath& unpack_path) {
[email protected]df353ab2011-12-16 23:57:57129 std::string name;
[email protected]14ca8872013-05-02 01:51:06130 manifest.GetStringASCII("name", &name);
[email protected]df353ab2011-12-16 23:57:57131 if (name != kSwiftShaderManifestName)
132 return false;
133 std::string proposed_version;
[email protected]14ca8872013-05-02 01:51:06134 manifest.GetStringASCII("version", &proposed_version);
[email protected]c5e4a2222014-01-03 16:06:13135 Version version(proposed_version.c_str());
[email protected]df353ab2011-12-16 23:57:57136 if (!version.IsValid())
137 return false;
138 if (current_version_.CompareTo(version) >= 0)
139 return false;
[email protected]7567484142013-07-11 17:36:07140 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
141 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
[email protected]df353ab2011-12-16 23:57:57142 return false;
143 // Passed the basic tests. Time to install it.
[email protected]650b2d52013-02-10 03:41:45144 base::FilePath path =
[email protected]df353ab2011-12-16 23:57:57145 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
[email protected]7567484142013-07-11 17:36:07146 if (base::PathExists(path))
[email protected]df353ab2011-12-16 23:57:57147 return false;
[email protected]5553d5b2013-07-01 23:07:36148 if (!base::Move(unpack_path, path))
[email protected]df353ab2011-12-16 23:57:57149 return false;
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));
154 return true;
155}
156
[email protected]e3e696d32013-06-21 20:41:36157bool SwiftShaderComponentInstaller::GetInstalledFile(
158 const std::string& file, base::FilePath* installed_file) {
159 return false;
160}
161
[email protected]df353ab2011-12-16 23:57:57162void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
[email protected]c5e4a2222014-01-03 16:06:13163 const Version& version) {
[email protected]df353ab2011-12-16 23:57:57164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
165
166 CrxComponent swiftshader;
167 swiftshader.name = "Swift Shader";
168 swiftshader.installer = new SwiftShaderComponentInstaller(version);
169 swiftshader.version = version;
[email protected]d4261bea2013-04-22 05:29:39170 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
[email protected]df353ab2011-12-16 23:57:57171 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) {
172 NOTREACHED() << "SwiftShader component registration fail";
173 }
174}
175
[email protected]79078df2012-02-16 01:22:32176class UpdateChecker : public content::GpuDataManagerObserver {
[email protected]df353ab2011-12-16 23:57:57177 public:
178 explicit UpdateChecker(ComponentUpdateService* cus);
179
180 virtual void OnGpuInfoUpdate() OVERRIDE;
181
182 private:
183 ComponentUpdateService* cus_;
184};
185
186UpdateChecker::UpdateChecker(ComponentUpdateService* cus)
187 : cus_(cus) {
188}
189
190void UpdateChecker::OnGpuInfoUpdate() {
191 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance();
192
[email protected]a0401382013-05-07 00:12:55193 if (!gpu_data_manager->GpuAccessAllowed(NULL) ||
[email protected]d7b5cc72013-05-23 20:05:00194 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) ||
[email protected]7dcb85832013-04-23 19:39:00195 gpu_data_manager->ShouldUseSwiftShader()) {
[email protected]df353ab2011-12-16 23:57:57196 gpu_data_manager->RemoveObserver(this);
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]650b2d52013-02-10 03:41:45198 base::FilePath path = GetSwiftShaderBaseDirectory();
[email protected]df353ab2011-12-16 23:57:57199
[email protected]c5e4a2222014-01-03 16:06:13200 Version version(kNullVersion);
[email protected]56177432012-06-06 00:37:49201 GetLatestSwiftShaderDirectory(&path, &version, NULL);
[email protected]df353ab2011-12-16 23:57:57202
203 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
204 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
205 }
206}
207
208// Check if there already is a version of swiftshader installed,
209// and if so register it.
210void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]650b2d52013-02-10 03:41:45212 base::FilePath path = GetSwiftShaderBaseDirectory();
[email protected]7567484142013-07-11 17:36:07213 if (!base::PathExists(path)) {
[email protected]426d1c92013-12-03 20:08:54214 if (!base::CreateDirectory(path)) {
[email protected]df353ab2011-12-16 23:57:57215 NOTREACHED() << "Could not create SwiftShader directory.";
216 return;
217 }
218 }
219
[email protected]c5e4a2222014-01-03 16:06:13220 Version version(kNullVersion);
[email protected]650b2d52013-02-10 03:41:45221 std::vector<base::FilePath> older_dirs;
[email protected]56177432012-06-06 00:37:49222 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
[email protected]df353ab2011-12-16 23:57:57223 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
224 base::Bind(&RegisterSwiftShaderWithChrome, path));
225
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.
[email protected]56177432012-06-06 00:37:49231
232 // Remove older versions of SwiftShader.
[email protected]650b2d52013-02-10 03:41:45233 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
[email protected]56177432012-06-06 00:37:49234 iter != older_dirs.end(); ++iter) {
[email protected]dd3aa792013-07-16 19:10:23235 base::DeleteFile(*iter, true);
[email protected]56177432012-06-06 00:37:49236 }
[email protected]df353ab2011-12-16 23:57:57237}
238
239void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
240#if defined(ENABLE_SWIFTSHADER)
[email protected]4d3c27f32012-03-31 09:11:59241 base::CPU cpu;
242
243 if (!cpu.has_sse2())
244 return;
[email protected]df353ab2011-12-16 23:57:57245 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
246 base::Bind(&RegisterSwiftShaderPath, cus));
247#endif
248}
[email protected]055981f2014-01-17 20:22:32249
250} // namespace component_updater
251