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;
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 // Pepper Flash plugins have the version encoded in the path itself
93 // so we need to enumerate the directories to find the full path.
94 // On success, |latest_dir| returns something like:
95 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
96 // |latest_version| returns the corresponding version number. |older_dirs|
97 // returns directories of all older versions.
98 bool GetPepperFlashDirectory(base::FilePath* latest_dir,
99 Version* latest_version,
100 std::vector<base::FilePath>* older_dirs) {
101 base::FilePath base_dir = GetPepperFlashBaseDirectory();
104 file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES);
105 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
106 path = file_enumerator.Next()) {
107 Version version(path.BaseName().MaybeAsASCII());
108 if (!version.IsValid())
111 if (version.CompareTo(*latest_version) > 0) {
112 older_dirs->push_back(*latest_dir);
114 *latest_version = version;
116 older_dirs->push_back(path);
120 *latest_version = version;
128 // Returns true if the Pepper |interface_name| is implemented by this browser.
129 // It does not check if the interface is proxied.
130 bool SupportsPepperInterface(const char* interface_name) {
131 if (IsSupportedPepperInterface(interface_name))
133 // The PDF interface is invisible to SupportsInterface() on the browser
134 // process because it is provided using PpapiInterfaceFactoryManager. We need
135 // to check for that as well.
136 // TODO(cpu): make this more sane.
137 return (strcmp(interface_name, PPB_PDF_INTERFACE) == 0);
140 bool MakePepperFlashPluginInfo(const base::FilePath& flash_path,
141 const Version& flash_version,
143 content::PepperPluginInfo* plugin_info) {
144 if (!flash_version.IsValid())
146 const std::vector<uint16> ver_nums = flash_version.components();
147 if (ver_nums.size() < 3)
150 plugin_info->is_internal = false;
151 plugin_info->is_out_of_process = out_of_process;
152 plugin_info->path = flash_path;
153 plugin_info->name = content::kFlashPluginName;
154 plugin_info->permissions = kPepperFlashPermissions;
156 // The description is like "Shockwave Flash 10.2 r154".
157 plugin_info->description = base::StringPrintf("%s %d.%d r%d",
158 content::kFlashPluginName, ver_nums[0], ver_nums[1], ver_nums[2]);
160 plugin_info->version = flash_version.GetString();
162 content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
163 content::kFlashPluginSwfExtension,
164 content::kFlashPluginName);
165 plugin_info->mime_types.push_back(swf_mime_type);
166 content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
167 content::kFlashPluginSplExtension,
168 content::kFlashPluginName);
169 plugin_info->mime_types.push_back(spl_mime_type);
173 bool IsPepperFlash(const content::WebPluginInfo& plugin) {
174 // We try to recognize Pepper Flash by the following criteria:
175 // * It is a Pepper plug-in.
176 // * It has the special Flash permissions.
177 return plugin.is_pepper_plugin() &&
178 (plugin.pepper_permissions & ppapi::PERMISSION_FLASH);
181 void RegisterPepperFlashWithChrome(const base::FilePath& path,
182 const Version& version) {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
184 content::PepperPluginInfo plugin_info;
185 if (!MakePepperFlashPluginInfo(path, version, true, &plugin_info))
188 std::vector<content::WebPluginInfo> plugins;
189 PluginService::GetInstance()->GetInternalPlugins(&plugins);
190 for (std::vector<content::WebPluginInfo>::const_iterator it = plugins.begin();
191 it != plugins.end(); ++it) {
192 if (!IsPepperFlash(*it))
195 // Do it only if the version we're trying to register is newer.
196 Version registered_version(UTF16ToUTF8(it->version));
197 if (registered_version.IsValid() &&
198 version.CompareTo(registered_version) <= 0) {
202 // If the version is newer, remove the old one first.
203 PluginService::GetInstance()->UnregisterInternalPlugin(it->path);
207 PluginService::GetInstance()->RegisterInternalPlugin(
208 plugin_info.ToWebPluginInfo(), true);
209 PluginService::GetInstance()->RefreshPlugins();
212 // Returns true if this browser implements one of the interfaces given in
213 // |interface_string|, which is a '|'-separated string of interface names.
214 bool CheckPepperFlashInterfaceString(const std::string& interface_string) {
215 std::vector<std::string> interface_names;
216 base::SplitString(interface_string, '|', &interface_names);
217 for (size_t i = 0; i < interface_names.size(); i++) {
218 if (SupportsPepperInterface(interface_names[i].c_str()))
224 // Returns true if this browser implements all the interfaces that Flash
225 // specifies in its component installer manifest.
226 bool CheckPepperFlashInterfaces(const base::DictionaryValue& manifest) {
227 const base::ListValue* interface_list = NULL;
229 // We don't *require* an interface list, apparently.
230 if (!manifest.GetList("x-ppapi-required-interfaces", &interface_list))
233 for (size_t i = 0; i < interface_list->GetSize(); i++) {
234 std::string interface_string;
235 if (!interface_list->GetString(i, &interface_string))
237 if (!CheckPepperFlashInterfaceString(interface_string))
246 class PepperFlashComponentInstaller : public ComponentInstaller {
248 explicit PepperFlashComponentInstaller(const Version& version);
250 virtual ~PepperFlashComponentInstaller() {}
252 virtual void OnUpdateError(int error) OVERRIDE;
254 virtual bool Install(const base::DictionaryValue& manifest,
255 const base::FilePath& unpack_path) OVERRIDE;
257 virtual bool GetInstalledFile(const std::string& file,
258 base::FilePath* installed_file) OVERRIDE;
261 Version current_version_;
264 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
265 const Version& version) : current_version_(version) {
266 DCHECK(version.IsValid());
269 void PepperFlashComponentInstaller::OnUpdateError(int error) {
270 NOTREACHED() << "Pepper Flash update error: " << error;
273 bool PepperFlashComponentInstaller::Install(
274 const base::DictionaryValue& manifest,
275 const base::FilePath& unpack_path) {
277 if (!CheckPepperFlashManifest(manifest, &version))
279 if (current_version_.CompareTo(version) > 0)
281 if (!base::PathExists(unpack_path.Append(
282 chrome::kPepperFlashPluginFilename)))
284 // Passed the basic tests. Time to install it.
285 base::FilePath path =
286 GetPepperFlashBaseDirectory().AppendASCII(version.GetString());
287 if (base::PathExists(path))
289 if (!base::Move(unpack_path, path))
291 // Installation is done. Now tell the rest of chrome. Both the path service
292 // and to the plugin service.
293 current_version_ = version;
294 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN, path);
295 path = path.Append(chrome::kPepperFlashPluginFilename);
296 BrowserThread::PostTask(
297 BrowserThread::UI, FROM_HERE,
298 base::Bind(&RegisterPepperFlashWithChrome, path, version));
302 bool PepperFlashComponentInstaller::GetInstalledFile(
303 const std::string& file, base::FilePath* installed_file) {
307 bool CheckPepperFlashManifest(const base::DictionaryValue& manifest,
308 Version* version_out) {
310 manifest.GetStringASCII("name", &name);
311 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
312 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
313 // Nov. 2011.) crbug.com/98458
314 if (name != kPepperFlashManifestName && name != "WinFlapper")
317 std::string proposed_version;
318 manifest.GetStringASCII("version", &proposed_version);
319 Version version(proposed_version.c_str());
320 if (!version.IsValid())
323 if (!CheckPepperFlashInterfaces(manifest))
326 // TODO(viettrungluu): See above TODO.
327 if (name == "WinFlapper") {
328 *version_out = version;
333 manifest.GetStringASCII("x-ppapi-os", &os);
334 if (os != kPepperFlashOperatingSystem)
338 manifest.GetStringASCII("x-ppapi-arch", &arch);
339 if (arch != kPepperFlashArch)
342 *version_out = version;
348 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
349 void FinishPepperFlashUpdateRegistration(ComponentUpdateService* cus,
350 const Version& version) {
351 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
352 CrxComponent pepflash;
353 pepflash.name = "pepper_flash";
354 pepflash.installer = new PepperFlashComponentInstaller(version);
355 pepflash.version = version;
356 pepflash.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
357 if (cus->RegisterComponent(pepflash) != ComponentUpdateService::kOk) {
358 NOTREACHED() << "Pepper Flash component registration failed.";
362 void StartPepperFlashUpdateRegistration(ComponentUpdateService* cus) {
363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
364 base::FilePath path = GetPepperFlashBaseDirectory();
365 if (!base::PathExists(path)) {
366 if (!file_util::CreateDirectory(path)) {
367 NOTREACHED() << "Could not create Pepper Flash directory.";
372 Version version(kNullVersion);
373 std::vector<base::FilePath> older_dirs;
374 if (GetPepperFlashDirectory(&path, &version, &older_dirs)) {
375 path = path.Append(chrome::kPepperFlashPluginFilename);
376 if (base::PathExists(path)) {
377 BrowserThread::PostTask(
378 BrowserThread::UI, FROM_HERE,
379 base::Bind(&RegisterPepperFlashWithChrome, path, version));
381 version = Version(kNullVersion);
385 BrowserThread::PostTask(
386 BrowserThread::UI, FROM_HERE,
387 base::Bind(&FinishPepperFlashUpdateRegistration, cus, version));
389 // Remove older versions of Pepper Flash.
390 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
391 iter != older_dirs.end(); ++iter) {
392 base::DeleteFile(*iter, true);
395 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
399 void RegisterPepperFlashComponent(ComponentUpdateService* cus) {
400 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
401 // Component updated flash supersedes bundled flash therefore if that one
402 // is disabled then this one should never install.
403 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
404 if (cmd_line->HasSwitch(switches::kDisableBundledPpapiFlash))
406 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
407 base::Bind(&StartPepperFlashUpdateRegistration, cus));