swiftshader_component_installer.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/component_updater/swiftshader_component_installer.h"
6
7#include "base/base_paths.h"
8#include "base/bind.h"
9#include "base/compiler_specific.h"
10#include "base/cpu.h"
11#include "base/file_util.h"
12#include "base/files/file_path.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"
20#include "content/public/browser/gpu_data_manager.h"
21#include "content/public/browser/gpu_data_manager_observer.h"
22
23using content::BrowserThread;
24using content::GpuDataManager;
25
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 base::FilePath::CharType kSwiftShaderEglName[] =
36    FILE_PATH_LITERAL("libegl.dll");
37const base::FilePath::CharType kSwiftShaderGlesName[] =
38    FILE_PATH_LITERAL("libglesv2.dll");
39
40const char kSwiftShaderManifestName[] = "SwiftShader";
41
42const base::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\.
50base::FilePath GetSwiftShaderBaseDirectory() {
51  base::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\.
60bool GetLatestSwiftShaderDirectory(base::FilePath* result,
61                                   Version* latest,
62                                   std::vector<base::FilePath>* older_dirs) {
63  base::FilePath base_dir = GetSwiftShaderBaseDirectory();
64  bool found = false;
65  file_util::FileEnumerator
66      file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES);
67  for (base::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))) {
75      if (found && older_dirs)
76          older_dirs->push_back(*result);
77      *latest = version;
78      *result = path;
79      found = true;
80    } else {
81      if (older_dirs)
82        older_dirs->push_back(path);
83    }
84  }
85  return found;
86}
87
88void RegisterSwiftShaderWithChrome(const base::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 base::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 base::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  base::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
163class UpdateChecker : public content::GpuDataManagerObserver {
164 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() ||
181      (gpu_data_manager->GetBlacklistedFeatures() &
182       content::GPU_FEATURE_TYPE_WEBGL) ||
183      gpu_data_manager->ShouldUseSoftwareRendering()) {
184    gpu_data_manager->RemoveObserver(this);
185    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
186    base::FilePath path = GetSwiftShaderBaseDirectory();
187
188    Version version(kNullVersion);
189    GetLatestSwiftShaderDirectory(&path, &version, NULL);
190
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  base::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);
209  std::vector<base::FilePath> older_dirs;
210  if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs))
211    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.
219
220  // Remove older versions of SwiftShader.
221  for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
222       iter != older_dirs.end(); ++iter) {
223    file_util::Delete(*iter, true);
224  }
225}
226
227void RegisterSwiftShaderComponent(ComponentUpdateService* cus) {
228#if defined(ENABLE_SWIFTSHADER)
229  base::CPU cpu;
230
231  if (!cpu.has_sse2())
232    return;
233  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
234      base::Bind(&RegisterSwiftShaderPath, cus));
235#endif
236}
237