| 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/blocked_content/blocked_content_tab_helper.h" | 5 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | 8 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 9 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/blocked_content/blocked_content_container.h" | 11 #include "chrome/browser/ui/blocked_content/blocked_content_container.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
| 15 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 16 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
| 17 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 | 22 |
| 23 using content::NavigationEntry; | 23 using content::NavigationEntry; |
| 24 | 24 |
| 25 BlockedContentTabHelper::BlockedContentTabHelper( | 25 BlockedContentTabHelper::BlockedContentTabHelper(TabContents* tab_contents) |
| 26 TabContentsWrapper* tab_contents) | 26 : content::WebContentsObserver(tab_contents->web_contents()), |
| 27 : content::WebContentsObserver(tab_contents->web_contents()), | 27 blocked_contents_(new BlockedContentContainer(tab_contents)), |
| 28 blocked_contents_(new BlockedContentContainer(tab_contents)), | 28 all_contents_blocked_(false), |
| 29 all_contents_blocked_(false), | 29 tab_contents_(tab_contents), |
| 30 tab_contents_wrapper_(tab_contents), | 30 delegate_(NULL) { |
| 31 delegate_(NULL) { | |
| 32 } | 31 } |
| 33 | 32 |
| 34 BlockedContentTabHelper::~BlockedContentTabHelper() { | 33 BlockedContentTabHelper::~BlockedContentTabHelper() { |
| 35 } | 34 } |
| 36 | 35 |
| 37 void BlockedContentTabHelper::DidNavigateMainFrame( | 36 void BlockedContentTabHelper::DidNavigateMainFrame( |
| 38 const content::LoadCommittedDetails& details, | 37 const content::LoadCommittedDetails& details, |
| 39 const content::FrameNavigateParams& params) { | 38 const content::FrameNavigateParams& params) { |
| 40 // Clear all page actions, blocked content notifications and browser actions | 39 // Clear all page actions, blocked content notifications and browser actions |
| 41 // for this tab, unless this is an in-page navigation. | 40 // for this tab, unless this is an in-page navigation. |
| 42 if (!details.is_in_page) { | 41 if (!details.is_in_page) { |
| 43 // Close blocked popups. | 42 // Close blocked popups. |
| 44 if (blocked_contents_->GetBlockedContentsCount()) { | 43 if (blocked_contents_->GetBlockedContentsCount()) { |
| 45 blocked_contents_->Clear(); | 44 blocked_contents_->Clear(); |
| 46 PopupNotificationVisibilityChanged(false); | 45 PopupNotificationVisibilityChanged(false); |
| 47 } | 46 } |
| 48 } | 47 } |
| 49 } | 48 } |
| 50 | 49 |
| 51 void BlockedContentTabHelper::PopupNotificationVisibilityChanged( | 50 void BlockedContentTabHelper::PopupNotificationVisibilityChanged( |
| 52 bool visible) { | 51 bool visible) { |
| 53 if (!tab_contents_wrapper_->in_destructor()) | 52 if (!tab_contents_->in_destructor()) |
| 54 tab_contents_wrapper_->content_settings()->SetPopupsBlocked(visible); | 53 tab_contents_->content_settings()->SetPopupsBlocked(visible); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void BlockedContentTabHelper::SendNotification(TabContentsWrapper* contents, | 56 void BlockedContentTabHelper::SendNotification(TabContents* contents, |
| 58 bool blocked_state) { | 57 bool blocked_state) { |
| 59 content::NotificationService::current()->Notify( | 58 content::NotificationService::current()->Notify( |
| 60 chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, | 59 chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, |
| 61 content::Source<TabContentsWrapper>(contents), | 60 content::Source<TabContents>(contents), |
| 62 content::Details<const bool>(&blocked_state)); | 61 content::Details<const bool>(&blocked_state)); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void BlockedContentTabHelper::SetAllContentsBlocked(bool value) { | 64 void BlockedContentTabHelper::SetAllContentsBlocked(bool value) { |
| 66 if (all_contents_blocked_ == value) | 65 if (all_contents_blocked_ == value) |
| 67 return; | 66 return; |
| 68 | 67 |
| 69 all_contents_blocked_ = value; | 68 all_contents_blocked_ = value; |
| 70 if (!all_contents_blocked_ && blocked_contents_->GetBlockedContentsCount()) { | 69 if (!all_contents_blocked_ && blocked_contents_->GetBlockedContentsCount()) { |
| 71 std::vector<TabContentsWrapper*> blocked; | 70 std::vector<TabContents*> blocked; |
| 72 blocked_contents_->GetBlockedContents(&blocked); | 71 blocked_contents_->GetBlockedContents(&blocked); |
| 73 for (size_t i = 0; i < blocked.size(); ++i) { | 72 for (size_t i = 0; i < blocked.size(); ++i) { |
| 74 SendNotification(blocked[i], false); | 73 SendNotification(blocked[i], false); |
| 75 blocked_contents_->LaunchForContents(blocked[i]); | 74 blocked_contents_->LaunchForContents(blocked[i]); |
| 76 } | 75 } |
| 77 } | 76 } |
| 78 } | 77 } |
| 79 | 78 |
| 80 void BlockedContentTabHelper::AddTabContents(TabContentsWrapper* new_contents, | 79 void BlockedContentTabHelper::AddTabContents(TabContents* new_contents, |
| 81 WindowOpenDisposition disposition, | 80 WindowOpenDisposition disposition, |
| 82 const gfx::Rect& initial_pos, | 81 const gfx::Rect& initial_pos, |
| 83 bool user_gesture) { | 82 bool user_gesture) { |
| 84 if (!blocked_contents_->GetBlockedContentsCount()) | 83 if (!blocked_contents_->GetBlockedContentsCount()) |
| 85 PopupNotificationVisibilityChanged(true); | 84 PopupNotificationVisibilityChanged(true); |
| 86 SendNotification(new_contents, true); | 85 SendNotification(new_contents, true); |
| 87 blocked_contents_->AddTabContents( | 86 blocked_contents_->AddTabContents( |
| 88 new_contents, disposition, initial_pos, user_gesture); | 87 new_contents, disposition, initial_pos, user_gesture); |
| 89 } | 88 } |
| 90 | 89 |
| 91 void BlockedContentTabHelper::AddPopup(TabContentsWrapper* new_contents, | 90 void BlockedContentTabHelper::AddPopup(TabContents* new_contents, |
| 92 const gfx::Rect& initial_pos, | 91 const gfx::Rect& initial_pos, |
| 93 bool user_gesture) { | 92 bool user_gesture) { |
| 94 // A page can't spawn popups (or do anything else, either) until its load | 93 // A page can't spawn popups (or do anything else, either) until its load |
| 95 // commits, so when we reach here, the popup was spawned by the | 94 // commits, so when we reach here, the popup was spawned by the |
| 96 // NavigationController's last committed entry, not the active entry. For | 95 // NavigationController's last committed entry, not the active entry. For |
| 97 // example, if a page opens a popup in an onunload() handler, then the active | 96 // example, if a page opens a popup in an onunload() handler, then the active |
| 98 // entry is the page to be loaded as we navigate away from the unloading | 97 // entry is the page to be loaded as we navigate away from the unloading |
| 99 // page. For this reason, we can't use GetURL() to get the opener URL, | 98 // page. For this reason, we can't use GetURL() to get the opener URL, |
| 100 // because it returns the active entry. | 99 // because it returns the active entry. |
| 101 NavigationEntry* entry = | 100 NavigationEntry* entry = |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 initial_pos, | 114 initial_pos, |
| 116 true); // user_gesture | 115 true); // user_gesture |
| 117 } else { | 116 } else { |
| 118 // Call blocked_contents_->AddTabContents with user_gesture == true | 117 // Call blocked_contents_->AddTabContents with user_gesture == true |
| 119 // so that the contents will not get blocked again. | 118 // so that the contents will not get blocked again. |
| 120 SendNotification(new_contents, true); | 119 SendNotification(new_contents, true); |
| 121 blocked_contents_->AddTabContents(new_contents, | 120 blocked_contents_->AddTabContents(new_contents, |
| 122 NEW_POPUP, | 121 NEW_POPUP, |
| 123 initial_pos, | 122 initial_pos, |
| 124 true); // user_gesture | 123 true); // user_gesture |
| 125 tab_contents_wrapper_->content_settings()->OnContentBlocked( | 124 tab_contents_->content_settings()->OnContentBlocked( |
| 126 CONTENT_SETTINGS_TYPE_POPUPS, std::string()); | 125 CONTENT_SETTINGS_TYPE_POPUPS, std::string()); |
| 127 } | 126 } |
| 128 } | 127 } |
| 129 | 128 |
| 130 void BlockedContentTabHelper::LaunchForContents( | 129 void BlockedContentTabHelper::LaunchForContents( |
| 131 TabContentsWrapper* tab_contents) { | 130 TabContents* tab_contents) { |
| 132 SendNotification(tab_contents, false); | 131 SendNotification(tab_contents, false); |
| 133 blocked_contents_->LaunchForContents(tab_contents); | 132 blocked_contents_->LaunchForContents(tab_contents); |
| 134 if (!blocked_contents_->GetBlockedContentsCount()) | 133 if (!blocked_contents_->GetBlockedContentsCount()) |
| 135 PopupNotificationVisibilityChanged(false); | 134 PopupNotificationVisibilityChanged(false); |
| 136 } | 135 } |
| 137 | 136 |
| 138 size_t BlockedContentTabHelper::GetBlockedContentsCount() const { | 137 size_t BlockedContentTabHelper::GetBlockedContentsCount() const { |
| 139 return blocked_contents_->GetBlockedContentsCount(); | 138 return blocked_contents_->GetBlockedContentsCount(); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void BlockedContentTabHelper::GetBlockedContents( | 141 void BlockedContentTabHelper::GetBlockedContents( |
| 143 std::vector<TabContentsWrapper*>* blocked_contents) const { | 142 std::vector<TabContents*>* blocked_contents) const { |
| 144 blocked_contents_->GetBlockedContents(blocked_contents); | 143 blocked_contents_->GetBlockedContents(blocked_contents); |
| 145 } | 144 } |
| OLD | NEW |