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.
5 #include "chrome/browser/component_updater/flash_component_installer.h"
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"
41 #include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR.
43 using content::BrowserThread
;
44 using content::PluginService
;
46 namespace component_updater
{
50 // File name of the Pepper Flash component manifest on different platforms.
51 const char kPepperFlashManifestName
[] = "Flapper";
53 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
54 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
55 const uint8 kSha2Hash
[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20,
56 0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11,
57 0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70,
58 0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c};
60 // If we don't have a Pepper Flash component, this is the version we claim.
61 const char kNullVersion
[] = "0.0.0.0";
63 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
65 // Name of the Pepper Flash OS in the component manifest.
66 const char kPepperFlashOperatingSystem
[] =
67 #if defined(OS_MACOSX)
71 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
75 // Name of the Pepper Flash architecture in the component manifest.
76 const char kPepperFlashArch
[] =
77 #if defined(ARCH_CPU_X86)
79 #elif defined(ARCH_CPU_X86_64)
81 #else // TODO(viettrungluu): Support an ARM check?
85 // The base directory on Windows looks like:
86 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\.
87 base::FilePath
GetPepperFlashBaseDirectory() {
88 base::FilePath result
;
89 PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &result
);
93 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
94 // Install directory for pepper flash debugger dlls will be like
95 // c:\windows\system32\macromed\flash\, or basically the Macromed\Flash
96 // subdirectory of the Windows system directory.
97 base::FilePath
GetPepperFlashDebuggerDirectory() {
98 base::FilePath result
;
99 PathService::Get(chrome::DIR_PEPPER_FLASH_DEBUGGER_PLUGIN
, &result
);
103 // Pepper Flash plugins have the version encoded in the path itself
104 // so we need to enumerate the directories to find the full path.
105 // On success, |latest_dir| returns something like:
106 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
107 // |latest_version| returns the corresponding version number. |older_dirs|
108 // returns directories of all older versions.
109 bool GetPepperFlashDirectory(base::FilePath
* latest_dir
,
110 Version
* latest_version
,
111 std::vector
<base::FilePath
>* older_dirs
) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
113 base::FilePath base_dir
= GetPepperFlashBaseDirectory();
116 file_enumerator(base_dir
, false, base::FileEnumerator::DIRECTORIES
);
117 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
118 path
= file_enumerator
.Next()) {
119 Version
version(path
.BaseName().MaybeAsASCII());
120 if (!version
.IsValid())
123 if (version
.CompareTo(*latest_version
) > 0) {
124 older_dirs
->push_back(*latest_dir
);
126 *latest_version
= version
;
128 older_dirs
->push_back(path
);
132 *latest_version
= version
;
140 const wchar_t kPepperFlashDebuggerDLLSearchString
[] =
141 #if defined(ARCH_CPU_X86)
142 L
"pepflashplayer32*.dll";
143 #elif defined(ARCH_CPU_X86_64)
144 L
"pepflashplayer64*.dll";
146 #error Unsupported Windows CPU architecture.
147 #endif // defined(ARCH_CPU_X86)
148 #endif // defined(OS_WIN)
150 bool GetPepperFlashDebuggerPath(base::FilePath
* dll_path
,
151 Version
* dll_version
) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
153 base::FilePath debugger_dir
= GetPepperFlashDebuggerDirectory();
154 // If path doesn't exist they simply don't have the flash debugger installed.
155 if (!base::PathExists(debugger_dir
))
160 // Enumerate any DLLs that match the appropriate pattern for this DLL, and
161 // pick the highest version number we find.
162 base::FileEnumerator
file_enumerator(debugger_dir
,
164 base::FileEnumerator::FILES
,
165 kPepperFlashDebuggerDLLSearchString
);
166 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
167 path
= file_enumerator
.Next()) {
168 // Version number is embedded in file name like basename_x_y_z.dll. Extract.
169 std::string
file_name(path
.BaseName().RemoveExtension().MaybeAsASCII());
170 // file_name should now be basename_x_y_z. Split along '_' for version.
171 std::vector
<std::string
> components
;
172 base::SplitString(file_name
, '_', &components
);
173 // Should have at least one version number.
174 if (components
.size() <= 1)
176 // Meld version components back into a string, now separated by periods, so
177 // Version can parse it.
178 std::string
version_string(components
[1]);
179 for (size_t i
= 2; i
< components
.size(); ++i
) {
180 version_string
+= "." + components
[i
];
182 Version
version(version_string
);
183 if (!version
.IsValid())
186 if (version
.CompareTo(*dll_version
) > 0) {
188 *dll_version
= version
;
192 *dll_version
= version
;
199 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
201 // Returns true if the Pepper |interface_name| is implemented by this browser.
202 // It does not check if the interface is proxied.
203 bool SupportsPepperInterface(const char* interface_name
) {
204 if (IsSupportedPepperInterface(interface_name
))
206 // The PDF interface is invisible to SupportsInterface() on the browser
207 // process because it is provided using PpapiInterfaceFactoryManager. We need
208 // to check for that as well.
209 // TODO(cpu): make this more sane.
210 return (strcmp(interface_name
, PPB_PDF_INTERFACE
) == 0);
213 bool MakePepperFlashPluginInfo(const base::FilePath
& flash_path
,
214 const Version
& flash_version
,
217 content::PepperPluginInfo
* plugin_info
) {
218 if (!flash_version
.IsValid())
220 const std::vector
<uint16
> ver_nums
= flash_version
.components();
221 if (ver_nums
.size() < 3)
224 plugin_info
->is_internal
= false;
225 plugin_info
->is_out_of_process
= out_of_process
;
226 plugin_info
->path
= flash_path
;
227 plugin_info
->name
= content::kFlashPluginName
;
228 plugin_info
->permissions
= kPepperFlashPermissions
;
230 // The description is like "Shockwave Flash 10.2 r154".
231 plugin_info
->description
= base::StringPrintf("%s %d.%d r%d",
232 content::kFlashPluginName
, ver_nums
[0], ver_nums
[1], ver_nums
[2]);
234 plugin_info
->description
+= " Debug";
237 plugin_info
->version
= flash_version
.GetString();
239 content::WebPluginMimeType
swf_mime_type(content::kFlashPluginSwfMimeType
,
240 content::kFlashPluginSwfExtension
,
241 content::kFlashPluginName
);
242 plugin_info
->mime_types
.push_back(swf_mime_type
);
243 content::WebPluginMimeType
spl_mime_type(content::kFlashPluginSplMimeType
,
244 content::kFlashPluginSplExtension
,
245 content::kFlashPluginName
);
246 plugin_info
->mime_types
.push_back(spl_mime_type
);
250 bool IsPepperFlash(const content::WebPluginInfo
& plugin
) {
251 // We try to recognize Pepper Flash by the following criteria:
252 // * It is a Pepper plug-in.
253 // * It has the special Flash permissions.
254 return plugin
.is_pepper_plugin() &&
255 (plugin
.pepper_permissions
& ppapi::PERMISSION_FLASH
);
258 void RegisterPepperFlashWithChrome(const base::FilePath
& path
,
259 const Version
& version
,
261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
262 content::PepperPluginInfo plugin_info
;
263 if (!MakePepperFlashPluginInfo(
264 path
, version
, true, is_debugger
, &plugin_info
))
267 // If this is the non-debugger version, we enumerate any installed versions of
268 // pepper flash to make sure we only replace the installed version with a
271 std::vector
<content::WebPluginInfo
> plugins
;
272 PluginService::GetInstance()->GetInternalPlugins(&plugins
);
273 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
277 if (!IsPepperFlash(*it
))
280 // Do it only if the version we're trying to register is newer.
281 Version
registered_version(base::UTF16ToUTF8(it
->version
));
282 if (registered_version
.IsValid() &&
283 version
.CompareTo(registered_version
) <= 0) {
287 // If the version is newer, remove the old one first.
288 PluginService::GetInstance()->UnregisterInternalPlugin(it
->path
);
293 // We only ask for registration at the beginning for the non-debugger plugin,
294 // that way the debugger plugin doesn't automatically clobber the built-in or
295 // updated non-debugger version.
296 PluginService::GetInstance()->RegisterInternalPlugin(
297 plugin_info
.ToWebPluginInfo(), !is_debugger
);
298 PluginService::GetInstance()->RefreshPlugins();
301 // Returns true if this browser implements one of the interfaces given in
302 // |interface_string|, which is a '|'-separated string of interface names.
303 bool CheckPepperFlashInterfaceString(const std::string
& interface_string
) {
304 std::vector
<std::string
> interface_names
;
305 base::SplitString(interface_string
, '|', &interface_names
);
306 for (size_t i
= 0; i
< interface_names
.size(); i
++) {
307 if (SupportsPepperInterface(interface_names
[i
].c_str()))
313 // Returns true if this browser implements all the interfaces that Flash
314 // specifies in its component installer manifest.
315 bool CheckPepperFlashInterfaces(const base::DictionaryValue
& manifest
) {
316 const base::ListValue
* interface_list
= NULL
;
318 // We don't *require* an interface list, apparently.
319 if (!manifest
.GetList("x-ppapi-required-interfaces", &interface_list
))
322 for (size_t i
= 0; i
< interface_list
->GetSize(); i
++) {
323 std::string interface_string
;
324 if (!interface_list
->GetString(i
, &interface_string
))
326 if (!CheckPepperFlashInterfaceString(interface_string
))
335 class PepperFlashComponentInstaller
: public ComponentInstaller
{
337 explicit PepperFlashComponentInstaller(const Version
& version
);
339 virtual ~PepperFlashComponentInstaller() {}
341 virtual void OnUpdateError(int error
) OVERRIDE
;
343 virtual bool Install(const base::DictionaryValue
& manifest
,
344 const base::FilePath
& unpack_path
) OVERRIDE
;
346 virtual bool GetInstalledFile(const std::string
& file
,
347 base::FilePath
* installed_file
) OVERRIDE
;
350 Version current_version_
;
353 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
354 const Version
& version
) : current_version_(version
) {
355 DCHECK(version
.IsValid());
358 void PepperFlashComponentInstaller::OnUpdateError(int error
) {
359 NOTREACHED() << "Pepper Flash update error: " << error
;
362 bool PepperFlashComponentInstaller::Install(
363 const base::DictionaryValue
& manifest
,
364 const base::FilePath
& unpack_path
) {
366 if (!CheckPepperFlashManifest(manifest
, &version
))
368 if (current_version_
.CompareTo(version
) > 0)
370 if (!base::PathExists(unpack_path
.Append(
371 chrome::kPepperFlashPluginFilename
)))
373 // Passed the basic tests. Time to install it.
374 base::FilePath path
=
375 GetPepperFlashBaseDirectory().AppendASCII(version
.GetString());
376 if (base::PathExists(path
))
378 if (!base::Move(unpack_path
, path
))
380 // Installation is done. Now tell the rest of chrome. Both the path service
381 // and to the plugin service.
382 current_version_
= version
;
383 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN
, path
);
384 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
385 BrowserThread::PostTask(
388 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
392 bool PepperFlashComponentInstaller::GetInstalledFile(
393 const std::string
& file
, base::FilePath
* installed_file
) {
397 bool CheckPepperFlashManifest(const base::DictionaryValue
& manifest
,
398 Version
* version_out
) {
400 manifest
.GetStringASCII("name", &name
);
401 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
402 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
403 // Nov. 2011.) crbug.com/98458
404 if (name
!= kPepperFlashManifestName
&& name
!= "WinFlapper")
407 std::string proposed_version
;
408 manifest
.GetStringASCII("version", &proposed_version
);
409 Version
version(proposed_version
.c_str());
410 if (!version
.IsValid())
413 if (!CheckPepperFlashInterfaces(manifest
))
416 // TODO(viettrungluu): See above TODO.
417 if (name
== "WinFlapper") {
418 *version_out
= version
;
423 manifest
.GetStringASCII("x-ppapi-os", &os
);
424 if (os
!= kPepperFlashOperatingSystem
)
428 manifest
.GetStringASCII("x-ppapi-arch", &arch
);
429 if (arch
!= kPepperFlashArch
)
432 *version_out
= version
;
438 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
439 void FinishPepperFlashUpdateRegistration(ComponentUpdateService
* cus
,
440 const Version
& version
) {
441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
442 CrxComponent pepflash
;
443 pepflash
.name
= "pepper_flash";
444 pepflash
.installer
= new PepperFlashComponentInstaller(version
);
445 pepflash
.version
= version
;
446 pepflash
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
447 if (cus
->RegisterComponent(pepflash
) != ComponentUpdateService::kOk
) {
448 NOTREACHED() << "Pepper Flash component registration failed.";
452 void StartPepperFlashUpdateRegistration(ComponentUpdateService
* cus
) {
453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
454 base::FilePath path
= GetPepperFlashBaseDirectory();
455 if (!base::PathExists(path
)) {
456 if (!base::CreateDirectory(path
)) {
457 NOTREACHED() << "Could not create Pepper Flash directory.";
462 Version
version(kNullVersion
);
463 std::vector
<base::FilePath
> older_dirs
;
464 if (GetPepperFlashDirectory(&path
, &version
, &older_dirs
)) {
465 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
466 if (base::PathExists(path
)) {
467 BrowserThread::PostTask(
470 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
, false));
472 version
= Version(kNullVersion
);
476 BrowserThread::PostTask(
477 BrowserThread::UI
, FROM_HERE
,
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(); ++iter
) {
483 base::DeleteFile(*iter
, true);
486 // Check for Debugging version of Flash and register if present.
487 base::FilePath debugger_path
;
488 Version
debugger_version(kNullVersion
);
489 if (GetPepperFlashDebuggerPath(&debugger_path
, &debugger_version
)) {
490 BrowserThread::PostTask(BrowserThread::UI
,
492 base::Bind(&RegisterPepperFlashWithChrome
,
498 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
502 void RegisterPepperFlashComponent(ComponentUpdateService
* cus
) {
503 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
504 // Component updated flash supersedes bundled flash therefore if that one
505 // is disabled then this one should never install.
506 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
507 if (cmd_line
->HasSwitch(switches::kDisableBundledPpapiFlash
))
509 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
510 base::Bind(&StartPepperFlashUpdateRegistration
, cus
));
514 } // namespace component_updater