This source file includes following definitions.
- instance
- m_isDispatching
- addController
- removeController
- purgeControllers
- didChangeScreenOrientation
- startListening
- stopListening
#include "config.h"
#include "modules/screen_orientation/ScreenOrientationDispatcher.h"
#include "modules/screen_orientation/ScreenOrientationController.h"
#include "public/platform/Platform.h"
#include "wtf/TemporaryChange.h"
namespace WebCore {
ScreenOrientationDispatcher& ScreenOrientationDispatcher::instance()
{
DEFINE_STATIC_LOCAL(ScreenOrientationDispatcher, screenOrientationDispatcher, ());
return screenOrientationDispatcher;
}
ScreenOrientationDispatcher::ScreenOrientationDispatcher()
: m_needsPurge(false)
, m_isDispatching(false)
{
}
void ScreenOrientationDispatcher::addController(ScreenOrientationController* controller)
{
bool wasEmpty = m_controllers.isEmpty();
if (!m_controllers.contains(controller))
m_controllers.append(controller);
if (wasEmpty)
startListening();
}
void ScreenOrientationDispatcher::removeController(ScreenOrientationController* controller)
{
size_t index = m_controllers.find(controller);
if (index == kNotFound)
return;
m_controllers[index] = 0;
m_needsPurge = true;
if (!m_isDispatching)
purgeControllers();
}
void ScreenOrientationDispatcher::purgeControllers()
{
ASSERT(m_needsPurge);
size_t i = 0;
while (i < m_controllers.size()) {
if (!m_controllers[i]) {
m_controllers[i] = m_controllers.last();
m_controllers.removeLast();
} else {
++i;
}
}
m_needsPurge = false;
if (m_controllers.isEmpty())
stopListening();
}
void ScreenOrientationDispatcher::didChangeScreenOrientation(blink::WebScreenOrientation orientation)
{
{
TemporaryChange<bool> changeIsDispatching(m_isDispatching, true);
size_t size = m_controllers.size();
for (size_t i = 0; i < size; ++i) {
if (m_controllers[i])
static_cast<ScreenOrientationController*>(m_controllers[i])->didChangeScreenOrientation(orientation);
}
}
if (m_needsPurge)
purgeControllers();
}
void ScreenOrientationDispatcher::startListening()
{
blink::Platform::current()->setScreenOrientationListener(this);
}
void ScreenOrientationDispatcher::stopListening()
{
blink::Platform::current()->setScreenOrientationListener(0);
}
}