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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 extension->permissions_data() | 54 extension->permissions_data() |
55 ->active_permissions() | 55 ->active_permissions() |
56 ->ShouldWarnAllHosts(); | 56 ->ShouldWarnAllHosts(); |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 ActiveScriptController::ActiveScriptController( | 61 ActiveScriptController::ActiveScriptController( |
62 content::WebContents* web_contents) | 62 content::WebContents* web_contents) |
63 : content::WebContentsObserver(web_contents), | 63 : content::WebContentsObserver(web_contents), |
64 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) { | 64 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()), |
| 65 extension_registry_observer_(this) { |
65 CHECK(web_contents); | 66 CHECK(web_contents); |
| 67 extension_registry_observer_.Add( |
| 68 ExtensionRegistry::Get(web_contents->GetBrowserContext())); |
66 } | 69 } |
67 | 70 |
68 ActiveScriptController::~ActiveScriptController() { | 71 ActiveScriptController::~ActiveScriptController() { |
69 LogUMA(); | 72 LogUMA(); |
70 } | 73 } |
71 | 74 |
72 // static | 75 // static |
73 ActiveScriptController* ActiveScriptController::GetForWebContents( | 76 ActiveScriptController* ActiveScriptController::GetForWebContents( |
74 content::WebContents* web_contents) { | 77 content::WebContents* web_contents) { |
75 if (!web_contents) | 78 if (!web_contents) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 165 |
163 linked_ptr<ExtensionAction> action(ExtensionActionManager::Get( | 166 linked_ptr<ExtensionAction> action(ExtensionActionManager::Get( |
164 Profile::FromBrowserContext(web_contents()->GetBrowserContext())) | 167 Profile::FromBrowserContext(web_contents()->GetBrowserContext())) |
165 ->GetBestFitAction(*extension, ActionInfo::TYPE_PAGE).release()); | 168 ->GetBestFitAction(*extension, ActionInfo::TYPE_PAGE).release()); |
166 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); | 169 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); |
167 | 170 |
168 active_script_actions_[extension->id()] = action; | 171 active_script_actions_[extension->id()] = action; |
169 return action.get(); | 172 return action.get(); |
170 } | 173 } |
171 | 174 |
172 void ActiveScriptController::OnExtensionUnloaded(const Extension* extension) { | |
173 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); | |
174 if (iter != pending_requests_.end()) | |
175 pending_requests_.erase(iter); | |
176 } | |
177 | |
178 PermissionsData::AccessType | 175 PermissionsData::AccessType |
179 ActiveScriptController::RequiresUserConsentForScriptInjection( | 176 ActiveScriptController::RequiresUserConsentForScriptInjection( |
180 const Extension* extension, | 177 const Extension* extension, |
181 UserScript::InjectionType type) { | 178 UserScript::InjectionType type) { |
182 CHECK(extension); | 179 CHECK(extension); |
183 | 180 |
184 // If the feature is not enabled, we automatically allow all extensions to | 181 // If the feature is not enabled, we automatically allow all extensions to |
185 // run scripts. | 182 // run scripts. |
186 if (!enabled_) | 183 if (!enabled_) |
187 permitted_extensions_.insert(extension->id()); | 184 permitted_extensions_.insert(extension->id()); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // This only sends the response to the renderer - the process of adding the | 313 // This only sends the response to the renderer - the process of adding the |
317 // extension to the list of |permitted_extensions_| is done elsewhere. | 314 // extension to the list of |permitted_extensions_| is done elsewhere. |
318 content::RenderViewHost* render_view_host = | 315 content::RenderViewHost* render_view_host = |
319 web_contents()->GetRenderViewHost(); | 316 web_contents()->GetRenderViewHost(); |
320 if (render_view_host) { | 317 if (render_view_host) { |
321 render_view_host->Send(new ExtensionMsg_PermitScriptInjection( | 318 render_view_host->Send(new ExtensionMsg_PermitScriptInjection( |
322 render_view_host->GetRoutingID(), request_id)); | 319 render_view_host->GetRoutingID(), request_id)); |
323 } | 320 } |
324 } | 321 } |
325 | 322 |
326 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { | |
327 bool handled = true; | |
328 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) | |
329 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestScriptInjectionPermission, | |
330 OnRequestScriptInjectionPermission) | |
331 IPC_MESSAGE_UNHANDLED(handled = false) | |
332 IPC_END_MESSAGE_MAP() | |
333 return handled; | |
334 } | |
335 | |
336 void ActiveScriptController::LogUMA() const { | 323 void ActiveScriptController::LogUMA() const { |
337 UMA_HISTOGRAM_COUNTS_100( | 324 UMA_HISTOGRAM_COUNTS_100( |
338 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | 325 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", |
339 pending_requests_.size()); | 326 pending_requests_.size()); |
340 | 327 |
341 // We only log the permitted extensions metric if the feature is enabled, | 328 // We only log the permitted extensions metric if the feature is enabled, |
342 // because otherwise the data will be boring (100% allowed). | 329 // because otherwise the data will be boring (100% allowed). |
343 if (enabled_) { | 330 if (enabled_) { |
344 UMA_HISTOGRAM_COUNTS_100( | 331 UMA_HISTOGRAM_COUNTS_100( |
345 "Extensions.ActiveScriptController.PermittedExtensions", | 332 "Extensions.ActiveScriptController.PermittedExtensions", |
346 permitted_extensions_.size()); | 333 permitted_extensions_.size()); |
347 UMA_HISTOGRAM_COUNTS_100( | 334 UMA_HISTOGRAM_COUNTS_100( |
348 "Extensions.ActiveScriptController.DeniedExtensions", | 335 "Extensions.ActiveScriptController.DeniedExtensions", |
349 pending_requests_.size()); | 336 pending_requests_.size()); |
350 } | 337 } |
351 } | 338 } |
352 | 339 |
| 340 bool ActiveScriptController::OnMessageReceived(const IPC::Message& message) { |
| 341 bool handled = true; |
| 342 IPC_BEGIN_MESSAGE_MAP(ActiveScriptController, message) |
| 343 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestScriptInjectionPermission, |
| 344 OnRequestScriptInjectionPermission) |
| 345 IPC_MESSAGE_UNHANDLED(handled = false) |
| 346 IPC_END_MESSAGE_MAP() |
| 347 return handled; |
| 348 } |
| 349 |
353 void ActiveScriptController::DidNavigateMainFrame( | 350 void ActiveScriptController::DidNavigateMainFrame( |
354 const content::LoadCommittedDetails& details, | 351 const content::LoadCommittedDetails& details, |
355 const content::FrameNavigateParams& params) { | 352 const content::FrameNavigateParams& params) { |
356 if (details.is_in_page) | 353 if (details.is_in_page) |
357 return; | 354 return; |
358 | 355 |
359 LogUMA(); | 356 LogUMA(); |
360 permitted_extensions_.clear(); | 357 permitted_extensions_.clear(); |
361 pending_requests_.clear(); | 358 pending_requests_.clear(); |
362 } | 359 } |
363 | 360 |
| 361 void ActiveScriptController::OnExtensionUnloaded( |
| 362 content::BrowserContext* browser_context, |
| 363 const Extension* extension, |
| 364 UnloadedExtensionInfo::Reason reason) { |
| 365 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); |
| 366 if (iter != pending_requests_.end()) { |
| 367 pending_requests_.erase(iter); |
| 368 ExtensionActionAPI::Get(web_contents()->GetBrowserContext())-> |
| 369 NotifyPageActionsChanged(web_contents()); |
| 370 } |
| 371 } |
| 372 |
364 } // namespace extensions | 373 } // namespace extensions |
OLD | NEW |