[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / pepper_plugin_list.cc
blob704c81996f8fbebfcf25e95a0c0bb542f72f805a
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.
5 #include "content/common/pepper_plugin_list.h"
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"
18 namespace content {
19 namespace {
21 // The maximum number of plugins allowed to be registered from command line.
22 const size_t kMaxPluginsToRegisterFromCommandLine = 64;
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 static_assert(
35 kMaxPluginsToRegisterFromCommandLine <= sizeof(skip_file_check_flags) * 8,
36 "max plugins to register from command line exceeds limit");
38 bool out_of_process = true;
39 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
40 switches::kPpapiInProcess))
41 out_of_process = false;
43 const std::string value =
44 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
45 switches::kRegisterPepperPlugins);
46 if (value.empty())
47 return;
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);
58 size_t plugins_to_register = modules.size();
59 if (plugins_to_register > kMaxPluginsToRegisterFromCommandLine) {
60 DVLOG(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;
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 DVLOG(1) << "Required mime-type not found";
71 continue;
74 std::vector<std::string> name_parts;
75 base::SplitString(parts[0], '#', &name_parts);
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
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 DVLOG(1) << "Plugin doesn't exist: " << plugin.path.MaybeAsASCII();
94 continue;
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);
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());
117 // Command-line plugins get full permissions.
118 plugin.permissions = ppapi::PERMISSION_ALL_BITS;
120 plugins->push_back(plugin);
124 } // namespace
126 bool MakePepperPluginInfo(const WebPluginInfo& webplugin_info,
127 PepperPluginInfo* pepper_info) {
128 if (!webplugin_info.is_pepper_plugin())
129 return false;
131 pepper_info->is_out_of_process =
132 webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS;
134 pepper_info->path = base::FilePath(webplugin_info.path);
135 pepper_info->name = base::UTF16ToASCII(webplugin_info.name);
136 pepper_info->description = base::UTF16ToASCII(webplugin_info.desc);
137 pepper_info->version = base::UTF16ToASCII(webplugin_info.version);
138 pepper_info->mime_types = webplugin_info.mime_types;
139 pepper_info->permissions = webplugin_info.pepper_permissions;
141 return true;
144 void ComputePepperPluginList(std::vector<PepperPluginInfo>* plugins) {
145 GetContentClient()->AddPepperPlugins(plugins);
146 ComputePluginsFromCommandLine(plugins);
149 } // namespace content