| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/content_settings/content_settings_tab_helper.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "chrome/browser/content_settings/content_settings_details.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/content_settings/blocked_content_container.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/common/notification_service.h" |
| 16 |
| 17 ContentSettingsTabHelper::ContentSettingsTabHelper( |
| 18 TabContentsWrapper* tab_contents) |
| 19 : TabContentsObserver(tab_contents->tab_contents()), |
| 20 blocked_contents_(new BlockedContentContainer(tab_contents)), |
| 21 all_contents_blocked_(false), |
| 22 dont_notify_render_view_(false), |
| 23 tab_contents_wrapper_(tab_contents) { |
| 24 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED, |
| 25 NotificationService::AllSources()); |
| 26 } |
| 27 |
| 28 ContentSettingsTabHelper::~ContentSettingsTabHelper() { |
| 29 registrar_.RemoveAll(); |
| 30 } |
| 31 |
| 32 void ContentSettingsTabHelper::DidNavigateMainFramePostCommit( |
| 33 const NavigationController::LoadCommittedDetails& details, |
| 34 const ViewHostMsg_FrameNavigate_Params& params) { |
| 35 // Clear all page actions, blocked content notifications and browser actions |
| 36 // for this tab, unless this is an in-page navigation. |
| 37 if (!details.is_in_page) { |
| 38 // Close blocked popups. |
| 39 if (blocked_contents_->GetBlockedContentsCount()) { |
| 40 AutoReset<bool> auto_reset(&dont_notify_render_view_, true); |
| 41 blocked_contents_->Clear(); |
| 42 PopupNotificationVisibilityChanged(false); |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 void ContentSettingsTabHelper::Observe(NotificationType type, |
| 48 const NotificationSource& source, |
| 49 const NotificationDetails& details) { |
| 50 switch (type.value) { |
| 51 case NotificationType::CONTENT_SETTINGS_CHANGED: { |
| 52 Details<const ContentSettingsDetails> settings_details(details); |
| 53 NavigationEntry* entry = tab_contents()->controller().GetActiveEntry(); |
| 54 GURL entry_url; |
| 55 if (entry) |
| 56 entry_url = entry->url(); |
| 57 if (settings_details.ptr()->update_all() || |
| 58 settings_details.ptr()->pattern().Matches(entry_url)) { |
| 59 tab_contents()->render_view_host()->SendContentSettings( |
| 60 entry_url, |
| 61 tab_contents()->profile()->GetHostContentSettingsMap()-> |
| 62 GetContentSettings(entry_url)); |
| 63 } |
| 64 break; |
| 65 } |
| 66 |
| 67 default: |
| 68 NOTREACHED(); |
| 69 } |
| 70 } |
| 71 |
| 72 void ContentSettingsTabHelper::PopupNotificationVisibilityChanged( |
| 73 bool visible) { |
| 74 if (tab_contents()->is_being_destroyed()) |
| 75 return; |
| 76 tab_contents()->GetTabSpecificContentSettings()->SetPopupsBlocked(visible); |
| 77 if (!dont_notify_render_view_) |
| 78 tab_contents()->render_view_host()->AllowScriptToClose(!visible); |
| 79 } |
| 80 |
| 81 void ContentSettingsTabHelper::SetAllContentsBlocked(bool value) { |
| 82 if (all_contents_blocked_ == value) |
| 83 return; |
| 84 |
| 85 all_contents_blocked_ = value; |
| 86 if (!all_contents_blocked_ && blocked_contents_->GetBlockedContentsCount()) { |
| 87 std::vector<TabContentsWrapper*> blocked; |
| 88 blocked_contents_->GetBlockedContents(&blocked); |
| 89 for (size_t i = 0; i < blocked.size(); ++i) |
| 90 blocked_contents_->LaunchForContents(blocked[i]); |
| 91 } |
| 92 } |
| 93 |
| 94 void ContentSettingsTabHelper::AddTabContents(TabContentsWrapper* new_contents, |
| 95 WindowOpenDisposition disposition, |
| 96 const gfx::Rect& initial_pos, |
| 97 bool user_gesture) { |
| 98 if (!blocked_contents_->GetBlockedContentsCount()) |
| 99 PopupNotificationVisibilityChanged(true); |
| 100 blocked_contents_->AddTabContents(new_contents, disposition, initial_pos, |
| 101 user_gesture); |
| 102 } |
| 103 |
| 104 void ContentSettingsTabHelper::AddPopup(TabContentsWrapper* new_contents, |
| 105 const gfx::Rect& initial_pos) { |
| 106 // A page can't spawn popups (or do anything else, either) until its load |
| 107 // commits, so when we reach here, the popup was spawned by the |
| 108 // NavigationController's last committed entry, not the active entry. For |
| 109 // example, if a page opens a popup in an onunload() handler, then the active |
| 110 // entry is the page to be loaded as we navigate away from the unloading |
| 111 // page. For this reason, we can't use GetURL() to get the opener URL, |
| 112 // because it returns the active entry. |
| 113 NavigationEntry* entry = tab_contents()->controller().GetLastCommittedEntry(); |
| 114 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL(); |
| 115 |
| 116 if (creator.is_valid() && |
| 117 tab_contents()->profile()->GetHostContentSettingsMap()->GetContentSetting( |
| 118 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) { |
| 119 tab_contents()->AddNewContents(new_contents->tab_contents(), NEW_POPUP, |
| 120 initial_pos, true); |
| 121 } else { |
| 122 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos, |
| 123 true); |
| 124 tab_contents()->GetContentSettingsDelegate()->OnContentBlocked( |
| 125 CONTENT_SETTINGS_TYPE_POPUPS, std::string()); |
| 126 } |
| 127 } |
| 128 |
| 129 void ContentSettingsTabHelper::LaunchForContents( |
| 130 TabContentsWrapper* tab_contents) { |
| 131 blocked_contents_->LaunchForContents(tab_contents); |
| 132 if (!blocked_contents_->GetBlockedContentsCount()) |
| 133 PopupNotificationVisibilityChanged(false); |
| 134 } |
| 135 |
| 136 size_t ContentSettingsTabHelper::GetBlockedContentsCount() const { |
| 137 return blocked_contents_->GetBlockedContentsCount(); |
| 138 } |
| 139 |
| 140 void ContentSettingsTabHelper::GetBlockedContents( |
| 141 std::vector<TabContentsWrapper*>* blocked_contents) const { |
| 142 blocked_contents_->GetBlockedContents(blocked_contents); |
| 143 } |
| 144 |
| OLD | NEW |