/**
   * The view calls this function to indicate that the viewport changed size. It must hold the
   * monitor while calling it.
   *
   * <p>TODO: Refactor this to use an interface. Expose that interface only to the view and not to
   * the layer client. That way, the layer client won't be tempted to call this, which might result
   * in an infinite loop.
   */
  void setViewportSize(int width, int height) {
    mViewportMetrics = mViewportMetrics.setViewportSize(width, height);

    if (mGeckoIsReady) {
      // here we send gecko a resize message. The code in browser.js is responsible for
      // picking up on that resize event, modifying the viewport as necessary, and informing
      // us of the new viewport.
      sendResizeEventIfNecessary(true);
      // the following call also sends gecko a message, which will be processed after the resize
      // message above has updated the viewport. this message ensures that if we have just put
      // focus in a text field, we scroll the content so that the text field is in view.
      GeckoAppShell.viewSizeChanged();
    }
  }
  /** Viewport message handler. */
  private DisplayPortMetrics handleViewportMessage(
      ImmutableViewportMetrics messageMetrics, ViewportMessageType type) {
    synchronized (this) {
      ImmutableViewportMetrics metrics;
      ImmutableViewportMetrics oldMetrics = getViewportMetrics();

      switch (type) {
        default:
        case UPDATE:
          // Keep the old viewport size
          metrics = messageMetrics.setViewportSize(oldMetrics.getWidth(), oldMetrics.getHeight());
          abortPanZoomAnimation();
          break;
        case PAGE_SIZE:
          // adjust the page dimensions to account for differences in zoom
          // between the rendered content (which is what Gecko tells us)
          // and our zoom level (which may have diverged).
          float scaleFactor = oldMetrics.zoomFactor / messageMetrics.zoomFactor;
          metrics =
              oldMetrics.setPageRect(
                  RectUtils.scale(messageMetrics.getPageRect(), scaleFactor),
                  messageMetrics.getCssPageRect());
          break;
      }

      final ImmutableViewportMetrics newMetrics = metrics;
      post(
          new Runnable() {
            public void run() {
              mGeckoViewport = newMetrics;
            }
          });
      setViewportMetrics(newMetrics, type == ViewportMessageType.UPDATE);
      mDisplayPort = DisplayPortCalculator.calculate(getViewportMetrics(), null);
    }
    return mDisplayPort;
  }