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

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_actions_model.cc

Issue 2613713005: Make ToolbarActionsModel own ComponentToolbarActionsFactory (Closed)
Patch Set: Created 3 years, 11 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/ui/toolbar/toolbar_actions_model.h" 5 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/metrics/histogram_base.h" 11 #include "base/metrics/histogram_base.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/component_migration_helper.h" 17 #include "chrome/browser/extensions/component_migration_helper.h"
18 #include "chrome/browser/extensions/extension_action_manager.h" 18 #include "chrome/browser/extensions/extension_action_manager.h"
19 #include "chrome/browser/extensions/extension_message_bubble_controller.h" 19 #include "chrome/browser/extensions/extension_message_bubble_controller.h"
20 #include "chrome/browser/extensions/extension_tab_util.h" 20 #include "chrome/browser/extensions/extension_tab_util.h"
21 #include "chrome/browser/extensions/extension_util.h" 21 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/tab_helper.h" 22 #include "chrome/browser/extensions/tab_helper.h"
23 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" 25 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
26 #include "chrome/browser/ui/extensions/extension_message_bubble_factory.h" 26 #include "chrome/browser/ui/extensions/extension_message_bubble_factory.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h" 27 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
29 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" 28 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
30 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" 29 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
31 #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h" 30 #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
32 #include "components/prefs/pref_service.h" 31 #include "components/prefs/pref_service.h"
33 #include "content/public/browser/notification_details.h" 32 #include "content/public/browser/notification_details.h"
34 #include "content/public/browser/notification_source.h" 33 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/web_contents.h" 34 #include "content/public/browser/web_contents.h"
36 #include "extensions/browser/extension_registry.h" 35 #include "extensions/browser/extension_registry.h"
37 #include "extensions/browser/extension_system.h" 36 #include "extensions/browser/extension_system.h"
38 #include "extensions/browser/pref_names.h" 37 #include "extensions/browser/pref_names.h"
39 #include "extensions/common/extension_set.h" 38 #include "extensions/common/extension_set.h"
40 #include "extensions/common/feature_switch.h" 39 #include "extensions/common/feature_switch.h"
41 #include "extensions/common/manifest_constants.h" 40 #include "extensions/common/manifest_constants.h"
42 #include "extensions/common/one_shot_event.h" 41 #include "extensions/common/one_shot_event.h"
43 42
44 ToolbarActionsModel::ToolbarActionsModel( 43 ToolbarActionsModel::ToolbarActionsModel(
45 Profile* profile, 44 Profile* profile,
46 extensions::ExtensionPrefs* extension_prefs) 45 extensions::ExtensionPrefs* extension_prefs)
47 : profile_(profile), 46 : profile_(profile),
48 extension_prefs_(extension_prefs), 47 extension_prefs_(extension_prefs),
49 prefs_(profile_->GetPrefs()), 48 prefs_(profile_->GetPrefs()),
50 extension_action_api_(extensions::ExtensionActionAPI::Get(profile_)), 49 extension_action_api_(extensions::ExtensionActionAPI::Get(profile_)),
51 extension_registry_(extensions::ExtensionRegistry::Get(profile_)), 50 extension_registry_(extensions::ExtensionRegistry::Get(profile_)),
52 extension_action_manager_( 51 extension_action_manager_(
53 extensions::ExtensionActionManager::Get(profile_)), 52 extensions::ExtensionActionManager::Get(profile_)),
54 component_migration_helper_( 53 component_actions_factory_(
55 new extensions::ComponentMigrationHelper(profile_, this)), 54 base::MakeUnique<ComponentToolbarActionsFactory>(profile_, this)),
56 actions_initialized_(false), 55 actions_initialized_(false),
57 use_redesign_( 56 use_redesign_(
58 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), 57 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()),
59 highlight_type_(HIGHLIGHT_NONE), 58 highlight_type_(HIGHLIGHT_NONE),
60 has_active_bubble_(false), 59 has_active_bubble_(false),
61 extension_action_observer_(this), 60 extension_action_observer_(this),
62 extension_registry_observer_(this), 61 extension_registry_observer_(this),
63 weak_ptr_factory_(this) { 62 weak_ptr_factory_(this) {
64 ComponentToolbarActionsFactory::GetInstance()->RegisterComponentMigrations( 63 component_actions_factory_->RegisterComponentMigrations();
65 component_migration_helper_.get());
66 extensions::ExtensionSystem::Get(profile_)->ready().Post( 64 extensions::ExtensionSystem::Get(profile_)->ready().Post(
67 FROM_HERE, base::Bind(&ToolbarActionsModel::OnReady, 65 FROM_HERE, base::Bind(&ToolbarActionsModel::OnReady,
68 weak_ptr_factory_.GetWeakPtr())); 66 weak_ptr_factory_.GetWeakPtr()));
69 visible_icon_count_ = 67 visible_icon_count_ =
70 prefs_->GetInteger(extensions::pref_names::kToolbarSize); 68 prefs_->GetInteger(extensions::pref_names::kToolbarSize);
71 69
72 // We only care about watching the prefs if not in incognito mode. 70 // We only care about watching the prefs if not in incognito mode.
73 if (!profile_->IsOffTheRecord()) { 71 if (!profile_->IsOffTheRecord()) {
74 pref_change_registrar_.Init(prefs_); 72 pref_change_registrar_.Init(prefs_);
75 pref_change_callback_ = 73 pref_change_callback_ =
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // don't store anything in incognito. 141 // don't store anything in incognito.
144 if (!is_highlighting() && !profile_->IsOffTheRecord()) { 142 if (!is_highlighting() && !profile_->IsOffTheRecord()) {
145 prefs_->SetInteger(extensions::pref_names::kToolbarSize, 143 prefs_->SetInteger(extensions::pref_names::kToolbarSize,
146 visible_icon_count_); 144 visible_icon_count_);
147 } 145 }
148 146
149 for (Observer& observer : observers_) 147 for (Observer& observer : observers_)
150 observer.OnToolbarVisibleCountChanged(); 148 observer.OnToolbarVisibleCountChanged();
151 } 149 }
152 150
151 extensions::ComponentMigrationHelper*
152 ToolbarActionsModel::GetComponentMigrationHelper() {
153 return component_actions_factory_->migration_helper();
154 }
155
153 void ToolbarActionsModel::OnExtensionActionUpdated( 156 void ToolbarActionsModel::OnExtensionActionUpdated(
154 ExtensionAction* extension_action, 157 ExtensionAction* extension_action,
155 content::WebContents* web_contents, 158 content::WebContents* web_contents,
156 content::BrowserContext* browser_context) { 159 content::BrowserContext* browser_context) {
157 // Notify observers if the extension exists and is in the model. 160 // Notify observers if the extension exists and is in the model.
158 if (HasItem( 161 if (HasItem(
159 ToolbarItem(extension_action->extension_id(), EXTENSION_ACTION))) { 162 ToolbarItem(extension_action->extension_id(), EXTENSION_ACTION))) {
160 for (Observer& observer : observers_) 163 for (Observer& observer : observers_)
161 observer.OnToolbarActionUpdated(extension_action->extension_id()); 164 observer.OnToolbarActionUpdated(extension_action->extension_id());
162 } 165 }
(...skipping 25 matching lines...) Expand all
188 DCHECK(extension); 191 DCHECK(extension);
189 192
190 // Create and add an ExtensionActionViewController for the extension. 193 // Create and add an ExtensionActionViewController for the extension.
191 result.reset(new ExtensionActionViewController( 194 result.reset(new ExtensionActionViewController(
192 extension, browser, 195 extension, browser,
193 extension_action_manager_->GetExtensionAction(*extension), bar)); 196 extension_action_manager_->GetExtensionAction(*extension), bar));
194 break; 197 break;
195 } 198 }
196 case COMPONENT_ACTION: { 199 case COMPONENT_ACTION: {
197 DCHECK(use_redesign_); 200 DCHECK(use_redesign_);
198 result = ComponentToolbarActionsFactory::GetInstance() 201 result = component_actions_factory_->GetComponentToolbarActionForId(
199 ->GetComponentToolbarActionForId(item.id, browser, bar); 202 item.id, browser, bar);
200 break; 203 break;
201 } 204 }
202 case UNKNOWN_ACTION: 205 case UNKNOWN_ACTION:
203 NOTREACHED(); // Should never have an UNKNOWN_ACTION in toolbar_items. 206 NOTREACHED(); // Should never have an UNKNOWN_ACTION in toolbar_items.
204 break; 207 break;
205 } 208 }
206 return result; 209 return result;
207 } 210 }
208 211
209 void ToolbarActionsModel::OnExtensionActionVisibilityChanged( 212 void ToolbarActionsModel::OnExtensionActionVisibilityChanged(
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 extension_action_observer_.Add(extension_action_api_); 274 extension_action_observer_.Add(extension_action_api_);
272 275
273 actions_initialized_ = true; 276 actions_initialized_ = true;
274 for (Observer& observer : observers_) 277 for (Observer& observer : observers_)
275 observer.OnToolbarModelInitialized(); 278 observer.OnToolbarModelInitialized();
276 279
277 if (use_redesign_) { 280 if (use_redesign_) {
278 // Handle component action migrations. We must make sure that observers are 281 // Handle component action migrations. We must make sure that observers are
279 // notified of initialization first, so that the associated widgets are 282 // notified of initialization first, so that the associated widgets are
280 // created. 283 // created.
281 ComponentToolbarActionsFactory::GetInstance()->HandleComponentMigrations( 284 component_actions_factory_->HandleComponentMigrations();
282 component_migration_helper_.get(), profile_);
283 } 285 }
284 } 286 }
285 287
286 size_t ToolbarActionsModel::FindNewPositionFromLastKnownGood( 288 size_t ToolbarActionsModel::FindNewPositionFromLastKnownGood(
287 const ToolbarItem& action) { 289 const ToolbarItem& action) {
288 // See if we have last known good position for this action. 290 // See if we have last known good position for this action.
289 size_t new_index = 0; 291 size_t new_index = 0;
290 // Loop through the ID list of known positions, to count the number of 292 // Loop through the ID list of known positions, to count the number of
291 // visible action icons preceding |action|'s id. 293 // visible action icons preceding |action|'s id.
292 for (const std::string& last_pos_id : last_known_positions_) { 294 for (const std::string& last_pos_id : last_known_positions_) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) { 442 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) {
441 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller; 443 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller;
442 if (has_active_bubble()) 444 if (has_active_bubble())
443 return controller; 445 return controller;
444 controller = ExtensionMessageBubbleFactory(browser).GetController(); 446 controller = ExtensionMessageBubbleFactory(browser).GetController();
445 if (controller) 447 if (controller)
446 controller->SetIsActiveBubble(); 448 controller->SetIsActiveBubble();
447 return controller; 449 return controller;
448 } 450 }
449 451
452 void ToolbarActionsModel::SetMockActionsFactory(
453 ComponentToolbarActionsFactory* mock_factory) {
454 component_actions_factory_.reset(mock_factory);
455 }
456
450 void ToolbarActionsModel::RemoveExtension( 457 void ToolbarActionsModel::RemoveExtension(
451 const extensions::Extension* extension) { 458 const extensions::Extension* extension) {
452 RemoveItem(ToolbarItem(extension->id(), EXTENSION_ACTION)); 459 RemoveItem(ToolbarItem(extension->id(), EXTENSION_ACTION));
453 } 460 }
454 461
455 // Combine the currently enabled extensions that have browser actions (which 462 // Combine the currently enabled extensions that have browser actions (which
456 // we get from the ExtensionRegistry) and component actions (which we get from 463 // we get from the ExtensionRegistry) and component actions (which we get from
457 // ComponentToolbarActionsFactory) with the ordering we get from the pref 464 // ComponentToolbarActionsFactory) with the ordering we get from the pref
458 // service. For robustness we use a somewhat inefficient process: 465 // service. For robustness we use a somewhat inefficient process:
459 // 1. Create a vector of actions sorted by their pref values. This vector may 466 // 1. Create a vector of actions sorted by their pref values. This vector may
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 if (!extension_action_api_->GetBrowserActionVisibility(extension->id())) 500 if (!extension_action_api_->GetBrowserActionVisibility(extension->id()))
494 ++hidden; 501 ++hidden;
495 continue; 502 continue;
496 } 503 }
497 504
498 all_actions.push_back(ToolbarItem(extension->id(), EXTENSION_ACTION)); 505 all_actions.push_back(ToolbarItem(extension->id(), EXTENSION_ACTION));
499 } 506 }
500 507
501 // Next, add the component action ids. 508 // Next, add the component action ids.
502 std::set<std::string> component_ids = 509 std::set<std::string> component_ids =
503 ComponentToolbarActionsFactory::GetInstance()->GetInitialComponentIds( 510 component_actions_factory_->GetInitialComponentIds();
504 profile_);
505 for (const std::string& id : component_ids) 511 for (const std::string& id : component_ids)
506 all_actions.push_back(ToolbarItem(id, COMPONENT_ACTION)); 512 all_actions.push_back(ToolbarItem(id, COMPONENT_ACTION));
507 513
508 // Add each action id to the appropriate list. Since the |sorted| list is 514 // Add each action id to the appropriate list. Since the |sorted| list is
509 // created with enough room for each id in |positions| (which helps with 515 // created with enough room for each id in |positions| (which helps with
510 // proper order insertion), holes can be present if there isn't an action 516 // proper order insertion), holes can be present if there isn't an action
511 // for each id. This is handled below when we add the actions to 517 // for each id. This is handled below when we add the actions to
512 // |toolbar_items_| to ensure that there are never any holes in 518 // |toolbar_items_| to ensure that there are never any holes in
513 // |toolbar_items_| itself (or, relatedly, CreateActions()). 519 // |toolbar_items_| itself (or, relatedly, CreateActions()).
514 for (const ToolbarItem& action : all_actions) { 520 for (const ToolbarItem& action : all_actions) {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 return base::ContainsValue(toolbar_items_, item); 630 return base::ContainsValue(toolbar_items_, item);
625 } 631 }
626 632
627 bool ToolbarActionsModel::HasComponentAction( 633 bool ToolbarActionsModel::HasComponentAction(
628 const std::string& action_id) const { 634 const std::string& action_id) const {
629 DCHECK(use_redesign_); 635 DCHECK(use_redesign_);
630 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); 636 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION));
631 } 637 }
632 638
633 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { 639 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) {
640 DCHECK(use_redesign_);
634 if (!actions_initialized_) { 641 if (!actions_initialized_) {
635 // TODO(crbug.com/660972): Add these component actions at initialization. 642 component_actions_factory_->OnAddComponentActionBeforeInit(action_id);
Devlin 2017/01/10 23:07:11 I'm not entirely sure this is less convoluted than
takumif 2017/02/17 03:22:51 I think things are a bit less complex after the re
636 return; 643 return;
637 } 644 }
638 645
639 DCHECK(use_redesign_);
640 ToolbarItem component_item(action_id, COMPONENT_ACTION); 646 ToolbarItem component_item(action_id, COMPONENT_ACTION);
641 DCHECK(!HasItem(component_item)); 647 DCHECK(!HasItem(component_item));
642 AddItem(component_item); 648 AddItem(component_item);
643 } 649 }
644 650
645 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { 651 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) {
646 DCHECK(use_redesign_); 652 DCHECK(use_redesign_);
653 if (!actions_initialized_) {
654 component_actions_factory_->OnRemoveComponentActionBeforeInit(action_id);
655 return;
656 }
647 // If the action was visible and there are overflowed actions, we reduce the 657 // If the action was visible and there are overflowed actions, we reduce the
648 // visible count so that we don't pop out a previously-hidden action. 658 // visible count so that we don't pop out a previously-hidden action.
649 if (IsActionVisible(action_id) && !all_icons_visible()) 659 if (IsActionVisible(action_id) && !all_icons_visible())
650 SetVisibleIconCount(visible_icon_count() - 1); 660 SetVisibleIconCount(visible_icon_count() - 1);
651 661
652 ToolbarItem component_item(action_id, COMPONENT_ACTION); 662 ToolbarItem component_item(action_id, COMPONENT_ACTION);
653 DCHECK(HasItem(component_item)); 663 DCHECK(HasItem(component_item));
654 RemoveItem(component_item); 664 RemoveItem(component_item);
655 RemovePref(component_item); 665 RemovePref(component_item);
656 } 666 }
657 667
658 void ToolbarActionsModel::IncognitoPopulate() { 668 void ToolbarActionsModel::IncognitoPopulate() {
659 DCHECK(profile_->IsOffTheRecord()); 669 DCHECK(profile_->IsOffTheRecord());
660 const ToolbarActionsModel* original_model = 670 const ToolbarActionsModel* original_model =
661 ToolbarActionsModel::Get(profile_->GetOriginalProfile()); 671 ToolbarActionsModel::Get(profile_->GetOriginalProfile());
662 672
663 // Find the absolute value of the original model's count. 673 // Find the absolute value of the original model's count.
664 int original_visible = original_model->visible_icon_count(); 674 int original_visible = original_model->visible_icon_count();
665 675
666 // In incognito mode, we show only those actions that are incognito-enabled 676 // In incognito mode, we show only those actions that are incognito-enabled
667 // Further, any actions that were overflowed in regular mode are still 677 // Further, any actions that were overflowed in regular mode are still
668 // overflowed. Order is the same as in regular mode. 678 // overflowed. Order is the same as in regular mode.
669 visible_icon_count_ = 0; 679 visible_icon_count_ = 0;
670 680
671 std::set<std::string> component_ids = 681 std::set<std::string> component_ids =
672 ComponentToolbarActionsFactory::GetInstance()->GetInitialComponentIds( 682 component_actions_factory_->GetInitialComponentIds();
673 profile_);
674 for (std::vector<ToolbarItem>::const_iterator iter = 683 for (std::vector<ToolbarItem>::const_iterator iter =
675 original_model->toolbar_items_.begin(); 684 original_model->toolbar_items_.begin();
676 iter != original_model->toolbar_items_.end(); ++iter) { 685 iter != original_model->toolbar_items_.end(); ++iter) {
677 // The extension might not be shown in incognito mode. 686 // The extension might not be shown in incognito mode.
678 // We may also disable certain component actions in incognito mode. 687 // We may also disable certain component actions in incognito mode.
679 bool should_add = false; 688 bool should_add = false;
680 switch (iter->type) { 689 switch (iter->type) {
681 case EXTENSION_ACTION: 690 case EXTENSION_ACTION:
682 should_add = ShouldAddExtension(GetExtensionById(iter->id)); 691 should_add = ShouldAddExtension(GetExtensionById(iter->id));
683 break; 692 break;
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 return extension_registry_->enabled_extensions().GetByID(id); 868 return extension_registry_->enabled_extensions().GetByID(id);
860 } 869 }
861 870
862 bool ToolbarActionsModel::IsActionVisible(const std::string& action_id) const { 871 bool ToolbarActionsModel::IsActionVisible(const std::string& action_id) const {
863 size_t index = 0u; 872 size_t index = 0u;
864 while (toolbar_items().size() > index && 873 while (toolbar_items().size() > index &&
865 toolbar_items()[index].id != action_id) 874 toolbar_items()[index].id != action_id)
866 ++index; 875 ++index;
867 return index < visible_icon_count(); 876 return index < visible_icon_count();
868 } 877 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698