File: pwa_bottom_sheet_controller.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (209 lines) | stat: -rw-r--r-- 8,367 bytes parent folder | download | duplicates (9)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webapps/browser/android/bottomsheet/pwa_bottom_sheet_controller.h"

#include <string>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/url_formatter/elide_url.h"
#include "components/webapps/browser/android/app_banner_manager_android.h"
#include "components/webapps/browser/banners/install_banner_config.h"
#include "components/webapps/browser/installable/installable_data.h"
#include "components/webapps/browser/webapps_client.h"
#include "components/webapps/common/constants.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/text_elider.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/webapps/browser/android/webapps_jni_headers/PwaBottomSheetControllerProvider_jni.h"
#include "components/webapps/browser/android/webapps_jni_headers/PwaBottomSheetController_jni.h"

using base::ASCIIToUTF16;
using base::android::ConvertUTF16ToJavaString;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;

namespace {

bool CanShowBottomSheet(content::WebContents* web_contents,
                        const std::vector<webapps::Screenshot>& screenshots) {
  if (screenshots.size() == 0)
    return false;

  JNIEnv* env = base::android::AttachCurrentThread();
  return Java_PwaBottomSheetControllerProvider_canShowPwaBottomSheetInstaller(
      env, web_contents->GetJavaWebContents());
}

}  // anonymous namespace

