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/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.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/flash_component_installer.h"
26 #include "chrome/browser/component_updater/ppapi_utils.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 "components/component_updater/component_updater_service.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
->version
= flash_version
.GetString();
237 content::WebPluginMimeType
swf_mime_type(content::kFlashPluginSwfMimeType
,
238 content::kFlashPluginSwfExtension
,
239 content::kFlashPluginName
);
240 plugin_info
->mime_types
.push_back(swf_mime_type
);
241 content::WebPluginMimeType
spl_mime_type(content::kFlashPluginSplMimeType
,
242 content::kFlashPluginSplExtension
,
243 content::kFlashPluginName
);
244 plugin_info
->mime_types
.push_back(spl_mime_type
);
248 bool IsPepperFlash(const content::WebPluginInfo
& plugin
) {
249 // We try to recognize Pepper Flash by the following criteria:
250 // * It is a Pepper plug-in.
251 // * It has the special Flash permissions.
252 return plugin
.is_pepper_plugin() &&
253 (plugin
.pepper_permissions
& ppapi::PERMISSION_FLASH
);
256 void RegisterPepperFlashWithChrome(const base::FilePath
& path
,
257 const Version
& version
,
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
260 content::PepperPluginInfo plugin_info
;
261 if (!MakePepperFlashPluginInfo(
262 path
, version
, true, is_debugger
, &plugin_info
))
265 // If this is the non-debugger version, we enumerate any installed versions of
266 // pepper flash to make sure we only replace the installed version with a
269 std::vector
<content::WebPluginInfo
> plugins
;
270 PluginService::GetInstance()->GetInternalPlugins(&plugins
);
271 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
275 if (!IsPepperFlash(*it
))
278 // Do it only if the version we're trying to register is newer.
279 Version
registered_version(base::UTF16ToUTF8(it
->version
));
280 if (registered_version
.IsValid() &&
281 version
.CompareTo(registered_version
) <= 0) {
285 // If the version is newer, remove the old one first.
286 PluginService::GetInstance()->UnregisterInternalPlugin(it
->path
);
291 // We only ask for registration at the beginning for the non-debugger plugin,
292 // that way the debugger plugin doesn't automatically clobber the built-in or
293 // updated non-debugger version.
294 PluginService::GetInstance()->RegisterInternalPlugin(
295 plugin_info
.ToWebPluginInfo(), !is_debugger
);
296 PluginService::GetInstance()->RefreshPlugins();
299 // Returns true if this browser implements one of the interfaces given in
300 // |interface_string|, which is a '|'-separated string of interface names.
301 bool CheckPepperFlashInterfaceString(const std::string
& interface_string
) {
302 std::vector
<std::string
> interface_names
;
303 base::SplitString(interface_string
, '|', &interface_names
);
304 for (size_t i
= 0; i
< interface_names
.size(); i
++) {
305 if (SupportsPepperInterface(interface_names
[i
].c_str()))
311 // Returns true if this browser implements all the interfaces that Flash
312 // specifies in its component installer manifest.
313 bool CheckPepperFlashInterfaces(const base::DictionaryValue
& manifest
) {
314 const base::ListValue
* interface_list
= NULL
;
316 // We don't *require* an interface list, apparently.
317 if (!manifest
.GetList("x-ppapi-required-interfaces", &interface_list
))
320 for (size_t i
= 0; i
< interface_list
->GetSize(); i
++) {
321 std::string interface_string
;
322 if (!interface_list
->GetString(i
, &interface_string
))
324 if (!CheckPepperFlashInterfaceString(interface_string
))
333 class PepperFlashComponentInstaller
: public ComponentInstaller
{
335 explicit PepperFlashComponentInstaller(const Version
& version
);
337 virtual ~PepperFlashComponentInstaller() {}
339 virtual void OnUpdateError(int error
) OVERRIDE
;
341 virtual bool Install(const base::DictionaryValue
& manifest
,
342 const base::FilePath
& unpack_path
) OVERRIDE
;
344 virtual bool GetInstalledFile(const std::string
& file
,
345 base::FilePath
* installed_file
) OVERRIDE
;
348 Version current_version_
;
351 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
352 const Version
& version
)
353 : current_version_(version
) {
354 DCHECK(version
.IsValid());
357 void PepperFlashComponentInstaller::OnUpdateError(int error
) {
358 NOTREACHED() << "Pepper Flash update error: " << error
;
361 bool PepperFlashComponentInstaller::Install(
362 const base::DictionaryValue
& manifest
,
363 const base::FilePath
& unpack_path
) {
365 if (!CheckPepperFlashManifest(manifest
, &version
))
367 if (current_version_
.CompareTo(version
) > 0)
369 if (!base::PathExists(unpack_path
.Append(chrome::kPepperFlashPluginFilename
)))
371 // Passed the basic tests. Time to install it.
372 base::FilePath path
=
373 GetPepperFlashBaseDirectory().AppendASCII(version
.GetString());
374 if (base::PathExists(path
))
376 if (!base::Move(unpack_path
, path
))
378 // Installation is done. Now tell the rest of chrome. Both the path service
379 // and to the plugin service.
380 current_version_
= version
;
381 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN
, path
);
382 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
383 BrowserThread::PostTask(
386 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
390 bool PepperFlashComponentInstaller::GetInstalledFile(
391 const std::string
& file
,
392 base::FilePath
* installed_file
) {
396 bool CheckPepperFlashManifest(const base::DictionaryValue
& manifest
,
397 Version
* version_out
) {
399 manifest
.GetStringASCII("name", &name
);
400 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
401 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
402 // Nov. 2011.) crbug.com/98458
403 if (name
!= kPepperFlashManifestName
&& name
!= "WinFlapper")
406 std::string proposed_version
;
407 manifest
.GetStringASCII("version", &proposed_version
);
408 Version
version(proposed_version
.c_str());
409 if (!version
.IsValid())
412 if (!CheckPepperFlashInterfaces(manifest
))
415 // TODO(viettrungluu): See above TODO.
416 if (name
== "WinFlapper") {
417 *version_out
= version
;
422 manifest
.GetStringASCII("x-ppapi-os", &os
);
423 if (os
!= kPepperFlashOperatingSystem
)
427 manifest
.GetStringASCII("x-ppapi-arch", &arch
);
428 if (arch
!= kPepperFlashArch
)
431 *version_out
= version
;
437 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
438 void FinishPepperFlashUpdateRegistration(ComponentUpdateService
* cus
,
439 const Version
& version
) {
440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
441 CrxComponent pepflash
;
442 pepflash
.name
= "pepper_flash";
443 pepflash
.installer
= new PepperFlashComponentInstaller(version
);
444 pepflash
.version
= version
;
445 pepflash
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
446 if (cus
->RegisterComponent(pepflash
) != ComponentUpdateService::kOk
) {
447 NOTREACHED() << "Pepper Flash component registration failed.";
451 void StartPepperFlashUpdateRegistration(ComponentUpdateService
* cus
) {
452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
453 base::FilePath path
= GetPepperFlashBaseDirectory();
454 if (!base::PathExists(path
)) {
455 if (!base::CreateDirectory(path
)) {
456 NOTREACHED() << "Could not create Pepper Flash directory.";
461 Version
version(kNullVersion
);
462 std::vector
<base::FilePath
> older_dirs
;
463 if (GetPepperFlashDirectory(&path
, &version
, &older_dirs
)) {
464 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
465 if (base::PathExists(path
)) {
466 BrowserThread::PostTask(
469 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
471 version
= Version(kNullVersion
);
475 BrowserThread::PostTask(
478 base::Bind(&FinishPepperFlashUpdateRegistration
, cus
, version
));
480 // Remove older versions of Pepper Flash.
481 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
482 iter
!= older_dirs
.end();
484 base::DeleteFile(*iter
, true);
487 // Check for Debugging version of Flash and register if present.
488 base::FilePath debugger_path
;
489 Version
debugger_version(kNullVersion
);
490 if (GetPepperFlashDebuggerPath(&debugger_path
, &debugger_version
)) {
491 BrowserThread::PostTask(BrowserThread::UI
,
493 base::Bind(&RegisterPepperFlashWithChrome
,
499 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
503 void RegisterPepperFlashComponent(ComponentUpdateService
* cus
) {
504 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
505 // Component updated flash supersedes bundled flash therefore if that one
506 // is disabled then this one should never install.
507 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
508 if (cmd_line
->HasSwitch(switches::kDisableBundledPpapiFlash
))
510 BrowserThread::PostTask(BrowserThread::FILE,
512 base::Bind(&StartPepperFlashUpdateRegistration
, cus
));
516 } // namespace component_updater