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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/emulation/DeviceModeModel.js

Issue 2844383003: [DevTools] Move the rest of emulation to EmulationModel (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/emulation/DeviceModeView.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 * @implements {SDK.TargetManager.Observer} 5 * @implements {SDK.SDKModelObserver<!SDK.EmulationModel>}
6 * @unrestricted 6 * @unrestricted
7 */ 7 */
8 Emulation.DeviceModeModel = class { 8 Emulation.DeviceModeModel = class {
9 /** 9 /**
10 * @param {function()} updateCallback 10 * @param {function()} updateCallback
11 */ 11 */
12 constructor(updateCallback) { 12 constructor(updateCallback) {
13 this._updateCallback = updateCallback; 13 this._updateCallback = updateCallback;
14 this._screenRect = new UI.Rect(0, 0, 1, 1); 14 this._screenRect = new UI.Rect(0, 0, 1, 1);
15 this._visiblePageRect = new UI.Rect(0, 0, 1, 1); 15 this._visiblePageRect = new UI.Rect(0, 0, 1, 1);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 51
52 /** @type {!Emulation.DeviceModeModel.Type} */ 52 /** @type {!Emulation.DeviceModeModel.Type} */
53 this._type = Emulation.DeviceModeModel.Type.None; 53 this._type = Emulation.DeviceModeModel.Type.None;
54 /** @type {?Emulation.EmulatedDevice} */ 54 /** @type {?Emulation.EmulatedDevice} */
55 this._device = null; 55 this._device = null;
56 /** @type {?Emulation.EmulatedDevice.Mode} */ 56 /** @type {?Emulation.EmulatedDevice.Mode} */
57 this._mode = null; 57 this._mode = null;
58 /** @type {number} */ 58 /** @type {number} */
59 this._fitScale = 1; 59 this._fitScale = 1;
60 60
61 /** @type {?SDK.Target} */ 61 /** @type {?SDK.EmulationModel} */
62 this._target = null; 62 this._emulationModel = null;
63 /** @type {?function()} */ 63 /** @type {?function()} */
64 this._onTargetAvailable = null; 64 this._onModelAvailable = null;
65 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Browser); 65 SDK.targetManager.observeModels(SDK.EmulationModel, this);
66 } 66 }
67 67
68 /** 68 /**
69 * @param {string} value 69 * @param {string} value
70 * @return {boolean} 70 * @return {boolean}
71 */ 71 */
72 static deviceSizeValidator(value) { 72 static deviceSizeValidator(value) {
73 if (/^[\d]+$/.test(value) && value >= Emulation.DeviceModeModel.MinDeviceSiz e && 73 if (/^[\d]+$/.test(value) && value >= Emulation.DeviceModeModel.MinDeviceSiz e &&
74 value <= Emulation.DeviceModeModel.MaxDeviceSize) 74 value <= Emulation.DeviceModeModel.MaxDeviceSize)
75 return true; 75 return true;
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 reset() { 319 reset() {
320 this._deviceScaleFactorSetting.set(0); 320 this._deviceScaleFactorSetting.set(0);
321 this._scaleSetting.set(1); 321 this._scaleSetting.set(1);
322 this.setWidth(400); 322 this.setWidth(400);
323 this.setHeight(0); 323 this.setHeight(0);
324 this._uaSetting.set(Emulation.DeviceModeModel.UA.Mobile); 324 this._uaSetting.set(Emulation.DeviceModeModel.UA.Mobile);
325 } 325 }
326 326
327 /** 327 /**
328 * @override 328 * @override
329 * @param {!SDK.Target} target 329 * @param {!SDK.EmulationModel} emulationModel
330 */ 330 */
331 targetAdded(target) { 331 modelAdded(emulationModel) {
332 if (!this._target) { 332 if (!this._emulationModel && emulationModel.supportsDeviceEmulation()) {
333 this._target = target; 333 this._emulationModel = emulationModel;
334 if (this._onTargetAvailable) { 334 if (this._onModelAvailable) {
335 var callback = this._onTargetAvailable; 335 var callback = this._onModelAvailable;
336 this._onTargetAvailable = null; 336 this._onModelAvailable = null;
337 callback(); 337 callback();
338 } 338 }
339 } 339 }
340 } 340 }
341 341
342 /** 342 /**
343 * @override 343 * @override
344 * @param {!SDK.Target} target 344 * @param {!SDK.EmulationModel} emulationModel
345 */ 345 */
346 targetRemoved(target) { 346 modelRemoved(emulationModel) {
347 if (this._target === target) 347 if (this._emulationModel === emulationModel)
348 this._target = null; 348 this._emulationModel = null;
349 } 349 }
350 350
351 /** 351 /**
352 * @return {?SDK.Target} 352 * @return {?string}
353 */ 353 */
354 target() { 354 inspectedURL() {
355 return this._target; 355 return this._emulationModel ? this._emulationModel.target().inspectedURL() : null;
356 } 356 }
357 357
358 _scaleSettingChanged() { 358 _scaleSettingChanged() {
359 this._calculateAndEmulate(false); 359 this._calculateAndEmulate(false);
360 } 360 }
361 361
362 _widthSettingChanged() { 362 _widthSettingChanged() {
363 this._calculateAndEmulate(false); 363 this._calculateAndEmulate(false);
364 } 364 }
365 365
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 _currentInsets() { 412 _currentInsets() {
413 if (this._type !== Emulation.DeviceModeModel.Type.Device) 413 if (this._type !== Emulation.DeviceModeModel.Type.Device)
414 return new UI.Insets(0, 0, 0, 0); 414 return new UI.Insets(0, 0, 0, 0);
415 return this._mode.insets; 415 return this._mode.insets;
416 } 416 }
417 417
418 /** 418 /**
419 * @param {boolean} resetPageScaleFactor 419 * @param {boolean} resetPageScaleFactor
420 */ 420 */
421 _calculateAndEmulate(resetPageScaleFactor) { 421 _calculateAndEmulate(resetPageScaleFactor) {
422 if (!this._target) 422 if (!this._emulationModel)
423 this._onTargetAvailable = this._calculateAndEmulate.bind(this, resetPageSc aleFactor); 423 this._onModelAvailable = this._calculateAndEmulate.bind(this, resetPageSca leFactor);
424 var mobile = this._isMobile(); 424 var mobile = this._isMobile();
425 if (this._type === Emulation.DeviceModeModel.Type.Device) { 425 if (this._type === Emulation.DeviceModeModel.Type.Device) {
426 var orientation = this._device.orientationByName(this._mode.orientation); 426 var orientation = this._device.orientationByName(this._mode.orientation);
427 var outline = this._currentOutline(); 427 var outline = this._currentOutline();
428 var insets = this._currentInsets(); 428 var insets = this._currentInsets();
429 this._fitScale = this._calculateFitScale(orientation.width, orientation.he ight, outline, insets); 429 this._fitScale = this._calculateFitScale(orientation.width, orientation.he ight, outline, insets);
430 if (mobile) { 430 if (mobile) {
431 this._appliedUserAgentType = 431 this._appliedUserAgentType =
432 this._device.touch() ? Emulation.DeviceModeModel.UA.Mobile : Emulati on.DeviceModeModel.UA.MobileNoTouch; 432 this._device.touch() ? Emulation.DeviceModeModel.UA.Mobile : Emulati on.DeviceModeModel.UA.MobileNoTouch;
433 } else { 433 } else {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defa ultDeviceScaleFactor, mobile, 465 this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defa ultDeviceScaleFactor, mobile,
466 screenHeight >= screenWidth ? Protocol.Emulation.ScreenOrientationType .PortraitPrimary : 466 screenHeight >= screenWidth ? Protocol.Emulation.ScreenOrientationType .PortraitPrimary :
467 Protocol.Emulation.ScreenOrientationType .LandscapePrimary, 467 Protocol.Emulation.ScreenOrientationType .LandscapePrimary,
468 resetPageScaleFactor); 468 resetPageScaleFactor);
469 this._applyUserAgent(mobile ? Emulation.DeviceModeModel._defaultMobileUser Agent : ''); 469 this._applyUserAgent(mobile ? Emulation.DeviceModeModel._defaultMobileUser Agent : '');
470 this._applyTouch( 470 this._applyTouch(
471 this._uaSetting.get() === Emulation.DeviceModeModel.UA.DesktopTouch || 471 this._uaSetting.get() === Emulation.DeviceModeModel.UA.DesktopTouch ||
472 this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile, 472 this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile,
473 this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile); 473 this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile);
474 } 474 }
475 var overlayModel = this._target ? this._target.model(SDK.OverlayModel) : nul l; 475 var overlayModel = this._emulationModel ? this._emulationModel.overlayModel( ) : null;
476 if (overlayModel) 476 if (overlayModel)
477 overlayModel.setShowViewportSizeOnResize(this._type === Emulation.DeviceMo deModel.Type.None); 477 overlayModel.setShowViewportSizeOnResize(this._type === Emulation.DeviceMo deModel.Type.None);
478 this._updateCallback.call(null); 478 this._updateCallback.call(null);
479 } 479 }
480 480
481 /** 481 /**
482 * @param {number} screenWidth 482 * @param {number} screenWidth
483 * @param {number} screenHeight 483 * @param {number} screenHeight
484 * @param {!UI.Insets=} outline 484 * @param {!UI.Insets=} outline
485 * @param {!UI.Insets=} insets 485 * @param {!UI.Insets=} insets
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 pageHeight = 0; 583 pageHeight = 0;
584 } 584 }
585 585
586 this._deviceMetricsThrottler.schedule(setDeviceMetricsOverride.bind(this)); 586 this._deviceMetricsThrottler.schedule(setDeviceMetricsOverride.bind(this));
587 587
588 /** 588 /**
589 * @this {Emulation.DeviceModeModel} 589 * @this {Emulation.DeviceModeModel}
590 * @return {!Promise.<?>} 590 * @return {!Promise.<?>}
591 */ 591 */
592 function setDeviceMetricsOverride() { 592 function setDeviceMetricsOverride() {
593 if (!this._target) 593 if (!this._emulationModel)
594 return Promise.resolve(); 594 return Promise.resolve();
595 595
596 var clear = !pageWidth && !pageHeight && !mobile && !deviceScaleFactor && scale === 1 && !screenOrientation; 596 var clear = !pageWidth && !pageHeight && !mobile && !deviceScaleFactor && scale === 1 && !screenOrientation;
597 var allPromises = []; 597 var allPromises = [];
598 if (resetPageScaleFactor) 598 if (resetPageScaleFactor)
599 allPromises.push(this._target.emulationAgent().resetPageScaleFactor()); 599 allPromises.push(this._emulationModel.resetPageScaleFactor());
600 var setDevicePromise; 600 var metrics = null;
601 if (clear) { 601 if (!clear) {
602 setDevicePromise = this._target.emulationAgent().clearDeviceMetricsOverr ide(); 602 metrics = {
603 } else {
604 var params = {
605 width: pageWidth, 603 width: pageWidth,
606 height: pageHeight, 604 height: pageHeight,
607 deviceScaleFactor: deviceScaleFactor, 605 deviceScaleFactor: deviceScaleFactor,
608 mobile: mobile, 606 mobile: mobile,
609 fitWindow: false, 607 fitWindow: false,
610 scale: scale, 608 scale: scale,
611 screenWidth: screenSize.width, 609 screenWidth: screenSize.width,
612 screenHeight: screenSize.height, 610 screenHeight: screenSize.height,
613 positionX: positionX, 611 positionX: positionX,
614 positionY: positionY 612 positionY: positionY
615 }; 613 };
616 if (screenOrientation) 614 if (screenOrientation)
617 params.screenOrientation = {type: screenOrientation, angle: screenOrie ntationAngle}; 615 metrics.screenOrientation = {type: screenOrientation, angle: screenOri entationAngle};
618 setDevicePromise = this._target.emulationAgent().invoke_setDeviceMetrics Override(params);
619 } 616 }
620 allPromises.push(setDevicePromise); 617 allPromises.push(this._emulationModel.emulateDevice(metrics));
621 return Promise.all(allPromises); 618 return Promise.all(allPromises);
622 } 619 }
623 } 620 }
624 621
625 /** 622 /**
626 * @param {boolean} fullSize 623 * @param {boolean} fullSize
627 * @return {!Promise<?string>} 624 * @return {!Promise<?string>}
628 */ 625 */
629 async captureScreenshot(fullSize) { 626 async captureScreenshot(fullSize) {
630 var screenCaptureModel = this._target ? this._target.model(SDK.ScreenCapture Model) : null; 627 var screenCaptureModel = this._emulationModel ? this._emulationModel.target( ).model(SDK.ScreenCaptureModel) : null;
631 if (!screenCaptureModel) 628 if (!screenCaptureModel)
632 return null; 629 return null;
633 630
634 var metrics = await screenCaptureModel.fetchLayoutMetrics(); 631 var metrics = await screenCaptureModel.fetchLayoutMetrics();
635 if (!metrics) 632 if (!metrics)
636 return null; 633 return null;
637 634
638 if (!this._emulatedPageSize) 635 if (!this._emulatedPageSize)
639 this._calculateAndEmulate(false); 636 this._calculateAndEmulate(false);
640 var overlayModel = this._target ? this._target.model(SDK.OverlayModel) : nul l; 637 var overlayModel = this._emulationModel ? this._emulationModel.overlayModel( ) : null;
641 if (overlayModel) 638 if (overlayModel)
642 overlayModel.setShowViewportSizeOnResize(false); 639 overlayModel.setShowViewportSizeOnResize(false);
643 640
644 var pageSize = fullSize ? new UI.Size(metrics.contentWidth, metrics.contentH eight) : this._emulatedPageSize; 641 var pageSize = fullSize ? new UI.Size(metrics.contentWidth, metrics.contentH eight) : this._emulatedPageSize;
645 var promises = []; 642 var promises = [];
646 promises.push( 643 promises.push(this._emulationModel.setVisibleSize(Math.floor(pageSize.width) , Math.floor(pageSize.height)));
647 this._target.emulationAgent().setVisibleSize(Math.floor(pageSize.width), Math.floor(pageSize.height)));
648 if (fullSize) { 644 if (fullSize) {
649 promises.push(this._target.emulationAgent().forceViewport(0, 0, 1)); 645 promises.push(this._emulationModel.forceViewport({x: 0, y: 0, scale: 1}));
650 } else { 646 } else {
651 promises.push(this._target.emulationAgent().forceViewport( 647 promises.push(this._emulationModel.forceViewport(
652 Math.floor(metrics.viewportX), Math.floor(metrics.viewportY), metrics. viewportScale)); 648 {x: Math.floor(metrics.viewportX), y: Math.floor(metrics.viewportY), s cale: metrics.viewportScale}));
653 } 649 }
654 promises.push(this._target.emulationAgent().invoke_setDeviceMetricsOverride( { 650 promises.push(this._emulationModel.emulateDevice({
655 width: 0, 651 width: 0,
656 height: 0, 652 height: 0,
657 deviceScaleFactor: this._appliedDeviceScaleFactor, 653 deviceScaleFactor: this._appliedDeviceScaleFactor,
658 mobile: this._isMobile(), 654 mobile: this._isMobile(),
659 fitWindow: false, 655 fitWindow: false,
660 scale: 1, 656 scale: 1,
661 })); 657 }));
662 await Promise.all(promises); 658 await Promise.all(promises);
663 659
664 var screenshot = await screenCaptureModel.captureScreenshot('png', 100); 660 var screenshot = await screenCaptureModel.captureScreenshot('png', 100);
665 this._target.emulationAgent().setVisibleSize( 661 this._emulationModel.setVisibleSize(
666 Math.floor(this._emulatedPageSize.width * this._scale), 662 Math.floor(this._emulatedPageSize.width * this._scale),
667 Math.floor(this._emulatedPageSize.height * this._scale)); 663 Math.floor(this._emulatedPageSize.height * this._scale));
668 this._target.emulationAgent().resetViewport(); 664 this._emulationModel.forceViewport(null);
669 this._calculateAndEmulate(false); 665 this._calculateAndEmulate(false);
670 return screenshot; 666 return screenshot;
671 } 667 }
672 668
673 /** 669 /**
674 * @param {boolean} touchEnabled 670 * @param {boolean} touchEnabled
675 * @param {boolean} mobile 671 * @param {boolean} mobile
676 */ 672 */
677 _applyTouch(touchEnabled, mobile) { 673 _applyTouch(touchEnabled, mobile) {
678 for (var emulationModel of SDK.targetManager.models(SDK.EmulationModel)) 674 for (var emulationModel of SDK.targetManager.models(SDK.EmulationModel))
(...skipping 18 matching lines...) Expand all
697 693
698 Emulation.DeviceModeModel.MinDeviceSize = 50; 694 Emulation.DeviceModeModel.MinDeviceSize = 50;
699 Emulation.DeviceModeModel.MaxDeviceSize = 9999; 695 Emulation.DeviceModeModel.MaxDeviceSize = 9999;
700 696
701 697
702 Emulation.DeviceModeModel._defaultMobileUserAgent = 698 Emulation.DeviceModeModel._defaultMobileUserAgent =
703 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/%s Mobile Safari/537.36'; 699 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/%s Mobile Safari/537.36';
704 Emulation.DeviceModeModel._defaultMobileUserAgent = 700 Emulation.DeviceModeModel._defaultMobileUserAgent =
705 SDK.MultitargetNetworkManager.patchUserAgentWithChromeVersion(Emulation.Devi ceModeModel._defaultMobileUserAgent); 701 SDK.MultitargetNetworkManager.patchUserAgentWithChromeVersion(Emulation.Devi ceModeModel._defaultMobileUserAgent);
706 Emulation.DeviceModeModel.defaultMobileScaleFactor = 2; 702 Emulation.DeviceModeModel.defaultMobileScaleFactor = 2;
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/emulation/DeviceModeView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698