

// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/editing/reveal_selection_scope.h"

#include "third_party/blink/renderer/core/editing/editor.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"

namespace blink {

RevealSelectionScope::RevealSelectionScope(LocalFrame& frame) : frame_(&frame) {
  GetEditor().IncreasePreventRevealSelection();
}

RevealSelectionScope::~RevealSelectionScope() {
  DCHECK(GetEditor().PreventRevealSelection());
  GetEditor().DecreasePreventRevealSelection();
  if (GetEditor().PreventRevealSelection())
    return;
  if (!frame_->Selection().IsAvailable())
    return;
  frame_->Selection().RevealSelection(ScrollAlignment::ToEdgeIfNeeded(),
                                      kRevealExtent);
}

Editor& RevealSelectionScope::GetEditor() {
  return frame_->GetEditor();
}

void RevealSelectionScope::Trace(Visitor* visitor) const {
  visitor->Trace(frame_);
}

}  // namespace blink
