Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "web/InspectorEmulationAgent.h" | 5 #include "web/InspectorEmulationAgent.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | |
| 8 #include "core/dom/DocumentLifecycle.h" | |
| 7 #include "core/frame/FrameHost.h" | 9 #include "core/frame/FrameHost.h" |
| 8 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| 9 #include "core/frame/Settings.h" | 11 #include "core/frame/Settings.h" |
| 12 #include "core/inspector/protocol/DOM.h" | |
| 10 #include "core/page/Page.h" | 13 #include "core/page/Page.h" |
| 11 #include "platform/geometry/DoubleRect.h" | 14 #include "platform/geometry/DoubleRect.h" |
| 15 #include "platform/graphics/Color.h" | |
| 12 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebFloatPoint.h" | 17 #include "public/platform/WebFloatPoint.h" |
| 14 #include "public/platform/WebThread.h" | 18 #include "public/platform/WebThread.h" |
| 15 #include "public/platform/WebViewScheduler.h" | 19 #include "public/platform/WebViewScheduler.h" |
| 16 #include "web/DevToolsEmulator.h" | 20 #include "web/DevToolsEmulator.h" |
| 17 #include "web/WebLocalFrameImpl.h" | 21 #include "web/WebLocalFrameImpl.h" |
| 18 #include "web/WebViewImpl.h" | 22 #include "web/WebViewImpl.h" |
| 19 | 23 |
| 20 namespace blink { | 24 namespace blink { |
| 21 | 25 |
| 22 namespace EmulationAgentState { | 26 namespace EmulationAgentState { |
| 23 static const char scriptExecutionDisabled[] = "scriptExecutionDisabled"; | 27 static const char scriptExecutionDisabled[] = "scriptExecutionDisabled"; |
| 24 static const char touchEventEmulationEnabled[] = "touchEventEmulationEnabled"; | 28 static const char touchEventEmulationEnabled[] = "touchEventEmulationEnabled"; |
| 25 static const char emulatedMedia[] = "emulatedMedia"; | 29 static const char emulatedMedia[] = "emulatedMedia"; |
| 26 static const char forcedViewportEnabled[] = "forcedViewportEnabled"; | 30 static const char forcedViewportEnabled[] = "forcedViewportEnabled"; |
| 27 static const char forcedViewportX[] = "forcedViewportX"; | 31 static const char forcedViewportX[] = "forcedViewportX"; |
| 28 static const char forcedViewportY[] = "forcedViewportY"; | 32 static const char forcedViewportY[] = "forcedViewportY"; |
| 29 static const char forcedViewportScale[] = "forcedViewportScale"; | 33 static const char forcedViewportScale[] = "forcedViewportScale"; |
| 34 static const char defaultBackgroundColorOverrideRGBA[] = | |
| 35 "defaultBackgroundColorOverrideRGBA"; | |
| 36 static const char defaultBackgroundColorOriginalRGBA[] = | |
| 37 "defaultBackgroundColorOriginalRGBA"; | |
| 30 } | 38 } |
| 31 | 39 |
| 32 InspectorEmulationAgent* InspectorEmulationAgent::create( | 40 InspectorEmulationAgent* InspectorEmulationAgent::create( |
| 33 WebLocalFrameImpl* webLocalFrameImpl, | 41 WebLocalFrameImpl* webLocalFrameImpl, |
| 34 Client* client) { | 42 Client* client) { |
| 35 return new InspectorEmulationAgent(webLocalFrameImpl, client); | 43 return new InspectorEmulationAgent(webLocalFrameImpl, client); |
| 36 } | 44 } |
| 37 | 45 |
| 38 InspectorEmulationAgent::InspectorEmulationAgent( | 46 InspectorEmulationAgent::InspectorEmulationAgent( |
| 39 WebLocalFrameImpl* webLocalFrameImpl, | 47 WebLocalFrameImpl* webLocalFrameImpl, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 57 m_state->getString(EmulationAgentState::emulatedMedia, &emulatedMedia); | 65 m_state->getString(EmulationAgentState::emulatedMedia, &emulatedMedia); |
| 58 setEmulatedMedia(emulatedMedia); | 66 setEmulatedMedia(emulatedMedia); |
| 59 if (m_state->booleanProperty(EmulationAgentState::forcedViewportEnabled, | 67 if (m_state->booleanProperty(EmulationAgentState::forcedViewportEnabled, |
| 60 false)) { | 68 false)) { |
| 61 forceViewport( | 69 forceViewport( |
| 62 | 70 |
| 63 m_state->doubleProperty(EmulationAgentState::forcedViewportX, 0), | 71 m_state->doubleProperty(EmulationAgentState::forcedViewportX, 0), |
| 64 m_state->doubleProperty(EmulationAgentState::forcedViewportY, 0), | 72 m_state->doubleProperty(EmulationAgentState::forcedViewportY, 0), |
| 65 m_state->doubleProperty(EmulationAgentState::forcedViewportScale, 1)); | 73 m_state->doubleProperty(EmulationAgentState::forcedViewportScale, 1)); |
| 66 } | 74 } |
| 75 auto rgbaValue = | |
| 76 m_state->get(EmulationAgentState::defaultBackgroundColorOverrideRGBA); | |
| 77 if (rgbaValue) { | |
| 78 blink::protocol::ErrorSupport errors; | |
| 79 auto rgba = protocol::DOM::RGBA::fromValue(rgbaValue, &errors); | |
| 80 if (!errors.hasErrors()) { | |
| 81 setDefaultBackgroundColorOverride( | |
| 82 Maybe<protocol::DOM::RGBA>(std::move(rgba))); | |
| 83 } | |
| 84 } | |
| 67 } | 85 } |
| 68 | 86 |
| 69 Response InspectorEmulationAgent::disable() { | 87 Response InspectorEmulationAgent::disable() { |
| 70 setScriptExecutionDisabled(false); | 88 setScriptExecutionDisabled(false); |
| 71 setTouchEmulationEnabled(false, Maybe<String>()); | 89 setTouchEmulationEnabled(false, Maybe<String>()); |
| 72 setEmulatedMedia(String()); | 90 setEmulatedMedia(String()); |
| 73 setCPUThrottlingRate(1); | 91 setCPUThrottlingRate(1); |
| 74 resetViewport(); | 92 resetViewport(); |
| 93 setDefaultBackgroundColorOverride(Maybe<protocol::DOM::RGBA>()); | |
| 75 return Response::OK(); | 94 return Response::OK(); |
| 76 } | 95 } |
| 77 | 96 |
| 78 Response InspectorEmulationAgent::forceViewport(double x, | 97 Response InspectorEmulationAgent::forceViewport(double x, |
| 79 double y, | 98 double y, |
| 80 double scale) { | 99 double scale) { |
| 81 if (x < 0 || y < 0) | 100 if (x < 0 || y < 0) |
| 82 return Response::Error("Coordinates must be non-negative"); | 101 return Response::Error("Coordinates must be non-negative"); |
| 83 | 102 |
| 84 if (scale <= 0) | 103 if (scale <= 0) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 } | 181 } |
| 163 return Response::OK(); | 182 return Response::OK(); |
| 164 } | 183 } |
| 165 | 184 |
| 166 void InspectorEmulationAgent::virtualTimeBudgetExpired() { | 185 void InspectorEmulationAgent::virtualTimeBudgetExpired() { |
| 167 m_webLocalFrameImpl->view()->scheduler()->setVirtualTimePolicy( | 186 m_webLocalFrameImpl->view()->scheduler()->setVirtualTimePolicy( |
| 168 WebViewScheduler::VirtualTimePolicy::PAUSE); | 187 WebViewScheduler::VirtualTimePolicy::PAUSE); |
| 169 frontend()->virtualTimeBudgetExpired(); | 188 frontend()->virtualTimeBudgetExpired(); |
| 170 } | 189 } |
| 171 | 190 |
| 191 Response InspectorEmulationAgent::setDefaultBackgroundColorOverride( | |
| 192 Maybe<protocol::DOM::RGBA> color) { | |
| 193 if (!color.isJust()) { | |
| 194 // Reset to the original color if one is set. | |
| 195 auto rgbaValue = | |
| 196 m_state->get(EmulationAgentState::defaultBackgroundColorOriginalRGBA); | |
|
dgozman
2017/01/24 20:12:49
You don't have to store original in the state. Ins
Eric Seckler
2017/01/25 10:33:37
Thanks! Done.
| |
| 197 if (rgbaValue) { | |
| 198 blink::protocol::ErrorSupport errors; | |
| 199 auto rgba = protocol::DOM::RGBA::fromValue(rgbaValue, &errors); | |
| 200 if (!errors.hasErrors()) | |
| 201 setBaseBackgroundColor(rgba.get()); | |
| 202 } | |
| 203 | |
| 204 // Clear the state. | |
| 205 m_state->remove(EmulationAgentState::defaultBackgroundColorOverrideRGBA); | |
| 206 m_state->remove(EmulationAgentState::defaultBackgroundColorOriginalRGBA); | |
| 207 | |
| 208 return Response::OK(); | |
| 209 } | |
| 210 | |
| 211 // Save override and original color to state. | |
| 212 blink::protocol::DOM::RGBA* override = color.fromJust(); | |
| 213 m_state->setValue(EmulationAgentState::defaultBackgroundColorOverrideRGBA, | |
| 214 override->toValue()); | |
| 215 if (!m_state->get(EmulationAgentState::defaultBackgroundColorOriginalRGBA)) { | |
| 216 auto original = getBaseBackgroundColor(); | |
| 217 m_state->setValue(EmulationAgentState::defaultBackgroundColorOriginalRGBA, | |
| 218 original->toValue()); | |
| 219 } | |
| 220 | |
| 221 setBaseBackgroundColor(override); | |
| 222 return Response::OK(); | |
| 223 } | |
| 224 | |
| 225 std::unique_ptr<protocol::DOM::RGBA> | |
| 226 InspectorEmulationAgent::getBaseBackgroundColor() { | |
| 227 auto color = webViewImpl()->baseBackgroundColor(); | |
| 228 double alpha = color.alpha() / 255.0f; | |
| 229 return blink::protocol::DOM::RGBA::create() | |
| 230 .setR(color.red()) | |
| 231 .setG(color.green()) | |
| 232 .setB(color.blue()) | |
| 233 .setA(alpha) | |
| 234 .build(); | |
| 235 } | |
| 236 | |
| 237 void InspectorEmulationAgent::setBaseBackgroundColor( | |
| 238 protocol::DOM::RGBA* rgba) { | |
| 239 // Clamping of values is done by Color() constructor. | |
| 240 int alpha = lroundf(255.0f * rgba->getA(1.0f)); | |
| 241 webViewImpl()->setBaseBackgroundColor( | |
| 242 Color(rgba->getR(), rgba->getG(), rgba->getB(), alpha).rgb()); | |
| 243 } | |
| 244 | |
| 172 DEFINE_TRACE(InspectorEmulationAgent) { | 245 DEFINE_TRACE(InspectorEmulationAgent) { |
| 173 visitor->trace(m_webLocalFrameImpl); | 246 visitor->trace(m_webLocalFrameImpl); |
| 174 InspectorBaseAgent::trace(visitor); | 247 InspectorBaseAgent::trace(visitor); |
| 175 } | 248 } |
| 176 | 249 |
| 177 } // namespace blink | 250 } // namespace blink |
| OLD | NEW |