[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] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 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" |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 20 | #include "content/public/browser/gpu_data_manager.h" |
| 21 | #include "content/public/browser/gpu_data_manager_observer.h" |
[email protected] | 9c2985a | 2013-04-02 18:11:57 | [diff] [blame] | 22 | #include "content/public/common/gpu_feature_type.h" |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 23 | |
| 24 | using content::BrowserThread; |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 25 | using content::GpuDataManager; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. |
[email protected] | d4261bea | 2013-04-22 05:29:39 | [diff] [blame^] | 30 | const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b, |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 31 | 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26, |
| 32 | 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43, |
| 33 | 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a}; |
| 34 | |
| 35 | // File name of the internal SwiftShader plugin on different platforms. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 36 | const base::FilePath::CharType kSwiftShaderEglName[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 37 | FILE_PATH_LITERAL("libegl.dll"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 38 | const base::FilePath::CharType kSwiftShaderGlesName[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 39 | FILE_PATH_LITERAL("libglesv2.dll"); |
| 40 | |
| 41 | const char kSwiftShaderManifestName[] = "SwiftShader"; |
| 42 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 43 | const base::FilePath::CharType kSwiftShaderBaseDirectory[] = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 44 | FILE_PATH_LITERAL("SwiftShader"); |
| 45 | |
| 46 | // If we don't have a SwiftShader component, this is the version we claim. |
| 47 | const char kNullVersion[] = "0.0.0.0"; |
| 48 | |
| 49 | // The base directory on windows looks like: |
| 50 | // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 51 | base::FilePath GetSwiftShaderBaseDirectory() { |
| 52 | base::FilePath result; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 53 | PathService::Get(chrome::DIR_USER_DATA, &result); |
| 54 | return result.Append(kSwiftShaderBaseDirectory); |
| 55 | } |
| 56 | |
| 57 | // SwiftShader has version encoded in the path itself |
| 58 | // so we need to enumerate the directories to find the full path. |
| 59 | // On success it returns something like: |
| 60 | // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 61 | bool GetLatestSwiftShaderDirectory(base::FilePath* result, |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 62 | Version* latest, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 63 | std::vector<base::FilePath>* older_dirs) { |
| 64 | base::FilePath base_dir = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 65 | bool found = false; |
| 66 | file_util::FileEnumerator |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 67 | file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 68 | for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 69 | path = file_enumerator.Next()) { |
| 70 | Version version(path.BaseName().MaybeAsASCII()); |
| 71 | if (!version.IsValid()) |
| 72 | continue; |
| 73 | if (version.CompareTo(*latest) > 0 && |
| 74 | file_util::PathExists(path.Append(kSwiftShaderEglName)) && |
| 75 | file_util::PathExists(path.Append(kSwiftShaderGlesName))) { |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 76 | if (found && older_dirs) |
| 77 | older_dirs->push_back(*result); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 78 | *latest = version; |
| 79 | *result = path; |
| 80 | found = true; |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 81 | } else { |
| 82 | if (older_dirs) |
| 83 | older_dirs->push_back(path); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | return found; |
| 87 | } |
| 88 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 89 | void RegisterSwiftShaderWithChrome(const base::FilePath& path) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 90 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 91 | GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | class SwiftShaderComponentInstaller : public ComponentInstaller { |
| 97 | public: |
| 98 | explicit SwiftShaderComponentInstaller(const Version& version); |
| 99 | |
| 100 | virtual ~SwiftShaderComponentInstaller() {} |
| 101 | |
| 102 | virtual void OnUpdateError(int error) OVERRIDE; |
| 103 | |
| 104 | virtual bool Install(base::DictionaryValue* manifest, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 105 | const base::FilePath& unpack_path) OVERRIDE; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | Version current_version_; |
| 109 | }; |
| 110 | |
| 111 | SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
| 112 | const Version& version) : current_version_(version) { |
| 113 | DCHECK(version.IsValid()); |
| 114 | } |
| 115 | |
| 116 | void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 117 | NOTREACHED() << "SwiftShader update error: " << error; |
| 118 | } |
| 119 | |
| 120 | bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 121 | const base::FilePath& unpack_path) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 122 | std::string name; |
| 123 | manifest->GetStringASCII("name", &name); |
| 124 | if (name != kSwiftShaderManifestName) |
| 125 | return false; |
| 126 | std::string proposed_version; |
| 127 | manifest->GetStringASCII("version", &proposed_version); |
| 128 | Version version(proposed_version.c_str()); |
| 129 | if (!version.IsValid()) |
| 130 | return false; |
| 131 | if (current_version_.CompareTo(version) >= 0) |
| 132 | return false; |
| 133 | if (!file_util::PathExists(unpack_path.Append(kSwiftShaderEglName)) || |
| 134 | !file_util::PathExists(unpack_path.Append(kSwiftShaderGlesName))) |
| 135 | return false; |
| 136 | // Passed the basic tests. Time to install it. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 137 | base::FilePath path = |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 138 | GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); |
| 139 | if (file_util::PathExists(path)) |
| 140 | return false; |
| 141 | if (!file_util::Move(unpack_path, path)) |
| 142 | return false; |
| 143 | // Installation is done. Now tell the rest of chrome. |
| 144 | current_version_ = version; |
| 145 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 146 | base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, |
| 151 | const Version& version) { |
| 152 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 153 | |
| 154 | CrxComponent swiftshader; |
| 155 | swiftshader.name = "Swift Shader"; |
| 156 | swiftshader.installer = new SwiftShaderComponentInstaller(version); |
| 157 | swiftshader.version = version; |
[email protected] | d4261bea | 2013-04-22 05:29:39 | [diff] [blame^] | 158 | swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 159 | if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { |
| 160 | NOTREACHED() << "SwiftShader component registration fail"; |
| 161 | } |
| 162 | } |
| 163 | |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 164 | class UpdateChecker : public content::GpuDataManagerObserver { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 165 | public: |
| 166 | explicit UpdateChecker(ComponentUpdateService* cus); |
| 167 | |
| 168 | virtual void OnGpuInfoUpdate() OVERRIDE; |
| 169 | |
| 170 | private: |
| 171 | ComponentUpdateService* cus_; |
| 172 | }; |
| 173 | |
| 174 | UpdateChecker::UpdateChecker(ComponentUpdateService* cus) |
| 175 | : cus_(cus) { |
| 176 | } |
| 177 | |
| 178 | void UpdateChecker::OnGpuInfoUpdate() { |
| 179 | GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); |
| 180 | |
| 181 | if (!gpu_data_manager->GpuAccessAllowed() || |
[email protected] | 9c2985a | 2013-04-02 18:11:57 | [diff] [blame] | 182 | gpu_data_manager->IsFeatureBlacklisted(content::GPU_FEATURE_TYPE_WEBGL) || |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 183 | gpu_data_manager->ShouldUseSoftwareRendering()) { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 184 | gpu_data_manager->RemoveObserver(this); |
| 185 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 186 | base::FilePath path = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 187 | |
| 188 | Version version(kNullVersion); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 189 | GetLatestSwiftShaderDirectory(&path, &version, NULL); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 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. |
| 198 | void RegisterSwiftShaderPath(ComponentUpdateService* cus) { |
| 199 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 200 | base::FilePath path = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 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); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 209 | std::vector<base::FilePath> older_dirs; |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 210 | if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs)) |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 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. |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 219 | |
| 220 | // Remove older versions of SwiftShader. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 221 | for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame] | 222 | iter != older_dirs.end(); ++iter) { |
| 223 | file_util::Delete(*iter, true); |
| 224 | } |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { |
| 228 | #if defined(ENABLE_SWIFTSHADER) |
[email protected] | 4d3c27f3 | 2012-03-31 09:11:59 | [diff] [blame] | 229 | base::CPU cpu; |
| 230 | |
| 231 | if (!cpu.has_sse2()) |
| 232 | return; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 233 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 234 | base::Bind(&RegisterSwiftShaderPath, cus)); |
| 235 | #endif |
| 236 | } |