Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / common / pepper_plugin_list.cc
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.
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(base::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 =
113           base::UTF16ToUTF8(plugin.path.BaseName().LossyDisplayName());
114     }
115
116     // Command-line plugins get full permissions.
117     plugin.permissions = ppapi::PERMISSION_ALL_BITS;
118
119     plugins->push_back(plugin);
120   }
121 }
122
123 }  // namespace
124
125 bool MakePepperPluginInfo(const WebPluginInfo& webplugin_info,
126                           PepperPluginInfo* pepper_info) {
127   if (!webplugin_info.is_pepper_plugin())
128     return false;
129
130   pepper_info->is_out_of_process =
131       webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS ||
132       webplugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
133   pepper_info->is_sandboxed = webplugin_info.type !=
134       WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED;
135
136   pepper_info->path = base::FilePath(webplugin_info.path);
137   pepper_info->name = base::UTF16ToASCII(webplugin_info.name);
138   pepper_info->description = base::UTF16ToASCII(webplugin_info.desc);
139   pepper_info->version = base::UTF16ToASCII(webplugin_info.version);
140   pepper_info->mime_types = webplugin_info.mime_types;
141   pepper_info->permissions = webplugin_info.pepper_permissions;
142
143   return true;
144 }
145
146 void ComputePepperPluginList(std::vector<PepperPluginInfo>* plugins) {
147   GetContentClient()->AddPepperPlugins(plugins);
148   ComputePluginsFromCommandLine(plugins);
149 }
150
151 }  // namespace content