| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/component_updater/swiftshader_component_installer.h" | 5 #include "chrome/browser/component_updater/swiftshader_component_installer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/cpu.h" | 13 #include "base/cpu.h" |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/component_updater/component_updater_service.h" | 21 #include "chrome/browser/component_updater/component_updater_service.h" |
| 22 #include "chrome/common/chrome_paths.h" | |
| 23 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/gpu_data_manager.h" | 23 #include "content/public/browser/gpu_data_manager.h" |
| 25 #include "content/public/browser/gpu_data_manager_observer.h" | 24 #include "content/public/browser/gpu_data_manager_observer.h" |
| 26 #include "gpu/config/gpu_feature_type.h" | 25 #include "gpu/config/gpu_feature_type.h" |
| 27 | 26 |
| 28 using content::BrowserThread; | 27 using content::BrowserThread; |
| 29 using content::GpuDataManager; | 28 using content::GpuDataManager; |
| 30 | 29 |
| 31 namespace component_updater { | 30 namespace component_updater { |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| 35 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. | 34 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. |
| 36 const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b, | 35 const uint8 kSha2Hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b, |
| 37 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26, | 36 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26, |
| 38 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43, | 37 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43, |
| 39 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a}; | 38 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a}; |
| 40 | 39 |
| 41 // File name of the internal SwiftShader plugin on different platforms. | 40 // File name of the internal SwiftShader plugin on different platforms. |
| 42 const base::FilePath::CharType kSwiftShaderEglName[] = | 41 const base::FilePath::CharType kSwiftShaderEglName[] = |
| 43 FILE_PATH_LITERAL("libegl.dll"); | 42 FILE_PATH_LITERAL("libegl.dll"); |
| 44 const base::FilePath::CharType kSwiftShaderGlesName[] = | 43 const base::FilePath::CharType kSwiftShaderGlesName[] = |
| 45 FILE_PATH_LITERAL("libglesv2.dll"); | 44 FILE_PATH_LITERAL("libglesv2.dll"); |
| 46 | 45 |
| 47 const char kSwiftShaderManifestName[] = "SwiftShader"; | 46 const char kSwiftShaderManifestName[] = "SwiftShader"; |
| 48 | 47 |
| 48 #if defined(ENABLE_SWIFTSHADER) |
| 49 const base::FilePath::CharType kSwiftShaderBaseDirectory[] = | 49 const base::FilePath::CharType kSwiftShaderBaseDirectory[] = |
| 50 FILE_PATH_LITERAL("SwiftShader"); | 50 FILE_PATH_LITERAL("SwiftShader"); |
| 51 #endif // ENABLE_SWIFTSHADER |
| 51 | 52 |
| 52 // If we don't have a SwiftShader component, this is the version we claim. | 53 // If we don't have a SwiftShader component, this is the version we claim. |
| 53 const char kNullVersion[] = "0.0.0.0"; | 54 const char kNullVersion[] = "0.0.0.0"; |
| 54 | 55 |
| 55 // The base directory on windows looks like: | |
| 56 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\. | |
| 57 base::FilePath GetSwiftShaderBaseDirectory() { | |
| 58 base::FilePath result; | |
| 59 PathService::Get(chrome::DIR_USER_DATA, &result); | |
| 60 return result.Append(kSwiftShaderBaseDirectory); | |
| 61 } | |
| 62 | |
| 63 // SwiftShader has version encoded in the path itself | 56 // SwiftShader has version encoded in the path itself |
| 64 // so we need to enumerate the directories to find the full path. | 57 // so we need to enumerate the directories to find the full path. |
| 65 // On success it returns something like: | 58 // On success it returns something like: |
| 66 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. | 59 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. |
| 67 bool GetLatestSwiftShaderDirectory(base::FilePath* result, | 60 // Return true if a SwiftShader directory newer than |latest| was found. |
| 61 // |result| must be initially an empty directory. |
| 62 bool GetLatestSwiftShaderDirectory(const base::FilePath& base_dir, |
| 63 base::FilePath* result, |
| 68 Version* latest, | 64 Version* latest, |
| 69 std::vector<base::FilePath>* older_dirs) { | 65 std::vector<base::FilePath>* older_dirs) { |
| 70 base::FilePath base_dir = GetSwiftShaderBaseDirectory(); | 66 DCHECK(result->empty()); |
| 71 bool found = false; | 67 bool found = false; |
| 72 base::FileEnumerator file_enumerator( | 68 base::FileEnumerator file_enumerator( |
| 73 base_dir, false, base::FileEnumerator::DIRECTORIES); | 69 base_dir, false, base::FileEnumerator::DIRECTORIES); |
| 74 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | 70 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
| 75 path = file_enumerator.Next()) { | 71 path = file_enumerator.Next()) { |
| 76 Version version(path.BaseName().MaybeAsASCII()); | 72 Version version(path.BaseName().MaybeAsASCII()); |
| 77 if (!version.IsValid()) | 73 if (!version.IsValid()) |
| 78 continue; | 74 continue; |
| 79 if (version.CompareTo(*latest) > 0 && | 75 if (version.CompareTo(*latest) > 0 && |
| 80 base::PathExists(path.Append(kSwiftShaderEglName)) && | 76 base::PathExists(path.Append(kSwiftShaderEglName)) && |
| 81 base::PathExists(path.Append(kSwiftShaderGlesName))) { | 77 base::PathExists(path.Append(kSwiftShaderGlesName))) { |
| 82 if (found && older_dirs) | 78 if (found && older_dirs && !result->empty()) |
| 83 older_dirs->push_back(*result); | 79 older_dirs->push_back(*result); |
| 84 *latest = version; | 80 *latest = version; |
| 85 *result = path; | 81 *result = path; |
| 86 found = true; | 82 found = true; |
| 87 } else { | 83 } else { |
| 88 if (older_dirs) | 84 if (older_dirs) |
| 89 older_dirs->push_back(path); | 85 older_dirs->push_back(path); |
| 90 } | 86 } |
| 91 } | 87 } |
| 92 return found; | 88 return found; |
| 93 } | 89 } |
| 94 | 90 |
| 95 void RegisterSwiftShaderWithChrome(const base::FilePath& path) { | 91 void RegisterSwiftShaderWithChrome(const base::FilePath& path) { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); | 93 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 98 } | 94 } |
| 99 | 95 |
| 100 class SwiftShaderComponentInstaller : public ComponentInstaller { | 96 class SwiftShaderComponentInstaller : public ComponentInstaller { |
| 101 public: | 97 public: |
| 102 explicit SwiftShaderComponentInstaller(const Version& version); | 98 explicit SwiftShaderComponentInstaller(const base::FilePath& base_dir, |
| 99 const Version& version); |
| 103 | 100 |
| 104 virtual ~SwiftShaderComponentInstaller() {} | 101 virtual ~SwiftShaderComponentInstaller() {} |
| 105 | 102 |
| 106 virtual void OnUpdateError(int error) OVERRIDE; | 103 virtual void OnUpdateError(int error) OVERRIDE; |
| 107 | 104 |
| 108 virtual bool Install(const base::DictionaryValue& manifest, | 105 virtual bool Install(const base::DictionaryValue& manifest, |
| 109 const base::FilePath& unpack_path) OVERRIDE; | 106 const base::FilePath& unpack_path) OVERRIDE; |
| 110 | 107 |
| 111 virtual bool GetInstalledFile(const std::string& file, | 108 virtual bool GetInstalledFile(const std::string& file, |
| 112 base::FilePath* installed_file) OVERRIDE; | 109 base::FilePath* installed_file) OVERRIDE; |
| 113 | 110 |
| 114 private: | 111 private: |
| 112 const base::FilePath base_dir_; |
| 115 Version current_version_; | 113 Version current_version_; |
| 116 }; | 114 }; |
| 117 | 115 |
| 118 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( | 116 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
| 117 const base::FilePath& base_dir, |
| 119 const Version& version) | 118 const Version& version) |
| 120 : current_version_(version) { | 119 : base_dir_(base_dir), |
| 120 current_version_(version) { |
| 121 DCHECK(version.IsValid()); | 121 DCHECK(version.IsValid()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void SwiftShaderComponentInstaller::OnUpdateError(int error) { | 124 void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 125 NOTREACHED() << "SwiftShader update error: " << error; | 125 NOTREACHED() << "SwiftShader update error: " << error; |
| 126 } | 126 } |
| 127 | 127 |
| 128 bool SwiftShaderComponentInstaller::Install( | 128 bool SwiftShaderComponentInstaller::Install( |
| 129 const base::DictionaryValue& manifest, | 129 const base::DictionaryValue& manifest, |
| 130 const base::FilePath& unpack_path) { | 130 const base::FilePath& unpack_path) { |
| 131 std::string name; | 131 std::string name; |
| 132 manifest.GetStringASCII("name", &name); | 132 manifest.GetStringASCII("name", &name); |
| 133 if (name != kSwiftShaderManifestName) | 133 if (name != kSwiftShaderManifestName) |
| 134 return false; | 134 return false; |
| 135 std::string proposed_version; | 135 std::string proposed_version; |
| 136 manifest.GetStringASCII("version", &proposed_version); | 136 manifest.GetStringASCII("version", &proposed_version); |
| 137 Version version(proposed_version.c_str()); | 137 Version version(proposed_version.c_str()); |
| 138 if (!version.IsValid()) | 138 if (!version.IsValid()) |
| 139 return false; | 139 return false; |
| 140 if (current_version_.CompareTo(version) >= 0) | 140 if (current_version_.CompareTo(version) >= 0) |
| 141 return false; | 141 return false; |
| 142 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) || | 142 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) || |
| 143 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName))) | 143 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName))) |
| 144 return false; | 144 return false; |
| 145 // Passed the basic tests. Time to install it. | 145 // Passed the basic tests. Time to install it. |
| 146 base::FilePath path = | 146 base::FilePath path = base_dir_.AppendASCII(version.GetString()); |
| 147 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); | |
| 148 if (base::PathExists(path)) | 147 if (base::PathExists(path)) |
| 149 return false; | 148 return false; |
| 150 if (!base::Move(unpack_path, path)) | 149 if (!base::Move(unpack_path, path)) |
| 151 return false; | 150 return false; |
| 152 // Installation is done. Now tell the rest of chrome. | 151 // Installation is done. Now tell the rest of chrome. |
| 153 current_version_ = version; | 152 current_version_ = version; |
| 154 BrowserThread::PostTask(BrowserThread::UI, | 153 BrowserThread::PostTask(BrowserThread::UI, |
| 155 FROM_HERE, | 154 FROM_HERE, |
| 156 base::Bind(&RegisterSwiftShaderWithChrome, path)); | 155 base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 157 return true; | 156 return true; |
| 158 } | 157 } |
| 159 | 158 |
| 160 bool SwiftShaderComponentInstaller::GetInstalledFile( | 159 bool SwiftShaderComponentInstaller::GetInstalledFile( |
| 161 const std::string& file, | 160 const std::string& file, |
| 162 base::FilePath* installed_file) { | 161 base::FilePath* installed_file) { |
| 163 return false; | 162 return false; |
| 164 } | 163 } |
| 165 | 164 |
| 166 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, | 165 void FinishSwiftShaderUpdateRegistration(const base::FilePath& base_dir, |
| 166 ComponentUpdateService* cus, |
| 167 const Version& version) { | 167 const Version& version) { |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 169 | 169 |
| 170 CrxComponent swiftshader; | 170 CrxComponent swiftshader; |
| 171 swiftshader.name = "Swift Shader"; | 171 swiftshader.name = "Swift Shader"; |
| 172 swiftshader.installer = new SwiftShaderComponentInstaller(version); | 172 swiftshader.installer = new SwiftShaderComponentInstaller(base_dir, version); |
| 173 swiftshader.version = version; | 173 swiftshader.version = version; |
| 174 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | 174 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
| 175 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { | 175 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { |
| 176 NOTREACHED() << "SwiftShader component registration fail"; | 176 NOTREACHED() << "SwiftShader component registration fail"; |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 class UpdateChecker : public content::GpuDataManagerObserver { | 180 class UpdateChecker : public content::GpuDataManagerObserver { |
| 181 public: | 181 public: |
| 182 explicit UpdateChecker(ComponentUpdateService* cus); | 182 explicit UpdateChecker(ComponentUpdateService* cus, |
| 183 const base::FilePath& base_dir); |
| 183 | 184 |
| 184 virtual void OnGpuInfoUpdate() OVERRIDE; | 185 virtual void OnGpuInfoUpdate() OVERRIDE; |
| 185 | 186 |
| 186 private: | 187 private: |
| 187 ComponentUpdateService* cus_; | 188 ComponentUpdateService* cus_; |
| 189 const base::FilePath base_dir_; |
| 188 }; | 190 }; |
| 189 | 191 |
| 190 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) : cus_(cus) { | 192 UpdateChecker::UpdateChecker(ComponentUpdateService* cus, |
| 193 const base::FilePath& base_dir) |
| 194 : cus_(cus), |
| 195 base_dir_(base_dir) { |
| 191 } | 196 } |
| 192 | 197 |
| 193 void UpdateChecker::OnGpuInfoUpdate() { | 198 void UpdateChecker::OnGpuInfoUpdate() { |
| 194 GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance(); | 199 GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance(); |
| 195 | 200 |
| 196 if (!gpu_data_manager->GpuAccessAllowed(NULL) || | 201 if (!gpu_data_manager->GpuAccessAllowed(NULL) || |
| 197 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) || | 202 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) || |
| 198 gpu_data_manager->ShouldUseSwiftShader()) { | 203 gpu_data_manager->ShouldUseSwiftShader()) { |
| 199 gpu_data_manager->RemoveObserver(this); | 204 gpu_data_manager->RemoveObserver(this); |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 201 base::FilePath path = GetSwiftShaderBaseDirectory(); | |
| 202 | 206 |
| 207 base::FilePath latest_dir; |
| 203 Version version(kNullVersion); | 208 Version version(kNullVersion); |
| 204 GetLatestSwiftShaderDirectory(&path, &version, NULL); | 209 GetLatestSwiftShaderDirectory(base_dir_, &latest_dir, &version, NULL); |
| 205 | 210 |
| 206 BrowserThread::PostTask( | 211 BrowserThread::PostTask( |
| 207 BrowserThread::UI, | 212 BrowserThread::UI, |
| 208 FROM_HERE, | 213 FROM_HERE, |
| 209 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); | 214 base::Bind(&FinishSwiftShaderUpdateRegistration, base_dir_, cus_, |
| 215 version)); |
| 210 } | 216 } |
| 211 } | 217 } |
| 212 | 218 |
| 213 #if defined(ENABLE_SWIFTSHADER) | 219 #if defined(ENABLE_SWIFTSHADER) |
| 214 | 220 |
| 215 // Check if there already is a version of swiftshader installed, | 221 // Check if there already is a version of swiftshader installed, |
| 216 // and if so register it. | 222 // and if so register it. |
| 217 void RegisterSwiftShaderPath(ComponentUpdateService* cus) { | 223 void RegisterSwiftShaderPath(ComponentUpdateService* cus, |
| 224 const base::FilePath& user_data_dir) { |
| 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 219 base::FilePath path = GetSwiftShaderBaseDirectory(); | 226 base::FilePath base_dir = user_data_dir.Append(kSwiftShaderBaseDirectory); |
| 220 if (!base::PathExists(path)) { | 227 if (!base::PathExists(base_dir)) { |
| 221 if (!base::CreateDirectory(path)) { | 228 if (!base::CreateDirectory(base_dir)) { |
| 222 NOTREACHED() << "Could not create SwiftShader directory."; | 229 NOTREACHED() << "Could not create SwiftShader directory."; |
| 223 return; | 230 return; |
| 224 } | 231 } |
| 225 } | 232 } |
| 226 | 233 |
| 234 base::FilePath latest_dir; |
| 227 Version version(kNullVersion); | 235 Version version(kNullVersion); |
| 228 std::vector<base::FilePath> older_dirs; | 236 std::vector<base::FilePath> older_dirs; |
| 229 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs)) | 237 if (GetLatestSwiftShaderDirectory(&latest_dir, &version, &older_dirs)) |
| 230 BrowserThread::PostTask(BrowserThread::UI, | 238 BrowserThread::PostTask(BrowserThread::UI, |
| 231 FROM_HERE, | 239 FROM_HERE, |
| 232 base::Bind(&RegisterSwiftShaderWithChrome, path)); | 240 base::Bind(&RegisterSwiftShaderWithChrome, |
| 241 latest_dir)); |
| 233 | 242 |
| 234 UpdateChecker* update_checker = new UpdateChecker(cus); | 243 UpdateChecker* update_checker = new UpdateChecker(cus, base_dir); |
| 235 GpuDataManager::GetInstance()->AddObserver(update_checker); | 244 GpuDataManager::GetInstance()->AddObserver(update_checker); |
| 236 update_checker->OnGpuInfoUpdate(); | 245 update_checker->OnGpuInfoUpdate(); |
| 237 // We leak update_checker here, because it has to stick around for the life | 246 // We leak update_checker here, because it has to stick around for the life |
| 238 // of the GpuDataManager. | 247 // of the GpuDataManager. |
| 239 | 248 |
| 240 // Remove older versions of SwiftShader. | 249 // Remove older versions of SwiftShader. |
| 241 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); | 250 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
| 242 iter != older_dirs.end(); | 251 iter != older_dirs.end(); |
| 243 ++iter) { | 252 ++iter) { |
| 244 base::DeleteFile(*iter, true); | 253 base::DeleteFile(*iter, true); |
| 245 } | 254 } |
| 246 } | 255 } |
| 247 | 256 |
| 248 #endif // ENABLE_SWIFTSHADER | 257 #endif // ENABLE_SWIFTSHADER |
| 249 | 258 |
| 250 } // namespace | 259 } // namespace |
| 251 | 260 |
| 252 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { | 261 void RegisterSwiftShaderComponent(ComponentUpdateService* cus, |
| 262 const base::FilePath& user_data_dir) { |
| 253 #if defined(ENABLE_SWIFTSHADER) | 263 #if defined(ENABLE_SWIFTSHADER) |
| 254 base::CPU cpu; | 264 base::CPU cpu; |
| 255 | 265 |
| 256 if (!cpu.has_sse2()) | 266 if (!cpu.has_sse2()) |
| 257 return; | 267 return; |
| 258 BrowserThread::PostTask(BrowserThread::FILE, | 268 BrowserThread::PostTask(BrowserThread::FILE, |
| 259 FROM_HERE, | 269 FROM_HERE, |
| 260 base::Bind(&RegisterSwiftShaderPath, cus)); | 270 base::Bind(&RegisterSwiftShaderPath, cus)); |
| 261 #endif | 271 #endif |
| 262 } | 272 } |
| 263 | 273 |
| 264 } // namespace component_updater | 274 } // namespace component_updater |
| OLD | NEW |