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