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