[email protected] | 4d3c27f3 | 2012-03-31 09:11:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 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 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 7 | #include "base/base_paths.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | 4d3c27f3 | 2012-03-31 09:11:59 | [diff] [blame] | 10 | #include "base/cpu.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 12 | #include "base/files/file_enumerator.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/path_service.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 17 | #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] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 21 | #include "content/public/browser/gpu_data_manager.h" |
| 22 | #include "content/public/browser/gpu_data_manager_observer.h" |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 23 | #include "gpu/config/gpu_feature_type.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 24 | |
| 25 | using content::BrowserThread; |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 26 | using content::GpuDataManager; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 27 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 28 | namespace component_updater { |
| 29 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. |
[email protected] | d4261bea | 2013-04-22 05:29:39 | [diff] [blame] | 33 | const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b, |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 34 | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 39 | const base::FilePath::CharType kSwiftShaderEglName[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 40 | FILE_PATH_LITERAL("libegl.dll"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 41 | const base::FilePath::CharType kSwiftShaderGlesName[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 42 | FILE_PATH_LITERAL("libglesv2.dll"); |
| 43 | |
| 44 | const char kSwiftShaderManifestName[] = "SwiftShader"; |
| 45 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 46 | const base::FilePath::CharType kSwiftShaderBaseDirectory[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 47 | FILE_PATH_LITERAL("SwiftShader"); |
| 48 | |
| 49 | // If we don't have a SwiftShader component, this is the version we claim. |
| 50 | const 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 54 | base::FilePath GetSwiftShaderBaseDirectory() { |
| 55 | base::FilePath result; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 56 | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 64 | bool GetLatestSwiftShaderDirectory(base::FilePath* result, |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 65 | Version* latest, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 66 | std::vector<base::FilePath>* older_dirs) { |
| 67 | base::FilePath base_dir = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 68 | bool found = false; |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 69 | base::FileEnumerator |
| 70 | file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 71 | for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 72 | path = file_enumerator.Next()) { |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 73 | Version version(path.BaseName().MaybeAsASCII()); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 74 | if (!version.IsValid()) |
| 75 | continue; |
| 76 | if (version.CompareTo(*latest) > 0 && |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 77 | base::PathExists(path.Append(kSwiftShaderEglName)) && |
| 78 | base::PathExists(path.Append(kSwiftShaderGlesName))) { |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 79 | if (found && older_dirs) |
| 80 | older_dirs->push_back(*result); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 81 | *latest = version; |
| 82 | *result = path; |
| 83 | found = true; |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 84 | } else { |
| 85 | if (older_dirs) |
| 86 | older_dirs->push_back(path); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | return found; |
| 90 | } |
| 91 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 92 | void RegisterSwiftShaderWithChrome(const base::FilePath& path) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 93 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 94 | GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 95 | } |
| 96 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 97 | class SwiftShaderComponentInstaller : public ComponentInstaller { |
| 98 | public: |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 99 | explicit SwiftShaderComponentInstaller(const Version& version); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 100 | |
| 101 | virtual ~SwiftShaderComponentInstaller() {} |
| 102 | |
| 103 | virtual void OnUpdateError(int error) OVERRIDE; |
| 104 | |
[email protected] | 14ca887 | 2013-05-02 01:51:06 | [diff] [blame] | 105 | virtual bool Install(const base::DictionaryValue& manifest, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 106 | const base::FilePath& unpack_path) OVERRIDE; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 107 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 108 | virtual bool GetInstalledFile(const std::string& file, |
| 109 | base::FilePath* installed_file) OVERRIDE; |
| 110 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 111 | private: |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 112 | Version current_version_; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 116 | const Version& version) : current_version_(version) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 117 | DCHECK(version.IsValid()); |
| 118 | } |
| 119 | |
| 120 | void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 121 | NOTREACHED() << "SwiftShader update error: " << error; |
| 122 | } |
| 123 | |
[email protected] | 14ca887 | 2013-05-02 01:51:06 | [diff] [blame] | 124 | bool SwiftShaderComponentInstaller::Install( |
| 125 | const base::DictionaryValue& manifest, |
| 126 | const base::FilePath& unpack_path) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 127 | std::string name; |
[email protected] | 14ca887 | 2013-05-02 01:51:06 | [diff] [blame] | 128 | manifest.GetStringASCII("name", &name); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 129 | if (name != kSwiftShaderManifestName) |
| 130 | return false; |
| 131 | std::string proposed_version; |
[email protected] | 14ca887 | 2013-05-02 01:51:06 | [diff] [blame] | 132 | manifest.GetStringASCII("version", &proposed_version); |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 133 | Version version(proposed_version.c_str()); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 134 | if (!version.IsValid()) |
| 135 | return false; |
| 136 | if (current_version_.CompareTo(version) >= 0) |
| 137 | return false; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 138 | if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) || |
| 139 | !base::PathExists(unpack_path.Append(kSwiftShaderGlesName))) |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 140 | return false; |
| 141 | // Passed the basic tests. Time to install it. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 142 | base::FilePath path = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 143 | GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 144 | if (base::PathExists(path)) |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 145 | return false; |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 146 | if (!base::Move(unpack_path, path)) |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 147 | return false; |
| 148 | // Installation is done. Now tell the rest of chrome. |
| 149 | current_version_ = version; |
| 150 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 151 | base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 152 | return true; |
| 153 | } |
| 154 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 155 | bool SwiftShaderComponentInstaller::GetInstalledFile( |
| 156 | const std::string& file, base::FilePath* installed_file) { |
| 157 | return false; |
| 158 | } |
| 159 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 160 | void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 161 | const Version& version) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 162 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 163 | |
| 164 | CrxComponent swiftshader; |
| 165 | swiftshader.name = "Swift Shader"; |
| 166 | swiftshader.installer = new SwiftShaderComponentInstaller(version); |
| 167 | swiftshader.version = version; |
[email protected] | d4261bea | 2013-04-22 05:29:39 | [diff] [blame] | 168 | swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 169 | if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { |
| 170 | NOTREACHED() << "SwiftShader component registration fail"; |
| 171 | } |
| 172 | } |
| 173 | |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 174 | class UpdateChecker : public content::GpuDataManagerObserver { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 175 | public: |
| 176 | explicit UpdateChecker(ComponentUpdateService* cus); |
| 177 | |
| 178 | virtual void OnGpuInfoUpdate() OVERRIDE; |
| 179 | |
| 180 | private: |
| 181 | ComponentUpdateService* cus_; |
| 182 | }; |
| 183 | |
| 184 | UpdateChecker::UpdateChecker(ComponentUpdateService* cus) |
| 185 | : cus_(cus) { |
| 186 | } |
| 187 | |
| 188 | void UpdateChecker::OnGpuInfoUpdate() { |
| 189 | GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); |
| 190 | |
[email protected] | a040138 | 2013-05-07 00:12:55 | [diff] [blame] | 191 | if (!gpu_data_manager->GpuAccessAllowed(NULL) || |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 192 | gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) || |
[email protected] | 7dcb8583 | 2013-04-23 19:39:00 | [diff] [blame] | 193 | gpu_data_manager->ShouldUseSwiftShader()) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 194 | gpu_data_manager->RemoveObserver(this); |
| 195 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 196 | base::FilePath path = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 197 | |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 198 | Version version(kNullVersion); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 199 | GetLatestSwiftShaderDirectory(&path, &version, NULL); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 200 | |
| 201 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 202 | base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); |
| 203 | } |
| 204 | } |
| 205 | |
[email protected] | 3462b251 | 2014-01-22 16:56:39 | [diff] [blame] | 206 | #if defined(ENABLE_SWIFTSHADER) |
| 207 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 208 | // Check if there already is a version of swiftshader installed, |
| 209 | // and if so register it. |
| 210 | void RegisterSwiftShaderPath(ComponentUpdateService* cus) { |
| 211 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 212 | base::FilePath path = GetSwiftShaderBaseDirectory(); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 213 | if (!base::PathExists(path)) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 214 | if (!base::CreateDirectory(path)) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 215 | NOTREACHED() << "Could not create SwiftShader directory."; |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 220 | Version version(kNullVersion); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 221 | std::vector<base::FilePath> older_dirs; |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 222 | if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs)) |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 223 | 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] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 231 | |
| 232 | // Remove older versions of SwiftShader. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 233 | for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 234 | iter != older_dirs.end(); ++iter) { |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 235 | base::DeleteFile(*iter, true); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 236 | } |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 3462b251 | 2014-01-22 16:56:39 | [diff] [blame] | 239 | #endif // ENABLE_SWIFTSHADER |
| 240 | |
| 241 | } // namespace |
| 242 | |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 243 | void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { |
| 244 | #if defined(ENABLE_SWIFTSHADER) |
[email protected] | 4d3c27f3 | 2012-03-31 09:11:59 | [diff] [blame] | 245 | base::CPU cpu; |
| 246 | |
| 247 | if (!cpu.has_sse2()) |
| 248 | return; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 249 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 250 | base::Bind(&RegisterSwiftShaderPath, cus)); |
| 251 | #endif |
| 252 | } |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 253 | |
| 254 | } // namespace component_updater |
| 255 | |