blob: 98e643a44e64dca77d52ce88f4565dad52213cf9 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "web/InspectorEmulationAgent.h"
#include "core/frame/FrameHost.h"
#include "core/frame/FrameView.h"
#include "core/frame/Settings.h"
#include "core/page/Page.h"
#include "platform/geometry/DoubleRect.h"
#include "public/platform/WebViewScheduler.h"
#include "web/DevToolsEmulator.h"
#include "web/WebLocalFrameImpl.h"
#include "web/WebViewImpl.h"
namespace blink {
namespace EmulationAgentState {
static const char scriptExecutionDisabled[] = "scriptExecutionDisabled";
static const char touchEventEmulationEnabled[] = "touchEventEmulationEnabled";
static const char emulatedMedia[] = "emulatedMedia";
}
InspectorEmulationAgent* InspectorEmulationAgent::create(WebLocalFrameImpl* webLocalFrameImpl, Client* client)
{
return new InspectorEmulationAgent(webLocalFrameImpl, client);
}
InspectorEmulationAgent::InspectorEmulationAgent(WebLocalFrameImpl* webLocalFrameImpl, Client* client)
: m_webLocalFrameImpl(webLocalFrameImpl)
, m_client(client)
{
}
InspectorEmulationAgent::~InspectorEmulationAgent()
{
}
WebViewImpl* InspectorEmulationAgent::webViewImpl()
{
return m_webLocalFrameImpl->viewImpl();
}
void InspectorEmulationAgent::restore()
{
ErrorString error;
setScriptExecutionDisabled(&error, m_state->booleanProperty(EmulationAgentState::scriptExecutionDisabled, false));
setTouchEmulationEnabled(&error, m_state->booleanProperty(EmulationAgentState::touchEventEmulationEnabled, false), protocol::Maybe<String>());
String16 emulatedMedia;
m_state->getString(EmulationAgentState::emulatedMedia, &emulatedMedia);
setEmulatedMedia(&error, emulatedMedia);
}
void InspectorEmulationAgent::disable(ErrorString*)
{
ErrorString error;
setScriptExecutionDisabled(&error, false);
setTouchEmulationEnabled(&error, false, protocol::Maybe<String>());
setEmulatedMedia(&error, String());
}
void InspectorEmulationAgent::resetPageScaleFactor(ErrorString*)
{
webViewImpl()->resetScaleStateImmediately();
}
void InspectorEmulationAgent::setPageScaleFactor(ErrorString*, double pageScaleFactor)
{
webViewImpl()->setPageScaleFactor(static_cast<float>(pageScaleFactor));
}
void InspectorEmulationAgent::setScriptExecutionDisabled(ErrorString*, bool value)
{
m_state->setBoolean(EmulationAgentState::scriptExecutionDisabled, value);
webViewImpl()->devToolsEmulator()->setScriptExecutionDisabled(value);
}
void InspectorEmulationAgent::setTouchEmulationEnabled(ErrorString*, bool enabled, const Maybe<String>& configuration)
{
m_state->setBoolean(EmulationAgentState::touchEventEmulationEnabled, enabled);
webViewImpl()->devToolsEmulator()->setTouchEventEmulationEnabled(enabled);
}
void InspectorEmulationAgent::setEmulatedMedia(ErrorString*, const String& media)
{
m_state->setString(EmulationAgentState::emulatedMedia, media);
webViewImpl()->page()->settings().setMediaTypeOverride(media);
}
void InspectorEmulationAgent::setCPUThrottlingRate(ErrorString*, double throttlingRate)
{
m_client->setCPUThrottlingRate(throttlingRate);
}
void InspectorEmulationAgent::setVirtualTimePolicy(ErrorString*, const String& in_policy)
{
if (protocol::Emulation::VirtualTimePolicyEnum::Advance == in_policy) {
m_webLocalFrameImpl->view()->scheduler()->enableVirtualTime();
m_webLocalFrameImpl->view()->scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePolicy::ADVANCE);
} else if (protocol::Emulation::VirtualTimePolicyEnum::Pause == in_policy) {
m_webLocalFrameImpl->view()->scheduler()->enableVirtualTime();
m_webLocalFrameImpl->view()->scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePolicy::PAUSE);
} else if (protocol::Emulation::VirtualTimePolicyEnum::PauseIfNetworkFetchesPending == in_policy) {
m_webLocalFrameImpl->view()->scheduler()->enableVirtualTime();
m_webLocalFrameImpl->view()->scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePolicy::PAUSE_IF_NETWORK_FETCHES_PENDING);
}
}
DEFINE_TRACE(InspectorEmulationAgent)
{
visitor->trace(m_webLocalFrameImpl);
InspectorBaseAgent::trace(visitor);
}
} // namespace blink