1 // Copyright 2015 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.
6 #include "web/InspectorEmulationAgent.h"
8 #include "core/frame/FrameHost.h"
9 #include "core/frame/FrameView.h"
10 #include "core/frame/Settings.h"
11 #include "core/inspector/InspectorState.h"
12 #include "core/page/Page.h"
13 #include "platform/geometry/DoubleRect.h"
14 #include "web/DevToolsEmulator.h"
15 #include "web/WebLocalFrameImpl.h"
16 #include "web/WebViewImpl.h"
20 namespace EmulationAgentState
{
21 static const char scriptExecutionDisabled
[] = "scriptExecutionDisabled";
22 static const char touchEventEmulationEnabled
[] = "touchEventEmulationEnabled";
23 static const char emulatedMedia
[] = "emulatedMedia";
26 PassOwnPtrWillBeRawPtr
<InspectorEmulationAgent
> InspectorEmulationAgent::create(WebViewImpl
* webViewImpl
)
28 return adoptPtrWillBeNoop(new InspectorEmulationAgent(webViewImpl
));
31 InspectorEmulationAgent::InspectorEmulationAgent(WebViewImpl
* webViewImpl
)
32 : InspectorBaseAgent
<InspectorEmulationAgent
, InspectorFrontend::Emulation
>("Emulation")
33 , m_webViewImpl(webViewImpl
)
35 m_webViewImpl
->devToolsEmulator()->setEmulationAgent(this);
38 InspectorEmulationAgent::~InspectorEmulationAgent()
42 void InspectorEmulationAgent::restore()
45 setScriptExecutionDisabled(&error
, m_state
->getBoolean(EmulationAgentState::scriptExecutionDisabled
));
46 setTouchEmulationEnabled(&error
, m_state
->getBoolean(EmulationAgentState::touchEventEmulationEnabled
), nullptr);
47 setEmulatedMedia(&error
, m_state
->getString(EmulationAgentState::emulatedMedia
));
50 void InspectorEmulationAgent::disable(ErrorString
*)
53 setScriptExecutionDisabled(&error
, false);
54 setTouchEmulationEnabled(&error
, false, nullptr);
55 setEmulatedMedia(&error
, String());
58 void InspectorEmulationAgent::discardAgent()
60 m_webViewImpl
->devToolsEmulator()->setEmulationAgent(nullptr);
63 void InspectorEmulationAgent::didCommitLoadForLocalFrame(LocalFrame
* frame
)
65 if (frame
== m_webViewImpl
->mainFrameImpl()->frame())
69 void InspectorEmulationAgent::resetScrollAndPageScaleFactor(ErrorString
*)
71 m_webViewImpl
->resetScrollAndScaleStateImmediately();
74 void InspectorEmulationAgent::setPageScaleFactor(ErrorString
*, double pageScaleFactor
)
76 m_webViewImpl
->setPageScaleFactor(static_cast<float>(pageScaleFactor
));
79 void InspectorEmulationAgent::setScriptExecutionDisabled(ErrorString
*, bool value
)
81 m_state
->setBoolean(EmulationAgentState::scriptExecutionDisabled
, value
);
82 m_webViewImpl
->devToolsEmulator()->setScriptExecutionDisabled(value
);
85 void InspectorEmulationAgent::setTouchEmulationEnabled(ErrorString
*, bool enabled
, const String
* configuration
)
87 m_state
->setBoolean(EmulationAgentState::touchEventEmulationEnabled
, enabled
);
88 m_webViewImpl
->devToolsEmulator()->setTouchEventEmulationEnabled(enabled
);
91 void InspectorEmulationAgent::setEmulatedMedia(ErrorString
*, const String
& media
)
93 m_state
->setString(EmulationAgentState::emulatedMedia
, media
);
94 m_webViewImpl
->page()->settings().setMediaTypeOverride(media
);
97 void InspectorEmulationAgent::viewportChanged()
99 if (!m_webViewImpl
->devToolsEmulator()->deviceEmulationEnabled() || !frontend())
102 FrameView
* view
= m_webViewImpl
->mainFrameImpl()->frameView();
103 IntSize contentsSize
= view
->contentsSize();
104 FloatPoint scrollOffset
;
105 scrollOffset
= FloatPoint(view
->scrollableArea()->visibleContentRectDouble().location());
107 RefPtr
<TypeBuilder::Emulation::Viewport
> viewport
= TypeBuilder::Emulation::Viewport::create()
108 .setScrollX(scrollOffset
.x())
109 .setScrollY(scrollOffset
.y())
110 .setContentsWidth(contentsSize
.width())
111 .setContentsHeight(contentsSize
.height())
112 .setPageScaleFactor(m_webViewImpl
->page()->pageScaleFactor())
113 .setMinimumPageScaleFactor(m_webViewImpl
->minimumPageScaleFactor())
114 .setMaximumPageScaleFactor(m_webViewImpl
->maximumPageScaleFactor());
115 frontend()->viewportChanged(viewport
);
118 DEFINE_TRACE(InspectorEmulationAgent
)
120 InspectorBaseAgent::trace(visitor
);