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_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 = Profile::FromBrowserContext( |
55 ExtensionToolbarModel::Get( | 56 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 ExtensionIdList& toolbar_extensions = toolbar_model->toolbar_items(); |
58 ExtensionActionManager* action_manager = | 59 ExtensionActionManager* action_manager = |
59 ExtensionActionManager::Get(web_contents->GetBrowserContext()); | 60 ExtensionActionManager::Get(web_contents->GetBrowserContext()); |
60 for (ExtensionList::const_iterator iter = toolbar_extensions.begin(); | 61 for (ExtensionIdList::const_iterator iter = toolbar_extensions.begin(); |
mark a. foltz
2015/08/05 20:21:53
Replace with range loop.
apacible
2015/08/05 23:21:43
Done.
| |
61 iter != toolbar_extensions.end(); ++iter) { | 62 iter != toolbar_extensions.end(); ++iter) { |
62 ExtensionAction* extension_action = action_manager->GetPageAction(**iter); | 63 const Extension* extension = |
64 ExtensionRegistry::Get(profile)->enabled_extensions().GetByID( | |
65 (*iter)); | |
mark a. foltz
2015/08/05 20:21:53
Extra ().
apacible
2015/08/05 23:21:43
Done.
| |
66 ExtensionAction* extension_action = | |
67 action_manager->GetPageAction(*extension); | |
63 if (extension_action && | 68 if (extension_action && |
64 (!only_count_visible || extension_action->GetIsVisible(tab_id))) | 69 (!only_count_visible || extension_action->GetIsVisible(tab_id))) |
65 ++count; | 70 ++count; |
66 } | 71 } |
67 } | 72 } |
68 | 73 |
69 return count; | 74 return count; |
70 } | 75 } |
71 | 76 |
72 // Creates a new ExtensionToolbarModel for the given |context|. | 77 // Creates a new ToolbarActionsModel for the given |context|. |
73 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) { | 78 scoped_ptr<KeyedService> BuildToolbarModel(content::BrowserContext* context) { |
74 return make_scoped_ptr(new extensions::ExtensionToolbarModel( | 79 return make_scoped_ptr(new ToolbarActionsModel( |
75 Profile::FromBrowserContext(context), | 80 Profile::FromBrowserContext(context), |
76 extensions::ExtensionPrefs::Get(context))); | 81 extensions::ExtensionPrefs::Get(context))); |
77 } | 82 } |
78 | 83 |
79 // Creates a new ExtensionToolbarModel for the given profile, optionally | 84 // Creates a new ToolbarActionsModel for the given profile, optionally |
80 // triggering the extension system's ready signal. | 85 // triggering the extension system's ready signal. |
81 ExtensionToolbarModel* CreateToolbarModelImpl(Profile* profile, | 86 ToolbarActionsModel* CreateToolbarModelImpl(Profile* profile, |
82 bool wait_for_ready) { | 87 bool wait_for_ready) { |
83 ExtensionToolbarModel* model = ExtensionToolbarModel::Get(profile); | 88 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile); |
84 if (model) | 89 if (model) |
85 return model; | 90 return model; |
86 | 91 |
87 // No existing model means it's a new profile (since we, by default, don't | 92 // No existing model means it's a new profile (since we, by default, don't |
88 // create the ToolbarModel in testing). | 93 // create the ToolbarModel in testing). |
89 ExtensionToolbarModelFactory::GetInstance()->SetTestingFactory( | 94 ToolbarActionsModelFactory::GetInstance()->SetTestingFactory( |
90 profile, &BuildToolbarModel); | 95 profile, &BuildToolbarModel); |
91 model = ExtensionToolbarModel::Get(profile); | 96 model = ToolbarActionsModel::Get(profile); |
92 if (wait_for_ready) { | 97 if (wait_for_ready) { |
93 // Fake the extension system ready signal. | 98 // Fake the extension system ready signal. |
94 // HACK ALERT! In production, the ready task on ExtensionSystem (and most | 99 // HACK ALERT! In production, the ready task on ExtensionSystem (and most |
95 // everything else on it, too) is shared between incognito and normal | 100 // everything else on it, too) is shared between incognito and normal |
96 // profiles, but a TestExtensionSystem doesn't have the concept of "shared". | 101 // 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 | 102 // Because of this, we have to set any new profile's TestExtensionSystem's |
98 // ready task, too. | 103 // ready task, too. |
99 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> | 104 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile))-> |
100 SetReady(); | 105 SetReady(); |
101 // Run tasks posted to TestExtensionSystem. | 106 // Run tasks posted to TestExtensionSystem. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 | 149 |
145 if (action_key) | 150 if (action_key) |
146 manifest.Set(action_key, DictionaryBuilder().Pass()); | 151 manifest.Set(action_key, DictionaryBuilder().Pass()); |
147 | 152 |
148 return ExtensionBuilder().SetManifest(manifest.Pass()). | 153 return ExtensionBuilder().SetManifest(manifest.Pass()). |
149 SetID(crx_file::id_util::GenerateId(name)). | 154 SetID(crx_file::id_util::GenerateId(name)). |
150 SetLocation(location). | 155 SetLocation(location). |
151 Build(); | 156 Build(); |
152 } | 157 } |
153 | 158 |
154 ExtensionToolbarModel* CreateToolbarModelForProfile(Profile* profile) { | 159 ToolbarActionsModel* CreateToolbarModelForProfile(Profile* profile) { |
155 return CreateToolbarModelImpl(profile, true); | 160 return CreateToolbarModelImpl(profile, true); |
156 } | 161 } |
157 | 162 |
158 ExtensionToolbarModel* CreateToolbarModelForProfileWithoutWaitingForReady( | 163 ToolbarActionsModel* CreateToolbarModelForProfileWithoutWaitingForReady( |
159 Profile* profile) { | 164 Profile* profile) { |
160 return CreateToolbarModelImpl(profile, false); | 165 return CreateToolbarModelImpl(profile, false); |
161 } | 166 } |
162 | 167 |
163 } // namespace extension_action_test_util | 168 } // namespace extension_action_test_util |
164 } // namespace extensions | 169 } // namespace extensions |
OLD | NEW |