0
|
1 |
/*
|
|
2 |
* Copyright (C) 2009 Apple Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
* 1. Redistributions of source code must retain the above copyright
|
|
8 |
* notice, this list of conditions and the following disclaimer.
|
|
9 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer in the
|
|
11 |
* documentation and/or other materials provided with the distribution.
|
|
12 |
*
|
|
13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
19 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
20 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
21 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
24 |
*/
|
|
25 |
|
|
26 |
#include "config.h"
|
|
27 |
|
|
28 |
#include "PluginHalter.h"
|
|
29 |
|
|
30 |
#include "HaltablePlugin.h"
|
|
31 |
#include <wtf/CurrentTime.h>
|
|
32 |
#include <wtf/Vector.h>
|
|
33 |
|
|
34 |
using namespace std;
|
|
35 |
|
|
36 |
namespace WebCore {
|
|
37 |
|
|
38 |
PluginHalter::PluginHalter(PluginHalterClient* client)
|
|
39 |
: m_client(client)
|
|
40 |
, m_timer(this, &PluginHalter::timerFired)
|
|
41 |
, m_pluginAllowedRunTime(numeric_limits<unsigned>::max())
|
|
42 |
{
|
|
43 |
ASSERT_ARG(client, client);
|
|
44 |
}
|
|
45 |
|
|
46 |
void PluginHalter::didStartPlugin(HaltablePlugin* obj)
|
|
47 |
{
|
|
48 |
ASSERT_ARG(obj, obj);
|
|
49 |
ASSERT_ARG(obj, !m_plugins.contains(obj));
|
|
50 |
|
|
51 |
if (!m_client->enabled())
|
|
52 |
return;
|
|
53 |
|
|
54 |
double currentTime = WTF::currentTime();
|
|
55 |
|
|
56 |
m_plugins.add(obj, currentTime);
|
|
57 |
|
|
58 |
if (m_plugins.size() == 1)
|
|
59 |
m_oldestStartTime = currentTime;
|
|
60 |
|
|
61 |
startTimerIfNecessary();
|
|
62 |
}
|
|
63 |
|
|
64 |
void PluginHalter::didStopPlugin(HaltablePlugin* obj)
|
|
65 |
{
|
|
66 |
if (!m_client->enabled())
|
|
67 |
return;
|
|
68 |
|
|
69 |
m_plugins.remove(obj);
|
|
70 |
}
|
|
71 |
|
|
72 |
void PluginHalter::timerFired(Timer<PluginHalter>*)
|
|
73 |
{
|
|
74 |
if (m_plugins.isEmpty())
|
|
75 |
return;
|
|
76 |
|
|
77 |
Vector<HaltablePlugin*> plugins;
|
|
78 |
copyKeysToVector(m_plugins, plugins);
|
|
79 |
|
|
80 |
// Plug-ins older than this are candidates to be halted.
|
|
81 |
double pluginCutOffTime = WTF::currentTime() - m_pluginAllowedRunTime;
|
|
82 |
|
|
83 |
m_oldestStartTime = numeric_limits<double>::max();
|
|
84 |
|
|
85 |
for (size_t i = 0; i < plugins.size(); ++i) {
|
|
86 |
double thisStartTime = m_plugins.get(plugins[i]);
|
|
87 |
if (thisStartTime > pluginCutOffTime) {
|
|
88 |
// This plug-in is too young to be halted. We find the oldest
|
|
89 |
// plug-in that is not old enough to be halted and use it to set
|
|
90 |
// the timer's next fire time.
|
|
91 |
if (thisStartTime < m_oldestStartTime)
|
|
92 |
m_oldestStartTime = thisStartTime;
|
|
93 |
continue;
|
|
94 |
}
|
|
95 |
|
|
96 |
if (m_client->shouldHaltPlugin(plugins[i]->node()))
|
|
97 |
plugins[i]->halt();
|
|
98 |
|
|
99 |
m_plugins.remove(plugins[i]);
|
|
100 |
}
|
|
101 |
|
|
102 |
startTimerIfNecessary();
|
|
103 |
}
|
|
104 |
|
|
105 |
void PluginHalter::startTimerIfNecessary()
|
|
106 |
{
|
|
107 |
if (m_timer.isActive())
|
|
108 |
return;
|
|
109 |
|
|
110 |
if (m_plugins.isEmpty())
|
|
111 |
return;
|
|
112 |
|
|
113 |
double nextFireInterval = static_cast<double>(m_pluginAllowedRunTime) - (currentTime() - m_oldestStartTime);
|
|
114 |
m_timer.startOneShot(nextFireInterval < 0 ? 0 : nextFireInterval);
|
|
115 |
}
|
|
116 |
|
|
117 |
} // namespace WebCore
|