Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / component_updater / swiftshader_component_installer.cc
blob1e82d3e49bcf83438631bca81a2f71778c293839
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 <stdint.h>
8 #include <string>
9 #include <vector>
11 #include "base/base_paths.h"
12 #include "base/bind.h"
13 #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 "components/update_client/update_client.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/gpu_data_manager.h"
26 #include "content/public/browser/gpu_data_manager_observer.h"
27 #include "gpu/config/gpu_feature_type.h"
29 using content::BrowserThread;
30 using content::GpuDataManager;
32 namespace component_updater {
34 namespace {
36 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg.
37 const uint8_t kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b,
38 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26,
39 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43,
40 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a};
42 // File name of the internal SwiftShader plugin on different platforms.
43 const base::FilePath::CharType kSwiftShaderEglName[] =
44 FILE_PATH_LITERAL("libegl.dll");
45 const base::FilePath::CharType kSwiftShaderGlesName[] =
46 FILE_PATH_LITERAL("libglesv2.dll");
48 const char kSwiftShaderManifestName[] = "SwiftShader";
50 // If we don't have a SwiftShader component, this is the version we claim.
51 const char kNullVersion[] = "0.0.0.0";
53 // The base directory on windows looks like:
54 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\.
55 base::FilePath GetSwiftShaderBaseDirectory() {
56 base::FilePath result;
57 if (!PathService::Get(DIR_SWIFT_SHADER, &result))
58 NOTREACHED() << "Couldn't get SwiftShader directory.";
59 return result;
62 // SwiftShader has version encoded in the path itself
63 // so we need to enumerate the directories to find the full path.
64 // On success it returns something like:
65 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\.
66 bool GetLatestSwiftShaderDirectory(base::FilePath* result,
67 Version* latest,
68 std::vector<base::FilePath>* older_dirs) {
69 base::FilePath base_dir = GetSwiftShaderBaseDirectory();
70 bool found = false;
71 base::FileEnumerator file_enumerator(
72 base_dir, false, base::FileEnumerator::DIRECTORIES);
73 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
74 path = file_enumerator.Next()) {
75 Version version(path.BaseName().MaybeAsASCII());
76 if (!version.IsValid())
77 continue;
78 if (version.CompareTo(*latest) > 0 &&
79 base::PathExists(path.Append(kSwiftShaderEglName)) &&
80 base::PathExists(path.Append(kSwiftShaderGlesName))) {
81 if (found && older_dirs)
82 older_dirs->push_back(*result);
83 *latest = version;
84 *result = path;
85 found = true;
86 } else {
87 if (older_dirs)
88 older_dirs->push_back(path);
91 return found;
94 void RegisterSwiftShaderWithChrome(const base::FilePath& path) {
95 DCHECK_CURRENTLY_ON(BrowserThread::UI);
96 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path);
99 class SwiftShaderComponentInstaller : public update_client::CrxInstaller {
100 public:
101 explicit SwiftShaderComponentInstaller(const Version& version);
103 // ComponentInstaller implementation:
104 void OnUpdateError(int error) override;
106 bool Install(const base::DictionaryValue& manifest,
107 const base::FilePath& unpack_path) override;
109 bool GetInstalledFile(const std::string& file,
110 base::FilePath* installed_file) override;
112 bool Uninstall() override;
114 private:
115 ~SwiftShaderComponentInstaller() override {}
117 Version current_version_;
120 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller(
121 const Version& version)
122 : current_version_(version) {
123 DCHECK(version.IsValid());
126 void SwiftShaderComponentInstaller::OnUpdateError(int error) {
127 NOTREACHED() << "SwiftShader update error: " << error;
130 bool SwiftShaderComponentInstaller::Install(
131 const base::DictionaryValue& manifest,
132 const base::FilePath& unpack_path) {
133 std::string name;
134 manifest.GetStringASCII("name", &name);
135 if (name != kSwiftShaderManifestName)
136 return false;
137 std::string proposed_version;
138 manifest.GetStringASCII("version", &proposed_version);
139 Version version(proposed_version.c_str());
140 if (!version.IsValid())
141 return false;
142 if (current_version_.CompareTo(version) >= 0)
143 return false;
144 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) ||
145 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName)))
146 return false;
147 // Passed the basic tests. Time to install it.
148 base::FilePath path =
149 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString());
150 if (base::PathExists(path))
151 return false;
152 if (!base::Move(unpack_path, path))
153 return false;
154 // Installation is done. Now tell the rest of chrome.
155 current_version_ = version;
156 BrowserThread::PostTask(BrowserThread::UI,
157 FROM_HERE,
158 base::Bind(&RegisterSwiftShaderWithChrome, path));
159 return true;
162 bool SwiftShaderComponentInstaller::GetInstalledFile(
163 const std::string& file,
164 base::FilePath* installed_file) {
165 return false;
168 bool SwiftShaderComponentInstaller::Uninstall() {
169 return false;
172 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus,
173 const Version& version) {
174 DCHECK_CURRENTLY_ON(BrowserThread::UI);
176 update_client::CrxComponent swiftshader;
177 swiftshader.name = "Swift Shader";
178 swiftshader.installer = new SwiftShaderComponentInstaller(version);
179 swiftshader.version = version;
180 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
181 if (!cus->RegisterComponent(swiftshader)) {
182 NOTREACHED() << "SwiftShader component registration fail";
186 class UpdateChecker : public content::GpuDataManagerObserver {
187 public:
188 explicit UpdateChecker(ComponentUpdateService* cus);
190 void OnGpuInfoUpdate() override;
192 private:
193 ComponentUpdateService* cus_;
196 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) : cus_(cus) {
199 void UpdateChecker::OnGpuInfoUpdate() {
200 GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance();
202 if (!gpu_data_manager->GpuAccessAllowed(NULL) ||
203 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) ||
204 gpu_data_manager->ShouldUseSwiftShader()) {
205 gpu_data_manager->RemoveObserver(this);
206 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
207 base::FilePath path = GetSwiftShaderBaseDirectory();
209 Version version(kNullVersion);
210 GetLatestSwiftShaderDirectory(&path, &version, NULL);
212 BrowserThread::PostTask(
213 BrowserThread::UI,
214 FROM_HERE,
215 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
219 #if defined(ENABLE_SWIFTSHADER)
221 // Check if there already is a version of swiftshader installed,
222 // and if so register it.
223 void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
224 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
225 base::FilePath path = GetSwiftShaderBaseDirectory();
226 if (!base::PathExists(path)) {
227 if (!base::CreateDirectory(path)) {
228 NOTREACHED() << "Could not create SwiftShader directory.";
229 return;
233 Version version(kNullVersion);
234 std::vector<base::FilePath> older_dirs;
235 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
236 BrowserThread::PostTask(BrowserThread::UI,
237 FROM_HERE,
238 base::Bind(&RegisterSwiftShaderWithChrome, path));
240 UpdateChecker* update_checker = new UpdateChecker(cus);
241 GpuDataManager::GetInstance()->AddObserver(update_checker);
242 update_checker->OnGpuInfoUpdate();
243 // We leak update_checker here, because it has to stick around for the life
244 // of the GpuDataManager.
246 // Remove older versions of SwiftShader.
247 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
248 iter != older_dirs.end();
249 ++iter) {
250 base::DeleteFile(*iter, true);
254 #endif // ENABLE_SWIFTSHADER
256 } // namespace
258 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
259 #if defined(ENABLE_SWIFTSHADER)
260 BrowserThread::PostTask(BrowserThread::FILE,
261 FROM_HERE,
262 base::Bind(&RegisterSwiftShaderPath, cus));
263 #endif
266 } // namespace component_updater