summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/web_contents_modal_dialog_manager.cc
blob: ab243b395d6c29b1e667d18b973c7cfe54859a00 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/web_contents_modal_dialog_manager.h"

#include "chrome/browser/ui/web_contents_modal_dialog_manager_delegate.h"
#include "chrome/common/render_messages.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"

using content::WebContents;

DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsModalDialogManager);

WebContentsModalDialogManager::~WebContentsModalDialogManager() {
  DCHECK(child_dialogs_.empty());
}

void WebContentsModalDialogManager::ShowDialog(
    NativeWebContentsModalDialog dialog) {
  child_dialogs_.push_back(dialog);

  native_manager_->ManageDialog(dialog);

  if (child_dialogs_.size() == 1) {
    native_manager_->ShowDialog(dialog);
    BlockWebContentsInteraction(true);
  }
}

void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) {
  WebContents* contents = web_contents();
  if (!contents) {
    // The WebContents has already disconnected.
    return;
  }

  // RenderViewHost may be NULL during shutdown.
  content::RenderViewHost* host = contents->GetRenderViewHost();
  if (host) {
    host->SetIgnoreInputEvents(blocked);
    host->Send(new ChromeViewMsg_SetVisuallyDeemphasized(
        host->GetRoutingID(), blocked));
  }
  if (delegate_)
    delegate_->SetWebContentsBlocked(contents, blocked);
}

bool WebContentsModalDialogManager::IsShowingDialog() const {
  return dialog_count() > 0;
}

void WebContentsModalDialogManager::FocusTopmostDialog() {
  DCHECK(dialog_count());
  native_manager_->FocusDialog(*dialog_begin());
}

content::WebContents* WebContentsModalDialogManager::GetWebContents() const {
  return web_contents();
}

void WebContentsModalDialogManager::WillClose(
    NativeWebContentsModalDialog dialog) {
  WebContentsModalDialogList::iterator i(
      std::find(child_dialogs_.begin(),
                child_dialogs_.end(),
                dialog));
  bool removed_topmost_dialog = i == child_dialogs_.begin();
  if (i != child_dialogs_.end())
    child_dialogs_.erase(i);
  if (child_dialogs_.empty()) {
    BlockWebContentsInteraction(false);
  } else {
    if (removed_topmost_dialog && !closing_all_dialogs_)
      native_manager_->ShowDialog(child_dialogs_[0]);
    BlockWebContentsInteraction(true);
  }
}

void WebContentsModalDialogManager::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED);

  if (child_dialogs_.empty())
    return;

  bool visible = *content::Details<bool>(details).ptr();
  if (visible)
    native_manager_->ShowDialog(child_dialogs_[0]);
  else
    native_manager_->HideDialog(child_dialogs_[0]);
}

WebContentsModalDialogManager::WebContentsModalDialogManager(
    content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      delegate_(NULL),
      native_manager_(
          ALLOW_THIS_IN_INITIALIZER_LIST(CreateNativeManager(this))),
      closing_all_dialogs_(false) {
  DCHECK(native_manager_);
  registrar_.Add(this,
                 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
                 content::Source<content::WebContents>(web_contents));
}

void WebContentsModalDialogManager::CloseAllDialogs() {
  closing_all_dialogs_ = true;

  // Clear out any dialogs since we are leaving this page entirely.  To ensure
  // that we iterate over every element in child_dialogs_ we need to use a copy
  // of child_dialogs_. Otherwise if closing a dialog causes child_dialogs_ to
  // be modified, we could end up skipping some elements.
  WebContentsModalDialogList child_dialogs_copy(child_dialogs_);
  for (WebContentsModalDialogList::iterator it = child_dialogs_copy.begin();
       it != child_dialogs_copy.end(); ++it) {
    native_manager_->CloseDialog(*it);
    BlockWebContentsInteraction(false);
  }

  closing_all_dialogs_ = false;
}

void WebContentsModalDialogManager::DidNavigateMainFrame(
    const content::LoadCommittedDetails& details,
    const content::FrameNavigateParams& params) {
  // Close constrained windows if necessary.
  if (!net::RegistryControlledDomainService::SameDomainOrHost(
          details.previous_url, details.entry->GetURL()))
    CloseAllDialogs();
}

void WebContentsModalDialogManager::DidGetIgnoredUIEvent() {
  if (dialog_count())
    native_manager_->FocusDialog(*dialog_begin());
}

void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) {
  // First cleanly close all child dialogs.
  // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
  // some of these to close.  CloseAllDialogs is async, so it might get called
  // twice before it runs.
  CloseAllDialogs();
}