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/stl_util.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/version.h"
23 #include "build/build_config.h"
24 #include "chrome/browser/component_updater/flash_component_installer.h"
25 #include "chrome/browser/plugins/plugin_prefs.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pepper_flash.h"
30 #include "chrome/common/ppapi_utils.h"
31 #include "components/component_updater/component_updater_service.h"
32 #include "components/update_client/update_client.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/shared_impl/ppapi_permissions.h"
41 #include "chrome/common/component_flash_hint_file_linux.h"
42 #endif // defined(OS_LINUX)
44 using content::BrowserThread
;
45 using content::PluginService
;
47 namespace component_updater
{
51 #if defined(GOOGLE_CHROME_BUILD)
52 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
53 const uint8_t 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 // Pepper Flash plugins have the version encoded in the path itself
62 // so we need to enumerate the directories to find the full path.
63 // On success, |latest_dir| returns something like:
64 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
65 // |latest_version| returns the corresponding version number. |older_dirs|
66 // returns directories of all older versions.
67 bool GetPepperFlashDirectory(base::FilePath
* latest_dir
,
68 Version
* latest_version
,
69 std::vector
<base::FilePath
>* older_dirs
) {
70 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
71 base::FilePath base_dir
;
72 if (!PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
,
78 base::FileEnumerator
file_enumerator(
79 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
80 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
81 path
= file_enumerator
.Next()) {
82 Version
version(path
.BaseName().MaybeAsASCII());
83 if (!version
.IsValid())
86 if (version
.CompareTo(*latest_version
) > 0) {
87 older_dirs
->push_back(*latest_dir
);
89 *latest_version
= version
;
91 older_dirs
->push_back(path
);
95 *latest_version
= version
;
101 #endif // defined(GOOGLE_CHROME_BUILD)
103 #if !defined(OS_LINUX) || defined(GOOGLE_CHROME_BUILD)
104 bool MakePepperFlashPluginInfo(const base::FilePath
& flash_path
,
105 const Version
& flash_version
,
107 content::PepperPluginInfo
* plugin_info
) {
108 if (!flash_version
.IsValid())
110 const std::vector
<uint32_t> ver_nums
= flash_version
.components();
111 if (ver_nums
.size() < 3)
114 plugin_info
->is_internal
= false;
115 plugin_info
->is_out_of_process
= out_of_process
;
116 plugin_info
->path
= flash_path
;
117 plugin_info
->name
= content::kFlashPluginName
;
118 plugin_info
->permissions
= chrome::kPepperFlashPermissions
;
120 // The description is like "Shockwave Flash 10.2 r154".
121 plugin_info
->description
= base::StringPrintf("%s %d.%d r%d",
122 content::kFlashPluginName
,
127 plugin_info
->version
= flash_version
.GetString();
129 content::WebPluginMimeType
swf_mime_type(content::kFlashPluginSwfMimeType
,
130 content::kFlashPluginSwfExtension
,
131 content::kFlashPluginName
);
132 plugin_info
->mime_types
.push_back(swf_mime_type
);
133 content::WebPluginMimeType
spl_mime_type(content::kFlashPluginSplMimeType
,
134 content::kFlashPluginSplExtension
,
135 content::kFlashPluginName
);
136 plugin_info
->mime_types
.push_back(spl_mime_type
);
140 bool IsPepperFlash(const content::WebPluginInfo
& plugin
) {
141 // We try to recognize Pepper Flash by the following criteria:
142 // * It is a Pepper plugin.
143 // * It has the special Flash permissions.
144 return plugin
.is_pepper_plugin() &&
145 (plugin
.pepper_permissions
& ppapi::PERMISSION_FLASH
);
148 void RegisterPepperFlashWithChrome(const base::FilePath
& path
,
149 const Version
& version
) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
151 content::PepperPluginInfo plugin_info
;
152 if (!MakePepperFlashPluginInfo(path
, version
, true, &plugin_info
))
155 std::vector
<content::WebPluginInfo
> plugins
;
156 PluginService::GetInstance()->GetInternalPlugins(&plugins
);
157 for (std::vector
<content::WebPluginInfo
>::const_iterator it
=
161 if (!IsPepperFlash(*it
))
164 // Do it only if the version we're trying to register is newer.
165 Version
registered_version(base::UTF16ToUTF8(it
->version
));
166 if (registered_version
.IsValid() &&
167 version
.CompareTo(registered_version
) <= 0) {
171 // If the version is newer, remove the old one first.
172 PluginService::GetInstance()->UnregisterInternalPlugin(it
->path
);
176 PluginService::GetInstance()->RegisterInternalPlugin(
177 plugin_info
.ToWebPluginInfo(), true);
178 PluginService::GetInstance()->RefreshPlugins();
180 #endif // !defined(OS_LINUX) || defined(GOOGLE_CHROME_BUILD)
184 class PepperFlashComponentInstaller
: public update_client::CrxInstaller
{
186 explicit PepperFlashComponentInstaller(const Version
& version
);
188 // ComponentInstaller implementation:
189 void OnUpdateError(int error
) override
;
191 bool Install(const base::DictionaryValue
& manifest
,
192 const base::FilePath
& unpack_path
) override
;
194 bool GetInstalledFile(const std::string
& file
,
195 base::FilePath
* installed_file
) override
;
197 bool Uninstall() override
;
200 ~PepperFlashComponentInstaller() override
{}
202 Version current_version_
;
205 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
206 const Version
& version
)
207 : current_version_(version
) {
208 DCHECK(version
.IsValid());
211 void PepperFlashComponentInstaller::OnUpdateError(int error
) {
212 NOTREACHED() << "Pepper Flash update error: " << error
;
215 bool PepperFlashComponentInstaller::Install(
216 const base::DictionaryValue
& manifest
,
217 const base::FilePath
& unpack_path
) {
219 if (!chrome::CheckPepperFlashManifest(manifest
, &version
))
221 if (current_version_
.CompareTo(version
) > 0)
223 const base::FilePath unpacked_plugin
=
224 unpack_path
.Append(chrome::kPepperFlashPluginFilename
);
225 if (!base::PathExists(unpacked_plugin
))
227 // Passed the basic tests. Time to install it.
229 if (!PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
,
233 path
= path
.AppendASCII(version
.GetString());
234 if (base::PathExists(path
))
236 current_version_
= version
;
238 if (!base::Move(unpack_path
, path
))
240 #if defined(OS_LINUX)
241 const base::FilePath flash_path
=
242 path
.Append(chrome::kPepperFlashPluginFilename
);
243 // Populate the component updated flash hint file so that the zygote can
244 // locate and preload the latest version of flash.
245 if (!component_flash_hint_file::RecordFlashUpdate(flash_path
, flash_path
,
246 version
.GetString())) {
247 if (!base::DeleteFile(path
, true))
248 LOG(ERROR
) << "Hint file creation failed, but unable to delete "
249 "installed flash plugin.";
253 // Installation is done. Now tell the rest of chrome. Both the path service
254 // and to the plugin service. On Linux, a restart is required to use the new
255 // Flash version, so we do not do this.
256 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN
, path
);
257 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
258 BrowserThread::PostTask(
261 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
));
262 #endif // !defined(OS_LINUX)
266 bool PepperFlashComponentInstaller::GetInstalledFile(
267 const std::string
& file
,
268 base::FilePath
* installed_file
) {
272 bool PepperFlashComponentInstaller::Uninstall() {
280 #if defined(GOOGLE_CHROME_BUILD)
281 void FinishPepperFlashUpdateRegistration(ComponentUpdateService
* cus
,
282 const Version
& version
) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
284 update_client::CrxComponent pepflash
;
285 pepflash
.name
= "pepper_flash";
286 pepflash
.installer
= new PepperFlashComponentInstaller(version
);
287 pepflash
.version
= version
;
288 pepflash
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
289 if (!cus
->RegisterComponent(pepflash
))
290 NOTREACHED() << "Pepper Flash component registration failed.";
293 void StartPepperFlashUpdateRegistration(ComponentUpdateService
* cus
) {
294 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
296 if (!PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
,
301 if (!base::PathExists(path
)) {
302 if (!base::CreateDirectory(path
)) {
303 NOTREACHED() << "Could not create Pepper Flash directory.";
308 Version
version(kNullVersion
);
309 std::vector
<base::FilePath
> older_dirs
;
310 if (GetPepperFlashDirectory(&path
, &version
, &older_dirs
)) {
311 path
= path
.Append(chrome::kPepperFlashPluginFilename
);
312 if (base::PathExists(path
)) {
313 BrowserThread::PostTask(
316 base::Bind(&RegisterPepperFlashWithChrome
, path
, version
));
318 version
= Version(kNullVersion
);
322 #if defined(FLAPPER_AVAILABLE)
323 // If a version of Flash is bundled with Chrome, and it's a higher version
324 // than the version of the component, or the component has never been updated,
325 // then set the bundled version as the current version.
326 if (version
.CompareTo(Version(FLAPPER_VERSION_STRING
)) < 0)
327 version
= Version(FLAPPER_VERSION_STRING
);
330 BrowserThread::PostTask(
333 base::Bind(&FinishPepperFlashUpdateRegistration
, cus
, version
));
335 // Remove older versions of Pepper Flash.
336 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
337 iter
!= older_dirs
.end();
339 base::DeleteFile(*iter
, true);
342 #endif // defined(GOOGLE_CHROME_BUILD)
346 void RegisterPepperFlashComponent(ComponentUpdateService
* cus
) {
347 #if defined(GOOGLE_CHROME_BUILD)
348 // Component updated flash supersedes bundled flash therefore if that one
349 // is disabled then this one should never install.
350 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
351 if (cmd_line
->HasSwitch(switches::kDisableBundledPpapiFlash
))
353 BrowserThread::PostTask(BrowserThread::FILE,
355 base::Bind(&StartPepperFlashUpdateRegistration
, cus
));
356 #endif // defined(GOOGLE_CHROME_BUILD)
359 } // namespace component_updater