deferred_update_dialog.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/unified/deferred_update_dialog.h"
  5. #include "ash/strings/grit/ash_strings.h"
  6. #include "chromeos/ash/components/dbus/update_engine/update_engine_client.h"
  7. #include "ui/base/l10n/l10n_util.h"
  8. #include "ui/chromeos/resources/grit/ui_chromeos_resources.h"
  9. #include "ui/views/bubble/bubble_dialog_model_host.h"
  10. #include "ui/views/window/dialog_delegate.h"
  11. namespace ash {
  12. DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(DeferredUpdateDialog,
  13. kAutoUpdateCheckboxId);
  14. DeferredUpdateDialog* DeferredUpdateDialog::dialog_ = nullptr;
  15. // static
  16. void DeferredUpdateDialog::CreateDialog(Action callback_action,
  17. base::OnceClosure callback) {
  18. // Avoid duplicate dialogs.
  19. if (dialog_)
  20. return;
  21. // dialog_ will be released when the dialog is closed.
  22. dialog_ = new DeferredUpdateDialog();
  23. auto ok_text = callback_action == kSignOut
  24. ? IDS_DEFERRED_UPDATE_DIALOG_UPDATE_SIGN_OUT
  25. : IDS_DEFERRED_UPDATE_DIALOG_UPDATE_SHUT_DOWN;
  26. auto cancel_text = callback_action == kSignOut
  27. ? IDS_DEFERRED_UPDATE_DIALOG_SIGN_OUT
  28. : IDS_DEFERRED_UPDATE_DIALOG_SHUT_DOWN;
  29. std::unique_ptr<ui::DialogModel> dialog_model =
  30. ui::DialogModel::Builder(std::make_unique<ui::DialogModelDelegate>())
  31. .SetTitle(l10n_util::GetStringUTF16(IDS_DEFERRED_UPDATE_DIALOG_TITLE))
  32. .AddOkButton(
  33. base::BindOnce(&DeferredUpdateDialog::OnApplyDeferredUpdate,
  34. base::Unretained(dialog_)),
  35. l10n_util::GetStringUTF16(ok_text))
  36. .AddCancelButton(
  37. base::BindOnce(&DeferredUpdateDialog::OnContinueWithoutUpdate,
  38. base::Unretained(dialog_)),
  39. l10n_util::GetStringUTF16(cancel_text))
  40. .AddBodyText(ui::DialogModelLabel(
  41. l10n_util::GetStringUTF16(IDS_DEFERRED_UPDATE_DIALOG_TEXT)))
  42. .AddCheckbox(kAutoUpdateCheckboxId,
  43. ui::DialogModelLabel(l10n_util::GetStringUTF16(
  44. IDS_DEFERRED_UPDATE_DIALOG_CHECKBOX)))
  45. .SetDialogDestroyingCallback(
  46. base::BindOnce(&DeferredUpdateDialog::OnDialogClosing,
  47. base::Unretained(dialog_), std::move(callback)))
  48. .Build();
  49. dialog_->dialog_model_ = dialog_model.get();
  50. dialog_->dialog_result_ = kClose;
  51. auto bubble = views::BubbleDialogModelHost::CreateModal(
  52. std::move(dialog_model), ui::MODAL_TYPE_SYSTEM);
  53. bubble->SetOwnedByWidget(true);
  54. views::DialogDelegate::CreateDialogWidget(std::move(bubble),
  55. /*context=*/nullptr,
  56. /*parent=*/nullptr)
  57. ->Show();
  58. }
  59. // Invoked when "ok" button is clicked.
  60. void DeferredUpdateDialog::OnApplyDeferredUpdate() {
  61. DCHECK(dialog_model_);
  62. ui::DialogModelCheckbox* check_box =
  63. dialog_model_->GetCheckboxByUniqueId(kAutoUpdateCheckboxId);
  64. if (check_box && check_box->is_checked()) {
  65. dialog_result_ = kApplyAutoUpdate;
  66. } else {
  67. dialog_result_ = kApplyUpdate;
  68. }
  69. }
  70. // Invoked when "cancel" button is clicked.
  71. void DeferredUpdateDialog::OnContinueWithoutUpdate() {
  72. dialog_result_ = kIgnoreUpdate;
  73. }
  74. // Invoked when the dialog is closing.
  75. void DeferredUpdateDialog::OnDialogClosing(base::OnceClosure callback) {
  76. dialog_model_ = nullptr;
  77. switch (dialog_result_) {
  78. case kApplyAutoUpdate:
  79. UpdateEngineClient::Get()->ToggleFeature(
  80. update_engine::kFeatureConsumerAutoUpdate,
  81. /*enable=*/true);
  82. [[fallthrough]];
  83. case kApplyUpdate:
  84. UpdateEngineClient::Get()->ApplyDeferredUpdate(std::move(callback));
  85. break;
  86. case kIgnoreUpdate:
  87. std::move(callback).Run();
  88. break;
  89. case kClose:
  90. break;
  91. }
  92. delete this;
  93. dialog_ = nullptr;
  94. }
  95. } // namespace ash