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 "components/update_client/update_client.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 "flapper_version.h" // In SHARED_INTERMEDIATE_DIR. NOLINT
39 #include "ppapi/c/private/ppb_pdf.h"
40 #include "ppapi/shared_impl/ppapi_permissions.h"
42 using content::BrowserThread
;
43 using content::PluginService
;
45 namespace component_updater
{
49 // File name of the Pepper Flash component manifest on different platforms.
50 const char kPepperFlashManifestName
[] = "Flapper";
52 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
53 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
54 const uint8_t kSha2Hash
[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20,
55 0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11,
56 0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70,
57 0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c};
59 // If we don't have a Pepper Flash component, this is the version we claim.
60 const char kNullVersion
[] = "0.0.0.0";
62 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
64 // Name of the Pepper Flash OS in the component manifest.
65 const char kPepperFlashOperatingSystem
[] =
66 #if defined(OS_MACOSX)
70 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
74 // Name of the Pepper Flash architecture in the component manifest.
75 const char kPepperFlashArch
[] =
76 #if defined(ARCH_CPU_X86)
78 #elif defined(ARCH_CPU_X86_64)
80 #else // TODO(viettrungluu): Support an ARM check?
84 // The base directory on Windows looks like:
85 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\.
86 base::FilePath
GetPepperFlashBaseDirectory() {
87 base::FilePath result
;
88 PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &result
);
92 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
93 // Install directory for pepper flash debugger dlls will be like
94 // c:\windows\system32\macromed\flash\, or basically the Macromed\Flash
95 // subdirectory of the Windows system directory.
96 base::FilePath
GetPepperFlashDebuggerDirectory() {
97 base::FilePath result
;
98 PathService::Get(chrome::DIR_PEPPER_FLASH_DEBUGGER_PLUGIN
, &result
);
102 // Pepper Flash plugins have the version encoded in the path itself
103 // so we need to enumerate the directories to find the full path.
104 // On success, |latest_dir| returns something like:
105 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
106 // |latest_version| returns the corresponding version number. |older_dirs|
107 // returns directories of all older versions.
108 bool GetPepperFlashDirectory(base::FilePath
* latest_dir
,
109 Version
* latest_version
,
110 std::vector
<base::FilePath
>* older_dirs
) {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
112 base::FilePath base_dir
= GetPepperFlashBaseDirectory();
114 base::FileEnumerator
file_enumerator(
115 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
116 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
117 path
= file_enumerator
.Next()) {
118 Version
version(path
.BaseName().MaybeAsASCII());
119 if (!version
.IsValid())
122 if (version
.CompareTo(*latest_version
) > 0) {
123 older_dirs
->push_back(*latest_dir
);
125 *latest_version
= version
;
127 older_dirs
->push_back(path
);
131 *latest_version
= version
;
139 const wchar_t kPepperFlashDebuggerDLLSearchString
[] =
140 #if defined(ARCH_CPU_X86)
141 L
"pepflashplayer32*.dll";
142 #elif defined(ARCH_CPU_X86_64)
143 L
"pepflashplayer64*.dll";
145 #error Unsupported Windows CPU architecture.
146 #endif // defined(ARCH_CPU_X86)
147 #endif // defined(OS_WIN)
149 bool GetPepperFlashDebuggerPath(base::FilePath
* dll_path
,
150 Version
* dll_version
) {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
152 base::FilePath debugger_dir
= GetPepperFlashDebuggerDirectory();
153 // If path doesn't exist they simply don't have the flash debugger installed.
154 if (!base::PathExists(debugger_dir
))
159 // Enumerate any DLLs that match the appropriate pattern for this DLL, and
160 // pick the highest version number we find.
161 base::FileEnumerator
file_enumerator(debugger_dir
,
163 base::FileEnumerator::FILES
,
164 kPepperFlashDebuggerDLLSearchString
);
165 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
166 path
= file_enumerator
.Next()) {
167 // Version number is embedded in file name like basename_x_y_z.dll. Extract.
168 std::string
file_name(path
.BaseName().RemoveExtension().MaybeAsASCII());
169 // file_name should now be basename_x_y_z. Split along '_' for version.
170 std::vector
<std::string
> components
;
171 base::SplitString(file_name
, '_', &components
);
172 // Should have at least one version number.
173 if (components
.size() <= 1)
175 // Meld version components back into a string, now separated by periods, so
176 // Version can parse it.
177 std::string
version_string(components
[1]);
178 for (size_t i
= 2; i
< components
.size(); ++i
) {
179 version_string
+= "." + components
[i
];
181 Version
version(version_string
);
182 if (!version
.IsValid())
185 if (version
.CompareTo(*dll_version
) > 0) {
187 *dll_version
= version
;
191 *dll_version
= version
;
198 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
200 // Returns true if the Pepper |interface_name| is implemented by this browser.
201 // It does not check if the interface is proxied.
202 bool SupportsPepperInterface(const char* interface_name
) {
203 if (IsSupportedPepperInterface(interface_name
))
205 // The PDF interface is invisible to SupportsInterface() on the browser
206 // process because it is provided using PpapiInterfaceFactoryManager. We need
207 // to check for that as well.
208 // TODO(cpu): make this more sane.
209 return (strcmp(interface_name
, PPB_PDF_INTERFACE
) == 0);
212 bool MakePepperFlashPluginInfo(const base::FilePath
& flash_path
,
213 const Version
& flash_version
,
216 content::PepperPluginInfo
* plugin_info
) {
217 if (!flash_version
.IsValid())
219 const std::vector
<uint16_t> ver_nums
= flash_version
.components();
220 if (ver_nums
.size() < 3)
223 plugin_info
->is_internal
= false;
224 plugin_info
->is_out_of_process
= out_of_process
;
225 plugin_info
->path
= flash_path
;
226 plugin_info
->name
= content::kFlashPluginName
;
227 plugin_info
->permissions
= kPepperFlashPermissions
;
229 // The description is like "Shockwave Flash 10.2 r154".
230 plugin_info
->description
= base::StringPrintf("%s %d.%d r%d",
231 content::kFlashPluginName
,
236 plugin_info
->version
= flash_version
.GetString();
238 content::WebPluginMimeType
swf_mime_type(content::kFlashPluginSwfMimeType
,
239 content::kFlashPluginSwfExtension
,
240 content::kFlashPluginName
);
241 plugin_info
->mime_types
.push_back(swf_mime_type
);
242 content::WebPluginMimeType
spl_mime_type(content::kFlashPluginSplMimeType
,
243 content::kFlashPluginSplExtension
,
244 content::kFlashPluginName
);
245 plugin_info
->mime_types
.push_back(spl_mime_type
);
249 bool IsPepperFlash(const content::WebPluginInfo
& plugin
) {
250 // We try to recognize Pepper Flash by the following criteria:
251 // * It is a Pepper plug-in.
252 // * It has the special Flash permissions.
253 return plugin
.is_pepper_plugin() &&
254 (plugin
.pepper_permissions
& ppapi::PERMISSION_FLASH
);
257 void RegisterPepperFlashWithChrome(const base::FilePath
& path
,
258 const Version
& version
,
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
261 content::PepperPluginInfo plugin_info
;
262 if (!MakePepperFlashPluginInfo(
263 path
, version
, true, is_debugger
, &plugin_info
))
266 // If this is the non-debugger version, we enumerate any installed versions of
267 // pepper flash to make sure we only replace the installed version with a
270 std::vector
<content::WebPluginInfo
> plugins
;
271 PluginService::GetInstance()->GetInternalPlugins(&plugins
);
272 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
276 if (!IsPepperFlash(*it
))
279 // Do it only if the version we're trying to register is newer.
280 Version
registered_version(base::UTF16ToUTF8(it
->version
));
281 if (registered_version
.IsValid() &&
282 version
.CompareTo(registered_version
) <= 0) {
286 // If the version is newer, remove the old one first.
287 PluginService::GetInstance()->UnregisterInternalPlugin(it
->path
);
292 // We only ask for registration at the beginning for the non-debugger plugin,
293 // that way the debugger plugin doesn't automatically clobber the built-in or
294 // updated non-debugger version.
295 PluginService::GetInstance()->RegisterInternalPlugin(
296 plugin_info
.ToWebPluginInfo(), !is_debugger
);
297 PluginService::GetInstance()->RefreshPlugins();
300 // Returns true if this browser implements one of the interfaces given in
301 // |interface_string|, which is a '|'-separated string of interface names.
302 bool CheckPepperFlashInterfaceString(const std::string
& interface_string
) {
303 std::vector
<std::string
> interface_names
;
304 base::SplitString(interface_string
, '|', &interface_names
);
305 for (size_t i
= 0; i
< interface_names
.size(); i
++) {
306 if (SupportsPepperInterface(interface_names
[i
].c_str()))
312 // Returns true if this browser implements all the interfaces that Flash
313 // specifies in its component installer manifest.
314 bool CheckPepperFlashInterfaces(const base::DictionaryValue
& manifest
) {
315 const base::ListValue
* interface_list
= NULL
;
317 // We don't *require* an interface list, apparently.
318 if (!manifest
.GetList("x-ppapi-required-interfaces", &interface_list
))
321 for (size_t i
= 0; i
< interface_list
->GetSize(); i
++) {
322 std::string interface_string
;
323 if (!interface_list
->GetString(i
, &interface_string
))
325 if (!CheckPepperFlashInterfaceString(interface_string
))
334 class PepperFlashComponentInstaller
: public update_client::ComponentInstaller
{
336 explicit PepperFlashComponentInstaller(const Version
& version
);
338 // ComponentInstaller implementation:
339 void OnUpdateError(int error
) override
;
341 bool Install(const base::DictionaryValue
& manifest
,
342 const base::FilePath
& unpack_path
) override
;
344 bool GetInstalledFile(const std::string
& file
,
345 base::FilePath
* installed_file
) override
;
347 bool Uninstall() override
;
350 ~PepperFlashComponentInstaller() override
{}
352 Version current_version_
;
355 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
356 const Version
& version
)
357 : current_version_(version
) {
358 DCHECK(version
.IsValid());
361 void PepperFlashComponentInstaller::OnUpdateError(int error
) {
362 NOTREACHED() << "Pepper Flash update error: " << error
;
365 bool PepperFlashComponentInstaller::Install(
366 const base::DictionaryValue
& manifest
,
367 const base::FilePath
& unpack_path
) {
369 if (!CheckPepperFlashManifest(manifest
, &version
))
371 if (current_version_
.CompareTo(version
) > 0)
373 if (!base::PathExists(unpack_path
.Append(chrome::kPepperFlashPluginFilename
)))
375 // Passed the basic tests. Time to install it.
376 base::FilePath path
=
377 GetPepperFlashBaseDirectory().AppendASCII(version
.GetString());
378 if (base::PathExists(path
))
380 if (!base::Move(unpack_path
, path
))
382 // Installation is done. Now tell the rest of chrome. Both the path service
383 // and to the plugin service.
384 current_version_
= version
;
385 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN
, path
);
386 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
387 BrowserThread::PostTask(
390 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
394 bool PepperFlashComponentInstaller::GetInstalledFile(
395 const std::string
& file
,
396 base::FilePath
* installed_file
) {
400 bool PepperFlashComponentInstaller::Uninstall() {
404 bool CheckPepperFlashManifest(const base::DictionaryValue
& manifest
,
405 Version
* version_out
) {
407 manifest
.GetStringASCII("name", &name
);
408 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
409 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
410 // Nov. 2011.) crbug.com/98458
411 if (name
!= kPepperFlashManifestName
&& name
!= "WinFlapper")
414 std::string proposed_version
;
415 manifest
.GetStringASCII("version", &proposed_version
);
416 Version
version(proposed_version
.c_str());
417 if (!version
.IsValid())
420 if (!CheckPepperFlashInterfaces(manifest
))
423 // TODO(viettrungluu): See above TODO.
424 if (name
== "WinFlapper") {
425 *version_out
= version
;
430 manifest
.GetStringASCII("x-ppapi-os", &os
);
431 if (os
!= kPepperFlashOperatingSystem
)
435 manifest
.GetStringASCII("x-ppapi-arch", &arch
);
436 if (arch
!= kPepperFlashArch
)
439 *version_out
= version
;
445 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
446 void FinishPepperFlashUpdateRegistration(ComponentUpdateService
* cus
,
447 const Version
& version
) {
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
449 update_client::CrxComponent pepflash
;
450 pepflash
.name
= "pepper_flash";
451 pepflash
.installer
= new PepperFlashComponentInstaller(version
);
452 pepflash
.version
= version
;
453 pepflash
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
454 if (cus
->RegisterComponent(pepflash
) != ComponentUpdateService::kOk
) {
455 NOTREACHED() << "Pepper Flash component registration failed.";
459 void StartPepperFlashUpdateRegistration(ComponentUpdateService
* cus
) {
460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
461 base::FilePath path
= GetPepperFlashBaseDirectory();
462 if (!base::PathExists(path
)) {
463 if (!base::CreateDirectory(path
)) {
464 NOTREACHED() << "Could not create Pepper Flash directory.";
469 Version
version(kNullVersion
);
470 std::vector
<base::FilePath
> older_dirs
;
471 if (GetPepperFlashDirectory(&path
, &version
, &older_dirs
)) {
472 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
473 if (base::PathExists(path
)) {
474 BrowserThread::PostTask(
477 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
479 version
= Version(kNullVersion
);
483 BrowserThread::PostTask(
486 base::Bind(&FinishPepperFlashUpdateRegistration
, cus
, version
));
488 // Remove older versions of Pepper Flash.
489 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
490 iter
!= older_dirs
.end();
492 base::DeleteFile(*iter
, true);
495 // Check for Debugging version of Flash and register if present.
496 base::FilePath debugger_path
;
497 Version
debugger_version(kNullVersion
);
498 if (GetPepperFlashDebuggerPath(&debugger_path
, &debugger_version
)) {
499 BrowserThread::PostTask(BrowserThread::UI
,
501 base::Bind(&RegisterPepperFlashWithChrome
,
507 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
511 void RegisterPepperFlashComponent(ComponentUpdateService
* cus
) {
512 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
513 // Component updated flash supersedes bundled flash therefore if that one
514 // is disabled then this one should never install.
515 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
516 if (cmd_line
->HasSwitch(switches::kDisableBundledPpapiFlash
))
518 BrowserThread::PostTask(BrowserThread::FILE,
520 base::Bind(&StartPepperFlashUpdateRegistration
, cus
));
524 } // namespace component_updater