| 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(size_t count) { | 124 void ExtensionToolbarModel::SetVisibleIconCount(int count) { |
| 125 visible_icon_count_ = (count == toolbar_items_.size()) ? -1 : count; | 125 visible_icon_count_ = |
| 126 count == static_cast<int>(toolbar_items_.size()) ? -1 : count; |
| 126 | 127 |
| 127 // Only set the prefs if we're not in highlight mode and the profile is not | 128 // Only set the prefs if we're not in highlight mode and the profile is not |
| 128 // incognito. Highlight mode is designed to be a transitory state, and should | 129 // incognito. Highlight mode is designed to be a transitory state, and should |
| 129 // not persist across browser restarts (though it may be re-entered), and we | 130 // not persist across browser restarts (though it may be re-entered), and we |
| 130 // don't store anything in incognito. | 131 // don't store anything in incognito. |
| 131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { | 132 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { |
| 132 // Additionally, if we are using the new toolbar, any icons which are in the | 133 // Additionally, if we are using the new toolbar, any icons which are in the |
| 133 // overflow menu are considered "hidden". But it so happens that the times | 134 // overflow menu are considered "hidden". But it so happens that the times |
| 134 // we are likely to call SetVisibleIconCount() are also those when we are | 135 // we are likely to call SetVisibleIconCount() are also those when we are |
| 135 // in flux. So wait for things to cool down before setting the prefs. | 136 // in flux. So wait for things to cool down before setting the prefs. |
| 136 base::MessageLoop::current()->PostTask( | 137 base::MessageLoop::current()->PostTask( |
| 137 FROM_HERE, | 138 FROM_HERE, |
| 138 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, | 139 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, |
| 139 weak_ptr_factory_.GetWeakPtr())); | 140 weak_ptr_factory_.GetWeakPtr())); |
| 140 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); | 141 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); |
| 141 } | 142 } |
| 142 | 143 |
| 143 FOR_EACH_OBSERVER(Observer, observers_, ToolbarVisibleCountChanged()); | 144 FOR_EACH_OBSERVER(Observer, observers_, ToolbarVisibleCountChanged()); |
| 144 } | 145 } |
| 145 | 146 |
| 146 void ExtensionToolbarModel::OnExtensionActionUpdated( | 147 void ExtensionToolbarModel::OnExtensionActionUpdated( |
| 147 ExtensionAction* extension_action, | 148 ExtensionAction* extension_action, |
| 148 content::WebContents* web_contents, | 149 content::WebContents* web_contents, |
| 149 content::BrowserContext* browser_context) { | 150 content::BrowserContext* browser_context) { |
| 150 const Extension* extension = | 151 const Extension* extension = |
| 151 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( | 152 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( |
| 152 extension_action->extension_id()); | 153 extension_action->extension_id()); |
| 153 // Notify observers if the extension exists and is in the model. | 154 // Notify observers if the extension exists and is in the model. |
| 154 ExtensionList::const_iterator iter = | 155 if (extension && |
| 155 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); | 156 std::find(toolbar_items_.begin(), |
| 156 if (iter != toolbar_items_.end()) { | 157 toolbar_items_.end(), |
| 157 FOR_EACH_OBSERVER( | 158 extension) != toolbar_items_.end()) { |
| 158 Observer, observers_, ToolbarExtensionUpdated(extension)); | 159 FOR_EACH_OBSERVER(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 } | |
| 166 } | 160 } |
| 167 } | 161 } |
| 168 | 162 |
| 169 void ExtensionToolbarModel::OnExtensionLoaded( | 163 void ExtensionToolbarModel::OnExtensionLoaded( |
| 170 content::BrowserContext* browser_context, | 164 content::BrowserContext* browser_context, |
| 171 const Extension* extension) { | 165 const Extension* extension) { |
| 172 // We don't want to add the same extension twice. It may have already been | 166 // We don't want to add the same extension twice. It may have already been |
| 173 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | 167 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
| 174 // hides the browser action and then disables and enables the extension. | 168 // hides the browser action and then disables and enables the extension. |
| 175 for (size_t i = 0; i < toolbar_items_.size(); i++) { | 169 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 | 493 |
| 500 void ExtensionToolbarModel::IncognitoPopulate() { | 494 void ExtensionToolbarModel::IncognitoPopulate() { |
| 501 DCHECK(profile_->IsOffTheRecord()); | 495 DCHECK(profile_->IsOffTheRecord()); |
| 502 // Clear the current items, if any. | 496 // Clear the current items, if any. |
| 503 ClearItems(); | 497 ClearItems(); |
| 504 | 498 |
| 505 const ExtensionToolbarModel* original_model = | 499 const ExtensionToolbarModel* original_model = |
| 506 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); | 500 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); |
| 507 | 501 |
| 508 // Find the absolute value of the original model's count. | 502 // Find the absolute value of the original model's count. |
| 509 int original_visible = original_model->visible_icon_count(); | 503 int original_visible = original_model->GetVisibleIconCount(); |
| 504 if (original_visible == -1) |
| 505 original_visible = original_model->toolbar_items_.size(); |
| 510 | 506 |
| 511 // In incognito mode, we show only those extensions that are | 507 // In incognito mode, we show only those extensions that are |
| 512 // incognito-enabled. Further, any actions that were overflowed in regular | 508 // incognito-enabled. Further, any actions that were overflowed in regular |
| 513 // mode are still overflowed. Order is the same as in regular mode. | 509 // mode are still overflowed. Order is the same as in regular mode. |
| 514 visible_icon_count_ = 0; | 510 visible_icon_count_ = 0; |
| 515 for (ExtensionList::const_iterator iter = | 511 for (ExtensionList::const_iterator iter = |
| 516 original_model->toolbar_items_.begin(); | 512 original_model->toolbar_items_.begin(); |
| 517 iter != original_model->toolbar_items_.end(); ++iter) { | 513 iter != original_model->toolbar_items_.end(); ++iter) { |
| 518 if (ShouldAddExtension(iter->get())) { | 514 if (ShouldAddExtension(iter->get())) { |
| 519 toolbar_items_.push_back(*iter); | 515 toolbar_items_.push_back(*iter); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 if (last_known_positions_.size() > pref_position_size) { | 592 if (last_known_positions_.size() > pref_position_size) { |
| 597 // Need to update pref because we have extra icons. But can't call | 593 // Need to update pref because we have extra icons. But can't call |
| 598 // UpdatePrefs() directly within observation closure. | 594 // UpdatePrefs() directly within observation closure. |
| 599 base::MessageLoop::current()->PostTask( | 595 base::MessageLoop::current()->PostTask( |
| 600 FROM_HERE, | 596 FROM_HERE, |
| 601 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 597 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
| 602 weak_ptr_factory_.GetWeakPtr())); | 598 weak_ptr_factory_.GetWeakPtr())); |
| 603 } | 599 } |
| 604 } | 600 } |
| 605 | 601 |
| 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 | |
| 649 bool ExtensionToolbarModel::ShowExtensionActionPopup( | 602 bool ExtensionToolbarModel::ShowExtensionActionPopup( |
| 650 const Extension* extension, | 603 const Extension* extension, |
| 651 Browser* browser, | 604 Browser* browser, |
| 652 bool grant_active_tab) { | 605 bool grant_active_tab) { |
| 653 ObserverListBase<Observer>::Iterator it(observers_); | 606 ObserverListBase<Observer>::Iterator it(observers_); |
| 654 Observer* obs = NULL; | 607 Observer* obs = NULL; |
| 655 // Look for the Observer associated with the browser. | 608 // Look for the Observer associated with the browser. |
| 656 // This would be cleaner if we had an abstract class for the Toolbar UI | 609 // This would be cleaner if we had an abstract class for the Toolbar UI |
| 657 // (like we do for LocationBar), but sadly, we don't. | 610 // (like we do for LocationBar), but sadly, we don't. |
| 658 while ((obs = it.GetNext()) != NULL) { | 611 while ((obs = it.GetNext()) != NULL) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 if (is_highlighting_) { | 682 if (is_highlighting_) { |
| 730 highlighted_items_.clear(); | 683 highlighted_items_.clear(); |
| 731 is_highlighting_ = false; | 684 is_highlighting_ = false; |
| 732 if (old_visible_icon_count_ != visible_icon_count_) | 685 if (old_visible_icon_count_ != visible_icon_count_) |
| 733 SetVisibleIconCount(old_visible_icon_count_); | 686 SetVisibleIconCount(old_visible_icon_count_); |
| 734 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 687 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); |
| 735 } | 688 } |
| 736 } | 689 } |
| 737 | 690 |
| 738 } // namespace extensions | 691 } // namespace extensions |
| OLD | NEW |