app_modal_dialog_queue.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2012 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 "components/javascript_dialogs/app_modal_dialog_queue.h"
  5. #include "base/memory/singleton.h"
  6. #include "components/javascript_dialogs/app_modal_dialog_controller.h"
  7. namespace javascript_dialogs {
  8. // static
  9. AppModalDialogQueue* AppModalDialogQueue::GetInstance() {
  10. return base::Singleton<AppModalDialogQueue>::get();
  11. }
  12. void AppModalDialogQueue::AddDialog(AppModalDialogController* dialog) {
  13. if (!active_dialog_) {
  14. ShowModalDialog(dialog);
  15. return;
  16. }
  17. app_modal_dialog_queue_.push_back(dialog);
  18. }
  19. void AppModalDialogQueue::ShowNextDialog() {
  20. AppModalDialogController* dialog = GetNextDialog();
  21. if (dialog)
  22. ShowModalDialog(dialog);
  23. else
  24. active_dialog_ = nullptr;
  25. }
  26. void AppModalDialogQueue::ActivateModalDialog() {
  27. if (showing_modal_dialog_) {
  28. // As part of showing a modal dialog we may end up back in this method
  29. // (showing a dialog activates the WebContents, which can trigger a call
  30. // to ActivateModalDialog). We ignore such a request as after the call to
  31. // activate the tab contents the dialog is shown.
  32. return;
  33. }
  34. if (active_dialog_)
  35. active_dialog_->ActivateModalDialog();
  36. }
  37. bool AppModalDialogQueue::HasActiveDialog() const {
  38. return active_dialog_ != nullptr;
  39. }
  40. AppModalDialogQueue::AppModalDialogQueue()
  41. : active_dialog_(nullptr), showing_modal_dialog_(false) {}
  42. AppModalDialogQueue::~AppModalDialogQueue() {}
  43. void AppModalDialogQueue::ShowModalDialog(AppModalDialogController* dialog) {
  44. // Be sure and set the active_dialog_ field first, otherwise if
  45. // ShowModalDialog triggers a call back to the queue they'll get the old
  46. // dialog. Also, if the dialog calls |ShowNextDialog()| before returning, that
  47. // would write nullptr into |active_dialog_| and this function would then undo
  48. // that.
  49. active_dialog_ = dialog;
  50. showing_modal_dialog_ = true;
  51. dialog->ShowModalDialog();
  52. showing_modal_dialog_ = false;
  53. }
  54. AppModalDialogController* AppModalDialogQueue::GetNextDialog() {
  55. while (!app_modal_dialog_queue_.empty()) {
  56. AppModalDialogController* dialog = app_modal_dialog_queue_.front();
  57. app_modal_dialog_queue_.pop_front();
  58. if (dialog->IsValid())
  59. return dialog;
  60. delete dialog;
  61. }
  62. return nullptr;
  63. }
  64. } // namespace javascript_dialogs