OLD | NEW |
| (Empty) |
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/web_contents_modal_dialog_manager.h" | |
6 | |
7 #include "chrome/browser/platform_util.h" | |
8 #include "chrome/browser/ui/web_contents_modal_dialog_manager_delegate.h" | |
9 #include "chrome/common/render_messages.h" | |
10 #include "content/public/browser/navigation_details.h" | |
11 #include "content/public/browser/navigation_entry.h" | |
12 #include "content/public/browser/notification_details.h" | |
13 #include "content/public/browser/notification_source.h" | |
14 #include "content/public/browser/notification_types.h" | |
15 #include "content/public/browser/render_view_host.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/browser/web_contents_view.h" | |
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
19 | |
20 using content::WebContents; | |
21 | |
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsModalDialogManager); | |
23 | |
24 WebContentsModalDialogManager::~WebContentsModalDialogManager() { | |
25 DCHECK(child_dialogs_.empty()); | |
26 } | |
27 | |
28 void WebContentsModalDialogManager::ShowDialog( | |
29 NativeWebContentsModalDialog dialog) { | |
30 child_dialogs_.push_back(dialog); | |
31 | |
32 native_manager_->ManageDialog(dialog); | |
33 | |
34 if (child_dialogs_.size() == 1) { | |
35 if (IsWebContentsVisible()) | |
36 native_manager_->ShowDialog(dialog); | |
37 BlockWebContentsInteraction(true); | |
38 } | |
39 } | |
40 | |
41 bool WebContentsModalDialogManager::IsShowingDialog() const { | |
42 return !child_dialogs_.empty(); | |
43 } | |
44 | |
45 void WebContentsModalDialogManager::FocusTopmostDialog() { | |
46 DCHECK(!child_dialogs_.empty()); | |
47 native_manager_->FocusDialog(child_dialogs_.front()); | |
48 } | |
49 | |
50 content::WebContents* WebContentsModalDialogManager::GetWebContents() const { | |
51 return web_contents(); | |
52 } | |
53 | |
54 void WebContentsModalDialogManager::WillClose( | |
55 NativeWebContentsModalDialog dialog) { | |
56 WebContentsModalDialogList::iterator i( | |
57 std::find(child_dialogs_.begin(), child_dialogs_.end(), dialog)); | |
58 | |
59 // The Views tab contents modal dialog calls WillClose twice. Ignore the | |
60 // second invocation. | |
61 if (i == child_dialogs_.end()) | |
62 return; | |
63 | |
64 bool removed_topmost_dialog = i == child_dialogs_.begin(); | |
65 child_dialogs_.erase(i); | |
66 if (!child_dialogs_.empty() && removed_topmost_dialog && | |
67 !closing_all_dialogs_) | |
68 native_manager_->ShowDialog(child_dialogs_.front()); | |
69 | |
70 BlockWebContentsInteraction(!child_dialogs_.empty()); | |
71 } | |
72 | |
73 void WebContentsModalDialogManager::Observe( | |
74 int type, | |
75 const content::NotificationSource& source, | |
76 const content::NotificationDetails& details) { | |
77 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED); | |
78 | |
79 if (child_dialogs_.empty()) | |
80 return; | |
81 | |
82 bool visible = *content::Details<bool>(details).ptr(); | |
83 if (visible) | |
84 native_manager_->ShowDialog(child_dialogs_.front()); | |
85 else | |
86 native_manager_->HideDialog(child_dialogs_.front()); | |
87 } | |
88 | |
89 WebContentsModalDialogManager::WebContentsModalDialogManager( | |
90 content::WebContents* web_contents) | |
91 : content::WebContentsObserver(web_contents), | |
92 delegate_(NULL), | |
93 native_manager_(CreateNativeManager(this)), | |
94 closing_all_dialogs_(false) { | |
95 DCHECK(native_manager_); | |
96 registrar_.Add(this, | |
97 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
98 content::Source<content::WebContents>(web_contents)); | |
99 } | |
100 | |
101 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) { | |
102 WebContents* contents = web_contents(); | |
103 if (!contents) { | |
104 // The WebContents has already disconnected. | |
105 return; | |
106 } | |
107 | |
108 // RenderViewHost may be NULL during shutdown. | |
109 content::RenderViewHost* host = contents->GetRenderViewHost(); | |
110 if (host) { | |
111 host->SetIgnoreInputEvents(blocked); | |
112 host->Send(new ChromeViewMsg_SetVisuallyDeemphasized( | |
113 host->GetRoutingID(), blocked)); | |
114 } | |
115 if (delegate_) | |
116 delegate_->SetWebContentsBlocked(contents, blocked); | |
117 } | |
118 | |
119 bool WebContentsModalDialogManager::IsWebContentsVisible() const { | |
120 return platform_util::IsVisible(web_contents()->GetView()->GetNativeView()); | |
121 } | |
122 | |
123 void WebContentsModalDialogManager::CloseAllDialogs() { | |
124 closing_all_dialogs_ = true; | |
125 | |
126 // Clear out any dialogs since we are leaving this page entirely. | |
127 while (!child_dialogs_.empty()) | |
128 native_manager_->CloseDialog(child_dialogs_.front()); | |
129 | |
130 closing_all_dialogs_ = false; | |
131 } | |
132 | |
133 void WebContentsModalDialogManager::DidNavigateMainFrame( | |
134 const content::LoadCommittedDetails& details, | |
135 const content::FrameNavigateParams& params) { | |
136 // Close constrained windows if necessary. | |
137 if (!net::registry_controlled_domains::SameDomainOrHost( | |
138 details.previous_url, details.entry->GetURL(), | |
139 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) | |
140 CloseAllDialogs(); | |
141 } | |
142 | |
143 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() { | |
144 if (!child_dialogs_.empty()) | |
145 native_manager_->FocusDialog(child_dialogs_.front()); | |
146 } | |
147 | |
148 void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) { | |
149 // First cleanly close all child dialogs. | |
150 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked | |
151 // some of these to close. CloseAllDialogs is async, so it might get called | |
152 // twice before it runs. | |
153 CloseAllDialogs(); | |
154 } | |
OLD | NEW |