1231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block/* 2231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Copyright (C) 2009 Apple Inc. All Rights Reserved. 3231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * 4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Redistribution and use in source and binary forms, with or without 5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * modification, are permitted provided that the following conditions 6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * are met: 7231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * 1. Redistributions of source code must retain the above copyright 8231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * notice, this list of conditions and the following disclaimer. 9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * 2. Redistributions in binary form must reproduce the above copyright 10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * notice, this list of conditions and the following disclaimer in the 11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * documentation and/or other materials provided with the distribution. 12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * 13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block */ 25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "config.h" 27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 28231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "PluginHalter.h" 29231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "HaltablePlugin.h" 31643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#include "PlatformString.h" 32231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include <wtf/CurrentTime.h> 33231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include <wtf/Vector.h> 34231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 35231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockusing namespace std; 36231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 37231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blocknamespace WebCore { 38231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 39231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve BlockPluginHalter::PluginHalter(PluginHalterClient* client) 40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block : m_client(client) 41231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block , m_timer(this, &PluginHalter::timerFired) 42231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block , m_pluginAllowedRunTime(numeric_limits<unsigned>::max()) 43231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 44231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block ASSERT_ARG(client, client); 45231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 46231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 47231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid PluginHalter::didStartPlugin(HaltablePlugin* obj) 48231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 49231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block ASSERT_ARG(obj, obj); 50231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block ASSERT_ARG(obj, !m_plugins.contains(obj)); 51231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 52cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block if (!m_client->enabled()) 53cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block return; 54cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block 55231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block double currentTime = WTF::currentTime(); 56231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 57231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_plugins.add(obj, currentTime); 58231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 59231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (m_plugins.size() == 1) 60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_oldestStartTime = currentTime; 61231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 62231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block startTimerIfNecessary(); 63231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 64231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 65231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid PluginHalter::didStopPlugin(HaltablePlugin* obj) 66231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 67cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block if (!m_client->enabled()) 68cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block return; 69cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block 70231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_plugins.remove(obj); 71231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 72231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 73231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid PluginHalter::timerFired(Timer<PluginHalter>*) 74231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 75231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (m_plugins.isEmpty()) 76231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block return; 77231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 78231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block Vector<HaltablePlugin*> plugins; 79231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block copyKeysToVector(m_plugins, plugins); 80231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 81231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block // Plug-ins older than this are candidates to be halted. 82231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block double pluginCutOffTime = WTF::currentTime() - m_pluginAllowedRunTime; 83231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 84231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_oldestStartTime = numeric_limits<double>::max(); 85231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 86231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block for (size_t i = 0; i < plugins.size(); ++i) { 87231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block double thisStartTime = m_plugins.get(plugins[i]); 88231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (thisStartTime > pluginCutOffTime) { 89231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block // This plug-in is too young to be halted. We find the oldest 90231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block // plug-in that is not old enough to be halted and use it to set 91231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block // the timer's next fire time. 92231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (thisStartTime < m_oldestStartTime) 93231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_oldestStartTime = thisStartTime; 94231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block continue; 95231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block } 96231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 97643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if (m_client->shouldHaltPlugin(plugins[i]->node(), plugins[i]->isWindowed(), plugins[i]->pluginName())) 98231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block plugins[i]->halt(); 99231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 100231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_plugins.remove(plugins[i]); 101231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block } 102231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 103231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block startTimerIfNecessary(); 104231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 105231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 106231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid PluginHalter::startTimerIfNecessary() 107231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 108231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (m_timer.isActive()) 109231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block return; 110231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 111231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block if (m_plugins.isEmpty()) 112231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block return; 113231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 114231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block double nextFireInterval = static_cast<double>(m_pluginAllowedRunTime) - (currentTime() - m_oldestStartTime); 115231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block m_timer.startOneShot(nextFireInterval < 0 ? 0 : nextFireInterval); 116231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 117231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 118231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} // namespace WebCore 119