Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(675)

Side by Side Diff: chrome/browser/extensions/extension_toolbar_model.cc

Issue 675023002: Make extensions that desire to act pop out if in overflow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test fixes Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 } 146 }
147 147
148 void ExtensionToolbarModel::OnExtensionActionUpdated( 148 void ExtensionToolbarModel::OnExtensionActionUpdated(
149 ExtensionAction* extension_action, 149 ExtensionAction* extension_action,
150 content::WebContents* web_contents, 150 content::WebContents* web_contents,
151 content::BrowserContext* browser_context) { 151 content::BrowserContext* browser_context) {
152 const Extension* extension = 152 const Extension* extension =
153 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( 153 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID(
154 extension_action->extension_id()); 154 extension_action->extension_id());
155 // Notify observers if the extension exists and is in the model. 155 // Notify observers if the extension exists and is in the model.
156 if (extension && 156 ExtensionList::const_iterator iter =
Finnur 2014/10/25 14:09:58 Is the extension check unnecessary now? Not saying
Devlin 2014/10/29 20:29:27 I think it actually always was - if extension is n
157 std::find(toolbar_items_.begin(), 157 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension);
158 toolbar_items_.end(), 158 if (iter != toolbar_items_.end()) {
159 extension) != toolbar_items_.end()) { 159 FOR_EACH_OBSERVER(
160 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionUpdated(extension)); 160 Observer, observers_, ToolbarExtensionUpdated(extension));
161 // If the action was in the overflow menu, we have to alert observers that
162 // the toolbar needs to be reordered (to show the action).
163 if (static_cast<size_t>(iter - toolbar_items_.begin()) >=
164 GetAbsoluteVisibleIconCount()) {
165 FOR_EACH_OBSERVER(
166 Observer, observers_, ToolbarReorderNecessary(web_contents));
167 }
161 } 168 }
162 } 169 }
163 170
164 void ExtensionToolbarModel::OnExtensionLoaded( 171 void ExtensionToolbarModel::OnExtensionLoaded(
165 content::BrowserContext* browser_context, 172 content::BrowserContext* browser_context,
166 const Extension* extension) { 173 const Extension* extension) {
167 // We don't want to add the same extension twice. It may have already been 174 // We don't want to add the same extension twice. It may have already been
168 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user 175 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user
169 // hides the browser action and then disables and enables the extension. 176 // hides the browser action and then disables and enables the extension.
170 for (size_t i = 0; i < toolbar_items_.size(); i++) { 177 for (size_t i = 0; i < toolbar_items_.size(); i++) {
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 if (last_known_positions_.size() > pref_position_size) { 568 if (last_known_positions_.size() > pref_position_size) {
562 // Need to update pref because we have extra icons. But can't call 569 // Need to update pref because we have extra icons. But can't call
563 // UpdatePrefs() directly within observation closure. 570 // UpdatePrefs() directly within observation closure.
564 base::MessageLoop::current()->PostTask( 571 base::MessageLoop::current()->PostTask(
565 FROM_HERE, 572 FROM_HERE,
566 base::Bind(&ExtensionToolbarModel::UpdatePrefs, 573 base::Bind(&ExtensionToolbarModel::UpdatePrefs,
567 weak_ptr_factory_.GetWeakPtr())); 574 weak_ptr_factory_.GetWeakPtr()));
568 } 575 }
569 } 576 }
570 577
578 int ExtensionToolbarModel::GetVisibleIconCountForTab(
579 content::WebContents* web_contents) const {
580 int base_visible = visible_icon_count_;
581 if (base_visible == -1)
582 return base_visible; // Already displaying all actions.
583
584 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_);
585 size_t overflowed_actions_to_run = 0u;
Finnur 2014/10/25 14:09:58 nit: to run? Seems a bit weird to use that phrase
Devlin 2014/10/29 20:29:27 Done.
586 for (size_t i = base_visible; i < toolbar_items_.size(); ++i) {
587 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(),
588 web_contents))
589 ++overflowed_actions_to_run;
590 }
591 return base_visible + overflowed_actions_to_run;
592 }
593
594 ExtensionList ExtensionToolbarModel::GetItemOrderForTab(
595 content::WebContents* web_contents) const {
596 // If we're highlighting, the items are always the same.
597 if (is_highlighting_)
598 return highlighted_items_;
599
600 // Start by initializing the array to be the same as toolbar items (this isn't
601 // any more expensive than initializing it to be of the same size with all
602 // nulls, and saves us time at the end).
603 ExtensionList result = toolbar_items_;
604 if (toolbar_items_.empty())
605 return result;
606
607 ExtensionList overflowed_actions_to_run;
608 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_);
609 size_t base_visible = GetAbsoluteVisibleIconCount();
610 size_t index = toolbar_items_.size() - 1;
611
612 // We need to pop out any extensions that are overflowed, but want to act.
613 // Iterate over the overflowed extensions in reverse, and append those that
614 // don't want to act, while storing those that do separately.
615 for (size_t i = toolbar_items_.size() - 1; i >= base_visible; --i) {
616 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(),
617 web_contents))
618 overflowed_actions_to_run.push_back(toolbar_items_[i]);
619 else
620 result[index--] = toolbar_items_[i];
621 }
622 // Now, append any that do want to act but were otherwise overflowed.
623 for (const scoped_refptr<const Extension>& extension :
624 overflowed_actions_to_run) {
625 result[index--] = extension;
626 }
627 // We should have appended all of (and only) those items that were in
628 // overflow.
629 DCHECK_EQ(base_visible, index + 1);
630 // The rest of the vector should be fine, since we don't need to reorder any
631 // of the already-visible extensions.
632 return result;
633 }
634
571 bool ExtensionToolbarModel::ShowExtensionActionPopup( 635 bool ExtensionToolbarModel::ShowExtensionActionPopup(
572 const Extension* extension, 636 const Extension* extension,
573 Browser* browser, 637 Browser* browser,
574 bool grant_active_tab) { 638 bool grant_active_tab) {
575 ObserverListBase<Observer>::Iterator it(observers_); 639 ObserverListBase<Observer>::Iterator it(observers_);
576 Observer* obs = NULL; 640 Observer* obs = NULL;
577 // Look for the Observer associated with the browser. 641 // Look for the Observer associated with the browser.
578 // This would be cleaner if we had an abstract class for the Toolbar UI 642 // This would be cleaner if we had an abstract class for the Toolbar UI
579 // (like we do for LocationBar), but sadly, we don't. 643 // (like we do for LocationBar), but sadly, we don't.
580 while ((obs = it.GetNext()) != NULL) { 644 while ((obs = it.GetNext()) != NULL) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 if (is_highlighting_) { 715 if (is_highlighting_) {
652 highlighted_items_.clear(); 716 highlighted_items_.clear();
653 is_highlighting_ = false; 717 is_highlighting_ = false;
654 if (old_visible_icon_count_ != visible_icon_count_) 718 if (old_visible_icon_count_ != visible_icon_count_)
655 SetVisibleIconCount(old_visible_icon_count_); 719 SetVisibleIconCount(old_visible_icon_count_);
656 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); 720 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false));
657 } 721 }
658 } 722 }
659 723
660 } // namespace extensions 724 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_toolbar_model.h ('k') | chrome/browser/extensions/extension_toolbar_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698