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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 id); | 108 id); |
109 toolbar_items_.insert(iter, extension); | 109 toolbar_items_.insert(iter, extension); |
110 } else { | 110 } else { |
111 // Otherwise, put |extension| at the end. | 111 // Otherwise, put |extension| at the end. |
112 DCHECK_EQ(toolbar_items_.size(), index); | 112 DCHECK_EQ(toolbar_items_.size(), index); |
113 index = toolbar_items_.size(); | 113 index = toolbar_items_.size(); |
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(Observer, observers_, |
119 Observer, observers_, ToolbarExtensionMoved(extension.get(), index)); | 119 OnToolbarExtensionMoved(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(size_t count) { |
125 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; | 125 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; |
126 | 126 |
127 // 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 |
128 // 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 |
129 // 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 |
130 // don't store anything in incognito. | 130 // don't store anything in incognito. |
131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { | 131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { |
132 // 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 |
133 // 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 |
134 // 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 |
135 // 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. |
136 base::MessageLoop::current()->PostTask( | 136 base::MessageLoop::current()->PostTask( |
137 FROM_HERE, | 137 FROM_HERE, |
138 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, | 138 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, |
139 weak_ptr_factory_.GetWeakPtr())); | 139 weak_ptr_factory_.GetWeakPtr())); |
140 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); | 140 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); |
141 } | 141 } |
142 | 142 |
143 FOR_EACH_OBSERVER(Observer, observers_, ToolbarVisibleCountChanged()); | 143 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarVisibleCountChanged()); |
144 } | 144 } |
145 | 145 |
146 void ExtensionToolbarModel::OnExtensionActionUpdated( | 146 void ExtensionToolbarModel::OnExtensionActionUpdated( |
147 ExtensionAction* extension_action, | 147 ExtensionAction* extension_action, |
148 content::WebContents* web_contents, | 148 content::WebContents* web_contents, |
149 content::BrowserContext* browser_context) { | 149 content::BrowserContext* browser_context) { |
150 const Extension* extension = | 150 const Extension* extension = |
151 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( | 151 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( |
152 extension_action->extension_id()); | 152 extension_action->extension_id()); |
153 // Notify observers if the extension exists and is in the model. | 153 // Notify observers if the extension exists and is in the model. |
154 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) != | 154 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) != |
155 toolbar_items_.end()) { | 155 toolbar_items_.end()) { |
156 FOR_EACH_OBSERVER( | 156 FOR_EACH_OBSERVER(Observer, observers_, |
157 Observer, observers_, ToolbarExtensionUpdated(extension)); | 157 OnToolbarExtensionUpdated(extension)); |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 void ExtensionToolbarModel::OnExtensionLoaded( | 161 void ExtensionToolbarModel::OnExtensionLoaded( |
162 content::BrowserContext* browser_context, | 162 content::BrowserContext* browser_context, |
163 const Extension* extension) { | 163 const Extension* extension) { |
164 // We don't want to add the same extension twice. It may have already been | 164 // We don't want to add the same extension twice. It may have already been |
165 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | 165 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
166 // hides the browser action and then disables and enables the extension. | 166 // hides the browser action and then disables and enables the extension. |
167 for (size_t i = 0; i < toolbar_items_.size(); i++) { | 167 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } else { | 340 } else { |
341 new_index = FindNewPositionFromLastKnownGood(extension); | 341 new_index = FindNewPositionFromLastKnownGood(extension); |
342 } | 342 } |
343 | 343 |
344 toolbar_items_.insert(toolbar_items_.begin() + new_index, extension); | 344 toolbar_items_.insert(toolbar_items_.begin() + new_index, extension); |
345 | 345 |
346 // If we're currently highlighting, then even though we add a browser action | 346 // If we're currently highlighting, then even though we add a browser action |
347 // to the full list (|toolbar_items_|, there won't be another *visible* | 347 // to the full list (|toolbar_items_|, there won't be another *visible* |
348 // browser action, which was what the observers care about. | 348 // browser action, which was what the observers care about. |
349 if (!is_highlighting_) { | 349 if (!is_highlighting_) { |
350 FOR_EACH_OBSERVER( | 350 FOR_EACH_OBSERVER(Observer, observers_, |
351 Observer, observers_, ToolbarExtensionAdded(extension, new_index)); | 351 OnToolbarExtensionAdded(extension, new_index)); |
352 | 352 |
353 int visible_count_delta = 0; | 353 int visible_count_delta = 0; |
354 if (is_new_extension && !all_icons_visible()) { | 354 if (is_new_extension && !all_icons_visible()) { |
355 // If this is a new extension (and not all extensions are visible), we | 355 // If this is a new extension (and not all extensions are visible), we |
356 // expand the toolbar out so that the new one can be seen. | 356 // expand the toolbar out so that the new one can be seen. |
357 visible_count_delta = 1; | 357 visible_count_delta = 1; |
358 } else if (profile_->IsOffTheRecord()) { | 358 } else if (profile_->IsOffTheRecord()) { |
359 // If this is an incognito profile, we also have to check to make sure the | 359 // If this is an incognito profile, we also have to check to make sure the |
360 // overflow matches the main bar's status. | 360 // overflow matches the main bar's status. |
361 ExtensionToolbarModel* main_model = | 361 ExtensionToolbarModel* main_model = |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 toolbar_items_.erase(pos); | 395 toolbar_items_.erase(pos); |
396 | 396 |
397 // If we're in highlight mode, we also have to remove the extension from | 397 // If we're in highlight mode, we also have to remove the extension from |
398 // the highlighted list. | 398 // the highlighted list. |
399 if (is_highlighting_) { | 399 if (is_highlighting_) { |
400 pos = std::find(highlighted_items_.begin(), | 400 pos = std::find(highlighted_items_.begin(), |
401 highlighted_items_.end(), | 401 highlighted_items_.end(), |
402 extension); | 402 extension); |
403 if (pos != highlighted_items_.end()) { | 403 if (pos != highlighted_items_.end()) { |
404 highlighted_items_.erase(pos); | 404 highlighted_items_.erase(pos); |
405 FOR_EACH_OBSERVER( | 405 FOR_EACH_OBSERVER(Observer, observers_, |
406 Observer, observers_, ToolbarExtensionRemoved(extension)); | 406 OnToolbarExtensionRemoved(extension)); |
407 // If the highlighted list is now empty, we stop highlighting. | 407 // If the highlighted list is now empty, we stop highlighting. |
408 if (highlighted_items_.empty()) | 408 if (highlighted_items_.empty()) |
409 StopHighlighting(); | 409 StopHighlighting(); |
410 } | 410 } |
411 } else { | 411 } else { |
412 FOR_EACH_OBSERVER(Observer, observers_, ToolbarExtensionRemoved(extension)); | 412 FOR_EACH_OBSERVER(Observer, observers_, |
| 413 OnToolbarExtensionRemoved(extension)); |
413 } | 414 } |
414 | 415 |
415 UpdatePrefs(); | 416 UpdatePrefs(); |
416 } | 417 } |
417 | 418 |
418 // Combine the currently enabled extensions that have browser actions (which | 419 // Combine the currently enabled extensions that have browser actions (which |
419 // we get from the ExtensionRegistry) with the ordering we get from the | 420 // we get from the ExtensionRegistry) with the ordering we get from the |
420 // pref service. For robustness we use a somewhat inefficient process: | 421 // pref service. For robustness we use a somewhat inefficient process: |
421 // 1. Create a vector of extensions sorted by their pref values. This vector may | 422 // 1. Create a vector of extensions sorted by their pref values. This vector may |
422 // have holes. | 423 // have holes. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 last_known_positions_[i]) == pref_positions.end()) { | 588 last_known_positions_[i]) == pref_positions.end()) { |
588 pref_positions.push_back(last_known_positions_[i]); | 589 pref_positions.push_back(last_known_positions_[i]); |
589 } | 590 } |
590 } | 591 } |
591 last_known_positions_.swap(pref_positions); | 592 last_known_positions_.swap(pref_positions); |
592 | 593 |
593 // Clear out the old... | 594 // Clear out the old... |
594 while (!toolbar_items_.empty()) { | 595 while (!toolbar_items_.empty()) { |
595 scoped_refptr<const Extension> extension = toolbar_items_.back(); | 596 scoped_refptr<const Extension> extension = toolbar_items_.back(); |
596 toolbar_items_.pop_back(); | 597 toolbar_items_.pop_back(); |
597 FOR_EACH_OBSERVER( | 598 FOR_EACH_OBSERVER(Observer, observers_, |
598 Observer, observers_, ToolbarExtensionRemoved(extension.get())); | 599 OnToolbarExtensionRemoved(extension.get())); |
599 } | 600 } |
600 DCHECK(toolbar_items_.empty()); | 601 DCHECK(toolbar_items_.empty()); |
601 | 602 |
602 // ...Add the new... | 603 // ...Add the new... |
603 Populate(&last_known_positions_); | 604 Populate(&last_known_positions_); |
604 | 605 |
605 // ...And notify. | 606 // ...And notify. |
606 for (size_t i = 0; i < toolbar_items().size(); ++i) { | 607 for (size_t i = 0; i < toolbar_items().size(); ++i) { |
607 FOR_EACH_OBSERVER(Observer, | 608 FOR_EACH_OBSERVER(Observer, observers_, |
608 observers_, | 609 OnToolbarExtensionAdded(toolbar_items()[i].get(), i)); |
609 ToolbarExtensionAdded(toolbar_items()[i].get(), i)); | |
610 } | 610 } |
611 | 611 |
612 if (last_known_positions_.size() > pref_position_size) { | 612 if (last_known_positions_.size() > pref_position_size) { |
613 // Need to update pref because we have extra icons. But can't call | 613 // Need to update pref because we have extra icons. But can't call |
614 // UpdatePrefs() directly within observation closure. | 614 // UpdatePrefs() directly within observation closure. |
615 base::MessageLoop::current()->PostTask( | 615 base::MessageLoop::current()->PostTask( |
616 FROM_HERE, | 616 FROM_HERE, |
617 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 617 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
618 weak_ptr_factory_.GetWeakPtr())); | 618 weak_ptr_factory_.GetWeakPtr())); |
619 } | 619 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 } | 687 } |
688 | 688 |
689 // If we have any items in |highlighted_items_|, then we entered highlighting | 689 // If we have any items in |highlighted_items_|, then we entered highlighting |
690 // mode. | 690 // mode. |
691 if (highlighted_items_.size()) { | 691 if (highlighted_items_.size()) { |
692 old_visible_icon_count_ = visible_icon_count_; | 692 old_visible_icon_count_ = visible_icon_count_; |
693 is_highlighting_ = true; | 693 is_highlighting_ = true; |
694 if (visible_icon_count() < extension_ids.size()) | 694 if (visible_icon_count() < extension_ids.size()) |
695 SetVisibleIconCount(extension_ids.size()); | 695 SetVisibleIconCount(extension_ids.size()); |
696 | 696 |
697 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(true)); | 697 FOR_EACH_OBSERVER(Observer, observers_, |
| 698 OnToolbarHighlightModeChanged(true)); |
698 return true; | 699 return true; |
699 } | 700 } |
700 | 701 |
701 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if | 702 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if |
702 // we were otherwise in it). | 703 // we were otherwise in it). |
703 if (is_highlighting_) | 704 if (is_highlighting_) |
704 StopHighlighting(); | 705 StopHighlighting(); |
705 return false; | 706 return false; |
706 } | 707 } |
707 | 708 |
708 void ExtensionToolbarModel::StopHighlighting() { | 709 void ExtensionToolbarModel::StopHighlighting() { |
709 if (is_highlighting_) { | 710 if (is_highlighting_) { |
710 highlighted_items_.clear(); | 711 highlighted_items_.clear(); |
711 is_highlighting_ = false; | 712 is_highlighting_ = false; |
712 if (old_visible_icon_count_ != visible_icon_count_) | 713 if (old_visible_icon_count_ != visible_icon_count_) |
713 SetVisibleIconCount(old_visible_icon_count_); | 714 SetVisibleIconCount(old_visible_icon_count_); |
714 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 715 FOR_EACH_OBSERVER(Observer, observers_, |
| 716 OnToolbarHighlightModeChanged(false)); |
715 } | 717 } |
716 } | 718 } |
717 | 719 |
718 } // namespace extensions | 720 } // namespace extensions |
OLD | NEW |