pepper_flash_component_installer.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#include "chrome/browser/component_updater/flash_component_installer.h"
6
7#include <string.h>
8
9#include <vector>
10
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/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/pepper_plugin_info.h"
36#include "ppapi/c/private/ppb_pdf.h"
37#include "webkit/common/plugins/ppapi/ppapi_utils.h"
38#include "webkit/plugins/plugin_constants.h"
39
40#include "flapper_version.h"  // In SHARED_INTERMEDIATE_DIR.
41
42using content::BrowserThread;
43using content::PluginService;
44
45namespace {
46
47// CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
48const uint8 kSha2Hash[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20,
49                           0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11,
50                           0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70,
51                           0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c};
52
53// File name of the Pepper Flash component manifest on different platforms.
54const char kPepperFlashManifestName[] = "Flapper";
55
56// Name of the Pepper Flash OS in the component manifest.
57const char kPepperFlashOperatingSystem[] =
58#if defined(OS_MACOSX)
59    "mac";
60#elif defined(OS_WIN)
61    "win";
62#else  // OS_LINUX, etc. TODO(viettrungluu): Separate out Chrome OS and Android?
63    "linux";
64#endif
65
66// Name of the Pepper Flash architecture in the component manifest.
67const char kPepperFlashArch[] =
68#if defined(ARCH_CPU_X86)
69    "ia32";
70#elif defined(ARCH_CPU_X86_64)
71    "x64";
72#else  // TODO(viettrungluu): Support an ARM check?
73    "???";
74#endif
75
76// If we don't have a Pepper Flash component, this is the version we claim.
77const char kNullVersion[] = "0.0.0.0";
78
79// The base directory on Windows looks like:
80// <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\.
81base::FilePath GetPepperFlashBaseDirectory() {
82  base::FilePath result;
83  PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &result);
84  return result;
85}
86
87#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
88// Pepper Flash plugins have the version encoded in the path itself
89// so we need to enumerate the directories to find the full path.
90// On success, |latest_dir| returns something like:
91// <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
92// |latest_version| returns the corresponding version number. |older_dirs|
93// returns directories of all older versions.
94bool GetPepperFlashDirectory(base::FilePath* latest_dir,
95                             Version* latest_version,
96                             std::vector<base::FilePath>* older_dirs) {
97  base::FilePath base_dir = GetPepperFlashBaseDirectory();
98  bool found = false;
99  base::FileEnumerator
100      file_enumerator(base_dir, false, base::FileEnumerator::DIRECTORIES);
101  for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
102       path = file_enumerator.Next()) {
103    Version version(path.BaseName().MaybeAsASCII());
104    if (!version.IsValid())
105      continue;
106    if (found) {
107      if (version.CompareTo(*latest_version) > 0) {
108        older_dirs->push_back(*latest_dir);
109        *latest_dir = path;
110        *latest_version = version;
111      } else {
112        older_dirs->push_back(path);
113      }
114    } else {
115      *latest_dir = path;
116      *latest_version = version;
117      found = true;
118    }
119  }
120  return found;
121}
122#endif
123
124// Returns true if the Pepper |interface_name| is implemented  by this browser.
125// It does not check if the interface is proxied.
126bool SupportsPepperInterface(const char* interface_name) {
127  if (webkit::ppapi::IsSupportedPepperInterface(interface_name))
128    return true;
129  // The PDF interface is invisible to SupportsInterface() on the browser
130  // process because it is provided using PpapiInterfaceFactoryManager. We need
131  // to check for that as well.
132  // TODO(cpu): make this more sane.
133  return (strcmp(interface_name, PPB_PDF_INTERFACE) == 0);
134}
135
136bool MakePepperFlashPluginInfo(const base::FilePath& flash_path,
137                               const Version& flash_version,
138                               bool out_of_process,
139                               content::PepperPluginInfo* plugin_info) {
140  if (!flash_version.IsValid())
141    return false;
142  const std::vector<uint16> ver_nums = flash_version.components();
143  if (ver_nums.size() < 3)
144    return false;
145
146  plugin_info->is_internal = false;
147  plugin_info->is_out_of_process = out_of_process;
148  plugin_info->path = flash_path;
149  plugin_info->name = kFlashPluginName;
150  plugin_info->permissions = kPepperFlashPermissions;
151
152  // The description is like "Shockwave Flash 10.2 r154".
153  plugin_info->description = base::StringPrintf("%s %d.%d r%d",
154      kFlashPluginName, ver_nums[0], ver_nums[1], ver_nums[2]);
155
156  plugin_info->version = flash_version.GetString();
157
158  webkit::WebPluginMimeType swf_mime_type(kFlashPluginSwfMimeType,
159                                          kFlashPluginSwfExtension,
160                                          kFlashPluginName);
161  plugin_info->mime_types.push_back(swf_mime_type);
162  webkit::WebPluginMimeType spl_mime_type(kFlashPluginSplMimeType,
163                                          kFlashPluginSplExtension,
164                                          kFlashPluginName);
165  plugin_info->mime_types.push_back(spl_mime_type);
166  return true;
167}
168
169bool IsPepperFlash(const webkit::WebPluginInfo& plugin) {
170  // We try to recognize Pepper Flash by the following criteria:
171  // * It is a Pepper plug-in.
172  // * It has the special Flash permissions.
173  return webkit::IsPepperPlugin(plugin) &&
174         (plugin.pepper_permissions & ppapi::PERMISSION_FLASH);
175}
176
177void RegisterPepperFlashWithChrome(const base::FilePath& path,
178                                   const Version& version) {
179  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
180  content::PepperPluginInfo plugin_info;
181  if (!MakePepperFlashPluginInfo(path, version, true, &plugin_info))
182    return;
183
184  std::vector<webkit::WebPluginInfo> plugins;
185  PluginService::GetInstance()->GetInternalPlugins(&plugins);
186  for (std::vector<webkit::WebPluginInfo>::const_iterator it = plugins.begin();
187       it != plugins.end(); ++it) {
188    if (!IsPepperFlash(*it))
189      continue;
190
191    // Do it only if the version we're trying to register is newer.
192    Version registered_version(UTF16ToUTF8(it->version));
193    if (registered_version.IsValid() &&
194        version.CompareTo(registered_version) <= 0) {
195      return;
196    }
197
198    // If the version is newer, remove the old one first.
199    PluginService::GetInstance()->UnregisterInternalPlugin(it->path);
200    break;
201  }
202
203  PluginService::GetInstance()->RegisterInternalPlugin(
204      plugin_info.ToWebPluginInfo(), true);
205  PluginService::GetInstance()->RefreshPlugins();
206}
207
208// Returns true if this browser implements one of the interfaces given in
209// |interface_string|, which is a '|'-separated string of interface names.
210bool CheckPepperFlashInterfaceString(const std::string& interface_string) {
211  std::vector<std::string> interface_names;
212  base::SplitString(interface_string, '|', &interface_names);
213  for (size_t i = 0; i < interface_names.size(); i++) {
214    if (SupportsPepperInterface(interface_names[i].c_str()))
215      return true;
216  }
217  return false;
218}
219
220// Returns true if this browser implements all the interfaces that Flash
221// specifies in its component installer manifest.
222bool CheckPepperFlashInterfaces(const base::DictionaryValue& manifest) {
223  const base::ListValue* interface_list = NULL;
224
225  // We don't *require* an interface list, apparently.
226  if (!manifest.GetList("x-ppapi-required-interfaces", &interface_list))
227    return true;
228
229  for (size_t i = 0; i < interface_list->GetSize(); i++) {
230    std::string interface_string;
231    if (!interface_list->GetString(i, &interface_string))
232      return false;
233    if (!CheckPepperFlashInterfaceString(interface_string))
234      return false;
235  }
236
237  return true;
238}
239
240}  // namespace
241
242class PepperFlashComponentInstaller : public ComponentInstaller {
243 public:
244  explicit PepperFlashComponentInstaller(const Version& version);
245
246  virtual ~PepperFlashComponentInstaller() {}
247
248  virtual void OnUpdateError(int error) OVERRIDE;
249
250  virtual bool Install(const base::DictionaryValue& manifest,
251                       const base::FilePath& unpack_path) OVERRIDE;
252
253  virtual bool GetInstalledFile(const std::string& file,
254                                base::FilePath* installed_file) OVERRIDE;
255
256 private:
257  Version current_version_;
258};
259
260PepperFlashComponentInstaller::PepperFlashComponentInstaller(
261    const Version& version) : current_version_(version) {
262  DCHECK(version.IsValid());
263}
264
265void PepperFlashComponentInstaller::OnUpdateError(int error) {
266  NOTREACHED() << "Pepper Flash update error: " << error;
267}
268
269bool PepperFlashComponentInstaller::Install(
270    const base::DictionaryValue& manifest,
271    const base::FilePath& unpack_path) {
272  Version version;
273  if (!CheckPepperFlashManifest(manifest, &version))
274    return false;
275  if (current_version_.CompareTo(version) > 0)
276    return false;
277  if (!file_util::PathExists(unpack_path.Append(
278          chrome::kPepperFlashPluginFilename)))
279    return false;
280  // Passed the basic tests. Time to install it.
281  base::FilePath path =
282      GetPepperFlashBaseDirectory().AppendASCII(version.GetString());
283  if (file_util::PathExists(path))
284    return false;
285  if (!base::Move(unpack_path, path))
286    return false;
287  // Installation is done. Now tell the rest of chrome. Both the path service
288  // and to the plugin service.
289  current_version_ = version;
290  PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN, path);
291  path = path.Append(chrome::kPepperFlashPluginFilename);
292  BrowserThread::PostTask(
293      BrowserThread::UI, FROM_HERE,
294      base::Bind(&RegisterPepperFlashWithChrome, path, version));
295  return true;
296}
297
298bool PepperFlashComponentInstaller::GetInstalledFile(
299    const std::string& file, base::FilePath* installed_file) {
300  return false;
301}
302
303bool CheckPepperFlashManifest(const base::DictionaryValue& manifest,
304                              Version* version_out) {
305  std::string name;
306  manifest.GetStringASCII("name", &name);
307  // TODO(viettrungluu): Support WinFlapper for now, while we change the format
308  // of the manifest. (Should be safe to remove checks for "WinFlapper" in, say,
309  // Nov. 2011.)  crbug.com/98458
310  if (name != kPepperFlashManifestName && name != "WinFlapper")
311    return false;
312
313  std::string proposed_version;
314  manifest.GetStringASCII("version", &proposed_version);
315  Version version(proposed_version.c_str());
316  if (!version.IsValid())
317    return false;
318
319  if (!CheckPepperFlashInterfaces(manifest))
320    return false;
321
322  // TODO(viettrungluu): See above TODO.
323  if (name == "WinFlapper") {
324    *version_out = version;
325    return true;
326  }
327
328  std::string os;
329  manifest.GetStringASCII("x-ppapi-os", &os);
330  if (os != kPepperFlashOperatingSystem)
331    return false;
332
333  std::string arch;
334  manifest.GetStringASCII("x-ppapi-arch", &arch);
335  if (arch != kPepperFlashArch)
336    return false;
337
338  *version_out = version;
339  return true;
340}
341
342namespace {
343
344#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
345void FinishPepperFlashUpdateRegistration(ComponentUpdateService* cus,
346                                         const Version& version) {
347  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348  CrxComponent pepflash;
349  pepflash.name = "pepper_flash";
350  pepflash.installer = new PepperFlashComponentInstaller(version);
351  pepflash.version = version;
352  pepflash.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
353  if (cus->RegisterComponent(pepflash) != ComponentUpdateService::kOk) {
354    NOTREACHED() << "Pepper Flash component registration failed.";
355  }
356}
357
358void StartPepperFlashUpdateRegistration(ComponentUpdateService* cus) {
359  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
360  base::FilePath path = GetPepperFlashBaseDirectory();
361  if (!file_util::PathExists(path)) {
362    if (!file_util::CreateDirectory(path)) {
363      NOTREACHED() << "Could not create Pepper Flash directory.";
364      return;
365    }
366  }
367
368  Version version(kNullVersion);
369  std::vector<base::FilePath> older_dirs;
370  if (GetPepperFlashDirectory(&path, &version, &older_dirs)) {
371    path = path.Append(chrome::kPepperFlashPluginFilename);
372    if (file_util::PathExists(path)) {
373      BrowserThread::PostTask(
374          BrowserThread::UI, FROM_HERE,
375          base::Bind(&RegisterPepperFlashWithChrome, path, version));
376    } else {
377      version = Version(kNullVersion);
378    }
379  }
380
381  BrowserThread::PostTask(
382      BrowserThread::UI, FROM_HERE,
383      base::Bind(&FinishPepperFlashUpdateRegistration, cus, version));
384
385  // Remove older versions of Pepper Flash.
386  for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
387       iter != older_dirs.end(); ++iter) {
388    base::Delete(*iter, true);
389  }
390}
391#endif  // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
392
393}  // namespace
394
395void RegisterPepperFlashComponent(ComponentUpdateService* cus) {
396#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
397  // Component updated flash supersedes bundled flash therefore if that one
398  // is disabled then this one should never install.
399  CommandLine* cmd_line = CommandLine::ForCurrentProcess();
400  if (cmd_line->HasSwitch(switches::kDisableBundledPpapiFlash))
401    return;
402  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
403                          base::Bind(&StartPepperFlashUpdateRegistration, cus));
404#endif
405}
406