Move SelectionTemplate::is_handle_visible_ to FrameSelection

This patch moves |is_handle_visible_| to |FrameSelection| from |SelectionTemplate|
since handle visibility is used only for setting |FrameSelection|, hence it is
a redundant member variable of |SelectionTemplate|.

Bug: 742093
Change-Id: I3add4da3844fb40be34dcb4d4b46b5fa6fed1d7e
Reviewed-on: https://chromium-review.googlesource.com/595389
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491660}
static SelectionInFlatTree AdjustEndpointsAtBidiBoundary(
    const VisiblePositionInFlatTree& visible_base,
    const VisiblePositionInFlatTree& visible_extent) {
  DCHECK(visible_base.IsValid());
  DCHECK(visible_extent.IsValid());

  RenderedPosition base(visible_base);
  RenderedPosition extent(visible_extent);

  const SelectionInFlatTree& unchanged_selection =
      SelectionInFlatTree::Builder()
          .SetBaseAndExtent(visible_base.DeepEquivalent(),
                            visible_extent.DeepEquivalent())
          .Build();

  if (base.IsNull() || extent.IsNull() || base.IsEquivalent(extent))
    return unchanged_selection;

  if (base.AtLeftBoundaryOfBidiRun()) {
    if (!extent.AtRightBoundaryOfBidiRun(base.BidiLevelOnRight()) &&
        base.IsEquivalent(
            extent.LeftBoundaryOfBidiRun(base.BidiLevelOnRight()))) {
      return SelectionInFlatTree::Builder()
          .SetBaseAndExtent(
              CreateVisiblePosition(
                  ToPositionInFlatTree(base.PositionAtLeftBoundaryOfBiDiRun()))
                  .DeepEquivalent(),
              visible_extent.DeepEquivalent())
          .Build();
    }
    return unchanged_selection;
  }

  if (base.AtRightBoundaryOfBidiRun()) {
    if (!extent.AtLeftBoundaryOfBidiRun(base.BidiLevelOnLeft()) &&
        base.IsEquivalent(
            extent.RightBoundaryOfBidiRun(base.BidiLevelOnLeft()))) {
      return SelectionInFlatTree::Builder()
          .SetBaseAndExtent(
              CreateVisiblePosition(
                  ToPositionInFlatTree(base.PositionAtRightBoundaryOfBiDiRun()))
                  .DeepEquivalent(),
              visible_extent.DeepEquivalent())
          .Build();
    }
    return unchanged_selection;
  }

  if (extent.AtLeftBoundaryOfBidiRun() &&
      extent.IsEquivalent(
          base.LeftBoundaryOfBidiRun(extent.BidiLevelOnRight()))) {
    return SelectionInFlatTree::Builder()
        .SetBaseAndExtent(
            visible_base.DeepEquivalent(),
            CreateVisiblePosition(
                ToPositionInFlatTree(extent.PositionAtLeftBoundaryOfBiDiRun()))
                .DeepEquivalent())
        .Build();
  }

  if (extent.AtRightBoundaryOfBidiRun() &&
      extent.IsEquivalent(
          base.RightBoundaryOfBidiRun(extent.BidiLevelOnLeft()))) {
    return SelectionInFlatTree::Builder()
        .SetBaseAndExtent(
            visible_base.DeepEquivalent(),
            CreateVisiblePosition(
                ToPositionInFlatTree(extent.PositionAtRightBoundaryOfBiDiRun()))
                .DeepEquivalent())
        .Build();
  }
  return unchanged_selection;
}

static SelectionInFlatTree AdjustEndpointsAtBidiBoundary(
    const VisiblePositionInFlatTree& visible_base,
    const VisiblePositionInFlatTree& visible_extent) {
  DCHECK(visible_base.IsValid());
  DCHECK(visible_extent.IsValid());

  RenderedPosition base(visible_base);
  RenderedPosition extent(visible_extent);

  const SelectionInFlatTree& unchanged_selection =
      SelectionInFlatTree::Builder()
          .SetBaseAndExtent(visible_base.DeepEquivalent(),
                            visible_extent.DeepEquivalent())
          .Build();

  if (base.IsNull() || extent.IsNull() || base.IsEquivalent(extent))
    return unchanged_selection;

  if (base.AtLeftBoundaryOfBidiRun()) {
    if (!extent.AtRightBoundaryOfBidiRun(base.BidiLevelOnRight()) &&
        base.IsEquivalent(
            extent.LeftBoundaryOfBidiRun(base.BidiLevelOnRight()))) {
      return SelectionInFlatTree::Builder()
          .SetBaseAndExtent(
              CreateVisiblePosition(
                  ToPositionInFlatTree(base.PositionAtLeftBoundaryOfBiDiRun()))
                  .DeepEquivalent(),
              visible_extent.DeepEquivalent())
          .Build();
    }
    return unchanged_selection;
  }

  if (base.AtRightBoundaryOfBidiRun()) {
    if (!extent.AtLeftBoundaryOfBidiRun(base.BidiLevelOnLeft()) &&
        base.IsEquivalent(
            extent.RightBoundaryOfBidiRun(base.BidiLevelOnLeft()))) {
      return SelectionInFlatTree::Builder()
          .SetBaseAndExtent(
              CreateVisiblePosition(
                  ToPositionInFlatTree(base.PositionAtRightBoundaryOfBiDiRun()))
                  .DeepEquivalent(),
              visible_extent.DeepEquivalent())
          .Build();
    }
    return unchanged_selection;
  }

  if (extent.AtLeftBoundaryOfBidiRun() &&
      extent.IsEquivalent(
          base.LeftBoundaryOfBidiRun(extent.BidiLevelOnRight()))) {
    return SelectionInFlatTree::Builder()
        .SetBaseAndExtent(
            visible_base.DeepEquivalent(),
            CreateVisiblePosition(
                ToPositionInFlatTree(extent.PositionAtLeftBoundaryOfBiDiRun()))
                .DeepEquivalent())
        .Build();
  }

  if (extent.AtRightBoundaryOfBidiRun() &&
      extent.IsEquivalent(
          base.RightBoundaryOfBidiRun(extent.BidiLevelOnLeft()))) {
    return SelectionInFlatTree::Builder()
        .SetBaseAndExtent(
            visible_base.DeepEquivalent(),
            CreateVisiblePosition(
                ToPositionInFlatTree(extent.PositionAtRightBoundaryOfBiDiRun()))
                .DeepEquivalent())
        .Build();
  }
  return unchanged_selection;
}
