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/extension_action_runner.h" | 5 #include "chrome/browser/extensions/extension_action_runner.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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 61 |
62 // static | 62 // static |
63 ExtensionActionRunner* ExtensionActionRunner::GetForWebContents( | 63 ExtensionActionRunner* ExtensionActionRunner::GetForWebContents( |
64 content::WebContents* web_contents) { | 64 content::WebContents* web_contents) { |
65 if (!web_contents) | 65 if (!web_contents) |
66 return NULL; | 66 return NULL; |
67 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); | 67 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents); |
68 return tab_helper ? tab_helper->extension_action_runner() : NULL; | 68 return tab_helper ? tab_helper->extension_action_runner() : NULL; |
69 } | 69 } |
70 | 70 |
71 void ExtensionActionRunner::OnActiveTabPermissionGranted( | 71 ExtensionAction::ShowAction ExtensionActionRunner::RunAction( |
72 const Extension* extension) { | 72 const Extension* extension, |
73 if (WantsToRun(extension)) | 73 bool grant_tab_permissions) { |
74 OnClicked(extension); | 74 bool has_pending_scripts = WantsToRun(extension); |
| 75 if (grant_tab_permissions) { |
| 76 TabHelper::FromWebContents(web_contents()) |
| 77 ->active_tab_permission_granter() |
| 78 ->GrantIfRequested(extension); |
| 79 // If the extension had blocked actions, granting active tab will have |
| 80 // run the extension. Don't execute further since clicking should run |
| 81 // blocked actions *or* the normal extension action, not both. |
| 82 if (has_pending_scripts) |
| 83 return ExtensionAction::ACTION_NONE; |
| 84 } |
| 85 |
| 86 ExtensionAction* extension_action = |
| 87 ExtensionActionManager::Get(browser_context_) |
| 88 ->GetExtensionAction(*extension); |
| 89 |
| 90 // Anything that gets here should have a page or browser action. |
| 91 DCHECK(extension_action); |
| 92 int tab_id = SessionTabHelper::IdForTab(web_contents()); |
| 93 if (!extension_action->GetIsVisible(tab_id)) |
| 94 return ExtensionAction::ACTION_NONE; |
| 95 |
| 96 if (extension_action->HasPopup(tab_id)) |
| 97 return ExtensionAction::ACTION_SHOW_POPUP; |
| 98 |
| 99 ExtensionActionAPI::Get(browser_context_) |
| 100 ->DispatchExtensionActionClicked(*extension_action, web_contents()); |
| 101 return ExtensionAction::ACTION_NONE; |
75 } | 102 } |
76 | 103 |
77 void ExtensionActionRunner::OnClicked(const Extension* extension) { | 104 void ExtensionActionRunner::RunBlockedActions(const Extension* extension) { |
78 DCHECK(ContainsKey(pending_scripts_, extension->id()) || | 105 DCHECK(ContainsKey(pending_scripts_, extension->id()) || |
79 web_request_blocked_.count(extension->id()) != 0); | 106 web_request_blocked_.count(extension->id()) != 0); |
80 | 107 |
81 // Clicking to run the extension counts as granting it permission to run on | 108 // Clicking to run the extension counts as granting it permission to run on |
82 // the given tab. | 109 // the given tab. |
83 // The extension may already have active tab at this point, but granting | 110 // The extension may already have active tab at this point, but granting |
84 // it twice is essentially a no-op. | 111 // it twice is essentially a no-op. |
85 TabHelper::FromWebContents(web_contents()) | 112 TabHelper::FromWebContents(web_contents()) |
86 ->active_tab_permission_granter() | 113 ->active_tab_permission_granter() |
87 ->GrantIfRequested(extension); | 114 ->GrantIfRequested(extension); |
88 | 115 |
89 RunPendingScriptsForExtension(extension); | 116 RunPendingScriptsForExtension(extension); |
90 web_request_blocked_.erase(extension->id()); | 117 web_request_blocked_.erase(extension->id()); |
91 | 118 |
92 // The extension ran, so we need to tell the ExtensionActionAPI that we no | 119 // The extension ran, so we need to tell the ExtensionActionAPI that we no |
93 // longer want to act. | 120 // longer want to act. |
94 NotifyChange(extension); | 121 NotifyChange(extension); |
95 } | 122 } |
96 | 123 |
| 124 void ExtensionActionRunner::OnActiveTabPermissionGranted( |
| 125 const Extension* extension) { |
| 126 if (WantsToRun(extension)) |
| 127 RunBlockedActions(extension); |
| 128 } |
| 129 |
97 void ExtensionActionRunner::OnWebRequestBlocked(const Extension* extension) { | 130 void ExtensionActionRunner::OnWebRequestBlocked(const Extension* extension) { |
98 web_request_blocked_.insert(extension->id()); | 131 web_request_blocked_.insert(extension->id()); |
99 } | 132 } |
100 | 133 |
101 int ExtensionActionRunner::GetBlockedActions(const Extension* extension) { | 134 int ExtensionActionRunner::GetBlockedActions(const Extension* extension) { |
102 int blocked_actions = BLOCKED_ACTION_NONE; | 135 int blocked_actions = BLOCKED_ACTION_NONE; |
103 if (web_request_blocked_.count(extension->id()) != 0) | 136 if (web_request_blocked_.count(extension->id()) != 0) |
104 blocked_actions |= BLOCKED_ACTION_WEB_REQUEST; | 137 blocked_actions |= BLOCKED_ACTION_WEB_REQUEST; |
105 auto iter = pending_scripts_.find(extension->id()); | 138 auto iter = pending_scripts_.find(extension->id()); |
106 if (iter != pending_scripts_.end()) { | 139 if (iter != pending_scripts_.end()) { |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 UnloadedExtensionInfo::Reason reason) { | 344 UnloadedExtensionInfo::Reason reason) { |
312 PendingScriptMap::iterator iter = pending_scripts_.find(extension->id()); | 345 PendingScriptMap::iterator iter = pending_scripts_.find(extension->id()); |
313 if (iter != pending_scripts_.end()) { | 346 if (iter != pending_scripts_.end()) { |
314 pending_scripts_.erase(iter); | 347 pending_scripts_.erase(iter); |
315 ExtensionActionAPI::Get(browser_context_) | 348 ExtensionActionAPI::Get(browser_context_) |
316 ->NotifyPageActionsChanged(web_contents()); | 349 ->NotifyPageActionsChanged(web_contents()); |
317 } | 350 } |
318 } | 351 } |
319 | 352 |
320 } // namespace extensions | 353 } // namespace extensions |
OLD | NEW |