Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / component_updater / pepper_flash_component_installer.cc
blob485a758965ebdf046fde0694d668d46aa81c1833
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 <stdint.h>
6 #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/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 {
47 namespace {
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)
67 "mac";
68 #elif defined(OS_WIN)
69 "win";
70 #else // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
71 "linux";
72 #endif
74 // Name of the Pepper Flash architecture in the component manifest.
75 const char kPepperFlashArch[] =
76 #if defined(ARCH_CPU_X86)
77 "ia32";
78 #elif defined(ARCH_CPU_X86_64)
79 "x64";
80 #else // TODO(viettrungluu): Support an ARM check?
81 "???";
82 #endif
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);
89 return 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);
99 return 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();
113 bool found = false;
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())
120 continue;
121 if (found) {
122 if (version.CompareTo(*latest_version) > 0) {
123 older_dirs->push_back(*latest_dir);
124 *latest_dir = path;
125 *latest_version = version;
126 } else {
127 older_dirs->push_back(path);
129 } else {
130 *latest_dir = path;
131 *latest_version = version;
132 found = true;
135 return found;
138 #if defined(OS_WIN)
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";
144 #else
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))
155 return false;
157 bool found = false;
158 #if defined(OS_WIN)
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,
162 false,
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)
174 continue;
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())
183 continue;
184 if (found) {
185 if (version.CompareTo(*dll_version) > 0) {
186 *dll_path = path;
187 *dll_version = version;
189 } else {
190 *dll_path = path;
191 *dll_version = version;
192 found = true;
195 #endif
196 return found;
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))
204 return true;
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,
214 bool out_of_process,
215 bool is_debugger,
216 content::PepperPluginInfo* plugin_info) {
217 if (!flash_version.IsValid())
218 return false;
219 const std::vector<uint16_t> ver_nums = flash_version.components();
220 if (ver_nums.size() < 3)
221 return false;
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,
232 ver_nums[0],
233 ver_nums[1],
234 ver_nums[2]);
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);
246 return true;
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,
259 bool is_debugger) {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
261 content::PepperPluginInfo plugin_info;
262 if (!MakePepperFlashPluginInfo(
263 path, version, true, is_debugger, &plugin_info))
264 return;
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
268 // newer version.
269 if (!is_debugger) {
270 std::vector<content::WebPluginInfo> plugins;
271 PluginService::GetInstance()->GetInternalPlugins(&plugins);
272 for (std::vector<content::WebPluginInfo>::const_iterator it =
273 plugins.begin();
274 it != plugins.end();
275 ++it) {
276 if (!IsPepperFlash(*it))
277 continue;
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) {
283 return;
286 // If the version is newer, remove the old one first.
287 PluginService::GetInstance()->UnregisterInternalPlugin(it->path);
288 break;
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()))
307 return true;
309 return false;
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))
319 return true;
321 for (size_t i = 0; i < interface_list->GetSize(); i++) {
322 std::string interface_string;
323 if (!interface_list->GetString(i, &interface_string))
324 return false;
325 if (!CheckPepperFlashInterfaceString(interface_string))
326 return false;
329 return true;
332 } // namespace
334 class PepperFlashComponentInstaller : public update_client::ComponentInstaller {
335 public:
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;
349 private:
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) {
368 Version version;
369 if (!CheckPepperFlashManifest(manifest, &version))
370 return false;
371 if (current_version_.CompareTo(version) > 0)
372 return false;
373 if (!base::PathExists(unpack_path.Append(chrome::kPepperFlashPluginFilename)))
374 return false;
375 // Passed the basic tests. Time to install it.
376 base::FilePath path =
377 GetPepperFlashBaseDirectory().AppendASCII(version.GetString());
378 if (base::PathExists(path))
379 return false;
380 if (!base::Move(unpack_path, path))
381 return false;
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(
388 BrowserThread::UI,
389 FROM_HERE,
390 base::Bind(&RegisterPepperFlashWithChrome, path, version, false));
391 return true;
394 bool PepperFlashComponentInstaller::GetInstalledFile(
395 const std::string& file,
396 base::FilePath* installed_file) {
397 return false;
400 bool PepperFlashComponentInstaller::Uninstall() {
401 return false;
404 bool CheckPepperFlashManifest(const base::DictionaryValue& manifest,
405 Version* version_out) {
406 std::string name;
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")
412 return false;
414 std::string proposed_version;
415 manifest.GetStringASCII("version", &proposed_version);
416 Version version(proposed_version.c_str());
417 if (!version.IsValid())
418 return false;
420 if (!CheckPepperFlashInterfaces(manifest))
421 return false;
423 // TODO(viettrungluu): See above TODO.
424 if (name == "WinFlapper") {
425 *version_out = version;
426 return true;
429 std::string os;
430 manifest.GetStringASCII("x-ppapi-os", &os);
431 if (os != kPepperFlashOperatingSystem)
432 return false;
434 std::string arch;
435 manifest.GetStringASCII("x-ppapi-arch", &arch);
436 if (arch != kPepperFlashArch)
437 return false;
439 *version_out = version;
440 return true;
443 namespace {
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.";
465 return;
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(
475 BrowserThread::UI,
476 FROM_HERE,
477 base::Bind(&RegisterPepperFlashWithChrome, path, version, false));
478 } else {
479 version = Version(kNullVersion);
483 BrowserThread::PostTask(
484 BrowserThread::UI,
485 FROM_HERE,
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();
491 ++iter) {
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,
500 FROM_HERE,
501 base::Bind(&RegisterPepperFlashWithChrome,
502 debugger_path,
503 debugger_version,
504 true));
507 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
509 } // namespace
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))
517 return;
518 BrowserThread::PostTask(BrowserThread::FILE,
519 FROM_HERE,
520 base::Bind(&StartPepperFlashUpdateRegistration, cus));
521 #endif
524 } // namespace component_updater