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.
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/file_util.h"
14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/values.h"
23 #include "base/version.h"
24 #include "build/build_config.h"
25 #include "chrome/browser/component_updater/component_updater_service.h"
26 #include "chrome/browser/component_updater/flash_component_installer.h"
27 #include "chrome/browser/component_updater/ppapi_utils.h"
28 #include "chrome/browser/plugins/plugin_prefs.h"
29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.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/content_constants.h"
36 #include "content/public/common/pepper_plugin_info.h"
37 #include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR. NOLINT
38 #include "ppapi/c/private/ppb_pdf.h"
39 #include "ppapi/shared_impl/ppapi_permissions.h"
41 using content::BrowserThread
;
42 using content::PluginService
;
44 namespace component_updater
{
48 // File name of the Pepper Flash component manifest on different platforms.
49 const char kPepperFlashManifestName
[] = "Flapper";
51 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
52 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
53 const 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};
58 // If we don't have a Pepper Flash component, this is the version we claim.
59 const char kNullVersion
[] = "0.0.0.0";
61 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
63 // Name of the Pepper Flash OS in the component manifest.
64 const char kPepperFlashOperatingSystem
[] =
65 #if defined(OS_MACOSX)
69 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
73 // Name of the Pepper Flash architecture in the component manifest.
74 const char kPepperFlashArch
[] =
75 #if defined(ARCH_CPU_X86)
77 #elif defined(ARCH_CPU_X86_64)
79 #else // TODO(viettrungluu): Support an ARM check?
83 // The base directory on Windows looks like:
84 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\.
85 base::FilePath
GetPepperFlashBaseDirectory() {
86 base::FilePath result
;
87 PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &result
);
91 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
92 // Install directory for pepper flash debugger dlls will be like
93 // c:\windows\system32\macromed\flash\, or basically the Macromed\Flash
94 // subdirectory of the Windows system directory.
95 base::FilePath
GetPepperFlashDebuggerDirectory() {
96 base::FilePath result
;
97 PathService::Get(chrome::DIR_PEPPER_FLASH_DEBUGGER_PLUGIN
, &result
);
101 // Pepper Flash plugins have the version encoded in the path itself
102 // so we need to enumerate the directories to find the full path.
103 // On success, |latest_dir| returns something like:
104 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
105 // |latest_version| returns the corresponding version number. |older_dirs|
106 // returns directories of all older versions.
107 bool GetPepperFlashDirectory(base::FilePath
* latest_dir
,
108 Version
* latest_version
,
109 std::vector
<base::FilePath
>* older_dirs
) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
111 base::FilePath base_dir
= GetPepperFlashBaseDirectory();
113 base::FileEnumerator
file_enumerator(
114 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
115 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
116 path
= file_enumerator
.Next()) {
117 Version
version(path
.BaseName().MaybeAsASCII());
118 if (!version
.IsValid())
121 if (version
.CompareTo(*latest_version
) > 0) {
122 older_dirs
->push_back(*latest_dir
);
124 *latest_version
= version
;
126 older_dirs
->push_back(path
);
130 *latest_version
= version
;
138 const wchar_t kPepperFlashDebuggerDLLSearchString
[] =
139 #if defined(ARCH_CPU_X86)
140 L
"pepflashplayer32*.dll";
141 #elif defined(ARCH_CPU_X86_64)
142 L
"pepflashplayer64*.dll";
144 #error Unsupported Windows CPU architecture.
145 #endif // defined(ARCH_CPU_X86)
146 #endif // defined(OS_WIN)
148 bool GetPepperFlashDebuggerPath(base::FilePath
* dll_path
,
149 Version
* dll_version
) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
151 base::FilePath debugger_dir
= GetPepperFlashDebuggerDirectory();
152 // If path doesn't exist they simply don't have the flash debugger installed.
153 if (!base::PathExists(debugger_dir
))
158 // Enumerate any DLLs that match the appropriate pattern for this DLL, and
159 // pick the highest version number we find.
160 base::FileEnumerator
file_enumerator(debugger_dir
,
162 base::FileEnumerator::FILES
,
163 kPepperFlashDebuggerDLLSearchString
);
164 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
165 path
= file_enumerator
.Next()) {
166 // Version number is embedded in file name like basename_x_y_z.dll. Extract.
167 std::string
file_name(path
.BaseName().RemoveExtension().MaybeAsASCII());
168 // file_name should now be basename_x_y_z. Split along '_' for version.
169 std::vector
<std::string
> components
;
170 base::SplitString(file_name
, '_', &components
);
171 // Should have at least one version number.
172 if (components
.size() <= 1)
174 // Meld version components back into a string, now separated by periods, so
175 // Version can parse it.
176 std::string
version_string(components
[1]);
177 for (size_t i
= 2; i
< components
.size(); ++i
) {
178 version_string
+= "." + components
[i
];
180 Version
version(version_string
);
181 if (!version
.IsValid())
184 if (version
.CompareTo(*dll_version
) > 0) {
186 *dll_version
= version
;
190 *dll_version
= version
;
197 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
199 // Returns true if the Pepper |interface_name| is implemented by this browser.
200 // It does not check if the interface is proxied.
201 bool SupportsPepperInterface(const char* interface_name
) {
202 if (IsSupportedPepperInterface(interface_name
))
204 // The PDF interface is invisible to SupportsInterface() on the browser
205 // process because it is provided using PpapiInterfaceFactoryManager. We need
206 // to check for that as well.
207 // TODO(cpu): make this more sane.
208 return (strcmp(interface_name
, PPB_PDF_INTERFACE
) == 0);
211 bool MakePepperFlashPluginInfo(const base::FilePath
& flash_path
,
212 const Version
& flash_version
,
215 content::PepperPluginInfo
* plugin_info
) {
216 if (!flash_version
.IsValid())
218 const std::vector
<uint16
> ver_nums
= flash_version
.components();
219 if (ver_nums
.size() < 3)
222 plugin_info
->is_internal
= false;
223 plugin_info
->is_out_of_process
= out_of_process
;
224 plugin_info
->path
= flash_path
;
225 plugin_info
->name
= content::kFlashPluginName
;
226 plugin_info
->permissions
= kPepperFlashPermissions
;
228 // The description is like "Shockwave Flash 10.2 r154".
229 plugin_info
->description
= base::StringPrintf("%s %d.%d r%d",
230 content::kFlashPluginName
,
235 plugin_info
->description
+= " Debug";
238 plugin_info
->version
= flash_version
.GetString();
240 content::WebPluginMimeType
swf_mime_type(content::kFlashPluginSwfMimeType
,
241 content::kFlashPluginSwfExtension
,
242 content::kFlashPluginName
);
243 plugin_info
->mime_types
.push_back(swf_mime_type
);
244 content::WebPluginMimeType
spl_mime_type(content::kFlashPluginSplMimeType
,
245 content::kFlashPluginSplExtension
,
246 content::kFlashPluginName
);
247 plugin_info
->mime_types
.push_back(spl_mime_type
);
251 bool IsPepperFlash(const content::WebPluginInfo
& plugin
) {
252 // We try to recognize Pepper Flash by the following criteria:
253 // * It is a Pepper plug-in.
254 // * It has the special Flash permissions.
255 return plugin
.is_pepper_plugin() &&
256 (plugin
.pepper_permissions
& ppapi::PERMISSION_FLASH
);
259 void RegisterPepperFlashWithChrome(const base::FilePath
& path
,
260 const Version
& version
,
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
263 content::PepperPluginInfo plugin_info
;
264 if (!MakePepperFlashPluginInfo(
265 path
, version
, true, is_debugger
, &plugin_info
))
268 // If this is the non-debugger version, we enumerate any installed versions of
269 // pepper flash to make sure we only replace the installed version with a
272 std::vector
<content::WebPluginInfo
> plugins
;
273 PluginService::GetInstance()->GetInternalPlugins(&plugins
);
274 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
278 if (!IsPepperFlash(*it
))
281 // Do it only if the version we're trying to register is newer.
282 Version
registered_version(base::UTF16ToUTF8(it
->version
));
283 if (registered_version
.IsValid() &&
284 version
.CompareTo(registered_version
) <= 0) {
288 // If the version is newer, remove the old one first.
289 PluginService::GetInstance()->UnregisterInternalPlugin(it
->path
);
294 // We only ask for registration at the beginning for the non-debugger plugin,
295 // that way the debugger plugin doesn't automatically clobber the built-in or
296 // updated non-debugger version.
297 PluginService::GetInstance()->RegisterInternalPlugin(
298 plugin_info
.ToWebPluginInfo(), !is_debugger
);
299 PluginService::GetInstance()->RefreshPlugins();
302 // Returns true if this browser implements one of the interfaces given in
303 // |interface_string|, which is a '|'-separated string of interface names.
304 bool CheckPepperFlashInterfaceString(const std::string
& interface_string
) {
305 std::vector
<std::string
> interface_names
;
306 base::SplitString(interface_string
, '|', &interface_names
);
307 for (size_t i
= 0; i
< interface_names
.size(); i
++) {
308 if (SupportsPepperInterface(interface_names
[i
].c_str()))
314 // Returns true if this browser implements all the interfaces that Flash
315 // specifies in its component installer manifest.
316 bool CheckPepperFlashInterfaces(const base::DictionaryValue
& manifest
) {
317 const base::ListValue
* interface_list
= NULL
;
319 // We don't *require* an interface list, apparently.
320 if (!manifest
.GetList("x-ppapi-required-interfaces", &interface_list
))
323 for (size_t i
= 0; i
< interface_list
->GetSize(); i
++) {
324 std::string interface_string
;
325 if (!interface_list
->GetString(i
, &interface_string
))
327 if (!CheckPepperFlashInterfaceString(interface_string
))
336 class PepperFlashComponentInstaller
: public ComponentInstaller
{
338 explicit PepperFlashComponentInstaller(const Version
& version
);
340 virtual ~PepperFlashComponentInstaller() {}
342 virtual void OnUpdateError(int error
) OVERRIDE
;
344 virtual bool Install(const base::DictionaryValue
& manifest
,
345 const base::FilePath
& unpack_path
) OVERRIDE
;
347 virtual bool GetInstalledFile(const std::string
& file
,
348 base::FilePath
* installed_file
) OVERRIDE
;
351 Version current_version_
;
354 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
355 const Version
& version
)
356 : current_version_(version
) {
357 DCHECK(version
.IsValid());
360 void PepperFlashComponentInstaller::OnUpdateError(int error
) {
361 NOTREACHED() << "Pepper Flash update error: " << error
;
364 bool PepperFlashComponentInstaller::Install(
365 const base::DictionaryValue
& manifest
,
366 const base::FilePath
& unpack_path
) {
368 if (!CheckPepperFlashManifest(manifest
, &version
))
370 if (current_version_
.CompareTo(version
) > 0)
372 if (!base::PathExists(unpack_path
.Append(chrome::kPepperFlashPluginFilename
)))
374 // Passed the basic tests. Time to install it.
375 base::FilePath path
=
376 GetPepperFlashBaseDirectory().AppendASCII(version
.GetString());
377 if (base::PathExists(path
))
379 if (!base::Move(unpack_path
, path
))
381 // Installation is done. Now tell the rest of chrome. Both the path service
382 // and to the plugin service.
383 current_version_
= version
;
384 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN
, path
);
385 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
386 BrowserThread::PostTask(
389 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
393 bool PepperFlashComponentInstaller::GetInstalledFile(
394 const std::string
& file
,
395 base::FilePath
* installed_file
) {
399 bool CheckPepperFlashManifest(const base::DictionaryValue
& manifest
,
400 Version
* version_out
) {
402 manifest
.GetStringASCII("name", &name
);
403 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
404 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
405 // Nov. 2011.) crbug.com/98458
406 if (name
!= kPepperFlashManifestName
&& name
!= "WinFlapper")
409 std::string proposed_version
;
410 manifest
.GetStringASCII("version", &proposed_version
);
411 Version
version(proposed_version
.c_str());
412 if (!version
.IsValid())
415 if (!CheckPepperFlashInterfaces(manifest
))
418 // TODO(viettrungluu): See above TODO.
419 if (name
== "WinFlapper") {
420 *version_out
= version
;
425 manifest
.GetStringASCII("x-ppapi-os", &os
);
426 if (os
!= kPepperFlashOperatingSystem
)
430 manifest
.GetStringASCII("x-ppapi-arch", &arch
);
431 if (arch
!= kPepperFlashArch
)
434 *version_out
= version
;
440 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
441 void FinishPepperFlashUpdateRegistration(ComponentUpdateService
* cus
,
442 const Version
& version
) {
443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
444 CrxComponent pepflash
;
445 pepflash
.name
= "pepper_flash";
446 pepflash
.installer
= new PepperFlashComponentInstaller(version
);
447 pepflash
.version
= version
;
448 pepflash
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
449 if (cus
->RegisterComponent(pepflash
) != ComponentUpdateService::kOk
) {
450 NOTREACHED() << "Pepper Flash component registration failed.";
454 void StartPepperFlashUpdateRegistration(ComponentUpdateService
* cus
) {
455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
456 base::FilePath path
= GetPepperFlashBaseDirectory();
457 if (!base::PathExists(path
)) {
458 if (!base::CreateDirectory(path
)) {
459 NOTREACHED() << "Could not create Pepper Flash directory.";
464 Version
version(kNullVersion
);
465 std::vector
<base::FilePath
> older_dirs
;
466 if (GetPepperFlashDirectory(&path
, &version
, &older_dirs
)) {
467 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
468 if (base::PathExists(path
)) {
469 BrowserThread::PostTask(
472 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
474 version
= Version(kNullVersion
);
478 BrowserThread::PostTask(
481 base::Bind(&FinishPepperFlashUpdateRegistration
, cus
, version
));
483 // Remove older versions of Pepper Flash.
484 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
485 iter
!= older_dirs
.end();
487 base::DeleteFile(*iter
, true);
490 // Check for Debugging version of Flash and register if present.
491 base::FilePath debugger_path
;
492 Version
debugger_version(kNullVersion
);
493 if (GetPepperFlashDebuggerPath(&debugger_path
, &debugger_version
)) {
494 BrowserThread::PostTask(BrowserThread::UI
,
496 base::Bind(&RegisterPepperFlashWithChrome
,
502 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
506 void RegisterPepperFlashComponent(ComponentUpdateService
* cus
) {
507 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
508 // Component updated flash supersedes bundled flash therefore if that one
509 // is disabled then this one should never install.
510 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
511 if (cmd_line
->HasSwitch(switches::kDisableBundledPpapiFlash
))
513 BrowserThread::PostTask(BrowserThread::FILE,
515 base::Bind(&StartPepperFlashUpdateRegistration
, cus
));
519 } // namespace component_updater