OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 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" |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/component_updater/component_updater_service.h" |
| 17 #include "chrome/common/chrome_paths.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/browser/gpu/gpu_data_manager.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // CRX hash. The extension id is: nhfgdggnnopgbfdlpeoalgcjdgfafocg. |
| 26 const uint8 sha2_hash[] = {0xd7, 0x56, 0x36, 0x6d, 0xde, 0xf6, 0x15, 0x3b, |
| 27 0xf4, 0xe0, 0xb6, 0x29, 0x36, 0x50, 0x5e, 0x26, |
| 28 0xbd, 0x77, 0x8b, 0x8e, 0x35, 0xc2, 0x7e, 0x43, |
| 29 0x52, 0x47, 0x62, 0xed, 0x12, 0xca, 0xcc, 0x6a}; |
| 30 |
| 31 // File name of the internal SwiftShader plugin on different platforms. |
| 32 const FilePath::CharType kSwiftShaderEglName[] = |
| 33 FILE_PATH_LITERAL("libegl.dll"); |
| 34 const FilePath::CharType kSwiftShaderGlesName[] = |
| 35 FILE_PATH_LITERAL("libglesv2.dll"); |
| 36 |
| 37 const char kSwiftShaderManifestName[] = "SwiftShader"; |
| 38 |
| 39 const FilePath::CharType kSwiftShaderBaseDirectory[] = |
| 40 FILE_PATH_LITERAL("SwiftShader"); |
| 41 |
| 42 // If we don't have a SwiftShader component, this is the version we claim. |
| 43 const char kNullVersion[] = "0.0.0.0"; |
| 44 |
| 45 // The base directory on windows looks like: |
| 46 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\. |
| 47 FilePath GetSwiftShaderBaseDirectory() { |
| 48 FilePath result; |
| 49 PathService::Get(chrome::DIR_USER_DATA, &result); |
| 50 return result.Append(kSwiftShaderBaseDirectory); |
| 51 } |
| 52 |
| 53 // SwiftShader has version encoded in the path itself |
| 54 // so we need to enumerate the directories to find the full path. |
| 55 // On success it returns something like: |
| 56 // <profile>\AppData\Local\Google\Chrome\User Data\SwiftShader\10.3.44.555\. |
| 57 bool GetLatestSwiftShaderDirectory(FilePath* result, Version* latest) { |
| 58 *result = GetSwiftShaderBaseDirectory(); |
| 59 bool found = false; |
| 60 file_util::FileEnumerator |
| 61 file_enumerator(*result, false, file_util::FileEnumerator::DIRECTORIES); |
| 62 for (FilePath path = file_enumerator.Next(); !path.value().empty(); |
| 63 path = file_enumerator.Next()) { |
| 64 Version version(path.BaseName().MaybeAsASCII()); |
| 65 if (!version.IsValid()) |
| 66 continue; |
| 67 if (version.CompareTo(*latest) > 0 && |
| 68 file_util::PathExists(path.Append(kSwiftShaderEglName)) && |
| 69 file_util::PathExists(path.Append(kSwiftShaderGlesName))) { |
| 70 *latest = version; |
| 71 *result = path; |
| 72 found = true; |
| 73 } |
| 74 } |
| 75 return found; |
| 76 } |
| 77 |
| 78 void RegisterSwiftShaderWithChrome(const FilePath& path) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 80 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 class SwiftShaderComponentInstaller : public ComponentInstaller { |
| 86 public: |
| 87 explicit SwiftShaderComponentInstaller(const Version& version); |
| 88 |
| 89 virtual ~SwiftShaderComponentInstaller() {} |
| 90 |
| 91 virtual void OnUpdateError(int error) OVERRIDE; |
| 92 |
| 93 virtual bool Install(base::DictionaryValue* manifest, |
| 94 const FilePath& unpack_path) OVERRIDE; |
| 95 |
| 96 private: |
| 97 Version current_version_; |
| 98 }; |
| 99 |
| 100 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( |
| 101 const Version& version) : current_version_(version) { |
| 102 DCHECK(version.IsValid()); |
| 103 } |
| 104 |
| 105 void SwiftShaderComponentInstaller::OnUpdateError(int error) { |
| 106 NOTREACHED() << "SwiftShader update error: " << error; |
| 107 } |
| 108 |
| 109 bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest, |
| 110 const FilePath& unpack_path) { |
| 111 std::string name; |
| 112 manifest->GetStringASCII("name", &name); |
| 113 if (name != kSwiftShaderManifestName) |
| 114 return false; |
| 115 std::string proposed_version; |
| 116 manifest->GetStringASCII("version", &proposed_version); |
| 117 Version version(proposed_version.c_str()); |
| 118 if (!version.IsValid()) |
| 119 return false; |
| 120 if (current_version_.CompareTo(version) >= 0) |
| 121 return false; |
| 122 if (!file_util::PathExists(unpack_path.Append(kSwiftShaderEglName)) || |
| 123 !file_util::PathExists(unpack_path.Append(kSwiftShaderGlesName))) |
| 124 return false; |
| 125 // Passed the basic tests. Time to install it. |
| 126 FilePath path = |
| 127 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); |
| 128 if (file_util::PathExists(path)) |
| 129 return false; |
| 130 if (!file_util::Move(unpack_path, path)) |
| 131 return false; |
| 132 // Installation is done. Now tell the rest of chrome. |
| 133 current_version_ = version; |
| 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 135 base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 136 return true; |
| 137 } |
| 138 |
| 139 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, |
| 140 const Version& version) { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 142 |
| 143 CrxComponent swiftshader; |
| 144 swiftshader.name = "Swift Shader"; |
| 145 swiftshader.installer = new SwiftShaderComponentInstaller(version); |
| 146 swiftshader.version = version; |
| 147 swiftshader.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]); |
| 148 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { |
| 149 NOTREACHED() << "SwiftShader component registration fail"; |
| 150 } |
| 151 } |
| 152 |
| 153 class UpdateChecker : public GpuDataManager::Observer { |
| 154 public: |
| 155 explicit UpdateChecker(ComponentUpdateService* cus); |
| 156 |
| 157 virtual void OnGpuInfoUpdate() OVERRIDE; |
| 158 |
| 159 private: |
| 160 ComponentUpdateService* cus_; |
| 161 }; |
| 162 |
| 163 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) |
| 164 : cus_(cus) { |
| 165 } |
| 166 |
| 167 void UpdateChecker::OnGpuInfoUpdate() { |
| 168 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); |
| 169 |
| 170 if (!gpu_data_manager->GpuAccessAllowed() || |
| 171 (gpu_data_manager->GetGpuFeatureFlags().flags() & |
| 172 GpuFeatureFlags::kGpuFeatureWebgl) || |
| 173 gpu_data_manager->software_rendering()) { |
| 174 gpu_data_manager->RemoveObserver(this); |
| 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 176 FilePath path = GetSwiftShaderBaseDirectory(); |
| 177 |
| 178 Version version(kNullVersion); |
| 179 GetLatestSwiftShaderDirectory(&path, &version); |
| 180 |
| 181 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 182 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); |
| 183 } |
| 184 } |
| 185 |
| 186 // Check if there already is a version of swiftshader installed, |
| 187 // and if so register it. |
| 188 void RegisterSwiftShaderPath(ComponentUpdateService* cus) { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 190 FilePath path = GetSwiftShaderBaseDirectory(); |
| 191 if (!file_util::PathExists(path)) { |
| 192 if (!file_util::CreateDirectory(path)) { |
| 193 NOTREACHED() << "Could not create SwiftShader directory."; |
| 194 return; |
| 195 } |
| 196 } |
| 197 |
| 198 Version version(kNullVersion); |
| 199 if (GetLatestSwiftShaderDirectory(&path, &version)) |
| 200 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 201 base::Bind(&RegisterSwiftShaderWithChrome, path)); |
| 202 |
| 203 UpdateChecker *update_checker = new UpdateChecker(cus); |
| 204 GpuDataManager::GetInstance()->AddObserver(update_checker); |
| 205 update_checker->OnGpuInfoUpdate(); |
| 206 // We leak update_checker here, because it has to stick around for the life |
| 207 // of the GpuDataManager. |
| 208 } |
| 209 |
| 210 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { |
| 211 #if defined(ENABLE_SWIFTSHADER) |
| 212 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 213 base::Bind(&RegisterSwiftShaderPath, cus)); |
| 214 #endif |
| 215 } |
OLD | NEW |