Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: Source/web/WebDevToolsAgentImpl.cpp

Issue 23187005: [DevTools] Use device metrics emulation implemented in content. (Closed) Base URL: svn://svn.chromium.org/blink/trunk/
Patch Set: Reupload Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010-2011 Google Inc. All rights reserved. 2 * Copyright (C) 2010-2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "WebDevToolsAgentImpl.h" 32 #include "WebDevToolsAgentImpl.h"
33 33
34 #include "InspectorBackendDispatcher.h" 34 #include "InspectorBackendDispatcher.h"
35 #include "InspectorFrontend.h" 35 #include "InspectorFrontend.h"
36 #include "InspectorProtocolVersion.h" 36 #include "InspectorProtocolVersion.h"
37 #include "RuntimeEnabledFeatures.h"
37 #include "WebDataSource.h" 38 #include "WebDataSource.h"
38 #include "WebDevToolsAgentClient.h" 39 #include "WebDevToolsAgentClient.h"
39 #include "WebFrameImpl.h" 40 #include "WebFrameImpl.h"
40 #include "WebInputEventConversion.h" 41 #include "WebInputEventConversion.h"
41 #include "WebMemoryUsageInfo.h" 42 #include "WebMemoryUsageInfo.h"
43 #include "WebSettings.h"
42 #include "WebViewClient.h" 44 #include "WebViewClient.h"
43 #include "WebViewImpl.h" 45 #include "WebViewImpl.h"
44 #include "bindings/v8/PageScriptDebugServer.h" 46 #include "bindings/v8/PageScriptDebugServer.h"
45 #include "bindings/v8/ScriptController.h" 47 #include "bindings/v8/ScriptController.h"
46 #include "bindings/v8/V8Binding.h" 48 #include "bindings/v8/V8Binding.h"
47 #include "bindings/v8/V8Utilities.h" 49 #include "bindings/v8/V8Utilities.h"
48 #include "core/dom/ExceptionCode.h" 50 #include "core/dom/ExceptionCode.h"
49 #include "core/fetch/MemoryCache.h" 51 #include "core/fetch/MemoryCache.h"
50 #include "core/inspector/InjectedScriptHost.h" 52 #include "core/inspector/InjectedScriptHost.h"
51 #include "core/inspector/InspectorController.h" 53 #include "core/inspector/InspectorController.h"
52 #include "core/page/Frame.h" 54 #include "core/page/Frame.h"
53 #include "core/page/FrameView.h" 55 #include "core/page/FrameView.h"
54 #include "core/page/Page.h" 56 #include "core/page/Page.h"
55 #include "core/page/PageGroup.h" 57 #include "core/page/PageGroup.h"
58 #include "core/page/Settings.h"
56 #include "core/platform/JSONValues.h" 59 #include "core/platform/JSONValues.h"
57 #include "core/platform/graphics/GraphicsContext.h" 60 #include "core/platform/graphics/GraphicsContext.h"
58 #include "core/platform/network/ResourceError.h" 61 #include "core/platform/network/ResourceError.h"
59 #include "core/platform/network/ResourceRequest.h" 62 #include "core/platform/network/ResourceRequest.h"
60 #include "core/platform/network/ResourceResponse.h" 63 #include "core/platform/network/ResourceResponse.h"
61 #include "core/rendering/RenderView.h" 64 #include "core/rendering/RenderView.h"
62 #include "public/platform/Platform.h" 65 #include "public/platform/Platform.h"
63 #include "public/platform/WebRect.h" 66 #include "public/platform/WebRect.h"
64 #include "public/platform/WebString.h" 67 #include "public/platform/WebString.h"
65 #include "public/platform/WebURL.h" 68 #include "public/platform/WebURL.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 virtual void run() 183 virtual void run()
181 { 184 {
182 if (WebDevToolsAgent* webagent = m_descriptor->agent()) 185 if (WebDevToolsAgent* webagent = m_descriptor->agent())
183 webagent->dispatchOnInspectorBackend(m_descriptor->message()); 186 webagent->dispatchOnInspectorBackend(m_descriptor->message());
184 } 187 }
185 188
186 private: 189 private:
187 OwnPtr<WebDevToolsAgent::MessageDescriptor> m_descriptor; 190 OwnPtr<WebDevToolsAgent::MessageDescriptor> m_descriptor;
188 }; 191 };
189 192
190 class DeviceMetricsSupport {
191 public:
192 DeviceMetricsSupport(WebViewImpl* webView)
193 : m_webView(webView)
194 , m_fitWindow(false)
195 , m_originalZoomFactor(0)
196 {
197 }
198
199 ~DeviceMetricsSupport()
200 {
201 restore();
202 }
203
204 void setDeviceMetrics(int width, int height, float textZoomFactor, bool fitW indow)
205 {
206 WebCore::FrameView* view = frameView();
207 if (!view)
208 return;
209
210 m_emulatedFrameSize = WebSize(width, height);
211 m_fitWindow = fitWindow;
212 m_originalZoomFactor = 0;
213 m_webView->setTextZoomFactor(textZoomFactor);
214 applySizeOverrideInternal(view, FitWindowAllowed);
215 autoZoomPageToFitWidth(&view->frame());
216
217 m_webView->sendResizeEventAndRepaint();
218 }
219
220 void autoZoomPageToFitWidthOnNavigation(Frame* frame)
221 {
222 FrameView* frameView = frame->view();
223 applySizeOverrideInternal(frameView, FitWindowNotAllowed);
224 m_originalZoomFactor = 0;
225 applySizeOverrideInternal(frameView, FitWindowAllowed);
226 autoZoomPageToFitWidth(frame);
227 }
228
229 void autoZoomPageToFitWidth(Frame* frame)
230 {
231 frame->setTextZoomFactor(m_webView->textZoomFactor());
232 ensureOriginalZoomFactor(frame->view());
233 Document* document = frame->document();
234 float numerator = document->renderView() ? document->renderView()->viewW idth() : frame->view()->contentsWidth();
235 float factor = m_originalZoomFactor * (numerator / m_emulatedFrameSize.w idth);
236 frame->setPageAndTextZoomFactors(factor, m_webView->textZoomFactor());
237 document->styleResolverChanged(RecalcStyleImmediately);
238 document->updateLayout();
239 }
240
241 void webViewResized()
242 {
243 if (!m_fitWindow)
244 return;
245
246 applySizeOverrideIfNecessary();
247 autoZoomPageToFitWidth(m_webView->mainFrameImpl()->frame());
248 }
249
250 void applySizeOverrideIfNecessary()
251 {
252 FrameView* view = frameView();
253 if (!view)
254 return;
255
256 applySizeOverrideInternal(view, FitWindowAllowed);
257 }
258
259 private:
260 enum FitWindowFlag { FitWindowAllowed, FitWindowNotAllowed };
261
262 void ensureOriginalZoomFactor(FrameView* frameView)
263 {
264 if (m_originalZoomFactor)
265 return;
266
267 m_webView->setPageScaleFactor(1, WebPoint());
268 m_webView->setZoomLevel(0);
269 WebSize scaledEmulatedSize = scaledEmulatedFrameSize(frameView);
270 double denominator = frameView->contentsWidth();
271 if (!denominator)
272 denominator = 1;
273 m_originalZoomFactor = static_cast<double>(scaledEmulatedSize.width) / d enominator;
274 }
275
276 void restore()
277 {
278 WebCore::FrameView* view = frameView();
279 if (!view)
280 return;
281
282 m_webView->setZoomLevel(0);
283 m_webView->setTextZoomFactor(1);
284 view->setHorizontalScrollbarLock(false);
285 view->setVerticalScrollbarLock(false);
286 view->setScrollbarModes(ScrollbarAuto, ScrollbarAuto, false, false);
287 view->setFrameRect(IntRect(IntPoint(), IntSize(m_webView->size())));
288 m_webView->sendResizeEventAndRepaint();
289 }
290
291 WebSize scaledEmulatedFrameSize(FrameView* frameView)
292 {
293 if (!m_fitWindow)
294 return m_emulatedFrameSize;
295
296 WebSize scrollbarDimensions = forcedScrollbarDimensions(frameView);
297
298 int overrideWidth = m_emulatedFrameSize.width;
299 int overrideHeight = m_emulatedFrameSize.height;
300
301 WebSize webViewSize = m_webView->size();
302 int availableViewWidth = max(webViewSize.width - scrollbarDimensions.wid th, 1);
303 int availableViewHeight = max(webViewSize.height - scrollbarDimensions.h eight, 1);
304
305 double widthRatio = static_cast<double>(overrideWidth) / availableViewWi dth;
306 double heightRatio = static_cast<double>(overrideHeight) / availableView Height;
307 double dimensionRatio = max(widthRatio, heightRatio);
308 overrideWidth = static_cast<int>(ceil(static_cast<double>(overrideWidth) / dimensionRatio));
309 overrideHeight = static_cast<int>(ceil(static_cast<double>(overrideHeigh t) / dimensionRatio));
310
311 return WebSize(overrideWidth, overrideHeight);
312 }
313
314 WebSize forcedScrollbarDimensions(FrameView* frameView)
315 {
316 frameView->setScrollbarModes(ScrollbarAlwaysOn, ScrollbarAlwaysOn, true, true);
317
318 int verticalScrollbarWidth = 0;
319 int horizontalScrollbarHeight = 0;
320 if (Scrollbar* verticalBar = frameView->verticalScrollbar())
321 verticalScrollbarWidth = !verticalBar->isOverlayScrollbar() ? vertic alBar->width() : 0;
322 if (Scrollbar* horizontalBar = frameView->horizontalScrollbar())
323 horizontalScrollbarHeight = !horizontalBar->isOverlayScrollbar() ? h orizontalBar->height() : 0;
324 return WebSize(verticalScrollbarWidth, horizontalScrollbarHeight);
325 }
326
327 void applySizeOverrideInternal(FrameView* frameView, FitWindowFlag fitWindow Flag)
328 {
329 WebSize scrollbarDimensions = forcedScrollbarDimensions(frameView);
330
331 WebSize effectiveEmulatedSize = (fitWindowFlag == FitWindowAllowed) ? sc aledEmulatedFrameSize(frameView) : m_emulatedFrameSize;
332 int overrideWidth = effectiveEmulatedSize.width + scrollbarDimensions.wi dth;
333 int overrideHeight = effectiveEmulatedSize.height + scrollbarDimensions. height;
334
335 if (IntSize(overrideWidth, overrideHeight) != frameView->size())
336 frameView->resize(overrideWidth, overrideHeight);
337
338 Document* doc = frameView->frame().document();
339 doc->styleResolverChanged(RecalcStyleImmediately);
340 doc->updateLayout();
341 }
342
343 WebCore::FrameView* frameView()
344 {
345 return m_webView->mainFrameImpl() ? m_webView->mainFrameImpl()->frameVie w() : 0;
346 }
347
348 WebViewImpl* m_webView;
349 WebSize m_emulatedFrameSize;
350 bool m_fitWindow;
351 double m_originalZoomFactor;
352 };
353
354 WebDevToolsAgentImpl::WebDevToolsAgentImpl( 193 WebDevToolsAgentImpl::WebDevToolsAgentImpl(
355 WebViewImpl* webViewImpl, 194 WebViewImpl* webViewImpl,
356 WebDevToolsAgentClient* client) 195 WebDevToolsAgentClient* client)
357 : m_hostId(client->hostIdentifier()) 196 : m_hostId(client->hostIdentifier())
358 , m_client(client) 197 , m_client(client)
359 , m_webViewImpl(webViewImpl) 198 , m_webViewImpl(webViewImpl)
360 , m_attached(false) 199 , m_attached(false)
200 , m_deviceMetricsEnabled(false)
201 , m_isCSSViewportEnabled(false)
202 , m_isOverlayScrollbarsEnabled(false)
361 { 203 {
362 ASSERT(m_hostId > 0); 204 ASSERT(m_hostId > 0);
363 ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client); 205 ClientMessageLoopAdapter::ensureClientMessageLoopCreated(m_client);
364 } 206 }
365 207
366 WebDevToolsAgentImpl::~WebDevToolsAgentImpl() 208 WebDevToolsAgentImpl::~WebDevToolsAgentImpl()
367 { 209 {
368 ClientMessageLoopAdapter::inspectedViewClosed(m_webViewImpl); 210 ClientMessageLoopAdapter::inspectedViewClosed(m_webViewImpl);
369 if (m_attached) 211 if (m_attached)
370 WebKit::Platform::current()->currentThread()->removeTaskObserver(this); 212 WebKit::Platform::current()->currentThread()->removeTaskObserver(this);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 275
434 void WebDevToolsAgentImpl::didCreateScriptContext(WebFrameImpl* webframe, int wo rldId) 276 void WebDevToolsAgentImpl::didCreateScriptContext(WebFrameImpl* webframe, int wo rldId)
435 { 277 {
436 // Skip non main world contexts. 278 // Skip non main world contexts.
437 if (worldId) 279 if (worldId)
438 return; 280 return;
439 if (WebCore::Frame* frame = webframe->frame()) 281 if (WebCore::Frame* frame = webframe->frame())
440 frame->script()->setContextDebugId(m_hostId); 282 frame->script()->setContextDebugId(m_hostId);
441 } 283 }
442 284
443 void WebDevToolsAgentImpl::mainFrameViewCreated(WebFrameImpl* webFrame)
444 {
445 if (m_metricsSupport)
446 m_metricsSupport->applySizeOverrideIfNecessary();
447 }
448
449 bool WebDevToolsAgentImpl::metricsOverridden()
450 {
451 return !!m_metricsSupport;
452 }
453
454 void WebDevToolsAgentImpl::webViewResized(const WebSize& size) 285 void WebDevToolsAgentImpl::webViewResized(const WebSize& size)
455 { 286 {
456 if (m_metricsSupport)
457 m_metricsSupport->webViewResized();
458 if (InspectorController* ic = inspectorController()) 287 if (InspectorController* ic = inspectorController())
459 ic->webViewResized(m_metricsSupport ? IntSize(size.width, size.height) : IntSize()); 288 ic->webViewResized(IntSize());
460 } 289 }
461 290
462 bool WebDevToolsAgentImpl::handleInputEvent(WebCore::Page* page, const WebInputE vent& inputEvent) 291 bool WebDevToolsAgentImpl::handleInputEvent(WebCore::Page* page, const WebInputE vent& inputEvent)
463 { 292 {
464 if (!m_attached) 293 if (!m_attached)
465 return false; 294 return false;
466 295
467 InspectorController* ic = inspectorController(); 296 InspectorController* ic = inspectorController();
468 if (!ic) 297 if (!ic)
469 return false; 298 return false;
(...skipping 10 matching lines...) Expand all
480 } 309 }
481 if (WebInputEvent::isTouchEventType(inputEvent.type)) { 310 if (WebInputEvent::isTouchEventType(inputEvent.type)) {
482 PlatformTouchEvent touchEvent = PlatformTouchEventBuilder(page->mainFram e()->view(), *static_cast<const WebTouchEvent*>(&inputEvent)); 311 PlatformTouchEvent touchEvent = PlatformTouchEventBuilder(page->mainFram e()->view(), *static_cast<const WebTouchEvent*>(&inputEvent));
483 return ic->handleTouchEvent(page->mainFrame(), touchEvent); 312 return ic->handleTouchEvent(page->mainFrame(), touchEvent);
484 } 313 }
485 return false; 314 return false;
486 } 315 }
487 316
488 void WebDevToolsAgentImpl::overrideDeviceMetrics(int width, int height, float fo ntScaleFactor, bool fitWindow) 317 void WebDevToolsAgentImpl::overrideDeviceMetrics(int width, int height, float fo ntScaleFactor, bool fitWindow)
489 { 318 {
319 // FIXME: enable viewport after UI problems with pageScaleFactor will be fix ed.
aelias_OOO_until_Jul13 2013/10/01 08:08:51 I don't think this should land with dead code, it
dgozman 2013/10/01 15:26:34 Done.
320 bool enableViewport = false;
321
322 bool needReload = false;
490 if (!width && !height) { 323 if (!width && !height) {
491 if (m_metricsSupport) 324 if (m_deviceMetricsEnabled) {
492 m_metricsSupport.clear(); 325 needReload = true;
493 if (InspectorController* ic = inspectorController()) 326 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(m_isOverlayScrol lbarsEnabled);
494 ic->webViewResized(IntSize()); 327 if (enableViewport) {
495 return; 328 RuntimeEnabledFeatures::setCSSViewportEnabled(m_isCSSViewportEna bled);
329 m_webViewImpl->enableFixedLayoutMode(false);
330 m_webViewImpl->settings()->setViewportEnabled(false);
331 m_webViewImpl->page()->settings().setTextAutosizingEnabled(false );
332 m_webViewImpl->setIgnoreViewportTagScaleLimits(false);
333 m_webViewImpl->setPageScaleFactorLimits(1, 1);
334 }
335 m_client->emulateDevice(false /* enabled */, IntSize(), IntRect(), 1 , false);
336 }
337 m_deviceMetricsEnabled = false;
338 } else {
339 if (!m_deviceMetricsEnabled) {
340 needReload = true;
341
342 m_isOverlayScrollbarsEnabled = RuntimeEnabledFeatures::overlayScroll barsEnabled();
343 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(true);
344
345 if (enableViewport) {
346 m_isCSSViewportEnabled = RuntimeEnabledFeatures::cssViewportEnab led();
347 RuntimeEnabledFeatures::setCSSViewportEnabled(true);
348 // FIXME: save and restore next two values when enableViewport i s switched on.
349 m_webViewImpl->enableFixedLayoutMode(true);
350 m_webViewImpl->settings()->setViewportEnabled(true);
351 // FIXME: text autosizing should be enabled only for several use r agents which implement it.
352 // Either pass it from frontend or whitelist here, when enableVi ewport is switched on.
353 m_webViewImpl->page()->settings().setTextAutosizingEnabled(true) ;
354 m_webViewImpl->setIgnoreViewportTagScaleLimits(fontScaleFactor > 1);
355 m_webViewImpl->setPageScaleFactorLimits(-1, -1);
356 }
357 }
358 if (enableViewport)
359 m_webViewImpl->page()->settings().setTextAutosizingFontScaleFactor(f ontScaleFactor);
360
361 m_client->emulateDevice(true /* enabled */, IntSize(width, height), IntR ect(0, 0, width, height), 2, fitWindow);
362 m_deviceMetricsEnabled = true;
496 } 363 }
497 364 if (needReload && m_webViewImpl->mainFrameImpl())
498 if (!m_metricsSupport) 365 m_webViewImpl->mainFrameImpl()->reload(false /* ignoreCache */);
499 m_metricsSupport = adoptPtr(new DeviceMetricsSupport(m_webViewImpl));
500
501 m_metricsSupport->setDeviceMetrics(width, height, fontScaleFactor, fitWindow );
502 if (InspectorController* ic = inspectorController()) {
503 WebSize size = m_webViewImpl->size();
504 ic->webViewResized(IntSize(size.width, size.height));
505 }
506 }
507
508 void WebDevToolsAgentImpl::autoZoomPageToFitWidth()
509 {
510 if (m_metricsSupport)
511 m_metricsSupport->autoZoomPageToFitWidthOnNavigation(m_webViewImpl->main FrameImpl()->frame());
512 } 366 }
513 367
514 void WebDevToolsAgentImpl::getAllocatedObjects(HashSet<const void*>& set) 368 void WebDevToolsAgentImpl::getAllocatedObjects(HashSet<const void*>& set)
515 { 369 {
516 class CountingVisitor : public WebDevToolsAgentClient::AllocatedObjectVisito r { 370 class CountingVisitor : public WebDevToolsAgentClient::AllocatedObjectVisito r {
517 public: 371 public:
518 CountingVisitor() : m_totalObjectsCount(0) 372 CountingVisitor() : m_totalObjectsCount(0)
519 { 373 {
520 } 374 }
521 375
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 || commandName == InspectorBackendDispatcher::commandNames[InspectorBack endDispatcher::kProfiler_getCPUProfileCmd] 610 || commandName == InspectorBackendDispatcher::commandNames[InspectorBack endDispatcher::kProfiler_getCPUProfileCmd]
757 || commandName == InspectorBackendDispatcher::commandNames[InspectorBack endDispatcher::kHeapProfiler_getHeapSnapshotCmd]; 611 || commandName == InspectorBackendDispatcher::commandNames[InspectorBack endDispatcher::kHeapProfiler_getHeapSnapshotCmd];
758 } 612 }
759 613
760 void WebDevToolsAgent::processPendingMessages() 614 void WebDevToolsAgent::processPendingMessages()
761 { 615 {
762 PageScriptDebugServer::shared().runPendingTasks(); 616 PageScriptDebugServer::shared().runPendingTasks();
763 } 617 }
764 618
765 } // namespace WebKit 619 } // namespace WebKit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698