| 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/extensions/extension_toolbar_model.h" | 5 #include "chrome/browser/extensions/extension_toolbar_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 toolbar_items_.push_back(extension); | 114 toolbar_items_.push_back(extension); |
| 115 last_known_positions_.push_back(id); | 115 last_known_positions_.push_back(id); |
| 116 } | 116 } |
| 117 | 117 |
| 118 FOR_EACH_OBSERVER( | 118 FOR_EACH_OBSERVER( |
| 119 Observer, observers_, ToolbarExtensionMoved(extension.get(), index)); | 119 Observer, observers_, ToolbarExtensionMoved(extension.get(), index)); |
| 120 MaybeUpdateVisibilityPref(extension.get(), index); | 120 MaybeUpdateVisibilityPref(extension.get(), index); |
| 121 UpdatePrefs(); | 121 UpdatePrefs(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void ExtensionToolbarModel::SetVisibleIconCount(int count) { | 124 void ExtensionToolbarModel::SetVisibleIconCount(size_t count) { |
| 125 visible_icon_count_ = | 125 visible_icon_count_ = (count == toolbar_items_.size()) ? -1 : count; |
| 126 count == static_cast<int>(toolbar_items_.size()) ? -1 : count; | |
| 127 | 126 |
| 128 // Only set the prefs if we're not in highlight mode and the profile is not | 127 // Only set the prefs if we're not in highlight mode and the profile is not |
| 129 // incognito. Highlight mode is designed to be a transitory state, and should | 128 // incognito. Highlight mode is designed to be a transitory state, and should |
| 130 // not persist across browser restarts (though it may be re-entered), and we | 129 // not persist across browser restarts (though it may be re-entered), and we |
| 131 // don't store anything in incognito. | 130 // don't store anything in incognito. |
| 132 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { | 131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { |
| 133 // Additionally, if we are using the new toolbar, any icons which are in the | 132 // Additionally, if we are using the new toolbar, any icons which are in the |
| 134 // overflow menu are considered "hidden". But it so happens that the times | 133 // overflow menu are considered "hidden". But it so happens that the times |
| 135 // we are likely to call SetVisibleIconCount() are also those when we are | 134 // we are likely to call SetVisibleIconCount() are also those when we are |
| 136 // in flux. So wait for things to cool down before setting the prefs. | 135 // in flux. So wait for things to cool down before setting the prefs. |
| 137 base::MessageLoop::current()->PostTask( | 136 base::MessageLoop::current()->PostTask( |
| 138 FROM_HERE, | 137 FROM_HERE, |
| 139 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, | 138 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, |
| 140 weak_ptr_factory_.GetWeakPtr())); | 139 weak_ptr_factory_.GetWeakPtr())); |
| 141 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); | 140 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); |
| 142 } | 141 } |
| 143 | 142 |
| 144 FOR_EACH_OBSERVER(Observer, observers_, ToolbarVisibleCountChanged()); | 143 FOR_EACH_OBSERVER(Observer, observers_, ToolbarVisibleCountChanged()); |
| 145 } | 144 } |
| 146 | 145 |
| 147 void ExtensionToolbarModel::OnExtensionActionUpdated( | 146 void ExtensionToolbarModel::OnExtensionActionUpdated( |
| 148 ExtensionAction* extension_action, | 147 ExtensionAction* extension_action, |
| 149 content::WebContents* web_contents, | 148 content::WebContents* web_contents, |
| 150 content::BrowserContext* browser_context) { | 149 content::BrowserContext* browser_context) { |
| 151 const Extension* extension = | 150 const Extension* extension = |
| 152 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( | 151 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( |
| 153 extension_action->extension_id()); | 152 extension_action->extension_id()); |
| 154 // Notify observers if the extension exists and is in the model. | 153 // Notify observers if the extension exists and is in the model. |
| 155 if (extension && | 154 ExtensionList::const_iterator iter = |
| 156 std::find(toolbar_items_.begin(), | 155 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); |
| 157 toolbar_items_.end(), | 156 if (iter != toolbar_items_.end()) { |
| 158 extension) != toolbar_items_.end()) { | 157 FOR_EACH_OBSERVER( |
| 159 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionUpdated(extension)); | 158 Observer, observers_, ToolbarExtensionUpdated(extension)); |
| 159 // If the action was in the overflow menu, we have to alert observers that |
| 160 // the toolbar needs to be reordered (to show the action). |
| 161 if (static_cast<size_t>(iter - toolbar_items_.begin()) >= |
| 162 visible_icon_count()) { |
| 163 FOR_EACH_OBSERVER( |
| 164 Observer, observers_, OnToolbarReorderNecessary(web_contents)); |
| 165 } |
| 160 } | 166 } |
| 161 } | 167 } |
| 162 | 168 |
| 163 void ExtensionToolbarModel::OnExtensionLoaded( | 169 void ExtensionToolbarModel::OnExtensionLoaded( |
| 164 content::BrowserContext* browser_context, | 170 content::BrowserContext* browser_context, |
| 165 const Extension* extension) { | 171 const Extension* extension) { |
| 166 // We don't want to add the same extension twice. It may have already been | 172 // We don't want to add the same extension twice. It may have already been |
| 167 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | 173 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
| 168 // hides the browser action and then disables and enables the extension. | 174 // hides the browser action and then disables and enables the extension. |
| 169 for (size_t i = 0; i < toolbar_items_.size(); i++) { | 175 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 | 499 |
| 494 void ExtensionToolbarModel::IncognitoPopulate() { | 500 void ExtensionToolbarModel::IncognitoPopulate() { |
| 495 DCHECK(profile_->IsOffTheRecord()); | 501 DCHECK(profile_->IsOffTheRecord()); |
| 496 // Clear the current items, if any. | 502 // Clear the current items, if any. |
| 497 ClearItems(); | 503 ClearItems(); |
| 498 | 504 |
| 499 const ExtensionToolbarModel* original_model = | 505 const ExtensionToolbarModel* original_model = |
| 500 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); | 506 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); |
| 501 | 507 |
| 502 // Find the absolute value of the original model's count. | 508 // Find the absolute value of the original model's count. |
| 503 int original_visible = original_model->GetVisibleIconCount(); | 509 int original_visible = original_model->visible_icon_count(); |
| 504 if (original_visible == -1) | |
| 505 original_visible = original_model->toolbar_items_.size(); | |
| 506 | 510 |
| 507 // In incognito mode, we show only those extensions that are | 511 // In incognito mode, we show only those extensions that are |
| 508 // incognito-enabled. Further, any actions that were overflowed in regular | 512 // incognito-enabled. Further, any actions that were overflowed in regular |
| 509 // mode are still overflowed. Order is the same as in regular mode. | 513 // mode are still overflowed. Order is the same as in regular mode. |
| 510 visible_icon_count_ = 0; | 514 visible_icon_count_ = 0; |
| 511 for (ExtensionList::const_iterator iter = | 515 for (ExtensionList::const_iterator iter = |
| 512 original_model->toolbar_items_.begin(); | 516 original_model->toolbar_items_.begin(); |
| 513 iter != original_model->toolbar_items_.end(); ++iter) { | 517 iter != original_model->toolbar_items_.end(); ++iter) { |
| 514 if (ShouldAddExtension(iter->get())) { | 518 if (ShouldAddExtension(iter->get())) { |
| 515 toolbar_items_.push_back(*iter); | 519 toolbar_items_.push_back(*iter); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 if (last_known_positions_.size() > pref_position_size) { | 596 if (last_known_positions_.size() > pref_position_size) { |
| 593 // Need to update pref because we have extra icons. But can't call | 597 // Need to update pref because we have extra icons. But can't call |
| 594 // UpdatePrefs() directly within observation closure. | 598 // UpdatePrefs() directly within observation closure. |
| 595 base::MessageLoop::current()->PostTask( | 599 base::MessageLoop::current()->PostTask( |
| 596 FROM_HERE, | 600 FROM_HERE, |
| 597 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 601 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
| 598 weak_ptr_factory_.GetWeakPtr())); | 602 weak_ptr_factory_.GetWeakPtr())); |
| 599 } | 603 } |
| 600 } | 604 } |
| 601 | 605 |
| 606 size_t ExtensionToolbarModel::GetVisibleIconCountForTab( |
| 607 content::WebContents* web_contents) const { |
| 608 if (all_icons_visible()) |
| 609 return visible_icon_count(); // Already displaying all actions. |
| 610 |
| 611 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); |
| 612 size_t total_icons = visible_icon_count_; |
| 613 for (size_t i = total_icons; i < toolbar_items_.size(); ++i) { |
| 614 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(), |
| 615 web_contents)) |
| 616 ++total_icons; |
| 617 } |
| 618 return total_icons; |
| 619 } |
| 620 |
| 621 ExtensionList ExtensionToolbarModel::GetItemOrderForTab( |
| 622 content::WebContents* web_contents) const { |
| 623 // If we're highlighting, the items are always the same. |
| 624 if (is_highlighting_) |
| 625 return highlighted_items_; |
| 626 |
| 627 // Start by initializing the array to be the same as toolbar items (this isn't |
| 628 // any more expensive than initializing it to be of the same size with all |
| 629 // nulls, and saves us time at the end). |
| 630 ExtensionList result = toolbar_items_; |
| 631 if (toolbar_items_.empty()) |
| 632 return result; |
| 633 |
| 634 ExtensionList overflowed_actions_wanting_to_run; |
| 635 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); |
| 636 size_t boundary = visible_icon_count(); |
| 637 // Rotate any actions that want to run to the boundary between visible and |
| 638 // overflowed actions. |
| 639 for (ExtensionList::iterator iter = result.begin() + boundary; |
| 640 iter != result.end(); ++iter) { |
| 641 if (extension_action_api->ExtensionWantsToRun(iter->get(), web_contents)) { |
| 642 std::rotate(result.begin() + boundary, iter, iter + 1); |
| 643 ++boundary; |
| 644 } |
| 645 } |
| 646 return result; |
| 647 } |
| 648 |
| 602 bool ExtensionToolbarModel::ShowExtensionActionPopup( | 649 bool ExtensionToolbarModel::ShowExtensionActionPopup( |
| 603 const Extension* extension, | 650 const Extension* extension, |
| 604 Browser* browser, | 651 Browser* browser, |
| 605 bool grant_active_tab) { | 652 bool grant_active_tab) { |
| 606 ObserverListBase<Observer>::Iterator it(observers_); | 653 ObserverListBase<Observer>::Iterator it(observers_); |
| 607 Observer* obs = NULL; | 654 Observer* obs = NULL; |
| 608 // Look for the Observer associated with the browser. | 655 // Look for the Observer associated with the browser. |
| 609 // This would be cleaner if we had an abstract class for the Toolbar UI | 656 // This would be cleaner if we had an abstract class for the Toolbar UI |
| 610 // (like we do for LocationBar), but sadly, we don't. | 657 // (like we do for LocationBar), but sadly, we don't. |
| 611 while ((obs = it.GetNext()) != NULL) { | 658 while ((obs = it.GetNext()) != NULL) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 if (is_highlighting_) { | 729 if (is_highlighting_) { |
| 683 highlighted_items_.clear(); | 730 highlighted_items_.clear(); |
| 684 is_highlighting_ = false; | 731 is_highlighting_ = false; |
| 685 if (old_visible_icon_count_ != visible_icon_count_) | 732 if (old_visible_icon_count_ != visible_icon_count_) |
| 686 SetVisibleIconCount(old_visible_icon_count_); | 733 SetVisibleIconCount(old_visible_icon_count_); |
| 687 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 734 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); |
| 688 } | 735 } |
| 689 } | 736 } |
| 690 | 737 |
| 691 } // namespace extensions | 738 } // namespace extensions |
| OLD | NEW |