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/location_bar_controller.h" | 5 #include "chrome/browser/extensions/location_bar_controller.h" |
6 | 6 |
7 #include "chrome/browser/extensions/active_script_controller.h" | 7 #include "chrome/browser/extensions/active_script_controller.h" |
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
9 #include "chrome/browser/extensions/extension_action_manager.h" | 9 #include "chrome/browser/extensions/extension_action_manager.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "extensions/browser/extension_registry.h" | 11 #include "extensions/browser/extension_registry.h" |
12 #include "extensions/common/feature_switch.h" | 12 #include "extensions/common/feature_switch.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 LocationBarController::LocationBarController( | 16 LocationBarController::LocationBarController( |
17 content::WebContents* web_contents) | 17 content::WebContents* web_contents) |
18 : web_contents_(web_contents), | 18 : web_contents_(web_contents), |
19 browser_context_(web_contents->GetBrowserContext()), | 19 browser_context_(web_contents->GetBrowserContext()), |
20 action_manager_(ExtensionActionManager::Get(browser_context_)), | 20 action_manager_(ExtensionActionManager::Get(browser_context_)), |
21 should_show_page_actions_( | 21 should_show_page_actions_( |
22 !FeatureSwitch::extension_action_redesign()->IsEnabled()), | 22 !FeatureSwitch::extension_action_redesign()->IsEnabled()), |
23 active_script_controller_(new ActiveScriptController(web_contents_)), | |
24 extension_registry_observer_(this) { | 23 extension_registry_observer_(this) { |
25 if (should_show_page_actions_) | 24 if (should_show_page_actions_) |
26 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 25 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
27 } | 26 } |
28 | 27 |
29 LocationBarController::~LocationBarController() { | 28 LocationBarController::~LocationBarController() { |
30 } | 29 } |
31 | 30 |
32 std::vector<ExtensionAction*> LocationBarController::GetCurrentActions() { | 31 std::vector<ExtensionAction*> LocationBarController::GetCurrentActions() { |
33 const ExtensionSet& extensions = | 32 const ExtensionSet& extensions = |
34 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); | 33 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); |
35 std::vector<ExtensionAction*> current_actions; | 34 std::vector<ExtensionAction*> current_actions; |
36 if (!should_show_page_actions_) | 35 if (!should_show_page_actions_) |
37 return current_actions; | 36 return current_actions; |
38 | 37 |
| 38 ActiveScriptController* active_script_controller = |
| 39 ActiveScriptController::GetForWebContents(web_contents_); |
39 for (ExtensionSet::const_iterator iter = extensions.begin(); | 40 for (ExtensionSet::const_iterator iter = extensions.begin(); |
40 iter != extensions.end(); | 41 iter != extensions.end(); |
41 ++iter) { | 42 ++iter) { |
42 // Right now, we can consolidate these actions because we only want to show | 43 // Right now, we can consolidate these actions because we only want to show |
43 // one action per extension. If clicking on an active script action ever | 44 // one action per extension. If clicking on an active script action ever |
44 // has a response, then we will need to split the actions. | 45 // has a response, then we will need to split the actions. |
45 ExtensionAction* action = action_manager_->GetPageAction(**iter); | 46 ExtensionAction* action = action_manager_->GetPageAction(**iter); |
46 if (!action) | 47 if (!action && active_script_controller->WantsToRun(*iter)) { |
47 action = active_script_controller_->GetActionForExtension(iter->get()); | 48 ExtensionActionMap::iterator existing = |
| 49 active_script_actions_.find((*iter)->id()); |
| 50 if (existing != active_script_actions_.end()) { |
| 51 action = existing->second.get(); |
| 52 } else { |
| 53 linked_ptr<ExtensionAction> active_script_action( |
| 54 ExtensionActionManager::Get(browser_context_)-> |
| 55 GetBestFitAction(**iter, ActionInfo::TYPE_PAGE).release()); |
| 56 active_script_action->SetIsVisible( |
| 57 ExtensionAction::kDefaultTabId, true); |
| 58 active_script_actions_[(*iter)->id()] = active_script_action; |
| 59 action = active_script_action.get(); |
| 60 } |
| 61 } |
| 62 |
48 if (action) | 63 if (action) |
49 current_actions.push_back(action); | 64 current_actions.push_back(action); |
50 } | 65 } |
51 | 66 |
52 return current_actions; | 67 return current_actions; |
53 } | 68 } |
54 | 69 |
55 void LocationBarController::OnExtensionLoaded( | 70 void LocationBarController::OnExtensionLoaded( |
56 content::BrowserContext* browser_context, | 71 content::BrowserContext* browser_context, |
57 const Extension* extension) { | 72 const Extension* extension) { |
58 if (action_manager_->GetPageAction(*extension) || | 73 if (action_manager_->GetPageAction(*extension)) { |
59 active_script_controller_->GetActionForExtension(extension)) { | |
60 ExtensionActionAPI::Get(browser_context)-> | 74 ExtensionActionAPI::Get(browser_context)-> |
61 NotifyPageActionsChanged(web_contents_); | 75 NotifyPageActionsChanged(web_contents_); |
62 } | 76 } |
63 } | 77 } |
64 | 78 |
65 void LocationBarController::OnExtensionUnloaded( | 79 void LocationBarController::OnExtensionUnloaded( |
66 content::BrowserContext* browser_context, | 80 content::BrowserContext* browser_context, |
67 const Extension* extension, | 81 const Extension* extension, |
68 UnloadedExtensionInfo::Reason reason) { | 82 UnloadedExtensionInfo::Reason reason) { |
69 if (action_manager_->GetPageAction(*extension)) { | 83 if (action_manager_->GetPageAction(*extension)) { |
70 ExtensionActionAPI::Get(browser_context)-> | 84 ExtensionActionAPI::Get(browser_context)-> |
71 NotifyPageActionsChanged(web_contents_); | 85 NotifyPageActionsChanged(web_contents_); |
72 } | 86 } |
73 } | 87 } |
74 | 88 |
75 } // namespace extensions | 89 } // namespace extensions |
OLD | NEW |