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 * @implements {SDK.TargetManager.Observer} | 5 * @implements {SDK.TargetManager.Observer} |
| 6 * @unrestricted | 6 * @unrestricted |
| 7 */ | 7 */ |
| 8 Emulation.DeviceModeModel = class { | 8 Emulation.DeviceModeModel = class { |
| 9 /** | 9 /** |
| 10 * @param {function()} updateCallback | 10 * @param {function()} updateCallback |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 } | 266 } |
| 267 | 267 |
| 268 /** | 268 /** |
| 269 * @return {boolean} | 269 * @return {boolean} |
| 270 */ | 270 */ |
| 271 isFullHeight() { | 271 isFullHeight() { |
| 272 return !this._heightSetting.get(); | 272 return !this._heightSetting.get(); |
| 273 } | 273 } |
| 274 | 274 |
| 275 /** | 275 /** |
| 276 * @return {boolean} | |
| 277 */ | |
| 278 _isMobile() { | |
| 279 switch (this._type) { | |
| 280 case Emulation.DeviceModeModel.Type.Device: | |
| 281 return this._device.mobile(); | |
| 282 | |
| 283 case Emulation.DeviceModeModel.Type.None: | |
| 284 return false; | |
| 285 | |
| 286 case Emulation.DeviceModeModel.Type.Responsive: | |
| 287 return this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile || | |
| 288 this._uaSetting.get() === Emulation.DeviceModeModel.UA.MobileNoTouch ; | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 /** | |
| 276 * @return {!Common.Setting} | 293 * @return {!Common.Setting} |
| 277 */ | 294 */ |
| 278 scaleSetting() { | 295 scaleSetting() { |
| 279 return this._scaleSetting; | 296 return this._scaleSetting; |
| 280 } | 297 } |
| 281 | 298 |
| 282 /** | 299 /** |
| 283 * @return {!Common.Setting} | 300 * @return {!Common.Setting} |
| 284 */ | 301 */ |
| 285 uaSetting() { | 302 uaSetting() { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 return new UI.Insets(0, 0, 0, 0); | 408 return new UI.Insets(0, 0, 0, 0); |
| 392 return this._mode.insets; | 409 return this._mode.insets; |
| 393 } | 410 } |
| 394 | 411 |
| 395 /** | 412 /** |
| 396 * @param {boolean} resetPageScaleFactor | 413 * @param {boolean} resetPageScaleFactor |
| 397 */ | 414 */ |
| 398 _calculateAndEmulate(resetPageScaleFactor) { | 415 _calculateAndEmulate(resetPageScaleFactor) { |
| 399 if (!this._target) | 416 if (!this._target) |
| 400 this._onTargetAvailable = this._calculateAndEmulate.bind(this, resetPageSc aleFactor); | 417 this._onTargetAvailable = this._calculateAndEmulate.bind(this, resetPageSc aleFactor); |
| 401 | 418 var mobile = this._isMobile(); |
| 402 if (this._type === Emulation.DeviceModeModel.Type.Device) { | 419 if (this._type === Emulation.DeviceModeModel.Type.Device) { |
| 403 var orientation = this._device.orientationByName(this._mode.orientation); | 420 var orientation = this._device.orientationByName(this._mode.orientation); |
| 404 var outline = this._currentOutline(); | 421 var outline = this._currentOutline(); |
| 405 var insets = this._currentInsets(); | 422 var insets = this._currentInsets(); |
| 406 this._fitScale = this._calculateFitScale(orientation.width, orientation.he ight, outline, insets); | 423 this._fitScale = this._calculateFitScale(orientation.width, orientation.he ight, outline, insets); |
| 407 if (this._device.mobile()) { | 424 if (mobile) { |
| 408 this._appliedUserAgentType = | 425 this._appliedUserAgentType = |
| 409 this._device.touch() ? Emulation.DeviceModeModel.UA.Mobile : Emulati on.DeviceModeModel.UA.MobileNoTouch; | 426 this._device.touch() ? Emulation.DeviceModeModel.UA.Mobile : Emulati on.DeviceModeModel.UA.MobileNoTouch; |
| 410 } else { | 427 } else { |
| 411 this._appliedUserAgentType = | 428 this._appliedUserAgentType = |
| 412 this._device.touch() ? Emulation.DeviceModeModel.UA.DesktopTouch : E mulation.DeviceModeModel.UA.Desktop; | 429 this._device.touch() ? Emulation.DeviceModeModel.UA.DesktopTouch : E mulation.DeviceModeModel.UA.Desktop; |
| 413 } | 430 } |
| 414 this._applyDeviceMetrics( | 431 this._applyDeviceMetrics( |
| 415 new UI.Size(orientation.width, orientation.height), insets, outline, t his._scaleSetting.get(), | 432 new UI.Size(orientation.width, orientation.height), insets, outline, t his._scaleSetting.get(), |
| 416 this._device.deviceScaleFactor, this._device.mobile(), | 433 this._device.deviceScaleFactor, mobile, |
| 417 this._mode.orientation === Emulation.EmulatedDevice.Horizontal ? 'land scapePrimary' : 'portraitPrimary', | 434 this._mode.orientation === Emulation.EmulatedDevice.Horizontal ? 'land scapePrimary' : 'portraitPrimary', |
| 418 resetPageScaleFactor); | 435 resetPageScaleFactor); |
| 419 this._applyUserAgent(this._device.userAgent); | 436 this._applyUserAgent(this._device.userAgent); |
| 420 this._applyTouch(this._device.touch(), this._device.mobile()); | 437 this._applyTouch(this._device.touch(), mobile); |
| 421 } else if (this._type === Emulation.DeviceModeModel.Type.None) { | 438 } else if (this._type === Emulation.DeviceModeModel.Type.None) { |
| 422 this._fitScale = this._calculateFitScale(this._availableSize.width, this._ availableSize.height); | 439 this._fitScale = this._calculateFitScale(this._availableSize.width, this._ availableSize.height); |
| 423 this._appliedUserAgentType = Emulation.DeviceModeModel.UA.Desktop; | 440 this._appliedUserAgentType = Emulation.DeviceModeModel.UA.Desktop; |
| 424 this._applyDeviceMetrics( | 441 this._applyDeviceMetrics( |
| 425 this._availableSize, new UI.Insets(0, 0, 0, 0), new UI.Insets(0, 0, 0, 0), 1, 0, false, '', | 442 this._availableSize, new UI.Insets(0, 0, 0, 0), new UI.Insets(0, 0, 0, 0), 1, 0, mobile, '', |
| 426 resetPageScaleFactor); | 443 resetPageScaleFactor); |
| 427 this._applyUserAgent(''); | 444 this._applyUserAgent(''); |
| 428 this._applyTouch(false, false); | 445 this._applyTouch(false, false); |
| 429 } else if (this._type === Emulation.DeviceModeModel.Type.Responsive) { | 446 } else if (this._type === Emulation.DeviceModeModel.Type.Responsive) { |
| 430 var screenWidth = this._widthSetting.get(); | 447 var screenWidth = this._widthSetting.get(); |
| 431 if (!screenWidth || screenWidth > this._preferredScaledWidth()) | 448 if (!screenWidth || screenWidth > this._preferredScaledWidth()) |
| 432 screenWidth = this._preferredScaledWidth(); | 449 screenWidth = this._preferredScaledWidth(); |
| 433 var screenHeight = this._heightSetting.get(); | 450 var screenHeight = this._heightSetting.get(); |
| 434 if (!screenHeight || screenHeight > this._preferredScaledHeight()) | 451 if (!screenHeight || screenHeight > this._preferredScaledHeight()) |
| 435 screenHeight = this._preferredScaledHeight(); | 452 screenHeight = this._preferredScaledHeight(); |
| 436 var mobile = this._uaSetting.get() === Emulation.DeviceModeModel.UA.Mobile || | |
| 437 this._uaSetting.get() === Emulation.DeviceModeModel.UA.MobileNoTouch; | |
| 438 var defaultDeviceScaleFactor = mobile ? Emulation.DeviceModeModel.defaultM obileScaleFactor : 0; | 453 var defaultDeviceScaleFactor = mobile ? Emulation.DeviceModeModel.defaultM obileScaleFactor : 0; |
| 439 this._fitScale = this._calculateFitScale(this._widthSetting.get(), this._h eightSetting.get()); | 454 this._fitScale = this._calculateFitScale(this._widthSetting.get(), this._h eightSetting.get()); |
| 440 this._appliedUserAgentType = this._uaSetting.get(); | 455 this._appliedUserAgentType = this._uaSetting.get(); |
| 441 this._applyDeviceMetrics( | 456 this._applyDeviceMetrics( |
| 442 new UI.Size(screenWidth, screenHeight), new UI.Insets(0, 0, 0, 0), new UI.Insets(0, 0, 0, 0), | 457 new UI.Size(screenWidth, screenHeight), new UI.Insets(0, 0, 0, 0), new UI.Insets(0, 0, 0, 0), |
| 443 this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defa ultDeviceScaleFactor, mobile, | 458 this._scaleSetting.get(), this._deviceScaleFactorSetting.get() || defa ultDeviceScaleFactor, mobile, |
| 444 screenHeight >= screenWidth ? 'portraitPrimary' : 'landscapePrimary', resetPageScaleFactor); | 459 screenHeight >= screenWidth ? 'portraitPrimary' : 'landscapePrimary', resetPageScaleFactor); |
| 445 this._applyUserAgent(mobile ? Emulation.DeviceModeModel._defaultMobileUser Agent : ''); | 460 this._applyUserAgent(mobile ? Emulation.DeviceModeModel._defaultMobileUser Agent : ''); |
| 446 this._applyTouch( | 461 this._applyTouch( |
| 447 this._uaSetting.get() === Emulation.DeviceModeModel.UA.DesktopTouch || | 462 this._uaSetting.get() === Emulation.DeviceModeModel.UA.DesktopTouch || |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 scale, | 532 scale, |
| 518 deviceScaleFactor, | 533 deviceScaleFactor, |
| 519 mobile, | 534 mobile, |
| 520 screenOrientation, | 535 screenOrientation, |
| 521 resetPageScaleFactor) { | 536 resetPageScaleFactor) { |
| 522 screenSize.width = Math.max(1, Math.floor(screenSize.width)); | 537 screenSize.width = Math.max(1, Math.floor(screenSize.width)); |
| 523 screenSize.height = Math.max(1, Math.floor(screenSize.height)); | 538 screenSize.height = Math.max(1, Math.floor(screenSize.height)); |
| 524 | 539 |
| 525 var pageWidth = screenSize.width - insets.left - insets.right; | 540 var pageWidth = screenSize.width - insets.left - insets.right; |
| 526 var pageHeight = screenSize.height - insets.top - insets.bottom; | 541 var pageHeight = screenSize.height - insets.top - insets.bottom; |
| 542 this._emulatedPageSize = new UI.Size(Math.floor(pageWidth * scale), Math.flo or(pageHeight * scale)); | |
| 527 | 543 |
| 528 var positionX = insets.left; | 544 var positionX = insets.left; |
| 529 var positionY = insets.top; | 545 var positionY = insets.top; |
| 530 var screenOrientationAngle = screenOrientation === 'landscapePrimary' ? 90 : 0; | 546 var screenOrientationAngle = screenOrientation === 'landscapePrimary' ? 90 : 0; |
| 531 | 547 |
| 532 this._appliedDeviceSize = screenSize; | 548 this._appliedDeviceSize = screenSize; |
| 533 this._appliedDeviceScaleFactor = deviceScaleFactor || window.devicePixelRati o; | 549 this._appliedDeviceScaleFactor = deviceScaleFactor || window.devicePixelRati o; |
| 534 this._screenRect = new UI.Rect( | 550 this._screenRect = new UI.Rect( |
| 535 Math.max(0, (this._availableSize.width - screenSize.width * scale) / 2), outline.top * scale, | 551 Math.max(0, (this._availableSize.width - screenSize.width * scale) / 2), outline.top * scale, |
| 536 screenSize.width * scale, screenSize.height * scale); | 552 screenSize.width * scale, screenSize.height * scale); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 590 if (screenOrientation) | 606 if (screenOrientation) |
| 591 params.screenOrientation = {type: screenOrientation, angle: screenOrie ntationAngle}; | 607 params.screenOrientation = {type: screenOrientation, angle: screenOrie ntationAngle}; |
| 592 setDevicePromise = this._target.emulationAgent().invoke_setDeviceMetrics Override( | 608 setDevicePromise = this._target.emulationAgent().invoke_setDeviceMetrics Override( |
| 593 params, this._deviceMetricsOverrideAppliedForTest.bind(this)); | 609 params, this._deviceMetricsOverrideAppliedForTest.bind(this)); |
| 594 } | 610 } |
| 595 allPromises.push(setDevicePromise); | 611 allPromises.push(setDevicePromise); |
| 596 return Promise.all(allPromises); | 612 return Promise.all(allPromises); |
| 597 } | 613 } |
| 598 } | 614 } |
| 599 | 615 |
| 616 /** | |
| 617 * @param {function(?Protocol.Error, string) callback | |
| 618 */ | |
| 619 captureFullHeightScreenshot(callback) { | |
| 620 this._target.pageAgent().getLayoutMetrics((err, layoutViewport, visualViewpo rt, contentSize) => { | |
| 621 var promises = []; | |
| 622 var scaledPageSize = | |
| 623 new UI.Rect(0, 0, contentSize.width, contentSize.height).scale(visualV iewport.scale * this._scale); | |
| 624 var params = { | |
| 625 width: 0, | |
| 626 height: 0, | |
| 627 deviceScaleFactor: this._appliedDeviceScaleFactor, | |
| 628 mobile: this._isMobile(), | |
| 629 fitWindow: false, | |
| 630 scale: this._scale, | |
| 631 }; | |
| 632 promises.push(this._target.emulationAgent().forceViewport(0, 0, visualView port.scale)); | |
|
dgozman
2017/01/19 23:40:42
I tried this out: with 150% scale in dropdown and
| |
| 633 promises.push(this._target.emulationAgent().invoke_setDeviceMetricsOverrid e(params)); | |
| 634 promises.push(this._target.emulationAgent().setVisibleSize(scaledPageSize. width, scaledPageSize.height)); | |
|
dgozman
2017/01/19 23:40:42
You have to Math.floor() these to send integers.
| |
| 635 Promise.all(promises).then(() => { | |
| 636 this._target.pageAgent().captureScreenshot(screenshotCaptured.bind(this) ); | |
| 637 }); | |
| 638 | |
| 639 /** | |
| 640 * @param {?Protocol.Error} error | |
| 641 * @param {string} content | |
| 642 * @this {Emulation.DeviceModeModel} | |
| 643 */ | |
| 644 function screenshotCaptured(error, content) { | |
| 645 this._target.emulationAgent().setVisibleSize(this._emulatedPageSize.widt h, this._emulatedPageSize.height); | |
| 646 this._target.emulationAgent().resetViewport(); | |
| 647 callback(error, content); | |
| 648 } | |
| 649 }); | |
| 650 } | |
| 651 | |
| 600 _deviceMetricsOverrideAppliedForTest() { | 652 _deviceMetricsOverrideAppliedForTest() { |
| 601 // Used for sniffing in tests. | 653 // Used for sniffing in tests. |
| 602 } | 654 } |
| 603 | 655 |
| 604 /** | 656 /** |
| 605 * @param {boolean} touchEnabled | 657 * @param {boolean} touchEnabled |
| 606 * @param {boolean} mobile | 658 * @param {boolean} mobile |
| 607 */ | 659 */ |
| 608 _applyTouch(touchEnabled, mobile) { | 660 _applyTouch(touchEnabled, mobile) { |
| 609 Emulation.MultitargetTouchModel.instance().setTouchEnabled(touchEnabled, mob ile); | 661 Emulation.MultitargetTouchModel.instance().setTouchEnabled(touchEnabled, mob ile); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 627 | 679 |
| 628 Emulation.DeviceModeModel.MinDeviceSize = 50; | 680 Emulation.DeviceModeModel.MinDeviceSize = 50; |
| 629 Emulation.DeviceModeModel.MaxDeviceSize = 9999; | 681 Emulation.DeviceModeModel.MaxDeviceSize = 9999; |
| 630 | 682 |
| 631 | 683 |
| 632 Emulation.DeviceModeModel._defaultMobileUserAgent = | 684 Emulation.DeviceModeModel._defaultMobileUserAgent = |
| 633 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/%s Mobile Safari/537.36'; | 685 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 ( KHTML, like Gecko) Chrome/%s Mobile Safari/537.36'; |
| 634 Emulation.DeviceModeModel._defaultMobileUserAgent = | 686 Emulation.DeviceModeModel._defaultMobileUserAgent = |
| 635 SDK.MultitargetNetworkManager.patchUserAgentWithChromeVersion(Emulation.Devi ceModeModel._defaultMobileUserAgent); | 687 SDK.MultitargetNetworkManager.patchUserAgentWithChromeVersion(Emulation.Devi ceModeModel._defaultMobileUserAgent); |
| 636 Emulation.DeviceModeModel.defaultMobileScaleFactor = 2; | 688 Emulation.DeviceModeModel.defaultMobileScaleFactor = 2; |
| OLD | NEW |