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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 } | 145 } |
146 | 146 |
147 void ExtensionToolbarModel::OnExtensionActionUpdated( | 147 void ExtensionToolbarModel::OnExtensionActionUpdated( |
148 ExtensionAction* extension_action, | 148 ExtensionAction* extension_action, |
149 content::WebContents* web_contents, | 149 content::WebContents* web_contents, |
150 content::BrowserContext* browser_context) { | 150 content::BrowserContext* browser_context) { |
151 const Extension* extension = | 151 const Extension* extension = |
152 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( | 152 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( |
153 extension_action->extension_id()); | 153 extension_action->extension_id()); |
154 // Notify observers if the extension exists and is in the model. | 154 // Notify observers if the extension exists and is in the model. |
155 if (extension && | 155 ExtensionList::const_iterator iter = |
156 std::find(toolbar_items_.begin(), | 156 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); |
157 toolbar_items_.end(), | 157 if (iter != toolbar_items_.end()) { |
158 extension) != toolbar_items_.end()) { | 158 FOR_EACH_OBSERVER( |
159 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionUpdated(extension)); | 159 Observer, observers_, ToolbarExtensionUpdated(extension)); |
160 // If the action was in the overflow menu, we have to alert observers that | |
161 // the toolbar needs to be reordered (to show the action). | |
162 if (static_cast<size_t>(iter - toolbar_items_.begin()) >= | |
163 GetAbsoluteVisibleIconCount()) { | |
164 FOR_EACH_OBSERVER( | |
165 Observer, observers_, ToolbarReorderNecessary(web_contents)); | |
166 } | |
160 } | 167 } |
161 } | 168 } |
162 | 169 |
163 void ExtensionToolbarModel::OnExtensionLoaded( | 170 void ExtensionToolbarModel::OnExtensionLoaded( |
164 content::BrowserContext* browser_context, | 171 content::BrowserContext* browser_context, |
165 const Extension* extension) { | 172 const Extension* extension) { |
166 // We don't want to add the same extension twice. It may have already been | 173 // 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 | 174 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
168 // hides the browser action and then disables and enables the extension. | 175 // hides the browser action and then disables and enables the extension. |
169 for (size_t i = 0; i < toolbar_items_.size(); i++) { | 176 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 if (last_known_positions_.size() > pref_position_size) { | 567 if (last_known_positions_.size() > pref_position_size) { |
561 // Need to update pref because we have extra icons. But can't call | 568 // Need to update pref because we have extra icons. But can't call |
562 // UpdatePrefs() directly within observation closure. | 569 // UpdatePrefs() directly within observation closure. |
563 base::MessageLoop::current()->PostTask( | 570 base::MessageLoop::current()->PostTask( |
564 FROM_HERE, | 571 FROM_HERE, |
565 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 572 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
566 weak_ptr_factory_.GetWeakPtr())); | 573 weak_ptr_factory_.GetWeakPtr())); |
567 } | 574 } |
568 } | 575 } |
569 | 576 |
577 int ExtensionToolbarModel::GetVisibleIconCountForTab( | |
578 content::WebContents* web_contents) const { | |
579 int base_visible = visible_icon_count_; | |
Peter Kasting
2014/10/30 22:05:59
Why do we need this temp? Does visible_icon_count
Devlin
2014/10/31 17:44:09
Whoops, leftover from old implementation. Fixed.
| |
580 if (base_visible == -1) | |
581 return base_visible; // Already displaying all actions. | |
582 | |
583 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); | |
584 size_t overflowed_actions_wanting_to_run = 0u; | |
585 for (size_t i = base_visible; i < toolbar_items_.size(); ++i) { | |
586 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(), | |
587 web_contents)) | |
588 ++overflowed_actions_wanting_to_run; | |
589 } | |
590 return base_visible + overflowed_actions_wanting_to_run; | |
591 } | |
592 | |
593 ExtensionList ExtensionToolbarModel::GetItemOrderForTab( | |
594 content::WebContents* web_contents) const { | |
595 // If we're highlighting, the items are always the same. | |
596 if (is_highlighting_) | |
597 return highlighted_items_; | |
598 | |
599 // Start by initializing the array to be the same as toolbar items (this isn't | |
600 // any more expensive than initializing it to be of the same size with all | |
601 // nulls, and saves us time at the end). | |
602 ExtensionList result = toolbar_items_; | |
603 if (toolbar_items_.empty()) | |
604 return result; | |
605 | |
606 ExtensionList overflowed_actions_wanting_to_run; | |
607 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); | |
608 int base_visible = GetAbsoluteVisibleIconCount(); | |
609 int index = toolbar_items_.size() - 1; | |
610 | |
611 // We need to pop out any extensions that are overflowed, but want to act. | |
612 // Iterate over the overflowed extensions in reverse, and append those that | |
613 // don't want to act, while storing those that do separately. | |
614 for (int i = toolbar_items_.size() - 1; i >= base_visible; --i) { | |
Peter Kasting
2014/10/30 22:05:59
This is a very complicated way of doing what I thi
Devlin
2014/10/31 17:44:09
Ah, yes, that is quite a bit simpler. Done.
| |
615 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(), | |
616 web_contents)) | |
617 overflowed_actions_wanting_to_run.push_back(toolbar_items_[i]); | |
618 else | |
619 result[index--] = toolbar_items_[i]; | |
620 } | |
621 // Now, append any that do want to act but were otherwise overflowed. | |
622 for (const scoped_refptr<const Extension>& extension : | |
623 overflowed_actions_wanting_to_run) { | |
624 result[index--] = extension; | |
625 } | |
626 // We should have appended all of (and only) those items that were in | |
627 // overflow. | |
628 DCHECK_EQ(base_visible, index + 1); | |
629 // The rest of the vector should be fine, since we don't need to reorder any | |
630 // of the already-visible extensions. | |
631 return result; | |
632 } | |
633 | |
570 bool ExtensionToolbarModel::ShowExtensionActionPopup( | 634 bool ExtensionToolbarModel::ShowExtensionActionPopup( |
571 const Extension* extension, | 635 const Extension* extension, |
572 Browser* browser, | 636 Browser* browser, |
573 bool grant_active_tab) { | 637 bool grant_active_tab) { |
574 ObserverListBase<Observer>::Iterator it(observers_); | 638 ObserverListBase<Observer>::Iterator it(observers_); |
575 Observer* obs = NULL; | 639 Observer* obs = NULL; |
576 // Look for the Observer associated with the browser. | 640 // Look for the Observer associated with the browser. |
577 // This would be cleaner if we had an abstract class for the Toolbar UI | 641 // This would be cleaner if we had an abstract class for the Toolbar UI |
578 // (like we do for LocationBar), but sadly, we don't. | 642 // (like we do for LocationBar), but sadly, we don't. |
579 while ((obs = it.GetNext()) != NULL) { | 643 while ((obs = it.GetNext()) != NULL) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
650 if (is_highlighting_) { | 714 if (is_highlighting_) { |
651 highlighted_items_.clear(); | 715 highlighted_items_.clear(); |
652 is_highlighting_ = false; | 716 is_highlighting_ = false; |
653 if (old_visible_icon_count_ != visible_icon_count_) | 717 if (old_visible_icon_count_ != visible_icon_count_) |
654 SetVisibleIconCount(old_visible_icon_count_); | 718 SetVisibleIconCount(old_visible_icon_count_); |
655 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 719 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); |
656 } | 720 } |
657 } | 721 } |
658 | 722 |
659 } // namespace extensions | 723 } // namespace extensions |
OLD | NEW |