Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / component_updater / pepper_flash_component_installer.cc
blob4fe0c80316612a078c7e89aa4cf5614ab5360311
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 <string.h>
7 #include <vector>
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/file_util.h"
14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_path.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/component_updater_service.h"
26 #include "chrome/browser/component_updater/flash_component_installer.h"
27 #include "chrome/browser/component_updater/ppapi_utils.h"
28 #include "chrome/browser/plugins/plugin_prefs.h"
29 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pepper_flash.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 {
46 namespace {
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)
66 "mac";
67 #elif defined(OS_WIN)
68 "win";
69 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
70 "linux";
71 #endif
73 // Name of the Pepper Flash architecture in the component manifest.
74 const char kPepperFlashArch[] =
75 #if defined(ARCH_CPU_X86)
76 "ia32";
77 #elif defined(ARCH_CPU_X86_64)
78 "x64";
79 #else // TODO(viettrungluu): Support an ARM check?
80 "???";
81 #endif
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);
88 return 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);
98 return 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();
112 bool found = false;
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())
119 continue;
120 if (found) {
121 if (version.CompareTo(*latest_version) > 0) {
122 older_dirs->push_back(*latest_dir);
123 *latest_dir = path;
124 *latest_version = version;
125 } else {
126 older_dirs->push_back(path);
128 } else {
129 *latest_dir = path;
130 *latest_version = version;
131 found = true;
134 return found;
137 #if defined(OS_WIN)
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";
143 #else
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))
154 return false;
156 bool found = false;
157 #if defined(OS_WIN)
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,
161 false,
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)
173 continue;
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())
182 continue;
183 if (found) {
184 if (version.CompareTo(*dll_version) > 0) {
185 *dll_path = path;
186 *dll_version = version;
188 } else {
189 *dll_path = path;
190 *dll_version = version;
191 found = true;
194 #endif
195 return found;
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))
203 return true;
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,
213 bool out_of_process,
214 bool is_debugger,
215 content::PepperPluginInfo* plugin_info) {
216 if (!flash_version.IsValid())
217 return false;
218 const std::vector<uint16> ver_nums = flash_version.components();
219 if (ver_nums.size() < 3)
220 return false;
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,
231 ver_nums[0],
232 ver_nums[1],
233 ver_nums[2]);
234 if (is_debugger) {
235 plugin_info->description += " Debug";
238 plugin_info->version = flash_version.GetString();
240 content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
241 content::kFlashPluginSwfExtension,
242 content::kFlashPluginName);
243 plugin_info->mime_types.push_back(swf_mime_type);
244 content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
245 content::kFlashPluginSplExtension,
246 content::kFlashPluginName);
247 plugin_info->mime_types.push_back(spl_mime_type);
248 return true;
251 bool IsPepperFlash(const content::WebPluginInfo& plugin) {
252 // We try to recognize Pepper Flash by the following criteria:
253 // * It is a Pepper plug-in.
254 // * It has the special Flash permissions.
255 return plugin.is_pepper_plugin() &&
256 (plugin.pepper_permissions & ppapi::PERMISSION_FLASH);
259 void RegisterPepperFlashWithChrome(const base::FilePath& path,
260 const Version& version,
261 bool is_debugger) {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
263 content::PepperPluginInfo plugin_info;
264 if (!MakePepperFlashPluginInfo(
265 path, version, true, is_debugger, &plugin_info))
266 return;
268 // If this is the non-debugger version, we enumerate any installed versions of
269 // pepper flash to make sure we only replace the installed version with a
270 // newer version.
271 if (!is_debugger) {
272 std::vector<content::WebPluginInfo> plugins;
273 PluginService::GetInstance()->GetInternalPlugins(&plugins);
274 for (std::vector<content::WebPluginInfo>::const_iterator it =
275 plugins.begin();
276 it != plugins.end();
277 ++it) {
278 if (!IsPepperFlash(*it))
279 continue;
281 // Do it only if the version we're trying to register is newer.
282 Version registered_version(base::UTF16ToUTF8(it->version));
283 if (registered_version.IsValid() &&
284 version.CompareTo(registered_version) <= 0) {
285 return;
288 // If the version is newer, remove the old one first.
289 PluginService::GetInstance()->UnregisterInternalPlugin(it->path);
290 break;
294 // We only ask for registration at the beginning for the non-debugger plugin,
295 // that way the debugger plugin doesn't automatically clobber the built-in or
296 // updated non-debugger version.
297 PluginService::GetInstance()->RegisterInternalPlugin(
298 plugin_info.ToWebPluginInfo(), !is_debugger);
299 PluginService::GetInstance()->RefreshPlugins();
302 // Returns true if this browser implements one of the interfaces given in
303 // |interface_string|, which is a '|'-separated string of interface names.
304 bool CheckPepperFlashInterfaceString(const std::string& interface_string) {
305 std::vector<std::string> interface_names;
306 base::SplitString(interface_string, '|', &interface_names);
307 for (size_t i = 0; i < interface_names.size(); i++) {
308 if (SupportsPepperInterface(interface_names[i].c_str()))
309 return true;
311 return false;
314 // Returns true if this browser implements all the interfaces that Flash
315 // specifies in its component installer manifest.
316 bool CheckPepperFlashInterfaces(const base::DictionaryValue& manifest) {
317 const base::ListValue* interface_list = NULL;
319 // We don't *require* an interface list, apparently.
320 if (!manifest.GetList("x-ppapi-required-interfaces", &interface_list))
321 return true;
323 for (size_t i = 0; i < interface_list->GetSize(); i++) {
324 std::string interface_string;
325 if (!interface_list->GetString(i, &interface_string))
326 return false;
327 if (!CheckPepperFlashInterfaceString(interface_string))
328 return false;
331 return true;
334 } // namespace
336 class PepperFlashComponentInstaller : public ComponentInstaller {
337 public:
338 explicit PepperFlashComponentInstaller(const Version& version);
340 virtual ~PepperFlashComponentInstaller() {}
342 virtual void OnUpdateError(int error) OVERRIDE;
344 virtual bool Install(const base::DictionaryValue& manifest,
345 const base::FilePath& unpack_path) OVERRIDE;
347 virtual bool GetInstalledFile(const std::string& file,
348 base::FilePath* installed_file) OVERRIDE;
350 private:
351 Version current_version_;
354 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
355 const Version& version)
356 : current_version_(version) {
357 DCHECK(version.IsValid());
360 void PepperFlashComponentInstaller::OnUpdateError(int error) {
361 NOTREACHED() << "Pepper Flash update error: " << error;
364 bool PepperFlashComponentInstaller::Install(
365 const base::DictionaryValue& manifest,
366 const base::FilePath& unpack_path) {
367 Version version;
368 if (!CheckPepperFlashManifest(manifest, &version))
369 return false;
370 if (current_version_.CompareTo(version) > 0)
371 return false;
372 if (!base::PathExists(unpack_path.Append(chrome::kPepperFlashPluginFilename)))
373 return false;
374 // Passed the basic tests. Time to install it.
375 base::FilePath path =
376 GetPepperFlashBaseDirectory().AppendASCII(version.GetString());
377 if (base::PathExists(path))
378 return false;
379 if (!base::Move(unpack_path, path))
380 return false;
381 // Installation is done. Now tell the rest of chrome. Both the path service
382 // and to the plugin service.
383 current_version_ = version;
384 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN, path);
385 path = path.Append(chrome::kPepperFlashPluginFilename);
386 BrowserThread::PostTask(
387 BrowserThread::UI,
388 FROM_HERE,
389 base::Bind(&RegisterPepperFlashWithChrome, path, version, false));
390 return true;
393 bool PepperFlashComponentInstaller::GetInstalledFile(
394 const std::string& file,
395 base::FilePath* installed_file) {
396 return false;
399 bool CheckPepperFlashManifest(const base::DictionaryValue& manifest,
400 Version* version_out) {
401 std::string name;
402 manifest.GetStringASCII("name", &name);
403 // TODO(viettrungluu): Support WinFlapper for now, while we change the format
404 // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
405 // Nov. 2011.) crbug.com/98458
406 if (name != kPepperFlashManifestName && name != "WinFlapper")
407 return false;
409 std::string proposed_version;
410 manifest.GetStringASCII("version", &proposed_version);
411 Version version(proposed_version.c_str());
412 if (!version.IsValid())
413 return false;
415 if (!CheckPepperFlashInterfaces(manifest))
416 return false;
418 // TODO(viettrungluu): See above TODO.
419 if (name == "WinFlapper") {
420 *version_out = version;
421 return true;
424 std::string os;
425 manifest.GetStringASCII("x-ppapi-os", &os);
426 if (os != kPepperFlashOperatingSystem)
427 return false;
429 std::string arch;
430 manifest.GetStringASCII("x-ppapi-arch", &arch);
431 if (arch != kPepperFlashArch)
432 return false;
434 *version_out = version;
435 return true;
438 namespace {
440 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
441 void FinishPepperFlashUpdateRegistration(ComponentUpdateService* cus,
442 const Version& version) {
443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
444 CrxComponent pepflash;
445 pepflash.name = "pepper_flash";
446 pepflash.installer = new PepperFlashComponentInstaller(version);
447 pepflash.version = version;
448 pepflash.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
449 if (cus->RegisterComponent(pepflash) != ComponentUpdateService::kOk) {
450 NOTREACHED() << "Pepper Flash component registration failed.";
454 void StartPepperFlashUpdateRegistration(ComponentUpdateService* cus) {
455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
456 base::FilePath path = GetPepperFlashBaseDirectory();
457 if (!base::PathExists(path)) {
458 if (!base::CreateDirectory(path)) {
459 NOTREACHED() << "Could not create Pepper Flash directory.";
460 return;
464 Version version(kNullVersion);
465 std::vector<base::FilePath> older_dirs;
466 if (GetPepperFlashDirectory(&path, &version, &older_dirs)) {
467 path = path.Append(chrome::kPepperFlashPluginFilename);
468 if (base::PathExists(path)) {
469 BrowserThread::PostTask(
470 BrowserThread::UI,
471 FROM_HERE,
472 base::Bind(&RegisterPepperFlashWithChrome, path, version, false));
473 } else {
474 version = Version(kNullVersion);
478 BrowserThread::PostTask(
479 BrowserThread::UI,
480 FROM_HERE,
481 base::Bind(&FinishPepperFlashUpdateRegistration, cus, version));
483 // Remove older versions of Pepper Flash.
484 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
485 iter != older_dirs.end();
486 ++iter) {
487 base::DeleteFile(*iter, true);
490 // Check for Debugging version of Flash and register if present.
491 base::FilePath debugger_path;
492 Version debugger_version(kNullVersion);
493 if (GetPepperFlashDebuggerPath(&debugger_path, &debugger_version)) {
494 BrowserThread::PostTask(BrowserThread::UI,
495 FROM_HERE,
496 base::Bind(&RegisterPepperFlashWithChrome,
497 debugger_path,
498 debugger_version,
499 true));
502 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
504 } // namespace
506 void RegisterPepperFlashComponent(ComponentUpdateService* cus) {
507 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
508 // Component updated flash supersedes bundled flash therefore if that one
509 // is disabled then this one should never install.
510 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
511 if (cmd_line->HasSwitch(switches::kDisableBundledPpapiFlash))
512 return;
513 BrowserThread::PostTask(BrowserThread::FILE,
514 FROM_HERE,
515 base::Bind(&StartPepperFlashUpdateRegistration, cus));
516 #endif
519 } // namespace component_updater