OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extensions/active_script_controller.h" | 5 #include "chrome/browser/extensions/active_script_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 CHECK(extension); | 94 CHECK(extension); |
95 PendingRequestList& list = pending_requests_[extension->id()]; | 95 PendingRequestList& list = pending_requests_[extension->id()]; |
96 list.push_back(PendingRequest(callback, page_id)); | 96 list.push_back(PendingRequest(callback, page_id)); |
97 | 97 |
98 // If this was the first entry, notify the location bar that there's a new | 98 // If this was the first entry, notify the location bar that there's a new |
99 // icon. | 99 // icon. |
100 if (list.size() == 1u) | 100 if (list.size() == 1u) |
101 LocationBarController::NotifyChange(web_contents()); | 101 LocationBarController::NotifyChange(web_contents()); |
102 } | 102 } |
103 | 103 |
| 104 void ActiveScriptController::OnActiveTabPermissionGranted( |
| 105 const Extension* extension) { |
| 106 RunPendingForExtension(extension); |
| 107 } |
| 108 |
104 void ActiveScriptController::OnAdInjectionDetected( | 109 void ActiveScriptController::OnAdInjectionDetected( |
105 const std::vector<std::string> ad_injectors) { | 110 const std::vector<std::string> ad_injectors) { |
106 // We're only interested in data if there are ad injectors detected. | 111 // We're only interested in data if there are ad injectors detected. |
107 if (ad_injectors.empty()) | 112 if (ad_injectors.empty()) |
108 return; | 113 return; |
109 | 114 |
110 size_t num_preventable_ad_injectors = | 115 size_t num_preventable_ad_injectors = |
111 base::STLSetIntersection<std::set<std::string> >( | 116 base::STLSetIntersection<std::set<std::string> >( |
112 ad_injectors, permitted_extensions_).size(); | 117 ad_injectors, permitted_extensions_).size(); |
113 | 118 |
(...skipping 28 matching lines...) Expand all Loading... |
142 action->set_default_icon( | 147 action->set_default_icon( |
143 make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); | 148 make_scoped_ptr(new ExtensionIconSet(action_info->default_icon))); |
144 } | 149 } |
145 | 150 |
146 active_script_actions_[extension->id()] = action; | 151 active_script_actions_[extension->id()] = action; |
147 return action.get(); | 152 return action.get(); |
148 } | 153 } |
149 | 154 |
150 LocationBarController::Action ActiveScriptController::OnClicked( | 155 LocationBarController::Action ActiveScriptController::OnClicked( |
151 const Extension* extension) { | 156 const Extension* extension) { |
| 157 DCHECK(ContainsKey(pending_requests_, extension->id())); |
| 158 RunPendingForExtension(extension); |
| 159 return LocationBarController::ACTION_NONE; |
| 160 } |
| 161 |
| 162 void ActiveScriptController::OnNavigated() { |
| 163 LogUMA(); |
| 164 permitted_extensions_.clear(); |
| 165 pending_requests_.clear(); |
| 166 } |
| 167 |
| 168 void ActiveScriptController::RunPendingForExtension( |
| 169 const Extension* extension) { |
152 DCHECK(extension); | 170 DCHECK(extension); |
153 PendingRequestMap::iterator iter = | 171 PendingRequestMap::iterator iter = |
154 pending_requests_.find(extension->id()); | 172 pending_requests_.find(extension->id()); |
155 DCHECK(iter != pending_requests_.end()); | 173 if (iter == pending_requests_.end()) |
| 174 return; |
156 | 175 |
157 content::NavigationEntry* visible_entry = | 176 content::NavigationEntry* visible_entry = |
158 web_contents()->GetController().GetVisibleEntry(); | 177 web_contents()->GetController().GetVisibleEntry(); |
159 // Refuse to run if there's no visible entry, because we have no idea of | 178 // Refuse to run if there's no visible entry, because we have no idea of |
160 // determining if it's the proper page. This should rarely, if ever, happen. | 179 // determining if it's the proper page. This should rarely, if ever, happen. |
161 if (!visible_entry) | 180 if (!visible_entry) |
162 return LocationBarController::ACTION_NONE; | 181 return; |
163 | 182 |
164 int page_id = visible_entry->GetPageID(); | 183 int page_id = visible_entry->GetPageID(); |
165 | 184 |
166 // We add this to the list of permitted extensions and erase pending entries | 185 // We add this to the list of permitted extensions and erase pending entries |
167 // *before* running them to guard against the crazy case where running the | 186 // *before* running them to guard against the crazy case where running the |
168 // callbacks adds more entries. | 187 // callbacks adds more entries. |
169 permitted_extensions_.insert(extension->id()); | 188 permitted_extensions_.insert(extension->id()); |
170 PendingRequestList requests; | 189 PendingRequestList requests; |
171 iter->second.swap(requests); | 190 iter->second.swap(requests); |
172 pending_requests_.erase(extension->id()); | 191 pending_requests_.erase(extension->id()); |
173 | 192 |
174 // Clicking to run the extension counts as granting it permission to run on | 193 // Clicking to run the extension counts as granting it permission to run on |
175 // the given tab. | 194 // the given tab. |
| 195 // The extension may already have active tab at this point, but granting |
| 196 // it twice is essentially a no-op. |
176 TabHelper::FromWebContents(web_contents())-> | 197 TabHelper::FromWebContents(web_contents())-> |
177 active_tab_permission_granter()->GrantIfRequested(extension); | 198 active_tab_permission_granter()->GrantIfRequested(extension); |
178 | 199 |
179 // Run all pending injections for the given extension. | 200 // Run all pending injections for the given extension. |
180 for (PendingRequestList::iterator request = requests.begin(); | 201 for (PendingRequestList::iterator request = requests.begin(); |
181 request != requests.end(); | 202 request != requests.end(); |
182 ++request) { | 203 ++request) { |
183 // Only run if it's on the proper page. | 204 // Only run if it's on the proper page. |
184 if (request->page_id == page_id) | 205 if (request->page_id == page_id) |
185 request->closure.Run(); | 206 request->closure.Run(); |
186 } | 207 } |
187 | 208 |
188 // Inform the location bar that the action is now gone. | 209 // Inform the location bar that the action is now gone. |
189 LocationBarController::NotifyChange(web_contents()); | 210 LocationBarController::NotifyChange(web_contents()); |
190 | |
191 return LocationBarController::ACTION_NONE; | |
192 } | |
193 | |
194 void ActiveScriptController::OnNavigated() { | |
195 LogUMA(); | |
196 permitted_extensions_.clear(); | |
197 pending_requests_.clear(); | |
198 } | 211 } |
199 | 212 |
200 void ActiveScriptController::OnNotifyExtensionScriptExecution( | 213 void ActiveScriptController::OnNotifyExtensionScriptExecution( |
201 const std::string& extension_id, | 214 const std::string& extension_id, |
202 int page_id) { | 215 int page_id) { |
203 if (!Extension::IdIsValid(extension_id)) { | 216 if (!Extension::IdIsValid(extension_id)) { |
204 NOTREACHED() << "'" << extension_id << "' is not a valid id."; | 217 NOTREACHED() << "'" << extension_id << "' is not a valid id."; |
205 return; | 218 return; |
206 } | 219 } |
207 | 220 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 UMA_HISTOGRAM_COUNTS_100( | 254 UMA_HISTOGRAM_COUNTS_100( |
242 "Extensions.ActiveScriptController.PermittedExtensions", | 255 "Extensions.ActiveScriptController.PermittedExtensions", |
243 permitted_extensions_.size()); | 256 permitted_extensions_.size()); |
244 UMA_HISTOGRAM_COUNTS_100( | 257 UMA_HISTOGRAM_COUNTS_100( |
245 "Extensions.ActiveScriptController.DeniedExtensions", | 258 "Extensions.ActiveScriptController.DeniedExtensions", |
246 pending_requests_.size()); | 259 pending_requests_.size()); |
247 } | 260 } |
248 } | 261 } |
249 | 262 |
250 } // namespace extensions | 263 } // namespace extensions |
OLD | NEW |