Chromium Code Reviews| 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 | |
|
cpu_(ooo_6.6-7.5)
2011/12/13 20:09:16
remove space
| |
| 81 GpuDataManager::GetInstance()->RegisterSwiftShaderPath(path); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 class SwiftShaderComponentInstaller : public ComponentInstaller { | |
| 87 public: | |
| 88 explicit SwiftShaderComponentInstaller(const Version& version); | |
| 89 | |
| 90 virtual ~SwiftShaderComponentInstaller() {} | |
| 91 | |
| 92 virtual void OnUpdateError(int error) OVERRIDE; | |
| 93 | |
| 94 virtual bool Install(base::DictionaryValue* manifest, | |
| 95 const FilePath& unpack_path) OVERRIDE; | |
| 96 | |
| 97 private: | |
| 98 Version current_version_; | |
| 99 }; | |
| 100 | |
| 101 SwiftShaderComponentInstaller::SwiftShaderComponentInstaller( | |
| 102 const Version& version) : current_version_(version) { | |
| 103 DCHECK(version.IsValid()); | |
| 104 } | |
| 105 | |
| 106 void SwiftShaderComponentInstaller::OnUpdateError(int error) { | |
| 107 NOTREACHED() << "SwiftShader update error: " << error; | |
| 108 } | |
| 109 | |
| 110 bool SwiftShaderComponentInstaller::Install(base::DictionaryValue* manifest, | |
| 111 const FilePath& unpack_path) { | |
| 112 std::string name; | |
| 113 manifest->GetStringASCII("name", &name); | |
| 114 if (name != kSwiftShaderManifestName) | |
| 115 return false; | |
| 116 std::string proposed_version; | |
| 117 manifest->GetStringASCII("version", &proposed_version); | |
| 118 Version version(proposed_version.c_str()); | |
| 119 if (!version.IsValid()) | |
| 120 return false; | |
| 121 if (current_version_.CompareTo(version) >= 0) | |
| 122 return false; | |
| 123 if (!file_util::PathExists(unpack_path.Append(kSwiftShaderEglName)) || | |
| 124 !file_util::PathExists(unpack_path.Append(kSwiftShaderGlesName))) | |
| 125 return false; | |
| 126 // Passed the basic tests. Time to install it. | |
| 127 FilePath path = | |
| 128 GetSwiftShaderBaseDirectory().AppendASCII(version.GetString()); | |
| 129 if (file_util::PathExists(path)) | |
| 130 return false; | |
| 131 if (!file_util::Move(unpack_path, path)) | |
| 132 return false; | |
| 133 // Installation is done. Now tell the rest of chrome. | |
| 134 current_version_ = version; | |
| 135 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 136 base::Bind(&RegisterSwiftShaderWithChrome, path)); | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 void FinishSwiftShaderUpdateRegistration(ComponentUpdateService* cus, | |
| 141 const Version& version) { | |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 143 | |
| 144 CrxComponent swiftshader; | |
| 145 swiftshader.name = "Swift Shader"; | |
| 146 swiftshader.installer = new SwiftShaderComponentInstaller(version); | |
| 147 swiftshader.version = version; | |
| 148 swiftshader.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]); | |
| 149 if (cus->RegisterComponent(swiftshader) != ComponentUpdateService::kOk) { | |
| 150 NOTREACHED() << "SwiftShader component registration fail"; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 class UpdateChecker : public GpuDataManager::Observer { | |
| 155 public: | |
| 156 explicit UpdateChecker(ComponentUpdateService* cus); | |
| 157 | |
| 158 virtual void OnGpuInfoUpdate() OVERRIDE; | |
| 159 | |
| 160 private: | |
| 161 ComponentUpdateService* cus_; | |
| 162 }; | |
| 163 | |
| 164 UpdateChecker::UpdateChecker(ComponentUpdateService* cus) | |
| 165 : cus_(cus) { | |
| 166 } | |
| 167 | |
| 168 void UpdateChecker::OnGpuInfoUpdate() { | |
| 169 GpuDataManager *gpu_data_manager = GpuDataManager::GetInstance(); | |
| 170 | |
| 171 if (!gpu_data_manager->GpuAccessAllowed() || | |
| 172 (gpu_data_manager->GetGpuFeatureFlags().flags() & | |
| 173 GpuFeatureFlags::kGpuFeatureWebgl) || | |
| 174 gpu_data_manager->software_rendering()) { | |
| 175 gpu_data_manager->RemoveObserver(this); | |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 177 FilePath path = GetSwiftShaderBaseDirectory(); | |
| 178 | |
| 179 Version version(kNullVersion); | |
| 180 GetLatestSwiftShaderDirectory(&path, &version); | |
| 181 | |
| 182 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 183 base::Bind(&FinishSwiftShaderUpdateRegistration, cus_, version)); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 UpdateChecker *g_update_checker; | |
|
cpu_(ooo_6.6-7.5)
2011/12/13 20:09:16
remove this from being a global variable.
| |
| 188 | |
| 189 // Check if there already is a version of swiftshader installed, | |
| 190 // and if so register it. | |
| 191 void RegisterSwiftShaderPath(ComponentUpdateService* cus) { | |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 193 FilePath path = GetSwiftShaderBaseDirectory(); | |
| 194 if (!file_util::PathExists(path)) { | |
| 195 if (!file_util::CreateDirectory(path)) { | |
| 196 NOTREACHED() << "Could not create SwiftShader directory."; | |
| 197 return; | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 Version version(kNullVersion); | |
| 202 if (GetLatestSwiftShaderDirectory(&path, &version)) | |
| 203 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 204 base::Bind(&RegisterSwiftShaderWithChrome, path)); | |
| 205 | |
| 206 g_update_checker = new UpdateChecker(cus); | |
|
cpu_(ooo_6.6-7.5)
2011/12/13 20:09:16
this is effectively a local variable, that we leak
| |
| 207 GpuDataManager::GetInstance()->AddObserver(g_update_checker); | |
| 208 g_update_checker->OnGpuInfoUpdate(); | |
| 209 } | |
| 210 | |
| 211 void RegisterSwiftShaderComponent(ComponentUpdateService* cus) { | |
| 212 #if defined(ENABLE_SWIFTSHADER) | |
| 213 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 214 base::Bind(&RegisterSwiftShaderPath, cus)); | |
| 215 #endif | |
| 216 } | |
| OLD | NEW |