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/pepper_flash_component_installer.h" |
| 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/component_updater/component_updater_service.h" |
| 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "content/browser/browser_thread.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb. |
| 21 const uint8 sha2_hash[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20, |
| 22 0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11, |
| 23 0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70, |
| 24 0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c}; |
| 25 |
| 26 // File name of the Pepper Flash plugin on different platforms. |
| 27 const FilePath::CharType kPepperFlashPluginFileName[] = |
| 28 #if defined(OS_MACOSX) |
| 29 FILE_PATH_LITERAL("PepperFlashPlayer.plugin"); |
| 30 #elif defined(OS_WIN) |
| 31 FILE_PATH_LITERAL("pepflashplayer.dll"); |
| 32 #else // OS_LINUX, etc. |
| 33 FILE_PATH_LITERAL("libpepflashplayer.so"); |
| 34 #endif |
| 35 |
| 36 // File name of the Pepper Flash component manifest on different platforms. |
| 37 const char kPepperFlashManifestName[] = |
| 38 #if defined(OS_MACOSX) |
| 39 "MacFlapper"; |
| 40 #elif defined(OS_WIN) |
| 41 "WinFlapper"; |
| 42 #else // OS_LINUX, etc. |
| 43 "NixFlapper"; |
| 44 #endif |
| 45 |
| 46 // The pepper flash plugins are in a directory with this name. |
| 47 const FilePath::CharType kPepperFlashBaseDirectory[] = |
| 48 FILE_PATH_LITERAL("PepperFlash"); |
| 49 |
| 50 // If we don't have a flash pepper component, this is the version we claim. |
| 51 const char kNullVersion[] = "0.0.0.0"; |
| 52 |
| 53 // The base directory on windows looks like: |
| 54 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\. |
| 55 FilePath GetPepperFlashBaseDirectory() { |
| 56 FilePath result; |
| 57 PathService::Get(chrome::DIR_USER_DATA, &result); |
| 58 return result.Append(kPepperFlashBaseDirectory); |
| 59 } |
| 60 |
| 61 // Pepper flash plugins have the version encoded in the path itself |
| 62 // so we need to enumerate the directories to find the full path. |
| 63 // On success it returns something like: |
| 64 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\. |
| 65 bool GetLatestPepperFlashDirectory(FilePath* result, Version* latest) { |
| 66 *result = GetPepperFlashBaseDirectory(); |
| 67 bool found = false; |
| 68 file_util::FileEnumerator |
| 69 file_enumerator(*result, false, file_util::FileEnumerator::DIRECTORIES); |
| 70 for (FilePath path = file_enumerator.Next(); !path.value().empty(); |
| 71 path = file_enumerator.Next()) { |
| 72 Version version(path.BaseName().MaybeAsASCII()); |
| 73 if (!version.IsValid()) |
| 74 continue; |
| 75 if (version.CompareTo(*latest) > 0) { |
| 76 *latest = version; |
| 77 *result = path; |
| 78 found = true; |
| 79 } |
| 80 } |
| 81 return found; |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
| 86 class PepperFlashComponentInstaller : public ComponentInstaller { |
| 87 public: |
| 88 explicit PepperFlashComponentInstaller(const Version& version); |
| 89 |
| 90 virtual ~PepperFlashComponentInstaller() {} |
| 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 PepperFlashComponentInstaller::PepperFlashComponentInstaller( |
| 102 const Version& version) : current_version_(version) { |
| 103 DCHECK(version.IsValid()); |
| 104 } |
| 105 |
| 106 void PepperFlashComponentInstaller::OnUpdateError(int error) { |
| 107 NOTREACHED() << "pepper flash update error :" << error; |
| 108 } |
| 109 |
| 110 bool PepperFlashComponentInstaller::Install(base::DictionaryValue* manifest, |
| 111 const FilePath& unpack_path) { |
| 112 std::string name; |
| 113 manifest->GetStringASCII("name", &name); |
| 114 if (name != kPepperFlashManifestName) |
| 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(kPepperFlashPluginFileName))) |
| 124 return false; |
| 125 // Passed the basic tests. Time to install it. |
| 126 FilePath path = |
| 127 GetPepperFlashBaseDirectory().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 update the path service. |
| 133 current_version_ = version; |
| 134 path = path.Append(kPepperFlashPluginFileName); |
| 135 PathService::Override(chrome::FILE_PEPPER_FLASH_PLUGIN, path); |
| 136 return true; |
| 137 } |
| 138 |
| 139 void FinishPepperFlashRegistration(ComponentUpdateService* cus, |
| 140 const Version& version) { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 142 CrxComponent pepflash; |
| 143 pepflash.name = "pepper_flash"; |
| 144 pepflash.installer = new PepperFlashComponentInstaller(version); |
| 145 pepflash.version = version; |
| 146 pepflash.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]); |
| 147 if (cus->RegisterComponent(pepflash) != ComponentUpdateService::kOk) { |
| 148 NOTREACHED() << "pepper flash component registration fail"; |
| 149 } |
| 150 } |
| 151 |
| 152 void StartPepperFlashRegistration(ComponentUpdateService* cus) { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 154 FilePath path = GetPepperFlashBaseDirectory(); |
| 155 if (!file_util::PathExists(path)) { |
| 156 if (!file_util::CreateDirectory(path)) { |
| 157 NOTREACHED() << "cannot create pepper flash directory"; |
| 158 return; |
| 159 } |
| 160 } |
| 161 |
| 162 Version version(kNullVersion); |
| 163 if (GetLatestPepperFlashDirectory(&path, &version)) { |
| 164 path = path.Append(kPepperFlashPluginFileName); |
| 165 if (file_util::PathExists(path)) |
| 166 PathService::Override(chrome::FILE_PEPPER_FLASH_PLUGIN, path); |
| 167 else |
| 168 version = Version(kNullVersion); |
| 169 } |
| 170 |
| 171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 172 NewRunnableFunction(&FinishPepperFlashRegistration, cus, version)); |
| 173 } |
| 174 |
| 175 void RegisterPepperFlashComponent(ComponentUpdateService* cus) { |
| 176 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) |
| 177 // TODO(cpu): support Mac and Linux flash pepper. |
| 178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 179 NewRunnableFunction(&StartPepperFlashRegistration, cus)); |
| 180 #endif |
| 181 } |
OLD | NEW |