blob: d78fcc91554a6a9368c2613abbb3eb1e4f81041c [file] [log] [blame]
Joel Hockey18f767a2024-08-20 03:23:441// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
William Liu7e940dd2024-09-12 13:16:445#ifndef UI_ANDROID_MODAL_DIALOG_WRAPPER_H_
6#define UI_ANDROID_MODAL_DIALOG_WRAPPER_H_
Joel Hockey18f767a2024-08-20 03:23:447
8#include <memory>
9
10#include "base/android/jni_string.h"
11#include "base/android/scoped_java_ref.h"
William Liu7e940dd2024-09-12 13:16:4412#include "base/gtest_prod_util.h"
13#include "base/memory/raw_ptr.h"
Joel Hockey18f767a2024-08-20 03:23:4414#include "ui/android/ui_android_export.h"
Jack Thiesen2a63a2e12025-07-01 15:30:4615#include "ui/base/interaction/element_identifier.h"
16#include "ui/base/models/dialog_model_field.h"
Joel Hockey18f767a2024-08-20 03:23:4417#include "ui/base/models/dialog_model_host.h"
18
19namespace ui {
20class DialogModel;
21class WindowAndroid;
22} // namespace ui
23
24namespace ui {
25
26// Allows dialogs defined by ui::DialogModel() to be shown in android.
27// This makes a best effort to map between ui::DialogModel() and Java
28// PropertyModel.
29//
30// Maps title, an optional single paragraph, and ok and cancel buttons.
31// Default labels for IDS_APP_OK and IDS_APP_CANCEL will be used for buttons
32// if labels are not specified.
33// Replacements (if any) are performed in paragraph text, but any emphasis is
34// not included since it is not supported in android dialogs.
Jack Thiesen2a63a2e12025-07-01 15:30:4635class UI_ANDROID_EXPORT ModalDialogWrapper : public DialogModelHost,
36 public DialogModelFieldHost {
Joel Hockey18f767a2024-08-20 03:23:4437 public:
Charles Hagerde93b2e52025-01-29 23:20:1838 // Mirrors Java's `ModalDialogProperties#ButtonStyles`.
39 // TODO(crbug.com/392977703): IntDef that enum in C++.
40 enum class ModalDialogButtonStyles {
41 kPrimaryOutlineNegativeOutline = 0,
42 kPrimaryFilledNegativeOutline = 1,
43 kPrimaryOutlineNegativeFilled = 2,
44 kPrimaryFilledNoNegative = 3,
45 };
46
Joel Hockey18f767a2024-08-20 03:23:4447 static void ShowTabModal(std::unique_ptr<ui::DialogModel> dialog_model,
William Liu7e940dd2024-09-12 13:16:4448 ui::WindowAndroid* window);
Joel Hockey18f767a2024-08-20 03:23:4449
William Liu7e940dd2024-09-12 13:16:4450 static ModalDialogWrapper* GetDialogForTesting();
51
52 ModalDialogWrapper(const ModalDialogWrapper&) = delete;
53 ModalDialogWrapper& operator=(const ModalDialogWrapper&) = delete;
54 virtual ~ModalDialogWrapper();
Joel Hockey18f767a2024-08-20 03:23:4455
56 // JNI methods.
57 void PositiveButtonClicked(JNIEnv* env);
58 void NegativeButtonClicked(JNIEnv* env);
Jack Thiesen2a63a2e12025-07-01 15:30:4659 void CheckboxToggled(JNIEnv* env, jboolean is_checked);
Joel Hockey18f767a2024-08-20 03:23:4460 void Dismissed(JNIEnv* env);
61 void Destroy(JNIEnv* env);
62
63 private:
William Liu7e940dd2024-09-12 13:16:4464 FRIEND_TEST_ALL_PREFIXES(ModalDialogWrapperTest, CloseDialogFromNative);
65
66 ModalDialogWrapper(std::unique_ptr<ui::DialogModel> dialog_model,
67 ui::WindowAndroid* window_android);
68
69 // `DialogModelHost`:
Joel Hockey18f767a2024-08-20 03:23:4470 void Close() override;
71 void OnDialogButtonChanged() override;
72
Jack Thiesenddbb446b2025-06-11 17:18:5973 // Helper function for BuildPropertyModel.
74 ModalDialogButtonStyles GetButtonStyles() const;
75
Joel Hockey18f767a2024-08-20 03:23:4476 // Build java PropertyModel from ui::DialogModel.
77 void BuildPropertyModel();
78
William Liu7e940dd2024-09-12 13:16:4479 const std::unique_ptr<ui::DialogModel> dialog_model_;
Joel Hockey18f767a2024-08-20 03:23:4480
Jack Thiesen2a63a2e12025-07-01 15:30:4681 ElementIdentifier checkbox_id_;
82
William Liu7e940dd2024-09-12 13:16:4483 const raw_ptr<WindowAndroid> window_android_;
Joel Hockey18f767a2024-08-20 03:23:4484
Joel Hockey18f767a2024-08-20 03:23:4485 base::android::ScopedJavaGlobalRef<jobject> java_obj_;
86};
87
88} // namespace ui
89
William Liu7e940dd2024-09-12 13:16:4490#endif // UI_ANDROID_MODAL_DIALOG_WRAPPER_H_