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/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.
ComputePluginsFromCommandLine(std::vector<PepperPluginInfo> * plugins)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 (CommandLine::ForCurrentProcess()->HasSwitch(switches::kPpapiInProcess))
40 out_of_process = false;
41
42 const std::string value =
43 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
44 switches::kRegisterPepperPlugins);
45 if (value.empty())
46 return;
47
48 // FORMAT:
49 // command-line = <plugin-entry> + *( LWS + "," + LWS + <plugin-entry> )
50 // plugin-entry =
51 // <file-path> +
52 // ["#" + <name> + ["#" + <description> + ["#" + <version>]]] +
53 // *1( LWS + ";" + LWS + <mime-type> )
54 std::vector<std::string> modules;
55 base::SplitString(value, ',', &modules);
56
57 size_t plugins_to_register = modules.size();
58 if (plugins_to_register > kMaxPluginsToRegisterFromCommandLine) {
59 VLOG(1) << plugins_to_register << " pepper plugins registered from"
60 << " command line which exceeds the limit (maximum "
61 << kMaxPluginsToRegisterFromCommandLine << " plugins allowed)";
62 plugins_to_register = kMaxPluginsToRegisterFromCommandLine;
63 }
64
65 for (size_t i = 0; i < plugins_to_register; ++i) {
66 std::vector<std::string> parts;
67 base::SplitString(modules[i], ';', &parts);
68 if (parts.size() < 2) {
69 VLOG(1) << "Required mime-type not found";
70 continue;
71 }
72
73 std::vector<std::string> name_parts;
74 base::SplitString(parts[0], '#', &name_parts);
75
76 PepperPluginInfo plugin;
77 plugin.is_out_of_process = out_of_process;
78 #if defined(OS_WIN)
79 // This means we can't provide plugins from non-ASCII paths, but
80 // since this switch is only for development I don't think that's
81 // too awful.
82 plugin.path = base::FilePath(ASCIIToUTF16(name_parts[0]));
83 #else
84 plugin.path = base::FilePath(name_parts[0]);
85 #endif
86
87 uint64 index_mask = 1ULL << i;
88 if (!(skip_file_check_flags & index_mask)) {
89 if (base::PathExists(plugin.path)) {
90 skip_file_check_flags |= index_mask;
91 } else {
92 VLOG(1) << "Plugin doesn't exist: " << plugin.path.MaybeAsASCII();
93 continue;
94 }
95 }
96
97 if (name_parts.size() > 1)
98 plugin.name = name_parts[1];
99 if (name_parts.size() > 2)
100 plugin.description = name_parts[2];
101 if (name_parts.size() > 3)
102 plugin.version = name_parts[3];
103 for (size_t j = 1; j < parts.size(); ++j) {
104 WebPluginMimeType mime_type(parts[j],
105 std::string(),
106 plugin.description);
107 plugin.mime_types.push_back(mime_type);
108 }
109
110 // If the plugin name is empty, use the filename.
111 if (plugin.name.empty())
112 plugin.name = UTF16ToUTF8(plugin.path.BaseName().LossyDisplayName());
113
114 // Command-line plugins get full permissions.
115 plugin.permissions = ppapi::PERMISSION_ALL_BITS;
116
117 plugins->push_back(plugin);
118 }
119 }
120
121 } // namespace
122
MakePepperPluginInfo(const WebPluginInfo & webplugin_info,PepperPluginInfo * pepper_info)123 bool MakePepperPluginInfo(const WebPluginInfo& webplugin_info,
124 PepperPluginInfo* pepper_info) {
125 if (!webplugin_info.is_pepper_plugin())
126 return false;
127
128 pepper_info->is_out_of_process =
129 webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS ||
130 webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
131 pepper_info->is_sandboxed = webplugin_info.type !=
132 WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
133
134 pepper_info->path = base::FilePath(webplugin_info.path);
135 pepper_info->name = UTF16ToASCII(webplugin_info.name);
136 pepper_info->description = UTF16ToASCII(webplugin_info.desc);
137 pepper_info->version = UTF16ToASCII(webplugin_info.version);
138 pepper_info->mime_types = webplugin_info.mime_types;
139 pepper_info->permissions = webplugin_info.pepper_permissions;
140
141 return true;
142 }
143
ComputePepperPluginList(std::vector<PepperPluginInfo> * plugins)144 void ComputePepperPluginList(std::vector<PepperPluginInfo>* plugins) {
145 GetContentClient()->AddPepperPlugins(plugins);
146 ComputePluginsFromCommandLine(plugins);
147 }
148
149 } // namespace content
150