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. 4 5 #include "chrome/browser/extensions/extension_action_test_util.h" 6 7 #include "chrome/browser/extensions/extension_action.h" 8 #include "chrome/browser/extensions/extension_action_manager.h" 9 #include "chrome/browser/extensions/extension_toolbar_model.h" 10 #include "chrome/browser/extensions/location_bar_controller.h" 11 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/sessions/session_tab_helper.h" 14 #include "content/public/browser/web_contents.h" 15 #include "extensions/common/extension.h" 16 #include "extensions/common/feature_switch.h" 17 18 namespace extensions { 19 namespace extension_action_test_util { 20 21 namespace { 22 23 size_t GetPageActionCount(content::WebContents* web_contents, 24 bool only_count_visible) { 25 DCHECK(web_contents); 26 size_t count = 0u; 27 int tab_id = SessionTabHelper::IdForTab(web_contents); 28 // Page actions are either stored in the location bar (and provided by the 29 // LocationBarController), or in the main toolbar (and provided by the 30 // ExtensionToolbarModel), depending on whether or not the extension action 31 // redesign is enabled. 32 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) { 33 std::vector<ExtensionAction*> page_actions = 34 TabHelper::FromWebContents(web_contents)-> 35 location_bar_controller()->GetCurrentActions(); 36 count = page_actions.size(); 37 // Trim any invisible page actions, if necessary. 38 if (only_count_visible) { 39 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin(); 40 iter != page_actions.end(); ++iter) { 41 if (!(*iter)->GetIsVisible(tab_id)) 42 --count; 43 } 44 } 45 } else { 46 ExtensionToolbarModel* toolbar_model = 47 ExtensionToolbarModel::Get( 48 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 49 const ExtensionList& toolbar_extensions = toolbar_model->toolbar_items(); 50 ExtensionActionManager* action_manager = 51 ExtensionActionManager::Get(web_contents->GetBrowserContext()); 52 for (ExtensionList::const_iterator iter = toolbar_extensions.begin(); 53 iter != toolbar_extensions.end(); ++iter) { 54 ExtensionAction* extension_action = action_manager->GetPageAction(**iter); 55 if (extension_action && 56 (!only_count_visible || extension_action->GetIsVisible(tab_id))) 57 ++count; 58 } 59 } 60 61 return count; 62 } 63 64 } // namespace 65 66 size_t GetVisiblePageActionCount(content::WebContents* web_contents) { 67 return GetPageActionCount(web_contents, true); 68 } 69 70 size_t GetTotalPageActionCount(content::WebContents* web_contents) { 71 return GetPageActionCount(web_contents, false); 72 } 73 74 } // namespace extension_action_test_util 75 } // namespace extensions 76