Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1042)

Side by Side Diff: chrome/browser/extensions/extension_action_test_util.cc

Issue 1241063003: Support Component Actions in the toolbar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch GetComponentActionId to unix_hacker_style. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_test_util.h" 5 #include "chrome/browser/extensions/extension_action_test_util.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "chrome/browser/extensions/extension_action.h" 9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h" 10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_toolbar_model.h"
12 #include "chrome/browser/extensions/extension_toolbar_model_factory.h"
13 #include "chrome/browser/extensions/location_bar_controller.h" 11 #include "chrome/browser/extensions/location_bar_controller.h"
14 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/extensions/test_extension_system.h" 13 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sessions/session_tab_helper.h" 15 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
17 #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
18 #include "components/crx_file/id_util.h" 18 #include "components/crx_file/id_util.h"
19 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_builder.h" 22 #include "extensions/common/extension_builder.h"
22 #include "extensions/common/feature_switch.h" 23 #include "extensions/common/feature_switch.h"
23 #include "extensions/common/manifest_constants.h" 24 #include "extensions/common/manifest_constants.h"
24 #include "extensions/common/value_builder.h" 25 #include "extensions/common/value_builder.h"
25 26
26 namespace extensions { 27 namespace extensions {
27 namespace extension_action_test_util { 28 namespace extension_action_test_util {
28 29
29 namespace { 30 namespace {
30 31
31 size_t GetPageActionCount(content::WebContents* web_contents, 32 size_t GetPageActionCount(content::WebContents* web_contents,
32 bool only_count_visible) { 33 bool only_count_visible) {
33 DCHECK(web_contents); 34 DCHECK(web_contents);
34 size_t count = 0u; 35 size_t count = 0u;
35 int tab_id = SessionTabHelper::IdForTab(web_contents); 36 int tab_id = SessionTabHelper::IdForTab(web_contents);
36 // Page actions are either stored in the location bar (and provided by the 37 // Page actions are either stored in the location bar (and provided by the
37 // LocationBarController), or in the main toolbar (and provided by the 38 // LocationBarController), or in the main toolbar (and provided by the
38 // ExtensionToolbarModel), depending on whether or not the extension action 39 // ToolbarActionsModel), depending on whether or not the extension action
39 // redesign is enabled. 40 // redesign is enabled.
40 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) { 41 if (!FeatureSwitch::extension_action_redesign()->IsEnabled()) {
41 std::vector<ExtensionAction*> page_actions = 42 std::vector<ExtensionAction*> page_actions =
42 TabHelper::FromWebContents(web_contents)-> 43 TabHelper::FromWebContents(web_contents)->
43 location_bar_controller()->GetCurrentActions(); 44 location_bar_controller()->GetCurrentActions();
44 count = page_actions.size(); 45 count = page_actions.size();
45 // Trim any invisible page actions, if necessary. 46 // Trim any invisible page actions, if necessary.
46 if (only_count_visible) { 47 if (only_count_visible) {
47 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin(); 48 for (std::vector<ExtensionAction*>::iterator iter = page_actions.begin();
48 iter != page_actions.end(); ++iter) { 49 iter != page_actions.end(); ++iter) {
49 if (!(*iter)->GetIsVisible(tab_id)) 50 if (!(*iter)->GetIsVisible(tab_id))
50 --count; 51 --count;
51 } 52 }
52 } 53 }
53 } else { 54 } else {
54 ExtensionToolbarModel* toolbar_model = 55 Profile* profile =
55 ExtensionToolbarModel::Get( 56 Profile::FromBrowserContext(web_contents->GetBrowserContext());
56 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 57 ToolbarActionsModel* toolbar_model = ToolbarActionsModel::Get(profile);
57 const ExtensionList& toolbar_extensions = toolbar_model->toolbar_items(); 58 const std::vector<ToolbarActionsModel::ToolbarItem>& toolbar_items =
59 toolbar_model->toolbar_items();
58 ExtensionActionManager* action_manager = 60 ExtensionActionManager* action_manager =
59 ExtensionActionManager::Get(web_contents->GetBrowserContext()); 61 ExtensionActionManager::Get(web_contents->GetBrowserContext());
60 for (ExtensionList::const_iterator iter = toolbar_extensions.begin(); 62 for (const ToolbarActionsModel::ToolbarItem& item : toolbar_items) {
61 iter != toolbar_extensions.end(); ++iter) { 63 if (item.type == ToolbarActionsModel::EXTENSION_ACTION) {
62 ExtensionAction* extension_action = action_manager->GetPageAction(**iter); 64 const Extension* extension =
63 if (extension_action && 65 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID(
64 (!only_count_visible || extension_action->GetIsVisible(tab_id))) 66 item.id);
65 ++count; 67 ExtensionAction* extension_action =
68 action_manager->GetPageAction(*extension);
69 if (extension_action &&
70 (!only_count_visible || extension_action->GetIsVisible(tab_id)))
71 ++count;
72 }
66 } 73 }
67 } 74 }
68 75
69 return count; 76 return count;
70 } 77 }
71 78
72 // Creates a new ExtensionToolbarModel for the given |context|. 79 // Creates a new ToolbarActionsModel for the given |context|.
73 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) { 80 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) {
74 return make_scoped_ptr(new extensions::ExtensionToolbarModel( 81 return make_scoped_ptr(
75 Profile::FromBrowserContext(context), 82 new ToolbarActionsModel(Profile::FromBrowserContext(context),
76 extensions::ExtensionPrefs::Get(context))); 83 extensions::ExtensionPrefs::Get(context)));
77 } 84 }
78 85
79 // Creates a new ExtensionToolbarModel for the given profile, optionally 86 // Creates a new ToolbarActionsModel for the given profile, optionally
80 // triggering the extension system's ready signal. 87 // triggering the extension system's ready signal.
81 ExtensionToolbarModel* CreateToolbarModelImpl(Profile* profile, 88 ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile,
82 bool wait_for_ready) { 89 bool wait_for_ready) {
83 ExtensionToolbarModel* model = ExtensionToolbarModel::Get(profile); 90 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
84 if (model) 91 if (model)
85 return model; 92 return model;
86 93
87 // No existing model means it's a new profile (since we, by default, don't 94 // No existing model means it's a new profile (since we, by default, don't
88 // create the ToolbarModel in testing). 95 // create the ToolbarModel in testing).
89 ExtensionToolbarModelFactory::GetInstance()->SetTestingFactory( 96 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory(
90 profile, &BuildToolbarModel); 97 profile, &BuildToolbarModel);
91 model = ExtensionToolbarModel::Get(profile); 98 model = ToolbarActionsModel::Get(profile);
92 if (wait_for_ready) { 99 if (wait_for_ready) {
93 // Fake the extension system ready signal. 100 // Fake the extension system ready signal.
94 // HACK ALERT! In production, the ready task on ExtensionSystem (and most 101 // HACK ALERT! In production, the ready task on ExtensionSystem (and most
95 // everything else on it, too) is shared between incognito and normal 102 // everything else on it, too) is shared between incognito and normal
96 // profiles, but a TestExtensionSystem doesn't have the concept of "shared". 103 // profiles, but a TestExtensionSystem doesn't have the concept of "shared".
97 // Because of this, we have to set any new profile's TestExtensionSystem's 104 // Because of this, we have to set any new profile's TestExtensionSystem's
98 // ready task, too. 105 // ready task, too.
99 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> 106 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))->
100 SetReady(); 107 SetReady();
101 // Run tasks posted to TestExtensionSystem. 108 // Run tasks posted to TestExtensionSystem.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 151
145 if (action_key) 152 if (action_key)
146 manifest.Set(action_key, DictionaryBuilder().Pass()); 153 manifest.Set(action_key, DictionaryBuilder().Pass());
147 154
148 return ExtensionBuilder().SetManifest(manifest.Pass()). 155 return ExtensionBuilder().SetManifest(manifest.Pass()).
149 SetID(crx_file::id_util::GenerateId(name)). 156 SetID(crx_file::id_util::GenerateId(name)).
150 SetLocation(location). 157 SetLocation(location).
151 Build(); 158 Build();
152 } 159 }
153 160
154 ExtensionToolbarModel* CreateToolbarModelForProfile(Profile* profile) { 161 ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) {
155 return CreateToolbarModelImpl(profile, true); 162 return CreateToolbarModelImpl(profile, true);
156 } 163 }
157 164
158 ExtensionToolbarModel* CreateToolbarModelForProfileWithoutWaitingForReady( 165 ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady(
159 Profile* profile) { 166 Profile* profile) {
160 return CreateToolbarModelImpl(profile, false); 167 return CreateToolbarModelImpl(profile, false);
161 } 168 }
162 169
163 } // namespace extension_action_test_util 170 } // namespace extension_action_test_util
164 } // namespace extensions 171 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698