Use WeakNSProtocol/WeakNSObject for WebControllerObserverBridge ivars.
[chromium-blink-merge.git] / chrome / browser / component_updater / swiftshader_component_installer.cc
blob34355ca726cbcb48141d74d05d133d47c1e39c50
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 ComponentUpdateService::Status::kOk) {
183 NOTREACHED() << "SwiftShader component registration fail";
187 class UpdateChecker : public content::GpuDataManagerObserver {
188 public:
189 explicit UpdateChecker(ComponentUpdateService* cus);
191 void OnGpuInfoUpdate() override;
193 private:
194 ComponentUpdateService* cus_;
197 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) : cus_(cus) {
200 void UpdateChecker::OnGpuInfoUpdate() {
201 GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance();
203 if (!gpu_data_manager->GpuAccessAllowed(NULL) ||
204 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) ||
205 gpu_data_manager->ShouldUseSwiftShader()) {
206 gpu_data_manager->RemoveObserver(this);
207 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
208 base::FilePath path = GetSwiftShaderBaseDirectory();
210 Version version(kNullVersion);
211 GetLatestSwiftShaderDirectory(&path, &version, NULL);
213 BrowserThread::PostTask(
214 BrowserThread::UI,
215 FROM_HERE,
216 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version));
220 #if defined(ENABLE_SWIFTSHADER)
222 // Check if there already is a version of swiftshader installed,
223 // and if so register it.
224 void RegisterSwiftShaderPath(ComponentUpdateService* cus) {
225 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
226 base::FilePath path = GetSwiftShaderBaseDirectory();
227 if (!base::PathExists(path)) {
228 if (!base::CreateDirectory(path)) {
229 NOTREACHED() << "Could not create SwiftShader directory.";
230 return;
234 Version version(kNullVersion);
235 std::vector<base::FilePath> older_dirs;
236 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
237 BrowserThread::PostTask(BrowserThread::UI,
238 FROM_HERE,
239 base::Bind(&RegisterSwiftShaderWithChrome, path));
241 UpdateChecker* update_checker = new UpdateChecker(cus);
242 GpuDataManager::GetInstance()->AddObserver(update_checker);
243 update_checker->OnGpuInfoUpdate();
244 // We leak update_checker here, because it has to stick around for the life
245 // of the GpuDataManager.
247 // Remove older versions of SwiftShader.
248 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
249 iter != older_dirs.end();
250 ++iter) {
251 base::DeleteFile(*iter, true);
255 #endif // ENABLE_SWIFTSHADER
257 } // namespace
259 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
260 #if defined(ENABLE_SWIFTSHADER)
261 BrowserThread::PostTask(BrowserThread::FILE,
262 FROM_HERE,
263 base::Bind(&RegisterSwiftShaderPath, cus));
264 #endif
267 } // namespace component_updater