OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 5 #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" |
6 | 6 |
7 #include "chrome/browser/ui/constrained_window.h" | 7 #include "chrome/browser/ui/web_contents_modal_dialog.h" |
8 #include "chrome/browser/ui/constrained_window_tab_helper_delegate.h" | 8 #include "chrome/browser/ui/web_contents_modal_dialog_manager_delegate.h" |
9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "content/public/browser/navigation_details.h" | 10 #include "content/public/browser/navigation_details.h" |
11 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
12 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
13 #include "content/public/browser/render_widget_host_view.h" | 13 #include "content/public/browser/render_widget_host_view.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
16 | 16 |
17 using content::WebContents; | 17 using content::WebContents; |
18 | 18 |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ConstrainedWindowTabHelper) | 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsModalDialogManager) |
20 | 20 |
21 ConstrainedWindowTabHelper::ConstrainedWindowTabHelper( | 21 WebContentsModalDialogManager::WebContentsModalDialogManager( |
22 content::WebContents* web_contents) | 22 content::WebContents* web_contents) |
23 : content::WebContentsObserver(web_contents), | 23 : content::WebContentsObserver(web_contents), |
24 delegate_(NULL) { | 24 delegate_(NULL) { |
25 } | 25 } |
26 | 26 |
27 ConstrainedWindowTabHelper::~ConstrainedWindowTabHelper() { | 27 WebContentsModalDialogManager::~WebContentsModalDialogManager() { |
28 DCHECK(child_dialogs_.empty()); | 28 DCHECK(child_dialogs_.empty()); |
29 } | 29 } |
30 | 30 |
31 void ConstrainedWindowTabHelper::AddDialog( | 31 void WebContentsModalDialogManager::AddDialog( |
32 ConstrainedWindow* window) { | 32 WebContentsModalDialog* dialog) { |
33 child_dialogs_.push_back(window); | 33 child_dialogs_.push_back(dialog); |
34 | 34 |
35 if (child_dialogs_.size() == 1 && window->CanShowWebContentsModalDialog()) { | 35 if (child_dialogs_.size() == 1 && dialog->CanShowWebContentsModalDialog()) { |
36 window->ShowWebContentsModalDialog(); | 36 dialog->ShowWebContentsModalDialog(); |
37 BlockWebContentsInteraction(true); | 37 BlockWebContentsInteraction(true); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 void ConstrainedWindowTabHelper::CloseAllDialogs() { | 41 void WebContentsModalDialogManager::CloseAllDialogs() { |
42 // Clear out any web contents modal dialogs since we are leaving this page | 42 // Clear out any dialogs since we are leaving this page entirely. To ensure |
43 // entirely. To ensure that we iterate over every element in child_dialogs_ | 43 // that we iterate over every element in child_dialogs_ we need to use a copy |
44 // we need to use a copy of child_dialogs_. Otherwise if | 44 // of child_dialogs_. Otherwise if dialog->CloseWebContentsModalDialog() |
45 // window->CloseWebContentsModalDialog() modifies child_dialogs_ we could end | 45 // modifies child_dialogs_ we could end up skipping some elements. |
46 // up skipping some elements. | |
47 WebContentsModalDialogList child_dialogs_copy(child_dialogs_); | 46 WebContentsModalDialogList child_dialogs_copy(child_dialogs_); |
48 for (WebContentsModalDialogList::iterator it = child_dialogs_copy.begin(); | 47 for (WebContentsModalDialogList::iterator it = child_dialogs_copy.begin(); |
49 it != child_dialogs_copy.end(); ++it) { | 48 it != child_dialogs_copy.end(); ++it) { |
50 ConstrainedWindow* window = *it; | 49 WebContentsModalDialog* dialog = *it; |
51 if (window) { | 50 if (dialog) { |
52 window->CloseWebContentsModalDialog(); | 51 dialog->CloseWebContentsModalDialog(); |
53 BlockWebContentsInteraction(false); | 52 BlockWebContentsInteraction(false); |
54 } | 53 } |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 void ConstrainedWindowTabHelper::WillClose(ConstrainedWindow* window) { | 57 void WebContentsModalDialogManager::WillClose(WebContentsModalDialog* dialog) { |
59 WebContentsModalDialogList::iterator i( | 58 WebContentsModalDialogList::iterator i( |
60 std::find(child_dialogs_.begin(), child_dialogs_.end(), window)); | 59 std::find(child_dialogs_.begin(), child_dialogs_.end(), dialog)); |
61 bool removed_topmost_window = i == child_dialogs_.begin(); | 60 bool removed_topmost_dialog = i == child_dialogs_.begin(); |
62 if (i != child_dialogs_.end()) | 61 if (i != child_dialogs_.end()) |
63 child_dialogs_.erase(i); | 62 child_dialogs_.erase(i); |
64 if (child_dialogs_.empty()) { | 63 if (child_dialogs_.empty()) { |
65 BlockWebContentsInteraction(false); | 64 BlockWebContentsInteraction(false); |
66 } else { | 65 } else { |
67 if (removed_topmost_window) | 66 if (removed_topmost_dialog) |
68 child_dialogs_[0]->ShowWebContentsModalDialog(); | 67 child_dialogs_[0]->ShowWebContentsModalDialog(); |
69 BlockWebContentsInteraction(true); | 68 BlockWebContentsInteraction(true); |
70 } | 69 } |
71 } | 70 } |
72 | 71 |
73 void ConstrainedWindowTabHelper::BlockWebContentsInteraction(bool blocked) { | 72 void WebContentsModalDialogManager::BlockWebContentsInteraction(bool blocked) { |
74 WebContents* contents = web_contents(); | 73 WebContents* contents = web_contents(); |
75 if (!contents) { | 74 if (!contents) { |
76 // The WebContents has already disconnected. | 75 // The WebContents has already disconnected. |
77 return; | 76 return; |
78 } | 77 } |
79 | 78 |
80 // RenderViewHost may be NULL during shutdown. | 79 // RenderViewHost may be NULL during shutdown. |
81 content::RenderViewHost* host = contents->GetRenderViewHost(); | 80 content::RenderViewHost* host = contents->GetRenderViewHost(); |
82 if (host) { | 81 if (host) { |
83 host->SetIgnoreInputEvents(blocked); | 82 host->SetIgnoreInputEvents(blocked); |
84 host->Send(new ChromeViewMsg_SetVisuallyDeemphasized( | 83 host->Send(new ChromeViewMsg_SetVisuallyDeemphasized( |
85 host->GetRoutingID(), blocked)); | 84 host->GetRoutingID(), blocked)); |
86 } | 85 } |
87 if (delegate_) | 86 if (delegate_) |
88 delegate_->SetWebContentsBlocked(contents, blocked); | 87 delegate_->SetWebContentsBlocked(contents, blocked); |
89 } | 88 } |
90 | 89 |
91 void ConstrainedWindowTabHelper::DidNavigateMainFrame( | 90 void WebContentsModalDialogManager::DidNavigateMainFrame( |
92 const content::LoadCommittedDetails& details, | 91 const content::LoadCommittedDetails& details, |
93 const content::FrameNavigateParams& params) { | 92 const content::FrameNavigateParams& params) { |
94 // Close constrained windows if necessary. | 93 // Close constrained windows if necessary. |
95 if (!net::RegistryControlledDomainService::SameDomainOrHost( | 94 if (!net::RegistryControlledDomainService::SameDomainOrHost( |
96 details.previous_url, details.entry->GetURL())) | 95 details.previous_url, details.entry->GetURL())) |
97 CloseAllDialogs(); | 96 CloseAllDialogs(); |
98 } | 97 } |
99 | 98 |
100 void ConstrainedWindowTabHelper::DidGetIgnoredUIEvent() { | 99 void WebContentsModalDialogManager::DidGetIgnoredUIEvent() { |
101 if (dialog_count()) { | 100 if (dialog_count()) { |
102 ConstrainedWindow* window = *dialog_begin(); | 101 WebContentsModalDialog* dialog = *dialog_begin(); |
103 window->FocusWebContentsModalDialog(); | 102 dialog->FocusWebContentsModalDialog(); |
104 } | 103 } |
105 } | 104 } |
106 | 105 |
107 void ConstrainedWindowTabHelper::WebContentsDestroyed(WebContents* tab) { | 106 void WebContentsModalDialogManager::WebContentsDestroyed(WebContents* tab) { |
108 // First cleanly close all child windows. | 107 // First cleanly close all child dialogs. |
109 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked | 108 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked |
110 // some of these to close. CloseWindows is async, so it might get called | 109 // some of these to close. CloseAllDialogs is async, so it might get called |
111 // twice before it runs. | 110 // twice before it runs. |
112 CloseAllDialogs(); | 111 CloseAllDialogs(); |
113 } | 112 } |
OLD | NEW |