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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 175 |
176 return LocationBarController::ACTION_NONE; | 176 return LocationBarController::ACTION_NONE; |
177 } | 177 } |
178 | 178 |
179 void ActiveScriptController::OnNavigated() { | 179 void ActiveScriptController::OnNavigated() { |
180 LogUMA(); | 180 LogUMA(); |
181 permitted_extensions_.clear(); | 181 permitted_extensions_.clear(); |
182 pending_requests_.clear(); | 182 pending_requests_.clear(); |
183 } | 183 } |
184 | 184 |
185 void ActiveScriptController::OnNotifyExtensionScriptExecution( | 185 void ActiveScriptController::OnRequestContentScriptPermission( |
186 const std::string& extension_id, | 186 const std::string& extension_id, |
187 int page_id) { | 187 int page_id, |
| 188 int request_id) { |
188 if (!Extension::IdIsValid(extension_id)) { | 189 if (!Extension::IdIsValid(extension_id)) { |
189 NOTREACHED() << "'" << extension_id << "' is not a valid id."; | 190 NOTREACHED() << "'" << extension_id << "' is not a valid id."; |
190 return; | 191 return; |
191 } | 192 } |
192 | 193 |
193 const Extension* extension = | 194 const Extension* extension = |
194 ExtensionRegistry::Get(web_contents()->GetBrowserContext()) | 195 ExtensionRegistry::Get(web_contents()->GetBrowserContext()) |
195 ->enabled_extensions().GetByID(extension_id); | 196 ->enabled_extensions().GetByID(extension_id); |
196 // We shouldn't allow extensions which are no longer enabled to run any | 197 // We shouldn't allow extensions which are no longer enabled to run any |
197 // scripts. Ignore the request. | 198 // scripts. Ignore the request. |
198 if (!extension) | 199 if (!extension) |
199 return; | 200 return; |
200 | 201 |
201 // Right now, we allow all content scripts to execute, but notify the | 202 if (RequiresUserConsentForScriptInjection(extension)) { |
202 // controller of them. | 203 // This base::Unretained() is safe, because the callback is only invoked by |
203 // TODO(rdevlin.cronin): Fix this in a future CL. | 204 // this object. |
204 if (RequiresUserConsentForScriptInjection(extension)) | 205 RequestScriptInjection( |
205 RequestScriptInjection(extension, page_id, base::Bind(&base::DoNothing)); | 206 extension, |
| 207 page_id, |
| 208 base::Bind(&ActiveScriptController::GrantContentScriptPermission, |
| 209 base::Unretained(this), |
| 210 request_id)); |
| 211 } else { |
| 212 GrantContentScriptPermission(request_id); |
| 213 } |
| 214 } |
| 215 |
| 216 void ActiveScriptController::GrantContentScriptPermission(int request_id) { |
| 217 content::RenderViewHost* render_view_host = |
| 218 web_contents()->GetRenderViewHost(); |
| 219 if (render_view_host) { |
| 220 render_view_host->Send(new ExtensionMsg_GrantContentScriptPermission( |
| 221 render_view_host->GetRoutingID(), |
| 222 request_id)); |
| 223 } |
206 } | 224 } |
207 | 225 |
208 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { | 226 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { |
209 bool handled = true; | 227 bool handled = true; |
210 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) | 228 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) |
211 IPC_MESSAGE_HANDLER(ExtensionHostMsg_NotifyExtensionScriptExecution, | 229 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestContentScriptPermission, |
212 OnNotifyExtensionScriptExecution) | 230 OnRequestContentScriptPermission) |
213 IPC_MESSAGE_UNHANDLED(handled = false) | 231 IPC_MESSAGE_UNHANDLED(handled = false) |
214 IPC_END_MESSAGE_MAP() | 232 IPC_END_MESSAGE_MAP() |
215 return handled; | 233 return handled; |
216 } | 234 } |
217 | 235 |
218 void ActiveScriptController::LogUMA() const { | 236 void ActiveScriptController::LogUMA() const { |
219 UMA_HISTOGRAM_COUNTS_100( | 237 UMA_HISTOGRAM_COUNTS_100( |
220 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | 238 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", |
221 pending_requests_.size()); | 239 pending_requests_.size()); |
222 | 240 |
223 // We only log the permitted extensions metric if the feature is enabled, | 241 // We only log the permitted extensions metric if the feature is enabled, |
224 // because otherwise the data will be boring (100% allowed). | 242 // because otherwise the data will be boring (100% allowed). |
225 if (enabled_) { | 243 if (enabled_) { |
226 UMA_HISTOGRAM_COUNTS_100( | 244 UMA_HISTOGRAM_COUNTS_100( |
227 "Extensions.ActiveScriptController.PermittedExtensions", | 245 "Extensions.ActiveScriptController.PermittedExtensions", |
228 permitted_extensions_.size()); | 246 permitted_extensions_.size()); |
229 UMA_HISTOGRAM_COUNTS_100( | 247 UMA_HISTOGRAM_COUNTS_100( |
230 "Extensions.ActiveScriptController.DeniedExtensions", | 248 "Extensions.ActiveScriptController.DeniedExtensions", |
231 pending_requests_.size()); | 249 pending_requests_.size()); |
232 } | 250 } |
233 } | 251 } |
234 | 252 |
235 } // namespace extensions | 253 } // namespace extensions |
OLD | NEW |