| 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" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 12 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
| 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 14 #include "chrome/browser/extensions/extension_action.h" | 14 #include "chrome/browser/extensions/extension_action.h" |
| 15 #include "chrome/browser/extensions/extension_action_manager.h" | 15 #include "chrome/browser/extensions/extension_action_manager.h" |
| 16 #include "chrome/browser/extensions/extension_util.h" | 16 #include "chrome/browser/extensions/extension_util.h" |
| 17 #include "chrome/browser/extensions/location_bar_controller.h" | |
| 18 #include "chrome/browser/extensions/permissions_updater.h" | 17 #include "chrome/browser/extensions/permissions_updater.h" |
| 19 #include "chrome/browser/extensions/tab_helper.h" | 18 #include "chrome/browser/extensions/tab_helper.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/sessions/session_tab_helper.h" | 20 #include "chrome/browser/sessions/session_tab_helper.h" |
| 22 #include "chrome/common/extensions/api/extension_action/action_info.h" | 21 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 23 #include "components/crx_file/id_util.h" | 22 #include "components/crx_file/id_util.h" |
| 24 #include "content/public/browser/navigation_controller.h" | 23 #include "content/public/browser/navigation_controller.h" |
| 25 #include "content/public/browser/navigation_details.h" | 24 #include "content/public/browser/navigation_details.h" |
| 26 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
| 27 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 ActiveScriptController::~ActiveScriptController() { | 70 ActiveScriptController::~ActiveScriptController() { |
| 72 LogUMA(); | 71 LogUMA(); |
| 73 } | 72 } |
| 74 | 73 |
| 75 // static | 74 // static |
| 76 ActiveScriptController* ActiveScriptController::GetForWebContents( | 75 ActiveScriptController* ActiveScriptController::GetForWebContents( |
| 77 content::WebContents* web_contents) { | 76 content::WebContents* web_contents) { |
| 78 if (!web_contents) | 77 if (!web_contents) |
| 79 return NULL; | 78 return NULL; |
| 80 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); | 79 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); |
| 81 if (!tab_helper) | 80 return tab_helper ? tab_helper->active_script_controller() : NULL; |
| 82 return NULL; | |
| 83 LocationBarController* location_bar_controller = | |
| 84 tab_helper->location_bar_controller(); | |
| 85 // This should never be NULL. | |
| 86 DCHECK(location_bar_controller); | |
| 87 return location_bar_controller->active_script_controller(); | |
| 88 } | 81 } |
| 89 | 82 |
| 90 void ActiveScriptController::OnActiveTabPermissionGranted( | 83 void ActiveScriptController::OnActiveTabPermissionGranted( |
| 91 const Extension* extension) { | 84 const Extension* extension) { |
| 92 RunPendingForExtension(extension); | 85 RunPendingForExtension(extension); |
| 93 } | 86 } |
| 94 | 87 |
| 95 void ActiveScriptController::OnAdInjectionDetected( | 88 void ActiveScriptController::OnAdInjectionDetected( |
| 96 const std::set<std::string>& ad_injectors) { | 89 const std::set<std::string>& ad_injectors) { |
| 97 // We're only interested in data if there are ad injectors detected. | 90 // We're only interested in data if there are ad injectors detected. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 135 |
| 143 // Allow current tab to run injection. | 136 // Allow current tab to run injection. |
| 144 OnClicked(extension); | 137 OnClicked(extension); |
| 145 } | 138 } |
| 146 | 139 |
| 147 void ActiveScriptController::OnClicked(const Extension* extension) { | 140 void ActiveScriptController::OnClicked(const Extension* extension) { |
| 148 DCHECK(ContainsKey(pending_requests_, extension->id())); | 141 DCHECK(ContainsKey(pending_requests_, extension->id())); |
| 149 RunPendingForExtension(extension); | 142 RunPendingForExtension(extension); |
| 150 } | 143 } |
| 151 | 144 |
| 152 bool ActiveScriptController::HasActiveScriptAction(const Extension* extension) { | 145 bool ActiveScriptController::WantsToRun(const Extension* extension) { |
| 153 return enabled_ && pending_requests_.count(extension->id()) > 0; | 146 return enabled_ && pending_requests_.count(extension->id()) > 0; |
| 154 } | 147 } |
| 155 | 148 |
| 156 ExtensionAction* ActiveScriptController::GetActionForExtension( | |
| 157 const Extension* extension) { | |
| 158 if (!enabled_ || pending_requests_.count(extension->id()) == 0) | |
| 159 return NULL; // No action for this extension. | |
| 160 | |
| 161 ActiveScriptMap::iterator existing = | |
| 162 active_script_actions_.find(extension->id()); | |
| 163 if (existing != active_script_actions_.end()) | |
| 164 return existing->second.get(); | |
| 165 | |
| 166 linked_ptr<ExtensionAction> action(ExtensionActionManager::Get( | |
| 167 Profile::FromBrowserContext(web_contents()->GetBrowserContext())) | |
| 168 ->GetBestFitAction(*extension, ActionInfo::TYPE_PAGE).release()); | |
| 169 action->SetIsVisible(ExtensionAction::kDefaultTabId, true); | |
| 170 | |
| 171 active_script_actions_[extension->id()] = action; | |
| 172 return action.get(); | |
| 173 } | |
| 174 | |
| 175 PermissionsData::AccessType | 149 PermissionsData::AccessType |
| 176 ActiveScriptController::RequiresUserConsentForScriptInjection( | 150 ActiveScriptController::RequiresUserConsentForScriptInjection( |
| 177 const Extension* extension, | 151 const Extension* extension, |
| 178 UserScript::InjectionType type) { | 152 UserScript::InjectionType type) { |
| 179 CHECK(extension); | 153 CHECK(extension); |
| 180 | 154 |
| 181 // If the feature is not enabled, we automatically allow all extensions to | 155 // If the feature is not enabled, we automatically allow all extensions to |
| 182 // run scripts. | 156 // run scripts. |
| 183 if (!enabled_) | 157 if (!enabled_) |
| 184 permitted_extensions_.insert(extension->id()); | 158 permitted_extensions_.insert(extension->id()); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 UnloadedExtensionInfo::Reason reason) { | 338 UnloadedExtensionInfo::Reason reason) { |
| 365 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); | 339 PendingRequestMap::iterator iter = pending_requests_.find(extension->id()); |
| 366 if (iter != pending_requests_.end()) { | 340 if (iter != pending_requests_.end()) { |
| 367 pending_requests_.erase(iter); | 341 pending_requests_.erase(iter); |
| 368 ExtensionActionAPI::Get(web_contents()->GetBrowserContext())-> | 342 ExtensionActionAPI::Get(web_contents()->GetBrowserContext())-> |
| 369 NotifyPageActionsChanged(web_contents()); | 343 NotifyPageActionsChanged(web_contents()); |
| 370 } | 344 } |
| 371 } | 345 } |
| 372 | 346 |
| 373 } // namespace extensions | 347 } // namespace extensions |
| OLD | NEW |