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