| 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 InitializeActionList(); | 269 InitializeActionList(); |
| 270 // Wait until the extension system is ready before observing any further | 270 // Wait until the extension system is ready before observing any further |
| 271 // changes so that the toolbar buttons can be shown in their stable ordering | 271 // changes so that the toolbar buttons can be shown in their stable ordering |
| 272 // taken from prefs. | 272 // taken from prefs. |
| 273 extension_registry_observer_.Add(extension_registry_); | 273 extension_registry_observer_.Add(extension_registry_); |
| 274 extension_action_observer_.Add(extension_action_api_); | 274 extension_action_observer_.Add(extension_action_api_); |
| 275 | 275 |
| 276 actions_initialized_ = true; | 276 actions_initialized_ = true; |
| 277 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); | 277 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); |
| 278 | 278 |
| 279 // Handle component action migrations. We must make sure that observers are | 279 if (use_redesign_) { |
| 280 // notified of initialization first, so that the associated widgets are | 280 // Handle component action migrations. We must make sure that observers are |
| 281 // created. | 281 // notified of initialization first, so that the associated widgets are |
| 282 ComponentToolbarActionsFactory::GetInstance()->HandleComponentMigrations( | 282 // created. |
| 283 component_migration_helper_.get(), profile_); | 283 ComponentToolbarActionsFactory::GetInstance()->HandleComponentMigrations( |
| 284 component_migration_helper_.get(), profile_); |
| 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_) { |
| 293 if (last_pos_id == action.id) | 295 if (last_pos_id == action.id) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 320 } | 322 } |
| 321 | 323 |
| 322 return extension_action_manager_->GetBrowserAction(*extension) && | 324 return extension_action_manager_->GetBrowserAction(*extension) && |
| 323 extension_action_api_->GetBrowserActionVisibility(extension->id()); | 325 extension_action_api_->GetBrowserActionVisibility(extension->id()); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void ToolbarActionsModel::AddExtension(const extensions::Extension* extension) { | 328 void ToolbarActionsModel::AddExtension(const extensions::Extension* extension) { |
| 327 if (!ShouldAddExtension(extension)) | 329 if (!ShouldAddExtension(extension)) |
| 328 return; | 330 return; |
| 329 | 331 |
| 330 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION), | 332 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION)); |
| 331 extensions::Manifest::IsComponentLocation(extension->location())); | |
| 332 } | 333 } |
| 333 | 334 |
| 334 void ToolbarActionsModel::AddItem(const ToolbarItem& item, bool is_component) { | 335 void ToolbarActionsModel::AddItem(const ToolbarItem& item) { |
| 335 // We only use AddItem() once the system is initialized. | 336 // We only use AddItem() once the system is initialized. |
| 336 DCHECK(actions_initialized_); | 337 DCHECK(actions_initialized_); |
| 337 | 338 |
| 338 // See if we have a last known good position for this extension. | 339 // See if we have a last known good position for this extension. |
| 339 bool is_new_extension = !base::ContainsValue(last_known_positions_, item.id); | 340 bool is_new_extension = !base::ContainsValue(last_known_positions_, item.id); |
| 340 | 341 |
| 341 // New extensions go at the right (end) of the visible extensions. Other | 342 // New extensions go at the right (end) of the visible extensions. Other |
| 342 // extensions go at their previous position. | 343 // extensions go at their previous position. |
| 343 size_t new_index = 0; | 344 size_t new_index = 0; |
| 344 if (is_new_extension) { | 345 if (is_new_extension) { |
| 345 new_index = is_component ? 0 : visible_icon_count(); | 346 new_index = visible_icon_count(); |
| 346 // For the last-known position, we use the index of the extension that is | 347 // For the last-known position, we use the index of the extension that is |
| 347 // just before this extension, plus one. (Note that this isn't the same | 348 // just before this extension, plus one. (Note that this isn't the same |
| 348 // as new_index + 1, because last_known_positions_ can include disabled | 349 // as new_index + 1, because last_known_positions_ can include disabled |
| 349 // extensions.) | 350 // extensions.) |
| 350 int new_last_known_index = | 351 int new_last_known_index = |
| 351 new_index == 0 ? 0 : std::find(last_known_positions_.begin(), | 352 new_index == 0 ? 0 : std::find(last_known_positions_.begin(), |
| 352 last_known_positions_.end(), | 353 last_known_positions_.end(), |
| 353 toolbar_items_[new_index - 1].id) - | 354 toolbar_items_[new_index - 1].id) - |
| 354 last_known_positions_.begin() + 1; | 355 last_known_positions_.begin() + 1; |
| 355 // In theory, the extension before this one should always | 356 // In theory, the extension before this one should always |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 bool ToolbarActionsModel::HasComponentAction( | 626 bool ToolbarActionsModel::HasComponentAction( |
| 626 const std::string& action_id) const { | 627 const std::string& action_id) const { |
| 627 DCHECK(use_redesign_); | 628 DCHECK(use_redesign_); |
| 628 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); | 629 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); |
| 629 } | 630 } |
| 630 | 631 |
| 631 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { | 632 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { |
| 632 DCHECK(use_redesign_); | 633 DCHECK(use_redesign_); |
| 633 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 634 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 634 DCHECK(!HasItem(component_item)); | 635 DCHECK(!HasItem(component_item)); |
| 635 AddItem(component_item, true); | 636 AddItem(component_item); |
| 636 } | 637 } |
| 637 | 638 |
| 638 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { | 639 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { |
| 639 DCHECK(use_redesign_); | 640 DCHECK(use_redesign_); |
| 640 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 641 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 641 DCHECK(HasItem(component_item)); | 642 DCHECK(HasItem(component_item)); |
| 642 RemoveItem(component_item); | 643 RemoveItem(component_item); |
| 643 RemovePref(component_item); | 644 RemovePref(component_item); |
| 644 } | 645 } |
| 645 | 646 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 prefs_->GetInteger(extensions::pref_names::kToolbarSize); | 838 prefs_->GetInteger(extensions::pref_names::kToolbarSize); |
| 838 if (saved_icon_count != visible_icon_count_) | 839 if (saved_icon_count != visible_icon_count_) |
| 839 SetVisibleIconCount(saved_icon_count); | 840 SetVisibleIconCount(saved_icon_count); |
| 840 } | 841 } |
| 841 } | 842 } |
| 842 | 843 |
| 843 const extensions::Extension* ToolbarActionsModel::GetExtensionById( | 844 const extensions::Extension* ToolbarActionsModel::GetExtensionById( |
| 844 const std::string& id) const { | 845 const std::string& id) const { |
| 845 return extension_registry_->enabled_extensions().GetByID(id); | 846 return extension_registry_->enabled_extensions().GetByID(id); |
| 846 } | 847 } |
| OLD | NEW |