OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/popup_blocker_tab_helper.h" | 5 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.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_window_params.h" | 11 #include "chrome/browser/ui/blocked_content/blocked_window_params.h" |
12 #include "chrome/browser/ui/browser_navigator.h" | 12 #include "chrome/browser/ui/browser_navigator.h" |
13 #include "chrome/browser/ui/browser_navigator_params.h" | 13 #include "chrome/browser/ui/browser_navigator_params.h" |
14 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
18 #include "content/public/browser/navigation_handle.h" | 18 #include "content/public/browser/navigation_handle.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "third_party/WebKit/public/web/WebWindowFeatures.h" | |
22 | 21 |
23 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
24 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 23 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
25 #endif | 24 #endif |
26 | 25 |
27 using blink::WebWindowFeatures; | |
28 | |
29 const size_t kMaximumNumberOfPopups = 25; | 26 const size_t kMaximumNumberOfPopups = 25; |
30 | 27 |
31 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PopupBlockerTabHelper); | 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PopupBlockerTabHelper); |
32 | 29 |
33 struct PopupBlockerTabHelper::BlockedRequest { | 30 struct PopupBlockerTabHelper::BlockedRequest { |
34 BlockedRequest(const chrome::NavigateParams& params, | 31 explicit BlockedRequest(const chrome::NavigateParams& params) |
35 const WebWindowFeatures& window_features) | 32 : params(params) {} |
36 : params(params), window_features(window_features) {} | |
37 | 33 |
38 chrome::NavigateParams params; | 34 chrome::NavigateParams params; |
39 WebWindowFeatures window_features; | |
40 }; | 35 }; |
41 | 36 |
42 PopupBlockerTabHelper::PopupBlockerTabHelper( | 37 PopupBlockerTabHelper::PopupBlockerTabHelper( |
43 content::WebContents* web_contents) | 38 content::WebContents* web_contents) |
44 : content::WebContentsObserver(web_contents) { | 39 : content::WebContentsObserver(web_contents) { |
45 } | 40 } |
46 | 41 |
47 PopupBlockerTabHelper::~PopupBlockerTabHelper() { | 42 PopupBlockerTabHelper::~PopupBlockerTabHelper() { |
48 } | 43 } |
49 | 44 |
(...skipping 17 matching lines...) Expand all Loading... |
67 | 62 |
68 void PopupBlockerTabHelper::PopupNotificationVisibilityChanged( | 63 void PopupBlockerTabHelper::PopupNotificationVisibilityChanged( |
69 bool visible) { | 64 bool visible) { |
70 if (!web_contents()->IsBeingDestroyed()) { | 65 if (!web_contents()->IsBeingDestroyed()) { |
71 TabSpecificContentSettings::FromWebContents(web_contents())-> | 66 TabSpecificContentSettings::FromWebContents(web_contents())-> |
72 SetPopupsBlocked(visible); | 67 SetPopupsBlocked(visible); |
73 } | 68 } |
74 } | 69 } |
75 | 70 |
76 bool PopupBlockerTabHelper::MaybeBlockPopup( | 71 bool PopupBlockerTabHelper::MaybeBlockPopup( |
77 const chrome::NavigateParams& params, | 72 const chrome::NavigateParams& params) { |
78 const WebWindowFeatures& window_features) { | |
79 // A page can't spawn popups (or do anything else, either) until its load | 73 // A page can't spawn popups (or do anything else, either) until its load |
80 // commits, so when we reach here, the popup was spawned by the | 74 // commits, so when we reach here, the popup was spawned by the |
81 // NavigationController's last committed entry, not the active entry. For | 75 // NavigationController's last committed entry, not the active entry. For |
82 // example, if a page opens a popup in an onunload() handler, then the active | 76 // example, if a page opens a popup in an onunload() handler, then the active |
83 // entry is the page to be loaded as we navigate away from the unloading | 77 // entry is the page to be loaded as we navigate away from the unloading |
84 // page. For this reason, we can't use GetURL() to get the opener URL, | 78 // page. For this reason, we can't use GetURL() to get the opener URL, |
85 // because it returns the active entry. | 79 // because it returns the active entry. |
86 content::NavigationEntry* entry = | 80 content::NavigationEntry* entry = |
87 web_contents()->GetController().GetLastCommittedEntry(); | 81 web_contents()->GetController().GetLastCommittedEntry(); |
88 GURL creator = entry ? entry->GetVirtualURL() : GURL(); | 82 GURL creator = entry ? entry->GetVirtualURL() : GURL(); |
89 Profile* profile = | 83 Profile* profile = |
90 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 84 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
91 | 85 |
92 if (creator.is_valid() && | 86 if (creator.is_valid() && |
93 HostContentSettingsMapFactory::GetForProfile(profile)->GetContentSetting( | 87 HostContentSettingsMapFactory::GetForProfile(profile)->GetContentSetting( |
94 creator, creator, CONTENT_SETTINGS_TYPE_POPUPS, std::string()) == | 88 creator, creator, CONTENT_SETTINGS_TYPE_POPUPS, std::string()) == |
95 CONTENT_SETTING_ALLOW) { | 89 CONTENT_SETTING_ALLOW) { |
96 return false; | 90 return false; |
97 } | 91 } |
98 | 92 |
99 AddBlockedPopup(params, window_features); | 93 AddBlockedPopup(params); |
100 return true; | 94 return true; |
101 } | 95 } |
102 | 96 |
103 void PopupBlockerTabHelper::AddBlockedPopup(const BlockedWindowParams& params) { | 97 void PopupBlockerTabHelper::AddBlockedPopup(const BlockedWindowParams& params) { |
104 AddBlockedPopup(params.CreateNavigateParams(web_contents()), | 98 AddBlockedPopup(params.CreateNavigateParams(web_contents())); |
105 params.features()); | |
106 } | 99 } |
107 | 100 |
108 void PopupBlockerTabHelper::AddBlockedPopup( | 101 void PopupBlockerTabHelper::AddBlockedPopup( |
109 const chrome::NavigateParams& params, | 102 const chrome::NavigateParams& params) { |
110 const WebWindowFeatures& window_features) { | |
111 if (blocked_popups_.size() >= kMaximumNumberOfPopups) | 103 if (blocked_popups_.size() >= kMaximumNumberOfPopups) |
112 return; | 104 return; |
113 | 105 |
114 blocked_popups_.Add( | 106 blocked_popups_.Add(base::MakeUnique<BlockedRequest>(params)); |
115 base::MakeUnique<BlockedRequest>(params, window_features)); | |
116 TabSpecificContentSettings::FromWebContents(web_contents())-> | 107 TabSpecificContentSettings::FromWebContents(web_contents())-> |
117 OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS); | 108 OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS); |
118 } | 109 } |
119 | 110 |
120 void PopupBlockerTabHelper::ShowBlockedPopup(int32_t id) { | 111 void PopupBlockerTabHelper::ShowBlockedPopup(int32_t id) { |
121 BlockedRequest* popup = blocked_popups_.Lookup(id); | 112 BlockedRequest* popup = blocked_popups_.Lookup(id); |
122 if (!popup) | 113 if (!popup) |
123 return; | 114 return; |
124 // We set user_gesture to true here, so the new popup gets correctly focused. | 115 // We set user_gesture to true here, so the new popup gets correctly focused. |
125 popup->params.user_gesture = true; | 116 popup->params.user_gesture = true; |
126 #if defined(OS_ANDROID) | 117 #if defined(OS_ANDROID) |
127 TabModelList::HandlePopupNavigation(&popup->params); | 118 TabModelList::HandlePopupNavigation(&popup->params); |
128 #else | 119 #else |
129 chrome::Navigate(&popup->params); | 120 chrome::Navigate(&popup->params); |
130 #endif | 121 #endif |
131 if (popup->params.target_contents) { | 122 if (popup->params.target_contents) { |
132 popup->params.target_contents->Send(new ChromeViewMsg_SetWindowFeatures( | 123 popup->params.target_contents->Send(new ChromeViewMsg_SetWindowFeatures( |
133 popup->params.target_contents->GetRenderViewHost()->GetRoutingID(), | 124 popup->params.target_contents->GetRenderViewHost()->GetRoutingID())); |
134 popup->window_features)); | |
135 } | 125 } |
136 blocked_popups_.Remove(id); | 126 blocked_popups_.Remove(id); |
137 if (blocked_popups_.IsEmpty()) | 127 if (blocked_popups_.IsEmpty()) |
138 PopupNotificationVisibilityChanged(false); | 128 PopupNotificationVisibilityChanged(false); |
139 } | 129 } |
140 | 130 |
141 size_t PopupBlockerTabHelper::GetBlockedPopupsCount() const { | 131 size_t PopupBlockerTabHelper::GetBlockedPopupsCount() const { |
142 return blocked_popups_.size(); | 132 return blocked_popups_.size(); |
143 } | 133 } |
144 | 134 |
145 PopupBlockerTabHelper::PopupIdMap | 135 PopupBlockerTabHelper::PopupIdMap |
146 PopupBlockerTabHelper::GetBlockedPopupRequests() { | 136 PopupBlockerTabHelper::GetBlockedPopupRequests() { |
147 PopupIdMap result; | 137 PopupIdMap result; |
148 for (IDMap<std::unique_ptr<BlockedRequest>>::const_iterator iter( | 138 for (IDMap<std::unique_ptr<BlockedRequest>>::const_iterator iter( |
149 &blocked_popups_); | 139 &blocked_popups_); |
150 !iter.IsAtEnd(); | 140 !iter.IsAtEnd(); |
151 iter.Advance()) { | 141 iter.Advance()) { |
152 result[iter.GetCurrentKey()] = iter.GetCurrentValue()->params.url; | 142 result[iter.GetCurrentKey()] = iter.GetCurrentValue()->params.url; |
153 } | 143 } |
154 return result; | 144 return result; |
155 } | 145 } |
OLD | NEW |