| OLD | NEW |
| 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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 ToolbarActionsModel::ToolbarActionsModel( | 43 ToolbarActionsModel::ToolbarActionsModel( |
| 44 Profile* profile, | 44 Profile* profile, |
| 45 extensions::ExtensionPrefs* extension_prefs) | 45 extensions::ExtensionPrefs* extension_prefs) |
| 46 : profile_(profile), | 46 : profile_(profile), |
| 47 extension_prefs_(extension_prefs), | 47 extension_prefs_(extension_prefs), |
| 48 prefs_(profile_->GetPrefs()), | 48 prefs_(profile_->GetPrefs()), |
| 49 extension_action_api_(extensions::ExtensionActionAPI::Get(profile_)), | 49 extension_action_api_(extensions::ExtensionActionAPI::Get(profile_)), |
| 50 extension_registry_(extensions::ExtensionRegistry::Get(profile_)), | 50 extension_registry_(extensions::ExtensionRegistry::Get(profile_)), |
| 51 extension_action_manager_( | 51 extension_action_manager_( |
| 52 extensions::ExtensionActionManager::Get(profile_)), | 52 extensions::ExtensionActionManager::Get(profile_)), |
| 53 component_actions_factory_( |
| 54 base::MakeUnique<ComponentToolbarActionsFactory>(profile_)), |
| 53 actions_initialized_(false), | 55 actions_initialized_(false), |
| 54 use_redesign_( | 56 use_redesign_( |
| 55 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), | 57 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), |
| 56 highlight_type_(HIGHLIGHT_NONE), | 58 highlight_type_(HIGHLIGHT_NONE), |
| 57 has_active_bubble_(false), | 59 has_active_bubble_(false), |
| 58 extension_action_observer_(this), | 60 extension_action_observer_(this), |
| 59 extension_registry_observer_(this), | 61 extension_registry_observer_(this), |
| 60 weak_ptr_factory_(this) { | 62 weak_ptr_factory_(this) { |
| 61 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 63 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
| 62 FROM_HERE, base::Bind(&ToolbarActionsModel::OnReady, | 64 FROM_HERE, base::Bind(&ToolbarActionsModel::OnReady, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 ToolbarActionsBar* bar, | 178 ToolbarActionsBar* bar, |
| 177 const ToolbarItem& item) { | 179 const ToolbarItem& item) { |
| 178 std::unique_ptr<ToolbarActionViewController> result; | 180 std::unique_ptr<ToolbarActionViewController> result; |
| 179 switch (item.type) { | 181 switch (item.type) { |
| 180 case EXTENSION_ACTION: { | 182 case EXTENSION_ACTION: { |
| 181 // Get the extension. | 183 // Get the extension. |
| 182 const extensions::Extension* extension = GetExtensionById(item.id); | 184 const extensions::Extension* extension = GetExtensionById(item.id); |
| 183 DCHECK(extension); | 185 DCHECK(extension); |
| 184 | 186 |
| 185 // Create and add an ExtensionActionViewController for the extension. | 187 // Create and add an ExtensionActionViewController for the extension. |
| 186 result.reset(new ExtensionActionViewController( | 188 result = base::MakeUnique<ExtensionActionViewController>( |
| 187 extension, browser, | 189 extension, browser, |
| 188 extension_action_manager_->GetExtensionAction(*extension), bar)); | 190 extension_action_manager_->GetExtensionAction(*extension), bar); |
| 189 break; | 191 break; |
| 190 } | 192 } |
| 191 case COMPONENT_ACTION: { | 193 case COMPONENT_ACTION: { |
| 192 DCHECK(use_redesign_); | 194 DCHECK(use_redesign_); |
| 193 result = ComponentToolbarActionsFactory::GetInstance() | 195 result = component_actions_factory_->GetComponentToolbarActionForId( |
| 194 ->GetComponentToolbarActionForId(item.id, browser, bar); | 196 item.id, browser, bar); |
| 195 break; | 197 break; |
| 196 } | 198 } |
| 197 case UNKNOWN_ACTION: | 199 case UNKNOWN_ACTION: |
| 198 NOTREACHED(); // Should never have an UNKNOWN_ACTION in toolbar_items. | 200 NOTREACHED(); // Should never have an UNKNOWN_ACTION in toolbar_items. |
| 199 break; | 201 break; |
| 200 } | 202 } |
| 201 return result; | 203 return result; |
| 202 } | 204 } |
| 203 | 205 |
| 204 void ToolbarActionsModel::OnExtensionActionVisibilityChanged( | 206 void ToolbarActionsModel::OnExtensionActionVisibilityChanged( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // changes so that the toolbar buttons can be shown in their stable ordering | 265 // changes so that the toolbar buttons can be shown in their stable ordering |
| 264 // taken from prefs. | 266 // taken from prefs. |
| 265 extension_registry_observer_.Add(extension_registry_); | 267 extension_registry_observer_.Add(extension_registry_); |
| 266 extension_action_observer_.Add(extension_action_api_); | 268 extension_action_observer_.Add(extension_action_api_); |
| 267 | 269 |
| 268 actions_initialized_ = true; | 270 actions_initialized_ = true; |
| 269 for (Observer& observer : observers_) | 271 for (Observer& observer : observers_) |
| 270 observer.OnToolbarModelInitialized(); | 272 observer.OnToolbarModelInitialized(); |
| 271 | 273 |
| 272 if (use_redesign_) { | 274 if (use_redesign_) { |
| 273 ComponentToolbarActionsFactory::GetInstance()->UnloadMigratedExtensions( | 275 component_actions_factory_->UnloadMigratedExtensions( |
| 274 extensions::ExtensionSystem::Get(profile_)->extension_service(), | 276 extensions::ExtensionSystem::Get(profile_)->extension_service(), |
| 275 extension_registry_); | 277 extension_registry_); |
| 276 } | 278 } |
| 277 } | 279 } |
| 278 | 280 |
| 279 size_t ToolbarActionsModel::FindNewPositionFromLastKnownGood( | 281 size_t ToolbarActionsModel::FindNewPositionFromLastKnownGood( |
| 280 const ToolbarItem& action) { | 282 const ToolbarItem& action) { |
| 281 // See if we have last known good position for this action. | 283 // See if we have last known good position for this action. |
| 282 size_t new_index = 0; | 284 size_t new_index = 0; |
| 283 // Loop through the ID list of known positions, to count the number of | 285 // Loop through the ID list of known positions, to count the number of |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) { | 435 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) { |
| 434 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller; | 436 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller; |
| 435 if (has_active_bubble()) | 437 if (has_active_bubble()) |
| 436 return controller; | 438 return controller; |
| 437 controller = ExtensionMessageBubbleFactory(browser).GetController(); | 439 controller = ExtensionMessageBubbleFactory(browser).GetController(); |
| 438 if (controller) | 440 if (controller) |
| 439 controller->SetIsActiveBubble(); | 441 controller->SetIsActiveBubble(); |
| 440 return controller; | 442 return controller; |
| 441 } | 443 } |
| 442 | 444 |
| 445 void ToolbarActionsModel::SetMockActionsFactoryForTest( |
| 446 std::unique_ptr<ComponentToolbarActionsFactory> mock_factory) { |
| 447 component_actions_factory_ = std::move(mock_factory); |
| 448 } |
| 449 |
| 443 void ToolbarActionsModel::RemoveExtension( | 450 void ToolbarActionsModel::RemoveExtension( |
| 444 const extensions::Extension* extension) { | 451 const extensions::Extension* extension) { |
| 445 RemoveItem(ToolbarItem(extension->id(), EXTENSION_ACTION)); | 452 RemoveItem(ToolbarItem(extension->id(), EXTENSION_ACTION)); |
| 446 } | 453 } |
| 447 | 454 |
| 448 // Combine the currently enabled extensions that have browser actions (which | 455 // Combine the currently enabled extensions that have browser actions (which |
| 449 // we get from the ExtensionRegistry) and component actions (which we get from | 456 // we get from the ExtensionRegistry) and component actions (which we get from |
| 450 // ComponentToolbarActionsFactory) with the ordering we get from the pref | 457 // ComponentToolbarActionsFactory) with the ordering we get from the pref |
| 451 // service. For robustness we use a somewhat inefficient process: | 458 // service. For robustness we use a somewhat inefficient process: |
| 452 // 1. Create a vector of actions sorted by their pref values. This vector may | 459 // 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 Loading... |
| 486 if (!extension_action_api_->GetBrowserActionVisibility(extension->id())) | 493 if (!extension_action_api_->GetBrowserActionVisibility(extension->id())) |
| 487 ++hidden; | 494 ++hidden; |
| 488 continue; | 495 continue; |
| 489 } | 496 } |
| 490 | 497 |
| 491 all_actions.push_back(ToolbarItem(extension->id(), EXTENSION_ACTION)); | 498 all_actions.push_back(ToolbarItem(extension->id(), EXTENSION_ACTION)); |
| 492 } | 499 } |
| 493 | 500 |
| 494 // Next, add the component action ids. | 501 // Next, add the component action ids. |
| 495 std::set<std::string> component_ids = | 502 std::set<std::string> component_ids = |
| 496 ComponentToolbarActionsFactory::GetInstance()->GetInitialComponentIds( | 503 component_actions_factory_->GetInitialComponentIds(); |
| 497 profile_); | |
| 498 for (const std::string& id : component_ids) | 504 for (const std::string& id : component_ids) |
| 499 all_actions.push_back(ToolbarItem(id, COMPONENT_ACTION)); | 505 all_actions.push_back(ToolbarItem(id, COMPONENT_ACTION)); |
| 500 | 506 |
| 501 // Add each action id to the appropriate list. Since the |sorted| list is | 507 // Add each action id to the appropriate list. Since the |sorted| list is |
| 502 // created with enough room for each id in |positions| (which helps with | 508 // created with enough room for each id in |positions| (which helps with |
| 503 // proper order insertion), holes can be present if there isn't an action | 509 // proper order insertion), holes can be present if there isn't an action |
| 504 // for each id. This is handled below when we add the actions to | 510 // for each id. This is handled below when we add the actions to |
| 505 // |toolbar_items_| to ensure that there are never any holes in | 511 // |toolbar_items_| to ensure that there are never any holes in |
| 506 // |toolbar_items_| itself (or, relatedly, CreateActions()). | 512 // |toolbar_items_| itself (or, relatedly, CreateActions()). |
| 507 for (const ToolbarItem& action : all_actions) { | 513 for (const ToolbarItem& action : all_actions) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 return base::ContainsValue(toolbar_items_, item); | 623 return base::ContainsValue(toolbar_items_, item); |
| 618 } | 624 } |
| 619 | 625 |
| 620 bool ToolbarActionsModel::HasComponentAction( | 626 bool ToolbarActionsModel::HasComponentAction( |
| 621 const std::string& action_id) const { | 627 const std::string& action_id) const { |
| 622 DCHECK(use_redesign_); | 628 DCHECK(use_redesign_); |
| 623 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); | 629 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); |
| 624 } | 630 } |
| 625 | 631 |
| 626 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { | 632 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { |
| 633 DCHECK(use_redesign_); |
| 627 if (!actions_initialized_) { | 634 if (!actions_initialized_) { |
| 628 // TODO(crbug.com/660972): Add these component actions at initialization. | 635 component_actions_factory_->OnAddComponentActionBeforeInit(action_id); |
| 629 return; | 636 return; |
| 630 } | 637 } |
| 631 | 638 |
| 632 DCHECK(use_redesign_); | |
| 633 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 639 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 634 DCHECK(!HasItem(component_item)); | 640 DCHECK(!HasItem(component_item)); |
| 635 AddItem(component_item); | 641 AddItem(component_item); |
| 636 } | 642 } |
| 637 | 643 |
| 638 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { | 644 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { |
| 639 DCHECK(use_redesign_); | 645 DCHECK(use_redesign_); |
| 646 if (!actions_initialized_) { |
| 647 component_actions_factory_->OnRemoveComponentActionBeforeInit(action_id); |
| 648 return; |
| 649 } |
| 640 // If the action was visible and there are overflowed actions, we reduce the | 650 // If the action was visible and there are overflowed actions, we reduce the |
| 641 // visible count so that we don't pop out a previously-hidden action. | 651 // visible count so that we don't pop out a previously-hidden action. |
| 642 if (IsActionVisible(action_id) && !all_icons_visible()) | 652 if (IsActionVisible(action_id) && !all_icons_visible()) |
| 643 SetVisibleIconCount(visible_icon_count() - 1); | 653 SetVisibleIconCount(visible_icon_count() - 1); |
| 644 | 654 |
| 645 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 655 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 646 DCHECK(HasItem(component_item)); | 656 DCHECK(HasItem(component_item)); |
| 647 RemoveItem(component_item); | 657 RemoveItem(component_item); |
| 648 RemovePref(component_item); | 658 RemovePref(component_item); |
| 649 } | 659 } |
| 650 | 660 |
| 651 void ToolbarActionsModel::IncognitoPopulate() { | 661 void ToolbarActionsModel::IncognitoPopulate() { |
| 652 DCHECK(profile_->IsOffTheRecord()); | 662 DCHECK(profile_->IsOffTheRecord()); |
| 653 const ToolbarActionsModel* original_model = | 663 const ToolbarActionsModel* original_model = |
| 654 ToolbarActionsModel::Get(profile_->GetOriginalProfile()); | 664 ToolbarActionsModel::Get(profile_->GetOriginalProfile()); |
| 655 | 665 |
| 656 // Find the absolute value of the original model's count. | 666 // Find the absolute value of the original model's count. |
| 657 int original_visible = original_model->visible_icon_count(); | 667 int original_visible = original_model->visible_icon_count(); |
| 658 | 668 |
| 659 // In incognito mode, we show only those actions that are incognito-enabled | 669 // In incognito mode, we show only those actions that are incognito-enabled |
| 660 // Further, any actions that were overflowed in regular mode are still | 670 // Further, any actions that were overflowed in regular mode are still |
| 661 // overflowed. Order is the same as in regular mode. | 671 // overflowed. Order is the same as in regular mode. |
| 662 visible_icon_count_ = 0; | 672 visible_icon_count_ = 0; |
| 663 | 673 |
| 664 std::set<std::string> component_ids = | 674 std::set<std::string> component_ids = |
| 665 ComponentToolbarActionsFactory::GetInstance()->GetInitialComponentIds( | 675 component_actions_factory_->GetInitialComponentIds(); |
| 666 profile_); | |
| 667 for (std::vector<ToolbarItem>::const_iterator iter = | 676 for (std::vector<ToolbarItem>::const_iterator iter = |
| 668 original_model->toolbar_items_.begin(); | 677 original_model->toolbar_items_.begin(); |
| 669 iter != original_model->toolbar_items_.end(); ++iter) { | 678 iter != original_model->toolbar_items_.end(); ++iter) { |
| 670 // The extension might not be shown in incognito mode. | 679 // The extension might not be shown in incognito mode. |
| 671 // We may also disable certain component actions in incognito mode. | 680 // We may also disable certain component actions in incognito mode. |
| 672 bool should_add = false; | 681 bool should_add = false; |
| 673 switch (iter->type) { | 682 switch (iter->type) { |
| 674 case EXTENSION_ACTION: | 683 case EXTENSION_ACTION: |
| 675 should_add = ShouldAddExtension(GetExtensionById(iter->id)); | 684 should_add = ShouldAddExtension(GetExtensionById(iter->id)); |
| 676 break; | 685 break; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 return extension_registry_->enabled_extensions().GetByID(id); | 861 return extension_registry_->enabled_extensions().GetByID(id); |
| 853 } | 862 } |
| 854 | 863 |
| 855 bool ToolbarActionsModel::IsActionVisible(const std::string& action_id) const { | 864 bool ToolbarActionsModel::IsActionVisible(const std::string& action_id) const { |
| 856 size_t index = 0u; | 865 size_t index = 0u; |
| 857 while (toolbar_items().size() > index && | 866 while (toolbar_items().size() > index && |
| 858 toolbar_items()[index].id != action_id) | 867 toolbar_items()[index].id != action_id) |
| 859 ++index; | 868 ++index; |
| 860 return index < visible_icon_count(); | 869 return index < visible_icon_count(); |
| 861 } | 870 } |
| OLD | NEW |