namespace webapps {

PwaBottomSheetController::~PwaBottomSheetController() = default;

// static
jboolean JNI_PwaBottomSheetController_RequestOrExpandBottomSheetInstaller(
    JNIEnv* env,
    const JavaParamRef<jobject>& jweb_contents,
    int install_trigger) {
  content::WebContents* web_contents =
      content::WebContents::FromJavaWebContents(jweb_contents);
  auto* app_banner_manager = static_cast<AppBannerManagerAndroid*>(
      WebappsClient::Get()->GetAppBannerManager(web_contents));

  std::optional<InstallBannerConfig> install_config =
      app_banner_manager->GetCurrentBannerConfig();
  if (!install_config.has_value()) {
    return false;
  }

  WebappInstallSource install_source = InstallableMetrics::GetInstallSource(
      web_contents, static_cast<InstallTrigger>(install_trigger));
  return app_banner_manager->MaybeShowPwaBottomSheetController(
      /* expand_sheet= */ true, install_source, install_config.value());
}

// static
bool PwaBottomSheetController::MaybeShow(
    content::WebContents* web_contents,
    const WebAppBannerData& web_app_banner_data,
    bool expand_sheet,
    base::RepeatingCallback<void(AddToHomescreenInstaller::Event,
                                 const AddToHomescreenParams&)>
        a2hs_event_callback,
    std::unique_ptr<AddToHomescreenParams> a2hs_params) {
  if (!CanShowBottomSheet(web_contents, web_app_banner_data.screenshots)) {
    return false;
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  if (Java_PwaBottomSheetControllerProvider_doesBottomSheetExist(
          env, web_contents->GetJavaWebContents())) {
    Java_PwaBottomSheetControllerProvider_updateState(
        env, web_contents->GetJavaWebContents(),
        jint(a2hs_params->install_source), expand_sheet);
  } else {
    // Lifetime of this object is managed by the Java counterpart, iff bottom
    // sheets can be shown (otherwise an infobar is used and this class is no
    // longer needed).
    PwaBottomSheetController* controller = new PwaBottomSheetController(
        web_app_banner_data, std::move(a2hs_params),
        std::move(a2hs_event_callback));
    controller->ShowBottomSheetInstaller(web_contents, expand_sheet);
  }
  return true;
}

PwaBottomSheetController::PwaBottomSheetController(
    const WebAppBannerData& web_app_banner_data,
    std::unique_ptr<AddToHomescreenParams> a2hs_params,
    base::RepeatingCallback<void(AddToHomescreenInstaller::Event,
                                 const AddToHomescreenParams&)>
        a2hs_event_callback)
    : web_app_banner_data_(web_app_banner_data),
      a2hs_params_(std::move(a2hs_params)),
      a2hs_event_callback_(a2hs_event_callback) {}

void PwaBottomSheetController::Destroy(JNIEnv* env) {
  // When the bottom sheet hasn't been expanded, it is considered equivalent to
  // the regular install infobar and the expanded state equivalent
  // to the regular install dialog prompt. Therefore, we send UI_CANCELLED
  // only if the bottom sheet was ever expanded and not closed.
  if (!install_triggered_ && !sheet_closed_ && sheet_expanded_) {
    a2hs_event_callback_.Run(AddToHomescreenInstaller::Event::UI_CANCELLED,
                             *a2hs_params_);
  }
  delete this;
}

void PwaBottomSheetController::UpdateInstallSource(JNIEnv* env,
                                                   int install_source) {
  a2hs_params_->install_source =
      static_cast<WebappInstallSource>(install_source);
}

void PwaBottomSheetController::OnSheetClosedWithSwipe(JNIEnv* env) {
  a2hs_event_callback_.Run(AddToHomescreenInstaller::Event::UI_CANCELLED,
                           *a2hs_params_);
  sheet_closed_ = true;
}

void PwaBottomSheetController::OnSheetExpanded(JNIEnv* env) {
  a2hs_event_callback_.Run(AddToHomescreenInstaller::Event::UI_SHOWN,
                           *a2hs_params_);
  sheet_expanded_ = true;
}

void PwaBottomSheetController::OnAddToHomescreen(
    JNIEnv* env,
    const JavaParamRef<jobject>& jweb_contents) {
  content::WebContents* web_contents =
      content::WebContents::FromJavaWebContents(jweb_contents);
  if (!web_contents)
    return;

  install_triggered_ = true;
  AddToHomescreenInstaller::Install(web_contents, *a2hs_params_,
                                    std::move(a2hs_event_callback_));
  PwaInstallPathTracker::TrackInstallPath(/* bottom_sheet= */ true,
                                          a2hs_params_->install_source);
}

void PwaBottomSheetController::ShowBottomSheetInstaller(
    content::WebContents* web_contents,
    bool expand_sheet) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jstring> j_user_title =
      ConvertUTF16ToJavaString(env, web_app_banner_data_.GetAppName());
  // Trim down the app URL to the origin. Elide cryptographic schemes so HTTP
  // is still shown.
  ScopedJavaLocalRef<jstring> j_url = ConvertUTF16ToJavaString(
      env, url_formatter::FormatUrlForSecurityDisplay(
               web_app_banner_data_.manifest().start_url,
               url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC));

  std::u16string elided_description = gfx::TruncateString(
      web_app_banner_data_.manifest().description.value_or(u""),
      webapps::kMaximumDescriptionLength, gfx::CHARACTER_BREAK);
  ScopedJavaLocalRef<jstring> j_description =
      ConvertUTF16ToJavaString(env, elided_description);

  ScopedJavaLocalRef<jobject> j_bitmap =
      gfx::ConvertToJavaBitmap(web_app_banner_data_.primary_icon);

  Java_PwaBottomSheetControllerProvider_showPwaBottomSheetInstaller(
      env, reinterpret_cast<intptr_t>(this), web_contents->GetJavaWebContents(),
      j_bitmap, web_app_banner_data_.has_maskable_primary_icon, j_user_title,
      j_url, j_description);

  for (const auto& screenshot : web_app_banner_data_.screenshots) {
    if (!screenshot.image.isNull())
      UpdateScreenshot(screenshot.image, web_contents);
  }

  if (expand_sheet) {
    Java_PwaBottomSheetControllerProvider_expandPwaBottomSheetInstaller(
        env, web_contents->GetJavaWebContents());
  }
}

void PwaBottomSheetController::UpdateScreenshot(
    const SkBitmap& screenshot,
    content::WebContents* web_contents) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> java_screenshot =
      gfx::ConvertToJavaBitmap(screenshot);
  // TODO(crbug.com/40870351): support passing label to use as
  // the accessibility string.
  Java_PwaBottomSheetController_addWebAppScreenshot(
      env, java_screenshot, web_contents->GetJavaWebContents());
}

}  // namespace webapps