Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / location_bar_controller.cc
blobf32d633697e39672cbea55f969f1fd45babb047f
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/location_bar_controller.h"
7 #include <algorithm>
9 #include "chrome/browser/extensions/active_script_controller.h"
10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/location_bar/location_bar.h"
16 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/feature_switch.h"
20 #include "extensions/common/permissions/api_permission.h"
21 #include "extensions/common/permissions/permissions_data.h"
23 namespace extensions {
25 LocationBarController::LocationBarController(
26 content::WebContents* web_contents)
27 : web_contents_(web_contents),
28 browser_context_(web_contents->GetBrowserContext()),
29 action_manager_(ExtensionActionManager::Get(browser_context_)),
30 should_show_page_actions_(
31 !FeatureSwitch::extension_action_redesign()->IsEnabled()),
32 extension_registry_observer_(this) {
33 if (should_show_page_actions_)
34 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
37 LocationBarController::~LocationBarController() {
40 std::vector<ExtensionAction*> LocationBarController::GetCurrentActions() {
41 const ExtensionSet& extensions =
42 ExtensionRegistry::Get(browser_context_)->enabled_extensions();
43 std::vector<ExtensionAction*> current_actions;
44 if (!should_show_page_actions_)
45 return current_actions;
47 ActiveScriptController* active_script_controller =
48 ActiveScriptController::GetForWebContents(web_contents_);
49 for (const scoped_refptr<const Extension>& extension: extensions) {
50 // Right now, we can consolidate these actions because we only want to show
51 // one action per extension. If clicking on an active script action ever
52 // has a response, then we will need to split the actions.
53 ExtensionAction* action = action_manager_->GetPageAction(*extension);
54 if (!action && active_script_controller->WantsToRun(extension.get())) {
55 ExtensionActionMap::iterator existing =
56 active_script_actions_.find(extension->id());
57 if (existing != active_script_actions_.end()) {
58 action = existing->second.get();
59 } else {
60 linked_ptr<ExtensionAction> active_script_action(
61 ExtensionActionManager::Get(browser_context_)->
62 GetBestFitAction(*extension, ActionInfo::TYPE_PAGE).release());
63 active_script_action->SetIsVisible(
64 ExtensionAction::kDefaultTabId, true);
65 active_script_actions_[extension->id()] = active_script_action;
66 action = active_script_action.get();
70 if (action)
71 current_actions.push_back(action);
74 // Sort by id to guarantee the extension actions are returned in the same
75 // order every time this function is called.
76 std::sort(current_actions.begin(), current_actions.end(),
77 [](ExtensionAction* a, ExtensionAction* b) {
78 return a->extension_id() < b->extension_id();
79 });
80 // Move extensions with BookmarkManagerPrivate permission to the start. This
81 // is to ensure they will always end up rightmost on the location bar.
82 std::stable_partition(
83 current_actions.begin(), current_actions.end(),
84 [&extensions](ExtensionAction* extension_action) {
85 return extensions.GetByID(extension_action->extension_id())
86 ->permissions_data()
87 ->HasAPIPermission(
88 extensions::APIPermission::kBookmarkManagerPrivate);
89 });
90 return current_actions;
93 void LocationBarController::OnExtensionLoaded(
94 content::BrowserContext* browser_context,
95 const Extension* extension) {
96 if (action_manager_->GetPageAction(*extension)) {
97 ExtensionActionAPI::Get(browser_context)->
98 NotifyPageActionsChanged(web_contents_);
101 // We might also need to update the location bar if the extension can remove
102 // the bookmark star.
103 if (UIOverrides::RemovesBookmarkButton(extension)) {
104 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
105 // In a perfect world, this can never be NULL. Unfortunately, since a
106 // LocationBarController is attached to most WebContents, we can't make that
107 // guarantee.
108 if (!browser)
109 return;
110 // window() can be NULL if this is called before CreateBrowserWindow()
111 // completes, and there won't be a location bar if the window has no toolbar
112 // (e.g., and app window).
113 LocationBar* location_bar =
114 browser->window() ? browser->window()->GetLocationBar() : NULL;
115 if (!location_bar)
116 return;
117 location_bar->UpdateBookmarkStarVisibility();
121 void LocationBarController::OnExtensionUnloaded(
122 content::BrowserContext* browser_context,
123 const Extension* extension,
124 UnloadedExtensionInfo::Reason reason) {
125 if (action_manager_->GetPageAction(*extension)) {
126 ExtensionActionAPI::Get(browser_context)->
127 NotifyPageActionsChanged(web_contents_);
131 } // namespace extensions