Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2012 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 "content/common/pepper_plugin_list.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/command_line.h"
      9 #include "base/files/file_util.h"
     10 #include "base/strings/string_split.h"
     11 #include "base/strings/string_util.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "content/public/common/content_client.h"
     14 #include "content/public/common/content_switches.h"
     15 #include "content/public/common/pepper_plugin_info.h"
     16 #include "ppapi/shared_impl/ppapi_permissions.h"
     17 
     18 namespace content {
     19 namespace {
     20 
     21 // The maximum number of plugins allowed to be registered from command line.
     22 const size_t kMaxPluginsToRegisterFromCommandLine = 64;
     23 
     24 // Appends any plugins from the command line to the given vector.
     25 void ComputePluginsFromCommandLine(std::vector<PepperPluginInfo>* plugins) {
     26   // On Linux, once we're sandboxed, we can't know if a plugin is available or
     27   // not. But (on Linux) this function is always called once before we're
     28   // sandboxed. So when this function is called for the first time we set a
     29   // flag if the plugin file is available. Then we can skip the check on file
     30   // existence in subsequent calls if the flag is set.
     31   // NOTE: In theory we could have unlimited number of plugins registered in
     32   // command line. But in practice, 64 plugins should be more than enough.
     33   static uint64 skip_file_check_flags = 0;
     34   COMPILE_ASSERT(
     35       kMaxPluginsToRegisterFromCommandLine <= sizeof(skip_file_check_flags) * 8,
     36       max_plugins_to_register_from_command_line_exceeds_limit);
     37 
     38   bool out_of_process = true;
     39   if (base::CommandLine::ForCurrentProcess()->HasSwitch(
     40           switches::kPpapiInProcess))
     41     out_of_process = false;
     42 
     43   const std::string value =
     44       base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
     45           switches::kRegisterPepperPlugins);
     46   if (value.empty())
     47     return;
     48 
     49   // FORMAT:
     50   // command-line = <plugin-entry> + *( LWS + "," + LWS + <plugin-entry> )
     51   // plugin-entry =
     52   //    <file-path> +
     53   //    ["#" + <name> + ["#" + <description> + ["#" + <version>]]] +
     54   //    *1( LWS + ";" + LWS + <mime-type> )
     55   std::vector<std::string> modules;
     56   base::SplitString(value, ',', &modules);
     57 
     58   size_t plugins_to_register = modules.size();
     59   if (plugins_to_register > kMaxPluginsToRegisterFromCommandLine) {
     60     VLOG(1) << plugins_to_register << " pepper plugins registered from"
     61         << " command line which exceeds the limit (maximum "
     62         << kMaxPluginsToRegisterFromCommandLine << " plugins allowed)";
     63     plugins_to_register = kMaxPluginsToRegisterFromCommandLine;
     64   }
     65 
     66   for (size_t i = 0; i < plugins_to_register; ++i) {
     67     std::vector<std::string> parts;
     68     base::SplitString(modules[i], ';', &parts);
     69     if (parts.size() < 2) {
     70       VLOG(1) << "Required mime-type not found";
     71       continue;
     72     }
     73 
     74     std::vector<std::string> name_parts;
     75     base::SplitString(parts[0], '#', &name_parts);
     76 
     77     PepperPluginInfo plugin;
     78     plugin.is_out_of_process = out_of_process;
     79 #if defined(OS_WIN)
     80     // This means we can't provide plugins from non-ASCII paths, but
     81     // since this switch is only for development I don't think that's
     82     // too awful.
     83     plugin.path = base::FilePath(base::ASCIIToUTF16(name_parts[0]));
     84 #else
     85     plugin.path = base::FilePath(name_parts[0]);
     86 #endif
     87 
     88     uint64 index_mask = 1ULL << i;
     89     if (!(skip_file_check_flags & index_mask)) {
     90       if (base::PathExists(plugin.path)) {
     91         skip_file_check_flags |= index_mask;
     92       } else {
     93         VLOG(1) << "Plugin doesn't exist: " << plugin.path.MaybeAsASCII();
     94         continue;
     95       }
     96     }
     97 
     98     if (name_parts.size() > 1)
     99       plugin.name = name_parts[1];
    100     if (name_parts.size() > 2)
    101       plugin.description = name_parts[2];
    102     if (name_parts.size() > 3)
    103       plugin.version = name_parts[3];
    104     for (size_t j = 1; j < parts.size(); ++j) {
    105       WebPluginMimeType mime_type(parts[j],
    106                                   std::string(),
    107                                   plugin.description);
    108       plugin.mime_types.push_back(mime_type);
    109     }
    110 
    111     // If the plugin name is empty, use the filename.
    112     if (plugin.name.empty()) {
    113       plugin.name =
    114           base::UTF16ToUTF8(plugin.path.BaseName().LossyDisplayName());
    115     }
    116 
    117     // Command-line plugins get full permissions.
    118     plugin.permissions = ppapi::PERMISSION_ALL_BITS;
    119 
    120     plugins->push_back(plugin);
    121   }
    122 }
    123 
    124 }  // namespace
    125 
    126 bool MakePepperPluginInfo(const WebPluginInfo& webplugin_info,
    127                           PepperPluginInfo* pepper_info) {
    128   if (!webplugin_info.is_pepper_plugin())
    129     return false;
    130 
    131   pepper_info->is_out_of_process =
    132       webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS ||
    133       webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
    134   pepper_info->is_sandboxed = webplugin_info.type !=
    135       WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
    136 
    137   pepper_info->path = base::FilePath(webplugin_info.path);
    138   pepper_info->name = base::UTF16ToASCII(webplugin_info.name);
    139   pepper_info->description = base::UTF16ToASCII(webplugin_info.desc);
    140   pepper_info->version = base::UTF16ToASCII(webplugin_info.version);
    141   pepper_info->mime_types = webplugin_info.mime_types;
    142   pepper_info->permissions = webplugin_info.pepper_permissions;
    143 
    144   return true;
    145 }
    146 
    147 void ComputePepperPluginList(std::vector<PepperPluginInfo>* plugins) {
    148   GetContentClient()->AddPepperPlugins(plugins);
    149   ComputePluginsFromCommandLine(plugins);
    150 }
    151 
    152 }  // namespace content
    153