| 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.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 #include "content/public/browser/web_contents_delegate.h" | 22 #include "content/public/browser/web_contents_delegate.h" |
| 23 | 23 |
| 24 using content::NavigationEntry; | 24 using content::NavigationEntry; |
| 25 | 25 |
| 26 BlockedContentTabHelper::BlockedContentTabHelper(TabContents* tab_contents) | 26 int BlockedContentTabHelper::kUserDataKey; |
| 27 : content::WebContentsObserver(tab_contents->web_contents()), | 27 |
| 28 blocked_contents_(new BlockedContentContainer(tab_contents)), | 28 BlockedContentTabHelper::BlockedContentTabHelper( |
| 29 content::WebContents* web_contents) |
| 30 : content::WebContentsObserver(web_contents), |
| 31 blocked_contents_(new BlockedContentContainer(web_contents)), |
| 29 all_contents_blocked_(false), | 32 all_contents_blocked_(false), |
| 30 tab_contents_(tab_contents), | |
| 31 delegate_(NULL) { | 33 delegate_(NULL) { |
| 32 } | 34 } |
| 33 | 35 |
| 34 BlockedContentTabHelper::~BlockedContentTabHelper() { | 36 BlockedContentTabHelper::~BlockedContentTabHelper() { |
| 35 } | 37 } |
| 36 | 38 |
| 37 void BlockedContentTabHelper::DidNavigateMainFrame( | 39 void BlockedContentTabHelper::DidNavigateMainFrame( |
| 38 const content::LoadCommittedDetails& details, | 40 const content::LoadCommittedDetails& details, |
| 39 const content::FrameNavigateParams& params) { | 41 const content::FrameNavigateParams& params) { |
| 40 // Clear all page actions, blocked content notifications and browser actions | 42 // Clear all page actions, blocked content notifications and browser actions |
| 41 // for this tab, unless this is an in-page navigation. | 43 // for this tab, unless this is an in-page navigation. |
| 42 if (!details.is_in_page) { | 44 if (!details.is_in_page) { |
| 43 // Close blocked popups. | 45 // Close blocked popups. |
| 44 if (blocked_contents_->GetBlockedContentsCount()) { | 46 if (blocked_contents_->GetBlockedContentsCount()) { |
| 45 blocked_contents_->Clear(); | 47 blocked_contents_->Clear(); |
| 46 PopupNotificationVisibilityChanged(false); | 48 PopupNotificationVisibilityChanged(false); |
| 47 } | 49 } |
| 48 } | 50 } |
| 49 } | 51 } |
| 50 | 52 |
| 51 void BlockedContentTabHelper::PopupNotificationVisibilityChanged( | 53 void BlockedContentTabHelper::PopupNotificationVisibilityChanged( |
| 52 bool visible) { | 54 bool visible) { |
| 53 if (!tab_contents_->in_destructor()) | 55 if (!web_contents()->IsBeingDestroyed()) { |
| 54 tab_contents_->content_settings()->SetPopupsBlocked(visible); | 56 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 57 tab_contents->content_settings()->SetPopupsBlocked(visible); |
| 58 } |
| 55 } | 59 } |
| 56 | 60 |
| 57 void BlockedContentTabHelper::SendNotification(TabContents* contents, | 61 void BlockedContentTabHelper::SendNotification(content::WebContents* contents, |
| 58 bool blocked_state) { | 62 bool blocked_state) { |
| 59 content::NotificationService::current()->Notify( | 63 content::NotificationService::current()->Notify( |
| 60 chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, | 64 chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, |
| 61 content::Source<content::WebContents>(contents->web_contents()), | 65 content::Source<content::WebContents>(contents), |
| 62 content::Details<const bool>(&blocked_state)); | 66 content::Details<const bool>(&blocked_state)); |
| 63 } | 67 } |
| 64 | 68 |
| 65 void BlockedContentTabHelper::SetAllContentsBlocked(bool value) { | 69 void BlockedContentTabHelper::SetAllContentsBlocked(bool value) { |
| 66 if (all_contents_blocked_ == value) | 70 if (all_contents_blocked_ == value) |
| 67 return; | 71 return; |
| 68 | 72 |
| 69 all_contents_blocked_ = value; | 73 all_contents_blocked_ = value; |
| 70 if (!all_contents_blocked_ && blocked_contents_->GetBlockedContentsCount()) { | 74 if (!all_contents_blocked_ && blocked_contents_->GetBlockedContentsCount()) { |
| 71 std::vector<TabContents*> blocked; | 75 std::vector<content::WebContents*> blocked; |
| 72 blocked_contents_->GetBlockedContents(&blocked); | 76 blocked_contents_->GetBlockedContents(&blocked); |
| 73 for (size_t i = 0; i < blocked.size(); ++i) { | 77 for (size_t i = 0; i < blocked.size(); ++i) { |
| 74 SendNotification(blocked[i], false); | 78 SendNotification(blocked[i], false); |
| 75 blocked_contents_->LaunchForContents(blocked[i]); | 79 blocked_contents_->LaunchForContents(blocked[i]); |
| 76 } | 80 } |
| 77 } | 81 } |
| 78 } | 82 } |
| 79 | 83 |
| 80 void BlockedContentTabHelper::AddTabContents(TabContents* new_contents, | 84 void BlockedContentTabHelper::AddWebContents(content::WebContents* new_contents, |
| 81 WindowOpenDisposition disposition, | 85 WindowOpenDisposition disposition, |
| 82 const gfx::Rect& initial_pos, | 86 const gfx::Rect& initial_pos, |
| 83 bool user_gesture) { | 87 bool user_gesture) { |
| 84 if (!blocked_contents_->GetBlockedContentsCount()) | 88 if (!blocked_contents_->GetBlockedContentsCount()) |
| 85 PopupNotificationVisibilityChanged(true); | 89 PopupNotificationVisibilityChanged(true); |
| 86 SendNotification(new_contents, true); | 90 SendNotification(new_contents, true); |
| 87 blocked_contents_->AddTabContents( | 91 blocked_contents_->AddWebContents( |
| 88 new_contents, disposition, initial_pos, user_gesture); | 92 new_contents, disposition, initial_pos, user_gesture); |
| 89 } | 93 } |
| 90 | 94 |
| 91 void BlockedContentTabHelper::AddPopup(TabContents* new_contents, | 95 void BlockedContentTabHelper::AddPopup(content::WebContents* new_contents, |
| 92 WindowOpenDisposition disposition, | 96 WindowOpenDisposition disposition, |
| 93 const gfx::Rect& initial_pos, | 97 const gfx::Rect& initial_pos, |
| 94 bool user_gesture) { | 98 bool user_gesture) { |
| 95 // A page can't spawn popups (or do anything else, either) until its load | 99 // A page can't spawn popups (or do anything else, either) until its load |
| 96 // commits, so when we reach here, the popup was spawned by the | 100 // commits, so when we reach here, the popup was spawned by the |
| 97 // NavigationController's last committed entry, not the active entry. For | 101 // NavigationController's last committed entry, not the active entry. For |
| 98 // example, if a page opens a popup in an onunload() handler, then the active | 102 // example, if a page opens a popup in an onunload() handler, then the active |
| 99 // entry is the page to be loaded as we navigate away from the unloading | 103 // entry is the page to be loaded as we navigate away from the unloading |
| 100 // page. For this reason, we can't use GetURL() to get the opener URL, | 104 // page. For this reason, we can't use GetURL() to get the opener URL, |
| 101 // because it returns the active entry. | 105 // because it returns the active entry. |
| 102 NavigationEntry* entry = | 106 NavigationEntry* entry = |
| 103 web_contents()->GetController().GetLastCommittedEntry(); | 107 web_contents()->GetController().GetLastCommittedEntry(); |
| 104 GURL creator = entry ? entry->GetVirtualURL() : GURL::EmptyGURL(); | 108 GURL creator = entry ? entry->GetVirtualURL() : GURL::EmptyGURL(); |
| 105 Profile* profile = | 109 Profile* profile = |
| 106 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 110 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 107 | 111 |
| 108 if (creator.is_valid() && | 112 if (creator.is_valid() && |
| 109 profile->GetHostContentSettingsMap()->GetContentSetting( | 113 profile->GetHostContentSettingsMap()->GetContentSetting( |
| 110 creator, | 114 creator, |
| 111 creator, | 115 creator, |
| 112 CONTENT_SETTINGS_TYPE_POPUPS, | 116 CONTENT_SETTINGS_TYPE_POPUPS, |
| 113 "") == CONTENT_SETTING_ALLOW) { | 117 "") == CONTENT_SETTING_ALLOW) { |
| 114 content::WebContentsDelegate* delegate = web_contents()->GetDelegate(); | 118 content::WebContentsDelegate* delegate = web_contents()->GetDelegate(); |
| 115 if (delegate) { | 119 if (delegate) { |
| 116 delegate->AddNewContents(web_contents(), | 120 delegate->AddNewContents(web_contents(), |
| 117 new_contents->web_contents(), | 121 new_contents, |
| 118 disposition, | 122 disposition, |
| 119 initial_pos, | 123 initial_pos, |
| 120 true, // user_gesture | 124 true, // user_gesture |
| 121 NULL); | 125 NULL); |
| 122 } | 126 } |
| 123 } else { | 127 } else { |
| 124 // Call blocked_contents_->AddTabContents with user_gesture == true | 128 // Call blocked_contents_->AddWebContents with user_gesture == true |
| 125 // so that the contents will not get blocked again. | 129 // so that the contents will not get blocked again. |
| 126 SendNotification(new_contents, true); | 130 SendNotification(new_contents, true); |
| 127 blocked_contents_->AddTabContents(new_contents, | 131 blocked_contents_->AddWebContents(new_contents, |
| 128 disposition, | 132 disposition, |
| 129 initial_pos, | 133 initial_pos, |
| 130 true); // user_gesture | 134 true); // user_gesture |
| 131 tab_contents_->content_settings()->OnContentBlocked( | 135 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 136 tab_contents->content_settings()->OnContentBlocked( |
| 132 CONTENT_SETTINGS_TYPE_POPUPS, std::string()); | 137 CONTENT_SETTINGS_TYPE_POPUPS, std::string()); |
| 133 } | 138 } |
| 134 } | 139 } |
| 135 | 140 |
| 136 void BlockedContentTabHelper::LaunchForContents( | 141 void BlockedContentTabHelper::LaunchForContents( |
| 137 TabContents* tab_contents) { | 142 content::WebContents* web_contents) { |
| 138 SendNotification(tab_contents, false); | 143 SendNotification(web_contents, false); |
| 139 blocked_contents_->LaunchForContents(tab_contents); | 144 blocked_contents_->LaunchForContents(web_contents); |
| 140 if (!blocked_contents_->GetBlockedContentsCount()) | 145 if (!blocked_contents_->GetBlockedContentsCount()) |
| 141 PopupNotificationVisibilityChanged(false); | 146 PopupNotificationVisibilityChanged(false); |
| 142 } | 147 } |
| 143 | 148 |
| 144 size_t BlockedContentTabHelper::GetBlockedContentsCount() const { | 149 size_t BlockedContentTabHelper::GetBlockedContentsCount() const { |
| 145 return blocked_contents_->GetBlockedContentsCount(); | 150 return blocked_contents_->GetBlockedContentsCount(); |
| 146 } | 151 } |
| 147 | 152 |
| 148 void BlockedContentTabHelper::GetBlockedContents( | 153 void BlockedContentTabHelper::GetBlockedContents( |
| 149 std::vector<TabContents*>* blocked_contents) const { | 154 std::vector<content::WebContents*>* blocked_contents) const { |
| 150 blocked_contents_->GetBlockedContents(blocked_contents); | 155 blocked_contents_->GetBlockedContents(blocked_contents); |
| 151 } | 156 } |
| OLD | NEW |