[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 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/base_paths.h" |
| 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_path.h" |
| 12 | #include "base/file_util.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" |
[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] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 22 | |
| 23 | using content::BrowserThread; |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 24 | using content::GpuDataManager; |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. |
| 29 | const 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. |
| 35 | const FilePath::CharType kSwiftShaderEglName[] = |
| 36 | FILE_PATH_LITERAL("libegl.dll"); |
| 37 | const FilePath::CharType kSwiftShaderGlesName[] = |
| 38 | FILE_PATH_LITERAL("libglesv2.dll"); |
| 39 | |
| 40 | const char kSwiftShaderManifestName[] = "SwiftShader"; |
| 41 | |
| 42 | const FilePath::CharType kSwiftShaderBaseDirectory[] = |
| 43 | FILE_PATH_LITERAL("SwiftShader"); |
| 44 | |
| 45 | // If we don't have a SwiftShader component, this is the version we claim. |
| 46 | const char kNullVersion[] = "0.0.0.0"; |
| 47 | |
| 48 | // The base directory on windows looks like: |
| 49 | // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\. |
| 50 | FilePath GetSwiftShaderBaseDirectory() { |
| 51 | 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\. |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame^] | 60 | bool GetLatestSwiftShaderDirectory(FilePath* result, |
| 61 | Version* latest, |
| 62 | std::vector<FilePath>* older_dirs) { |
| 63 | FilePath base_dir = GetSwiftShaderBaseDirectory(); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 64 | bool found = false; |
| 65 | file_util::FileEnumerator |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame^] | 66 | file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 67 | for (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))) { |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame^] | 75 | if (found && older_dirs) |
| 76 | older_dirs->push_back(*result); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 77 | *latest = version; |
| 78 | *result = path; |
| 79 | found = true; |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame^] | 80 | } else { |
| 81 | if (older_dirs) |
| 82 | older_dirs->push_back(path); |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return found; |
| 86 | } |
| 87 | |
| 88 | void RegisterSwiftShaderWithChrome(const FilePath& path) { |
| 89 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 | GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | class 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 FilePath& unpack_path) OVERRIDE; |
| 105 | |
| 106 | private: |
| 107 | Version current_version_; |
| 108 | }; |
| 109 | |
| 110 | SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
| 111 | const Version& version) : current_version_(version) { |
| 112 | DCHECK(version.IsValid()); |
| 113 | } |
| 114 | |
| 115 | void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 116 | NOTREACHED() << "SwiftShader update error: " << error; |
| 117 | } |
| 118 | |
| 119 | bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest, |
| 120 | const 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 | 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 | |
| 149 | void 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 | |
[email protected] | 79078df | 2012-02-16 01:22:32 | [diff] [blame] | 163 | class UpdateChecker : public content::GpuDataManagerObserver { |
[email protected] | df353ab | 2011-12-16 23:57:57 | [diff] [blame] | 164 | public: |
| 165 | explicit UpdateChecker(ComponentUpdateService* cus); |
| 166 | |
| 167 | virtual void OnGpuInfoUpdate() OVERRIDE; |
| 168 | |
| 169 | private: |
| 170 | ComponentUpdateService* cus_; |
| 171 | }; |
| 172 | |
| 173 | UpdateChecker::UpdateChecker(ComponentUpdateService* cus) |
| 174 | : cus_(cus) { |
| 175 | } |
| 176 | |
| 177 | void UpdateChecker::OnGpuInfoUpdate() { |
| 178 | GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); |
| 179 | |
| 180 | if (!gpu_data_manager->GpuAccessAllowed() || |
[email protected] | d56ecf92 | 2012-02-15 16:03:11 | [diff] [blame] | 181 | (gpu_data_manager->GetGpuFeatureType() & |
| 182 | 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)); |
| 186 | FilePath path = GetSwiftShaderBaseDirectory(); |
| 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)); |
| 200 | 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); |
[email protected] | 5617743 | 2012-06-06 00:37:49 | [diff] [blame^] | 209 | std::vector<FilePath> older_dirs; |
| 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. |
| 221 | for (std::vector<FilePath>::iterator iter = older_dirs.begin(); |
| 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 | } |