| 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 "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/cpu.h" | 10 #include "base/cpu.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 base::FilePath result; | 53 base::FilePath result; |
| 54 PathService::Get(chrome::DIR_USER_DATA, &result); | 54 PathService::Get(chrome::DIR_USER_DATA, &result); |
| 55 return result.Append(kSwiftShaderBaseDirectory); | 55 return result.Append(kSwiftShaderBaseDirectory); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // SwiftShader has version encoded in the path itself | 58 // SwiftShader has version encoded in the path itself |
| 59 // so we need to enumerate the directories to find the full path. | 59 // so we need to enumerate the directories to find the full path. |
| 60 // On success it returns something like: | 60 // On success it returns something like: |
| 61 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. | 61 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. |
| 62 bool GetLatestSwiftShaderDirectory(base::FilePath* result, | 62 bool GetLatestSwiftShaderDirectory(base::FilePath* result, |
| 63 base::Version* latest, | 63 Version* latest, |
| 64 std::vector<base::FilePath>* older_dirs) { | 64 std::vector<base::FilePath>* older_dirs) { |
| 65 base::FilePath base_dir = GetSwiftShaderBaseDirectory(); | 65 base::FilePath base_dir = GetSwiftShaderBaseDirectory(); |
| 66 bool found = false; | 66 bool found = false; |
| 67 base::FileEnumerator | 67 base::FileEnumerator |
| 68 file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES); | 68 file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES); |
| 69 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | 69 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
| 70 path = file_enumerator.Next()) { | 70 path = file_enumerator.Next()) { |
| 71 base::Version version(path.BaseName().MaybeAsASCII()); | 71 Version version(path.BaseName().MaybeAsASCII()); |
| 72 if (!version.IsValid()) | 72 if (!version.IsValid()) |
| 73 continue; | 73 continue; |
| 74 if (version.CompareTo(*latest) > 0 && | 74 if (version.CompareTo(*latest) > 0 && |
| 75 base::PathExists(path.Append(kSwiftShaderEglName)) && | 75 base::PathExists(path.Append(kSwiftShaderEglName)) && |
| 76 base::PathExists(path.Append(kSwiftShaderGlesName))) { | 76 base::PathExists(path.Append(kSwiftShaderGlesName))) { |
| 77 if (found && older_dirs) | 77 if (found && older_dirs) |
| 78 older_dirs->push_back(*result); | 78 older_dirs->push_back(*result); |
| 79 *latest = version; | 79 *latest = version; |
| 80 *result = path; | 80 *result = path; |
| 81 found = true; | 81 found = true; |
| 82 } else { | 82 } else { |
| 83 if (older_dirs) | 83 if (older_dirs) |
| 84 older_dirs->push_back(path); | 84 older_dirs->push_back(path); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 return found; | 87 return found; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void RegisterSwiftShaderWithChrome(const base::FilePath& path) { | 90 void RegisterSwiftShaderWithChrome(const base::FilePath& path) { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 92 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); | 92 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 93 } | 93 } |
| 94 | 94 |
| 95 } // namespace | 95 } // namespace |
| 96 | 96 |
| 97 class SwiftShaderComponentInstaller : public ComponentInstaller { | 97 class SwiftShaderComponentInstaller : public ComponentInstaller { |
| 98 public: | 98 public: |
| 99 explicit SwiftShaderComponentInstaller(const base::Version& version); | 99 explicit SwiftShaderComponentInstaller(const Version& version); |
| 100 | 100 |
| 101 virtual ~SwiftShaderComponentInstaller() {} | 101 virtual ~SwiftShaderComponentInstaller() {} |
| 102 | 102 |
| 103 virtual void OnUpdateError(int error) OVERRIDE; | 103 virtual void OnUpdateError(int error) OVERRIDE; |
| 104 | 104 |
| 105 virtual bool Install(const base::DictionaryValue& manifest, | 105 virtual bool Install(const base::DictionaryValue& manifest, |
| 106 const base::FilePath& unpack_path) OVERRIDE; | 106 const base::FilePath& unpack_path) OVERRIDE; |
| 107 | 107 |
| 108 virtual bool GetInstalledFile(const std::string& file, | 108 virtual bool GetInstalledFile(const std::string& file, |
| 109 base::FilePath* installed_file) OVERRIDE; | 109 base::FilePath* installed_file) OVERRIDE; |
| 110 | 110 |
| 111 private: | 111 private: |
| 112 base::Version current_version_; | 112 Version current_version_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( | 115 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
| 116 const base::Version& version) : current_version_(version) { | 116 const Version& version) : current_version_(version) { |
| 117 DCHECK(version.IsValid()); | 117 DCHECK(version.IsValid()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void SwiftShaderComponentInstaller::OnUpdateError(int error) { | 120 void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 121 NOTREACHED() << "SwiftShader update error: " << error; | 121 NOTREACHED() << "SwiftShader update error: " << error; |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool SwiftShaderComponentInstaller::Install( | 124 bool SwiftShaderComponentInstaller::Install( |
| 125 const base::DictionaryValue& manifest, | 125 const base::DictionaryValue& manifest, |
| 126 const base::FilePath& unpack_path) { | 126 const base::FilePath& unpack_path) { |
| 127 std::string name; | 127 std::string name; |
| 128 manifest.GetStringASCII("name", &name); | 128 manifest.GetStringASCII("name", &name); |
| 129 if (name != kSwiftShaderManifestName) | 129 if (name != kSwiftShaderManifestName) |
| 130 return false; | 130 return false; |
| 131 std::string proposed_version; | 131 std::string proposed_version; |
| 132 manifest.GetStringASCII("version", &proposed_version); | 132 manifest.GetStringASCII("version", &proposed_version); |
| 133 base::Version version(proposed_version.c_str()); | 133 Version version(proposed_version.c_str()); |
| 134 if (!version.IsValid()) | 134 if (!version.IsValid()) |
| 135 return false; | 135 return false; |
| 136 if (current_version_.CompareTo(version) >= 0) | 136 if (current_version_.CompareTo(version) >= 0) |
| 137 return false; | 137 return false; |
| 138 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) || | 138 if (!base::PathExists(unpack_path.Append(kSwiftShaderEglName)) || |
| 139 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName))) | 139 !base::PathExists(unpack_path.Append(kSwiftShaderGlesName))) |
| 140 return false; | 140 return false; |
| 141 // Passed the basic tests. Time to install it. | 141 // Passed the basic tests. Time to install it. |
| 142 base::FilePath path = | 142 base::FilePath path = |
| 143 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); | 143 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); |
| 144 if (base::PathExists(path)) | 144 if (base::PathExists(path)) |
| 145 return false; | 145 return false; |
| 146 if (!base::Move(unpack_path, path)) | 146 if (!base::Move(unpack_path, path)) |
| 147 return false; | 147 return false; |
| 148 // Installation is done. Now tell the rest of chrome. | 148 // Installation is done. Now tell the rest of chrome. |
| 149 current_version_ = version; | 149 current_version_ = version; |
| 150 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 150 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 151 base::Bind(&RegisterSwiftShaderWithChrome, path)); | 151 base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 152 return true; | 152 return true; |
| 153 } | 153 } |
| 154 | 154 |
| 155 bool SwiftShaderComponentInstaller::GetInstalledFile( | 155 bool SwiftShaderComponentInstaller::GetInstalledFile( |
| 156 const std::string& file, base::FilePath* installed_file) { | 156 const std::string& file, base::FilePath* installed_file) { |
| 157 return false; | 157 return false; |
| 158 } | 158 } |
| 159 | 159 |
| 160 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, | 160 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, |
| 161 const base::Version& version) { | 161 const Version& version) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 163 | 163 |
| 164 CrxComponent swiftshader; | 164 CrxComponent swiftshader; |
| 165 swiftshader.name = "Swift Shader"; | 165 swiftshader.name = "Swift Shader"; |
| 166 swiftshader.installer = new SwiftShaderComponentInstaller(version); | 166 swiftshader.installer = new SwiftShaderComponentInstaller(version); |
| 167 swiftshader.version = version; | 167 swiftshader.version = version; |
| 168 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | 168 swiftshader.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
| 169 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { | 169 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { |
| 170 NOTREACHED() << "SwiftShader component registration fail"; | 170 NOTREACHED() << "SwiftShader component registration fail"; |
| 171 } | 171 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 188 void UpdateChecker::OnGpuInfoUpdate() { | 188 void UpdateChecker::OnGpuInfoUpdate() { |
| 189 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); | 189 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); |
| 190 | 190 |
| 191 if (!gpu_data_manager->GpuAccessAllowed(NULL) || | 191 if (!gpu_data_manager->GpuAccessAllowed(NULL) || |
| 192 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) || | 192 gpu_data_manager->IsFeatureBlacklisted(gpu::GPU_FEATURE_TYPE_WEBGL) || |
| 193 gpu_data_manager->ShouldUseSwiftShader()) { | 193 gpu_data_manager->ShouldUseSwiftShader()) { |
| 194 gpu_data_manager->RemoveObserver(this); | 194 gpu_data_manager->RemoveObserver(this); |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 196 base::FilePath path = GetSwiftShaderBaseDirectory(); | 196 base::FilePath path = GetSwiftShaderBaseDirectory(); |
| 197 | 197 |
| 198 base::Version version(kNullVersion); | 198 Version version(kNullVersion); |
| 199 GetLatestSwiftShaderDirectory(&path, &version, NULL); | 199 GetLatestSwiftShaderDirectory(&path, &version, NULL); |
| 200 | 200 |
| 201 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 201 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 202 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); | 202 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Check if there already is a version of swiftshader installed, | 206 // Check if there already is a version of swiftshader installed, |
| 207 // and if so register it. | 207 // and if so register it. |
| 208 void RegisterSwiftShaderPath(ComponentUpdateService* cus) { | 208 void RegisterSwiftShaderPath(ComponentUpdateService* cus) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 210 base::FilePath path = GetSwiftShaderBaseDirectory(); | 210 base::FilePath path = GetSwiftShaderBaseDirectory(); |
| 211 if (!base::PathExists(path)) { | 211 if (!base::PathExists(path)) { |
| 212 if (!base::CreateDirectory(path)) { | 212 if (!base::CreateDirectory(path)) { |
| 213 NOTREACHED() << "Could not create SwiftShader directory."; | 213 NOTREACHED() << "Could not create SwiftShader directory."; |
| 214 return; | 214 return; |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 base::Version version(kNullVersion); | 218 Version version(kNullVersion); |
| 219 std::vector<base::FilePath> older_dirs; | 219 std::vector<base::FilePath> older_dirs; |
| 220 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs)) | 220 if (GetLatestSwiftShaderDirectory(&path, &version, &older_dirs)) |
| 221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 222 base::Bind(&RegisterSwiftShaderWithChrome, path)); | 222 base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 223 | 223 |
| 224 UpdateChecker *update_checker = new UpdateChecker(cus); | 224 UpdateChecker *update_checker = new UpdateChecker(cus); |
| 225 GpuDataManager::GetInstance()->AddObserver(update_checker); | 225 GpuDataManager::GetInstance()->AddObserver(update_checker); |
| 226 update_checker->OnGpuInfoUpdate(); | 226 update_checker->OnGpuInfoUpdate(); |
| 227 // We leak update_checker here, because it has to stick around for the life | 227 // We leak update_checker here, because it has to stick around for the life |
| 228 // of the GpuDataManager. | 228 // of the GpuDataManager. |
| 229 | 229 |
| 230 // Remove older versions of SwiftShader. | 230 // Remove older versions of SwiftShader. |
| 231 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); | 231 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
| 232 iter != older_dirs.end(); ++iter) { | 232 iter != older_dirs.end(); ++iter) { |
| 233 base::DeleteFile(*iter, true); | 233 base::DeleteFile(*iter, true); |
| 234 } | 234 } |
| 235 } | 235 } |
| 236 | 236 |
| 237 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { | 237 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { |
| 238 #if defined(ENABLE_SWIFTSHADER) | 238 #if defined(ENABLE_SWIFTSHADER) |
| 239 base::CPU cpu; | 239 base::CPU cpu; |
| 240 | 240 |
| 241 if (!cpu.has_sse2()) | 241 if (!cpu.has_sse2()) |
| 242 return; | 242 return; |
| 243 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 243 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 244 base::Bind(&RegisterSwiftShaderPath, cus)); | 244 base::Bind(&RegisterSwiftShaderPath, cus)); |
| 245 #endif | 245 #endif |
| 246 } | 246 } |
| OLD | NEW |