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