Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / plugin / plugin_thread.cc
blob76251094fc8b8f371b7c42c4821660c920d8f913
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/plugin/plugin_thread.h"
7 #include "build/build_config.h"
9 #if defined(OS_MACOSX)
10 #include <CoreFoundation/CoreFoundation.h>
11 #endif
13 #include <string>
14 #include <vector>
16 #include "base/bind.h"
17 #include "base/command_line.h"
18 #include "base/lazy_instance.h"
19 #include "base/process/process.h"
20 #include "base/threading/thread_local.h"
21 #include "content/child/blink_platform_impl.h"
22 #include "content/child/child_process.h"
23 #include "content/child/npapi/npobject_util.h"
24 #include "content/child/npapi/plugin_lib.h"
25 #include "content/common/plugin_process_messages.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/plugin/content_plugin_client.h"
28 #include "third_party/WebKit/public/web/WebKit.h"
29 #include "ipc/ipc_channel_handle.h"
30 #include "ipc/message_filter.h"
32 namespace content {
34 namespace {
36 class EnsureTerminateMessageFilter : public IPC::MessageFilter {
37 public:
38 EnsureTerminateMessageFilter() {}
40 protected:
41 ~EnsureTerminateMessageFilter() override {}
43 // IPC::MessageFilter:
44 void OnChannelError() override {
45 // How long we wait before forcibly shutting down the process.
46 const base::TimeDelta kPluginProcessTerminateTimeout =
47 base::TimeDelta::FromSeconds(3);
48 // Ensure that we don't wait indefinitely for the plugin to shutdown.
49 // as the browser does not terminate plugin processes on shutdown.
50 // We achieve this by posting an exit process task on the IO thread.
51 base::MessageLoop::current()->PostDelayedTask(
52 FROM_HERE,
53 base::Bind(&EnsureTerminateMessageFilter::Terminate, this),
54 kPluginProcessTerminateTimeout);
57 private:
58 void Terminate() {
59 base::Process::Current().Terminate(0, false);
63 } // namespace
65 static base::LazyInstance<base::ThreadLocalPointer<PluginThread> > lazy_tls =
66 LAZY_INSTANCE_INITIALIZER;
68 PluginThread::PluginThread()
69 : preloaded_plugin_module_(NULL),
70 forcefully_terminate_plugin_process_(false) {
71 base::FilePath plugin_path =
72 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
73 switches::kPluginPath);
75 lazy_tls.Pointer()->Set(this);
77 PatchNPNFunctions();
79 // Preload the library to avoid loading, unloading then reloading
80 preloaded_plugin_module_ = base::LoadNativeLibrary(plugin_path, NULL);
82 scoped_refptr<PluginLib> plugin(PluginLib::CreatePluginLib(plugin_path));
83 if (plugin.get()) {
84 plugin->NP_Initialize();
85 // For OOP plugins the plugin dll will be unloaded during process shutdown
86 // time.
87 plugin->set_defer_unload(true);
90 GetContentClient()->plugin()->PluginProcessStarted(
91 plugin.get() ? plugin->plugin_info().name : base::string16());
93 channel()->AddFilter(new EnsureTerminateMessageFilter());
95 // This is needed because we call some code which uses Blink strings.
96 blink_platform_impl_.reset(new BlinkPlatformImpl);
97 blink::initialize(blink_platform_impl_.get());
100 PluginThread::~PluginThread() {
103 void PluginThread::SetForcefullyTerminatePluginProcess() {
104 forcefully_terminate_plugin_process_ = true;
107 void PluginThread::Shutdown() {
108 ChildThreadImpl::Shutdown();
110 if (preloaded_plugin_module_) {
111 base::UnloadNativeLibrary(preloaded_plugin_module_);
112 preloaded_plugin_module_ = NULL;
114 NPChannelBase::CleanupChannels();
115 PluginLib::UnloadAllPlugins();
117 if (forcefully_terminate_plugin_process_)
118 base::Process::Current().Terminate(0, /* wait= */ false);
120 lazy_tls.Pointer()->Set(NULL);
123 PluginThread* PluginThread::current() {
124 return lazy_tls.Pointer()->Get();
127 bool PluginThread::OnControlMessageReceived(const IPC::Message& msg) {
128 bool handled = true;
129 IPC_BEGIN_MESSAGE_MAP(PluginThread, msg)
130 IPC_MESSAGE_HANDLER(PluginProcessMsg_CreateChannel, OnCreateChannel)
131 IPC_MESSAGE_HANDLER(PluginProcessMsg_NotifyRenderersOfPendingShutdown,
132 OnNotifyRenderersOfPendingShutdown)
133 IPC_MESSAGE_UNHANDLED(handled = false)
134 IPC_END_MESSAGE_MAP()
135 return handled;
138 void PluginThread::OnCreateChannel(int renderer_id,
139 bool incognito) {
140 scoped_refptr<PluginChannel> channel(PluginChannel::GetPluginChannel(
141 renderer_id, ChildProcess::current()->io_task_runner()));
142 IPC::ChannelHandle channel_handle;
143 if (channel.get()) {
144 channel_handle.name = channel->channel_handle().name;
145 #if defined(OS_POSIX)
146 // On POSIX, pass the renderer-side FD.
147 channel_handle.socket =
148 base::FileDescriptor(channel->TakeRendererFileDescriptor());
149 #endif
150 channel->set_incognito(incognito);
153 Send(new PluginProcessHostMsg_ChannelCreated(channel_handle));
156 void PluginThread::OnNotifyRenderersOfPendingShutdown() {
157 PluginChannel::NotifyRenderersOfPendingShutdown();
160 } // namespace content