1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300

content / browser / navigation_transitions / back_forward_transition_animation_manager_android.cc [blame]

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

#include "content/browser/navigation_transitions/back_forward_transition_animation_manager_android.h"

#include "content/browser/navigation_transitions/back_forward_transition_animator.h"
#include "content/browser/renderer_host/navigation_controller_impl.h"
#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot.h"
#include "content/browser/renderer_host/navigation_transitions/navigation_transition_config.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/browser/web_contents/web_contents_view_android.h"
#include "content/public/browser/back_forward_transition_animation_manager.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents_delegate.h"

namespace content {

namespace {

// TODO(crbug/353766658): Move these shorthands to a proper header file.
using NavigationDirection =
    BackForwardTransitionAnimationManager::NavigationDirection;

using AnimationStage = BackForwardTransitionAnimationManager::AnimationStage;
using SwipeEdge = ui::BackGestureEventSwipeEdge;
using AnimationAbortReason =
    BackForwardTransitionAnimator::AnimationAbortReason;

}  // namespace

BackForwardTransitionAnimationManagerAndroid::
    BackForwardTransitionAnimationManagerAndroid(
        WebContentsViewAndroid* web_contents_view_android,
        NavigationControllerImpl* navigation_controller)
    : web_contents_view_android_(web_contents_view_android),
      navigation_controller_(navigation_controller),
      animator_factory_(
          std::make_unique<BackForwardTransitionAnimator::Factory>()) {}

BackForwardTransitionAnimationManagerAndroid::
    ~BackForwardTransitionAnimationManagerAndroid() {
  if (animator_) {
    animator_->AbortAnimation(AnimationAbortReason::kAnimationManagerDestroyed);
    DestroyAnimator();
  }
}

void BackForwardTransitionAnimationManagerAndroid::OnGestureStarted(
    const ui::BackGestureEvent& gesture,
    SwipeEdge edge,
    NavigationDirection navigation_direction) {
  std::optional<int> index =
      navigation_direction == NavigationDirection::kForward
          ? navigation_controller_->GetIndexForGoForward()
          : navigation_controller_->GetIndexForGoBack();
  CHECK(index.has_value());
  auto* destination_entry = navigation_controller_->GetEntryAtIndex(*index);

  CHECK(destination_entry)
      << "The embedder should only delegate the history navigation task "
         "to this manager if there is a destination entry.";

  // Each previous gesture should finished with `OnGestureCancelled()` or
  // `OnGestureInvoked()`. In both cases we reset `destination_entry_index_` to
  // -1.
  CHECK_EQ(destination_entry_index_, -1);
  destination_entry_index_ = *index;

  if (animator_) {
    // It's possible for a user to start a second gesture when the first gesture
    // animation is still on-going (aka "chained back"). For now, abort the
    // previous animation (impl's dtor will reset the layer's position and
    // reclaim all the resources).
    //
    // TODO(crbug.com/40261105): We need a proper UX to support this.
    animator_->AbortAnimation(AnimationAbortReason::kChainedBack);
    DestroyAnimator();
  }

  // Handle the case where the screenshot's dimension does not match the
  // physical viewport:
  // - TODO(https://crbug.com/346979589): Screenshot is captured in a landscape
  // / portrait mode but used for transition in the different mode.
  if (!ShouldAnimateNavigationTransition(navigation_direction, edge)) {
    TRACE_EVENT(
        "browser,navigation",
        "BackForwardTransitionAnimationManagerAndroid::OnGestureStarted");
    return;
  }

  CHECK(animator_factory_);
  animator_ = animator_factory_->Create(
      web_contents_view_android_.get(), navigation_controller_.get(), gesture,
      navigation_direction, edge, destination_entry,
      MaybeCopyContentAreaAsBitmapSync(), this);

  // Become a WCO as soon as this class is created, because we want to
  // observe all navigations while this class is controlling the UI. This
  // allows us to ensure the visuals displayed align with the active page
  // and URL in the URL bar.
  WebContentsObserver::Observe(
      this->web_contents_view_android()->web_contents());
  auto* window = web_contents_view_android()->GetTopLevelNativeWindow();
  CHECK(window);
  window->AddObserver(this);
  web_contents_view_android()->GetNativeView()->AddObserver(this);

  OnAnimationStageChanged();
}

void BackForwardTransitionAnimationManagerAndroid::OnGestureProgressed(
    const ui::BackGestureEvent& gesture) {
  if (animator_) {
    animator_->OnGestureProgressed(gesture);
  }
}

void BackForwardTransitionAnimationManagerAndroid::OnGestureCancelled() {
  CHECK_NE(destination_entry_index_, -1);
  if (animator_) {
    animator_->OnGestureCancelled();
    MaybeDestroyAnimator();
  }
  destination_entry_index_ = -1;
}

void BackForwardTransitionAnimationManagerAndroid::OnGestureInvoked() {
  CHECK_NE(destination_entry_index_, -1);
  if (animator_) {
    animator_->OnGestureInvoked();
    MaybeDestroyAnimator();
  } else {
    navigation_controller_->GoToIndex(destination_entry_index_);
  }
  destination_entry_index_ = -1;
}

void BackForwardTransitionAnimationManagerAndroid::
    OnContentForNavigationEntryShown() {
  if (animator_) {
    animator_->OnContentForNavigationEntryShown();
    MaybeDestroyAnimator();
  }
}

AnimationStage
BackForwardTransitionAnimationManagerAndroid::GetCurrentAnimationStage() {
  return animator_ ? animator_->GetCurrentAnimationStage()
                   : AnimationStage::kNone;
}

void BackForwardTransitionAnimationManagerAndroid::SetFavicon(
    const SkBitmap& favicon) {
  CHECK(NavigationTransitionConfig::AreBackForwardTransitionsEnabled());
  auto* entry = web_contents_view_android_->web_contents()
                    ->GetController()
                    .GetLastCommittedEntry();
  CHECK(entry);
  entry->navigation_transition_data().set_favicon(favicon);
}

void BackForwardTransitionAnimationManagerAndroid::OnDetachedFromWindow() {
  // The WebContentsViewAndroid's native view is detached from the top level
  // window. We must abort the transition.
  CHECK(animator_);
  animator_->AbortAnimation(AnimationAbortReason::kDetachedFromWindow);
  DestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::
    OnRootWindowVisibilityChanged(bool visible) {
  CHECK(animator_);
  if (!visible) {
    animator_->AbortAnimation(
        AnimationAbortReason::kRootWindowVisibilityChanged);
    DestroyAnimator();
  }
}

void BackForwardTransitionAnimationManagerAndroid::OnDetachCompositor() {
  CHECK(animator_);
  animator_->AbortAnimation(AnimationAbortReason::kCompositorDetached);
  DestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::OnAnimate(
    base::TimeTicks frame_begin_time) {
  animator_->OnAnimate(frame_begin_time);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::RenderWidgetHostDestroyed(
    RenderWidgetHost* widget_host) {
  animator_->OnRenderWidgetHostDestroyed(widget_host);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::
    OnRenderFrameMetadataChangedAfterActivation(
        base::TimeTicks activation_time) {
  animator_->OnRenderFrameMetadataChangedAfterActivation(activation_time);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::DidStartNavigation(
    NavigationHandle* navigation_handle) {
  animator_->DidStartNavigation(navigation_handle);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::ReadyToCommitNavigation(
    NavigationHandle* navigation_handle) {
  animator_->ReadyToCommitNavigation(navigation_handle);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::DidFinishNavigation(
    NavigationHandle* navigation_handle) {
  animator_->DidFinishNavigation(navigation_handle);
  MaybeDestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::
    OnDidNavigatePrimaryMainFramePreCommit(
        NavigationRequest* navigation_request,
        RenderFrameHostImpl* old_host,
        RenderFrameHostImpl* new_host) {
  if (animator_) {
    animator_->OnDidNavigatePrimaryMainFramePreCommit(navigation_request,
                                                      old_host, new_host);
    MaybeDestroyAnimator();
  }
}

void BackForwardTransitionAnimationManagerAndroid::
    OnNavigationCancelledBeforeStart(NavigationHandle* navigation_handle) {
  if (animator_) {
    animator_->OnNavigationCancelledBeforeStart(navigation_handle);
    MaybeDestroyAnimator();
  }
}

void BackForwardTransitionAnimationManagerAndroid::OnAnimationStageChanged() {
  if (auto* delegate =
          web_contents_view_android()->web_contents()->GetDelegate()) {
    delegate->DidBackForwardTransitionAnimationChange();
  }
}

void BackForwardTransitionAnimationManagerAndroid::
    OnPostNavigationFirstFrameTimeout() {
  CHECK(animator_);
  CHECK(animator_->IsTerminalState());
  DestroyAnimator();
}

void BackForwardTransitionAnimationManagerAndroid::
    OnPhysicalBackingSizeChanged() {
  if (!animator_) {
    return;
  }
  animator_->AbortAnimation(AnimationAbortReason::kPhysicalSizeChanged);
  DestroyAnimator();
}

SkBitmap BackForwardTransitionAnimationManagerAndroid::
    MaybeCopyContentAreaAsBitmapSync() {
  return web_contents_view_android()
      ->web_contents()
      ->GetDelegate()
      ->MaybeCopyContentAreaAsBitmapSync();
}

void BackForwardTransitionAnimationManagerAndroid::MaybeRecordIgnoredInput(
    const blink::WebInputEvent& event) {
  if (animator_) {
    animator_->MaybeRecordIgnoredInput(event);
  }
}

void BackForwardTransitionAnimationManagerAndroid::MaybeDestroyAnimator() {
  CHECK(animator_);
  if (animator_->IsTerminalState()) {
    DestroyAnimator();
  }
}

void BackForwardTransitionAnimationManagerAndroid::DestroyAnimator() {
  CHECK(animator_);
  WebContentsObserver::Observe(nullptr);
  auto* window = web_contents_view_android()->GetTopLevelNativeWindow();
  CHECK(window);
  window->RemoveObserver(this);
  web_contents_view_android()->GetNativeView()->RemoveObserver(this);
  animator_.reset();
  OnAnimationStageChanged();
}

}  // namespace content