pepper_flash_component_installer.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 3// found in the LICENSE file. 4 5#include "chrome/browser/component_updater/flash_component_installer.h" 6 7#include <string.h> 8 9#include <vector> 10 11#include "base/base_paths.h" 12#include "base/bind.h" 13#include "base/command_line.h" 14#include "base/compiler_specific.h" 15#include "base/file_util.h" 16#include "base/files/file_path.h" 17#include "base/logging.h" 18#include "base/path_service.h" 19#include "base/string_util.h" 20#include "base/stringprintf.h" 21#include "base/strings/string_split.h" 22#include "base/utf_string_conversions.h" 23#include "base/values.h" 24#include "base/version.h" 25#include "build/build_config.h" 26#include "chrome/browser/component_updater/component_updater_service.h" 27#include "chrome/browser/plugins/plugin_prefs.h" 28#include "chrome/common/chrome_constants.h" 29#include "chrome/common/chrome_paths.h" 30#include "chrome/common/chrome_switches.h" 31#include "chrome/common/pepper_flash.h" 32#include "chrome/common/pepper_flash.h" 33#include "content/public/browser/browser_thread.h" 34#include "content/public/browser/plugin_service.h" 35#include "content/public/common/pepper_plugin_info.h" 36#include "ppapi/c/private/ppb_pdf.h" 37#include "webkit/plugins/plugin_constants.h" 38#include "webkit/plugins/ppapi/plugin_module.h" 39 40#include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR. 41 42using content::BrowserThread; 43using content::PluginService; 44 45namespace { 46 47// CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb. 48const uint8 sha2_hash[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20, 49 0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11, 50 0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70, 51 0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c}; 52 53// File name of the Pepper Flash component manifest on different platforms. 54const char kPepperFlashManifestName[] = "Flapper"; 55 56// Name of the Pepper Flash OS in the component manifest. 57const char kPepperFlashOperatingSystem[] = 58#if defined(OS_MACOSX) 59 "mac"; 60#elif defined(OS_WIN) 61 "win"; 62#else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android? 63 "linux"; 64#endif 65 66// Name of the Pepper Flash architecture in the component manifest. 67const char kPepperFlashArch[] = 68#if defined(ARCH_CPU_X86) 69 "ia32"; 70#elif defined(ARCH_CPU_X86_64) 71 "x64"; 72#else // TODO(viettrungluu): Support an ARM check? 73 "???"; 74#endif 75 76// If we don't have a Pepper Flash component, this is the version we claim. 77const char kNullVersion[] = "0.0.0.0"; 78 79// The base directory on Windows looks like: 80// <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\. 81base::FilePath GetPepperFlashBaseDirectory() { 82 base::FilePath result; 83 PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &result); 84 return result; 85} 86 87#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX) 88// Pepper Flash plugins have the version encoded in the path itself 89// so we need to enumerate the directories to find the full path. 90// On success, |latest_dir| returns something like: 91// <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\. 92// |latest_version| returns the corresponding version number. |older_dirs| 93// returns directories of all older versions. 94bool GetPepperFlashDirectory(base::FilePath* latest_dir, 95 Version* latest_version, 96 std::vector<base::FilePath>* older_dirs) { 97 base::FilePath base_dir = GetPepperFlashBaseDirectory(); 98 bool found = false; 99 file_util::FileEnumerator 100 file_enumerator(base_dir, false, file_util::FileEnumerator::DIRECTORIES); 101 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); 102 path = file_enumerator.Next()) { 103 Version version(path.BaseName().MaybeAsASCII()); 104 if (!version.IsValid()) 105 continue; 106 if (found) { 107 if (version.CompareTo(*latest_version) > 0) { 108 older_dirs->push_back(*latest_dir); 109 *latest_dir = path; 110 *latest_version = version; 111 } else { 112 older_dirs->push_back(path); 113 } 114 } else { 115 *latest_dir = path; 116 *latest_version = version; 117 found = true; 118 } 119 } 120 return found; 121} 122#endif 123 124// Returns true if the Pepper |interface_name| is implemented by this browser. 125// It does not check if the interface is proxied. 126bool SupportsPepperInterface(const char* interface_name) { 127 if (webkit::ppapi::PluginModule::SupportsInterface(interface_name)) 128 return true; 129 // The PDF interface is invisible to SupportsInterface() on the browser 130 // process because it is provided using PpapiInterfaceFactoryManager. We need 131 // to check for that as well. 132 // TODO(cpu): make this more sane. 133 return (strcmp(interface_name, PPB_PDF_INTERFACE) == 0); 134} 135 136bool MakePepperFlashPluginInfo(const base::FilePath& flash_path, 137 const Version& flash_version, 138 bool out_of_process, 139 content::PepperPluginInfo* plugin_info) { 140 if (!flash_version.IsValid()) 141 return false; 142 const std::vector<uint16> ver_nums = flash_version.components(); 143 if (ver_nums.size() < 3) 144 return false; 145 146 plugin_info->is_internal = false; 147 plugin_info->is_out_of_process = out_of_process; 148 plugin_info->path = flash_path; 149 plugin_info->name = kFlashPluginName; 150 plugin_info->permissions = kPepperFlashPermissions; 151 152 // The description is like "Shockwave Flash 10.2 r154". 153 plugin_info->description = base::StringPrintf("%s %d.%d r%d", 154 kFlashPluginName, ver_nums[0], ver_nums[1], ver_nums[2]); 155 156 plugin_info->version = flash_version.GetString(); 157 158 webkit::WebPluginMimeType swf_mime_type(kFlashPluginSwfMimeType, 159 kFlashPluginSwfExtension, 160 kFlashPluginName); 161 plugin_info->mime_types.push_back(swf_mime_type); 162 webkit::WebPluginMimeType spl_mime_type(kFlashPluginSplMimeType, 163 kFlashPluginSplExtension, 164 kFlashPluginName); 165 plugin_info->mime_types.push_back(spl_mime_type); 166 return true; 167} 168 169bool IsPepperFlash(const webkit::WebPluginInfo& plugin) { 170 // We try to recognize Pepper Flash by the following criteria: 171 // * It is a Pepper plug-in. 172 // * It has the special Flash permissions. 173 return webkit::IsPepperPlugin(plugin) && 174 (plugin.pepper_permissions & ppapi::PERMISSION_FLASH); 175} 176 177void RegisterPepperFlashWithChrome(const base::FilePath& path, 178 const Version& version) { 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 180 content::PepperPluginInfo plugin_info; 181 if (!MakePepperFlashPluginInfo(path, version, true, &plugin_info)) 182 return; 183 184 std::vector<webkit::WebPluginInfo> plugins; 185 PluginService::GetInstance()->GetInternalPlugins(&plugins); 186 for (std::vector<webkit::WebPluginInfo>::const_iterator it = plugins.begin(); 187 it != plugins.end(); ++it) { 188 if (!IsPepperFlash(*it)) 189 continue; 190 191 // Do it only if the version we're trying to register is newer. 192 Version registered_version(UTF16ToUTF8(it->version)); 193 if (registered_version.IsValid() && 194 version.CompareTo(registered_version) <= 0) { 195 return; 196 } 197 198 // If the version is newer, remove the old one first. 199 PluginService::GetInstance()->UnregisterInternalPlugin(it->path); 200 break; 201 } 202 203 PluginService::GetInstance()->RegisterInternalPlugin( 204 plugin_info.ToWebPluginInfo(), true); 205 PluginService::GetInstance()->RefreshPlugins(); 206} 207 208// Returns true if this browser implements one of the interfaces given in 209// |interface_string|, which is a '|'-separated string of interface names. 210bool CheckPepperFlashInterfaceString(const std::string& interface_string) { 211 std::vector<std::string> interface_names; 212 base::SplitString(interface_string, '|', &interface_names); 213 for (size_t i = 0; i < interface_names.size(); i++) { 214 if (SupportsPepperInterface(interface_names[i].c_str())) 215 return true; 216 } 217 return false; 218} 219 220// Returns true if this browser implements all the interfaces that Flash 221// specifies in its component installer manifest. 222bool CheckPepperFlashInterfaces(base::DictionaryValue* manifest) { 223 base::ListValue* interface_list = NULL; 224 225 // We don't *require* an interface list, apparently. 226 if (!manifest->GetList("x-ppapi-required-interfaces", &interface_list)) 227 return true; 228 229 for (size_t i = 0; i < interface_list->GetSize(); i++) { 230 std::string interface_string; 231 if (!interface_list->GetString(i, &interface_string)) 232 return false; 233 if (!CheckPepperFlashInterfaceString(interface_string)) 234 return false; 235 } 236 237 return true; 238} 239 240} // namespace 241 242class PepperFlashComponentInstaller : public ComponentInstaller { 243 public: 244 explicit PepperFlashComponentInstaller(const Version& version); 245 246 virtual ~PepperFlashComponentInstaller() {} 247 248 virtual void OnUpdateError(int error) OVERRIDE; 249 250 virtual bool Install(base::DictionaryValue* manifest, 251 const base::FilePath& unpack_path) OVERRIDE; 252 253 private: 254 Version current_version_; 255}; 256 257PepperFlashComponentInstaller::PepperFlashComponentInstaller( 258 const Version& version) : current_version_(version) { 259 DCHECK(version.IsValid()); 260} 261 262void PepperFlashComponentInstaller::OnUpdateError(int error) { 263 NOTREACHED() << "Pepper Flash update error: " << error; 264} 265 266bool PepperFlashComponentInstaller::Install(base::DictionaryValue* manifest, 267 const base::FilePath& unpack_path) { 268 Version version; 269 if (!CheckPepperFlashManifest(manifest, &version)) 270 return false; 271 if (current_version_.CompareTo(version) > 0) 272 return false; 273 if (!file_util::PathExists(unpack_path.Append( 274 chrome::kPepperFlashPluginFilename))) 275 return false; 276 // Passed the basic tests. Time to install it. 277 base::FilePath path = 278 GetPepperFlashBaseDirectory().AppendASCII(version.GetString()); 279 if (file_util::PathExists(path)) 280 return false; 281 if (!file_util::Move(unpack_path, path)) 282 return false; 283 // Installation is done. Now tell the rest of chrome. Both the path service 284 // and to the plugin service. 285 current_version_ = version; 286 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN, path); 287 path = path.Append(chrome::kPepperFlashPluginFilename); 288 BrowserThread::PostTask( 289 BrowserThread::UI, FROM_HERE, 290 base::Bind(&RegisterPepperFlashWithChrome, path, version)); 291 return true; 292} 293 294bool CheckPepperFlashManifest(base::DictionaryValue* manifest, 295 Version* version_out) { 296 std::string name; 297 manifest->GetStringASCII("name", &name); 298 // TODO(viettrungluu): Support WinFlapper for now, while we change the format 299 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say, 300 // Nov. 2011.) crbug.com/98458 301 if (name != kPepperFlashManifestName && name != "WinFlapper") 302 return false; 303 304 std::string proposed_version; 305 manifest->GetStringASCII("version", &proposed_version); 306 Version version(proposed_version.c_str()); 307 if (!version.IsValid()) 308 return false; 309 310 if (!CheckPepperFlashInterfaces(manifest)) 311 return false; 312 313 // TODO(viettrungluu): See above TODO. 314 if (name == "WinFlapper") { 315 *version_out = version; 316 return true; 317 } 318 319 std::string os; 320 manifest->GetStringASCII("x-ppapi-os", &os); 321 if (os != kPepperFlashOperatingSystem) 322 return false; 323 324 std::string arch; 325 manifest->GetStringASCII("x-ppapi-arch", &arch); 326 if (arch != kPepperFlashArch) 327 return false; 328 329 *version_out = version; 330 return true; 331} 332 333namespace { 334 335#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX) 336void FinishPepperFlashUpdateRegistration(ComponentUpdateService* cus, 337 const Version& version) { 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 339 CrxComponent pepflash; 340 pepflash.name = "pepper_flash"; 341 pepflash.installer = new PepperFlashComponentInstaller(version); 342 pepflash.version = version; 343 pepflash.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]); 344 if (cus->RegisterComponent(pepflash) != ComponentUpdateService::kOk) { 345 NOTREACHED() << "Pepper Flash component registration failed."; 346 } 347} 348 349void StartPepperFlashUpdateRegistration(ComponentUpdateService* cus) { 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 351 base::FilePath path = GetPepperFlashBaseDirectory(); 352 if (!file_util::PathExists(path)) { 353 if (!file_util::CreateDirectory(path)) { 354 NOTREACHED() << "Could not create Pepper Flash directory."; 355 return; 356 } 357 } 358 359 Version version(kNullVersion); 360 std::vector<base::FilePath> older_dirs; 361 if (GetPepperFlashDirectory(&path, &version, &older_dirs)) { 362 path = path.Append(chrome::kPepperFlashPluginFilename); 363 if (file_util::PathExists(path)) { 364 BrowserThread::PostTask( 365 BrowserThread::UI, FROM_HERE, 366 base::Bind(&RegisterPepperFlashWithChrome, path, version)); 367 } else { 368 version = Version(kNullVersion); 369 } 370 } 371 372 BrowserThread::PostTask( 373 BrowserThread::UI, FROM_HERE, 374 base::Bind(&FinishPepperFlashUpdateRegistration, cus, version)); 375 376 // Remove older versions of Pepper Flash. 377 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); 378 iter != older_dirs.end(); ++iter) { 379 file_util::Delete(*iter, true); 380 } 381} 382#endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX) 383 384} // namespace 385 386void RegisterPepperFlashComponent(ComponentUpdateService* cus) { 387#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX) 388 // Component updated flash supersedes bundled flash therefore if that one 389 // is disabled then this one should never install. 390 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 391 if (cmd_line->HasSwitch(switches::kDisableBundledPpapiFlash)) 392 return; 393 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 394 base::Bind(&StartPepperFlashUpdateRegistration, cus)); 395#endif 396} 397