Print Preview: Refactoring print/cancel button and print summary.
[chromium-blink-merge.git] / content / common / pepper_plugin_registry.cc
blob9e68f441218cc0d619d2393580a0e4ac3d991ca0
1 // Copyright (c) 2011 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_registry.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/native_library.h"
10 #include "base/string_split.h"
11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h"
13 #include "content/common/child_process.h"
14 #include "content/common/content_client.h"
15 #include "content/common/content_switches.h"
16 #include "webkit/plugins/npapi/plugin_list.h"
18 namespace {
20 // Appends any plugins from the command line to the given vector.
21 void ComputePluginsFromCommandLine(std::vector<PepperPluginInfo>* plugins) {
22 bool out_of_process =
23 CommandLine::ForCurrentProcess()->HasSwitch(switches::kPpapiOutOfProcess);
24 const std::string value =
25 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
26 switches::kRegisterPepperPlugins);
27 if (value.empty())
28 return;
30 // FORMAT:
31 // command-line = <plugin-entry> + *( LWS + "," + LWS + <plugin-entry> )
32 // plugin-entry =
33 // <file-path> +
34 // ["#" + <name> + ["#" + <description> + ["#" + <version>]]] +
35 // *1( LWS + ";" + LWS + <mime-type> )
37 std::vector<std::string> modules;
38 base::SplitString(value, ',', &modules);
39 for (size_t i = 0; i < modules.size(); ++i) {
40 std::vector<std::string> parts;
41 base::SplitString(modules[i], ';', &parts);
42 if (parts.size() < 2) {
43 DLOG(ERROR) << "Required mime-type not found";
44 continue;
47 std::vector<std::string> name_parts;
48 base::SplitString(parts[0], '#', &name_parts);
50 PepperPluginInfo plugin;
51 plugin.is_out_of_process = out_of_process;
52 #if defined(OS_WIN)
53 // This means we can't provide plugins from non-ASCII paths, but
54 // since this switch is only for development I don't think that's
55 // too awful.
56 plugin.path = FilePath(ASCIIToUTF16(name_parts[0]));
57 #else
58 plugin.path = FilePath(name_parts[0]);
59 #endif
60 if (name_parts.size() > 1)
61 plugin.name = name_parts[1];
62 if (name_parts.size() > 2)
63 plugin.description = name_parts[2];
64 if (name_parts.size() > 3)
65 plugin.version = name_parts[3];
66 for (size_t j = 1; j < parts.size(); ++j) {
67 webkit::npapi::WebPluginMimeType mime_type(parts[j],
68 std::string(),
69 plugin.description);
70 plugin.mime_types.push_back(mime_type);
73 plugins->push_back(plugin);
77 } // namespace
79 webkit::npapi::WebPluginInfo PepperPluginInfo::ToWebPluginInfo() const {
80 webkit::npapi::WebPluginInfo info;
82 info.name = name.empty() ? path.BaseName().LossyDisplayName() :
83 ASCIIToUTF16(name);
84 info.path = path;
85 info.version = ASCIIToUTF16(version);
86 info.desc = ASCIIToUTF16(description);
87 info.mime_types = mime_types;
89 webkit::npapi::WebPluginInfo::EnabledStates enabled_state =
90 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED;
92 if (!enabled) {
93 enabled_state =
94 webkit::npapi::WebPluginInfo::USER_DISABLED_POLICY_UNMANAGED;
97 info.enabled = enabled_state;
98 return info;
101 PepperPluginInfo::PepperPluginInfo()
102 : is_internal(false),
103 is_out_of_process(false),
104 enabled(true) {
107 PepperPluginInfo::~PepperPluginInfo() {
110 // static
111 PepperPluginRegistry* PepperPluginRegistry::GetInstance() {
112 static PepperPluginRegistry* registry = NULL;
113 // This object leaks. It is a temporary hack to work around a crash.
114 // http://code.google.com/p/chromium/issues/detail?id=63234
115 if (!registry)
116 registry = new PepperPluginRegistry;
117 return registry;
120 // static
121 void PepperPluginRegistry::ComputeList(std::vector<PepperPluginInfo>* plugins) {
122 content::GetContentClient()->AddPepperPlugins(plugins);
123 ComputePluginsFromCommandLine(plugins);
126 // static
127 void PepperPluginRegistry::PreloadModules() {
128 std::vector<PepperPluginInfo> plugins;
129 ComputeList(&plugins);
130 for (size_t i = 0; i < plugins.size(); ++i) {
131 if (!plugins[i].is_internal) {
132 std::string error;
133 base::NativeLibrary library = base::LoadNativeLibrary(plugins[i].path,
134 &error);
135 LOG_IF(WARNING, !library) << "Unable to load plugin "
136 << plugins[i].path.value() << " "
137 << error;
142 const PepperPluginInfo* PepperPluginRegistry::GetInfoForPlugin(
143 const FilePath& path) const {
144 for (size_t i = 0; i < plugin_list_.size(); ++i) {
145 if (path == plugin_list_[i].path)
146 return &plugin_list_[i];
148 return NULL;
151 webkit::ppapi::PluginModule* PepperPluginRegistry::GetLiveModule(
152 const FilePath& path) {
153 NonOwningModuleMap::iterator it = live_modules_.find(path);
154 if (it == live_modules_.end())
155 return NULL;
156 return it->second;
159 void PepperPluginRegistry::AddLiveModule(const FilePath& path,
160 webkit::ppapi::PluginModule* module) {
161 DCHECK(live_modules_.find(path) == live_modules_.end());
162 live_modules_[path] = module;
165 void PepperPluginRegistry::PluginModuleDead(
166 webkit::ppapi::PluginModule* dead_module) {
167 // DANGER: Don't dereference the dead_module pointer! It may be in the
168 // process of being deleted.
170 // Modules aren't destroyed very often and there are normally at most a
171 // couple of them. So for now we just do a brute-force search.
172 for (NonOwningModuleMap::iterator i = live_modules_.begin();
173 i != live_modules_.end(); ++i) {
174 if (i->second == dead_module) {
175 live_modules_.erase(i);
176 return;
179 NOTREACHED(); // Should have always found the module above.
182 PepperPluginRegistry::~PepperPluginRegistry() {
183 // Explicitly clear all preloaded modules first. This will cause callbacks
184 // to erase these modules from the live_modules_ list, and we don't want
185 // that to happen implicitly out-of-order.
186 preloaded_modules_.clear();
188 DCHECK(live_modules_.empty());
191 PepperPluginRegistry::PepperPluginRegistry() {
192 ComputeList(&plugin_list_);
194 // Note that in each case, AddLiveModule must be called before completing
195 // initialization. If we bail out (in the continue clauses) before saving
196 // the initialized module, it will still try to unregister itself in its
197 // destructor.
198 for (size_t i = 0; i < plugin_list_.size(); i++) {
199 const PepperPluginInfo& current = plugin_list_[i];
200 if (current.is_out_of_process)
201 continue; // Out of process plugins need no special pre-initialization.
203 scoped_refptr<webkit::ppapi::PluginModule> module =
204 new webkit::ppapi::PluginModule(current.name, current.path, this);
205 AddLiveModule(current.path, module);
206 if (current.is_internal) {
207 if (!module->InitAsInternalPlugin(current.internal_entry_points)) {
208 DLOG(ERROR) << "Failed to load pepper module: " << current.path.value();
209 continue;
211 } else {
212 // Preload all external plugins we're not running out of process.
213 if (!module->InitAsLibrary(current.path)) {
214 DLOG(ERROR) << "Failed to load pepper module: " << current.path.value();
215 continue;
218 preloaded_modules_[current.path] = module;
222 base::MessageLoopProxy* PepperPluginRegistry::GetIPCMessageLoop() {
223 // This is called only in the renderer so we know we have a child process.
224 DCHECK(ChildProcess::current()) << "Must be in the renderer.";
225 return ChildProcess::current()->io_message_loop_proxy();
228 base::WaitableEvent* PepperPluginRegistry::GetShutdownEvent() {
229 DCHECK(ChildProcess::current()) << "Must be in the renderer.";
230 return ChildProcess::current()->GetShutDownEvent();