This source file includes following definitions.
- BrowserActionShowPopup
- weak_ptr_factory_
- Get
- AddObserver
- RemoveObserver
- MoveBrowserAction
- ExecuteBrowserAction
- SetVisibleIconCount
- Observe
- FindNewPositionFromLastKnownGood
- AddExtension
- RemoveExtension
- UninstalledExtension
- InitializeExtensionList
- Populate
- UpdatePrefs
- IncognitoIndexToOriginal
- OriginalIndexToIncognito
- OnExtensionToolbarPrefChange
- ShowBrowserActionPopup
- EnsureVisibility
- HighlightExtensions
- StopHighlighting
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include <string>
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_base.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/extension_toolbar_model_factory.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/feature_switch.h"
namespace extensions {
bool ExtensionToolbarModel::Observer::BrowserActionShowPopup(
const extensions::Extension* extension) {
return false;
}
ExtensionToolbarModel::ExtensionToolbarModel(
Profile* profile,
extensions::ExtensionPrefs* extension_prefs)
: profile_(profile),
extension_prefs_(extension_prefs),
prefs_(profile_->GetPrefs()),
extensions_initialized_(false),
is_highlighting_(false),
weak_ptr_factory_(this) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_));
registrar_.Add(
this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED,
content::Source<extensions::ExtensionPrefs>(extension_prefs_));
visible_icon_count_ = prefs_->GetInteger(
extensions::pref_names::kToolbarSize);
pref_change_registrar_.Init(prefs_);
pref_change_callback_ =
base::Bind(&ExtensionToolbarModel::OnExtensionToolbarPrefChange,
base::Unretained(this));
pref_change_registrar_.Add(extensions::pref_names::kToolbar,
pref_change_callback_);
}
ExtensionToolbarModel::~ExtensionToolbarModel() {
}
ExtensionToolbarModel* ExtensionToolbarModel::Get(Profile* profile) {
return ExtensionToolbarModelFactory::GetForProfile(profile);
}
void ExtensionToolbarModel::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void ExtensionToolbarModel::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void ExtensionToolbarModel::MoveBrowserAction(const Extension* extension,
int index) {
ExtensionList::iterator pos = std::find(toolbar_items_.begin(),
toolbar_items_.end(), extension);
if (pos == toolbar_items_.end()) {
NOTREACHED();
return;
}
toolbar_items_.erase(pos);
ExtensionIdList::iterator pos_id;
pos_id = std::find(last_known_positions_.begin(),
last_known_positions_.end(), extension->id());
if (pos_id != last_known_positions_.end())
last_known_positions_.erase(pos_id);
int i = 0;
bool inserted = false;
for (ExtensionList::iterator iter = toolbar_items_.begin();
iter != toolbar_items_.end();
++iter, ++i) {
if (i == index) {
pos_id = std::find(last_known_positions_.begin(),
last_known_positions_.end(), (*iter)->id());
last_known_positions_.insert(pos_id, extension->id());
toolbar_items_.insert(iter, make_scoped_refptr(extension));
inserted = true;
break;
}
}
if (!inserted) {
DCHECK_EQ(index, static_cast<int>(toolbar_items_.size()));
index = toolbar_items_.size();
toolbar_items_.push_back(make_scoped_refptr(extension));
last_known_positions_.push_back(extension->id());
}
FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index));
UpdatePrefs();
}
ExtensionToolbarModel::Action ExtensionToolbarModel::ExecuteBrowserAction(
const Extension* extension,
Browser* browser,
GURL* popup_url_out,
bool should_grant) {
content::WebContents* web_contents = NULL;
int tab_id = 0;
if (!extensions::ExtensionTabUtil::GetDefaultTab(
browser, &web_contents, &tab_id)) {
return ACTION_NONE;
}
ExtensionAction* browser_action =
extensions::ExtensionActionManager::Get(profile_)->
GetBrowserAction(*extension);
if (!browser_action->GetIsVisible(tab_id))
return ACTION_NONE;
if (should_grant) {
extensions::TabHelper::FromWebContents(web_contents)->
active_tab_permission_granter()->GrantIfRequested(extension);
}
if (browser_action->HasPopup(tab_id)) {
if (popup_url_out)
*popup_url_out = browser_action->GetPopupUrl(tab_id);
return ACTION_SHOW_POPUP;
}
extensions::ExtensionActionAPI::BrowserActionExecuted(
browser->profile(), *browser_action, web_contents);
return ACTION_NONE;
}
void ExtensionToolbarModel::SetVisibleIconCount(int count) {
visible_icon_count_ =
count == static_cast<int>(toolbar_items_.size()) ? -1 : count;
if (!is_highlighting_) {
prefs_->SetInteger(extensions::pref_names::kToolbarSize,
visible_icon_count_);
}
}
void ExtensionToolbarModel::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
ExtensionService* extension_service =
extensions::ExtensionSystem::Get(profile_)->extension_service();
if (!extension_service || !extension_service->is_ready())
return;
if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
InitializeExtensionList(extension_service);
return;
}
const Extension* extension = NULL;
if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
extension = content::Details<extensions::UnloadedExtensionInfo>(
details)->extension;
} else if (type ==
chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) {
extension = extension_service->GetExtensionById(
*content::Details<const std::string>(details).ptr(), true);
} else {
extension = content::Details<const Extension>(details).ptr();
}
if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
for (size_t i = 0; i < toolbar_items_.size(); i++) {
if (toolbar_items_[i].get() == extension)
return;
}
if (ExtensionActionAPI::GetBrowserActionVisibility(
extension_prefs_, extension->id())) {
AddExtension(extension);
}
} else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
RemoveExtension(extension);
} else if (type == chrome::NOTIFICATION_EXTENSION_UNINSTALLED) {
UninstalledExtension(extension);
} else if (type ==
chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) {
if (ExtensionActionAPI::GetBrowserActionVisibility(
extension_prefs_, extension->id())) {
AddExtension(extension);
} else {
RemoveExtension(extension);
}
} else {
NOTREACHED() << "Received unexpected notification";
}
}
size_t ExtensionToolbarModel::FindNewPositionFromLastKnownGood(
const Extension* extension) {
size_t new_index = 0;
for (ExtensionIdList::const_iterator iter_id = last_known_positions_.begin();
iter_id < last_known_positions_.end(); ++iter_id) {
if ((*iter_id) == extension->id())
return new_index;
for (ExtensionList::const_iterator iter_ext = toolbar_items_.begin();
iter_ext < toolbar_items_.end(); ++iter_ext) {
if ((*iter_ext)->id().compare(*iter_id) == 0) {
++new_index;
break;
}
}
}
return -1;
}
void ExtensionToolbarModel::AddExtension(const Extension* extension) {
if (!ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension))
return;
size_t new_index = -1;
ExtensionIdList::iterator last_pos = std::find(last_known_positions_.begin(),
last_known_positions_.end(),
extension->id());
if (last_pos != last_known_positions_.end()) {
new_index = FindNewPositionFromLastKnownGood(extension);
if (new_index != toolbar_items_.size()) {
toolbar_items_.insert(toolbar_items_.begin() + new_index,
make_scoped_refptr(extension));
} else {
toolbar_items_.push_back(make_scoped_refptr(extension));
}
} else {
toolbar_items_.push_back(make_scoped_refptr(extension));
last_known_positions_.push_back(extension->id());
new_index = toolbar_items_.size() - 1;
UpdatePrefs();
}
if (!is_highlighting_) {
FOR_EACH_OBSERVER(Observer, observers_,
BrowserActionAdded(extension, new_index));
}
}
void ExtensionToolbarModel::RemoveExtension(const Extension* extension) {
ExtensionList::iterator pos =
std::find(toolbar_items_.begin(), toolbar_items_.end(), extension);
if (pos == toolbar_items_.end())
return;
toolbar_items_.erase(pos);
if (is_highlighting_) {
pos = std::find(highlighted_items_.begin(),
highlighted_items_.end(),
extension);
if (pos != highlighted_items_.end()) {
highlighted_items_.erase(pos);
FOR_EACH_OBSERVER(Observer, observers_, BrowserActionRemoved(extension));
if (highlighted_items_.empty())
StopHighlighting();
}
} else {
FOR_EACH_OBSERVER(Observer, observers_, BrowserActionRemoved(extension));
}
UpdatePrefs();
}
void ExtensionToolbarModel::UninstalledExtension(const Extension* extension) {
ExtensionIdList::iterator pos =
std::find(last_known_positions_.begin(),
last_known_positions_.end(), extension->id());
if (pos != last_known_positions_.end()) {
last_known_positions_.erase(pos);
UpdatePrefs();
}
}
void ExtensionToolbarModel::InitializeExtensionList(ExtensionService* service) {
DCHECK(service->is_ready());
last_known_positions_ = extension_prefs_->GetToolbarOrder();
Populate(last_known_positions_, service);
extensions_initialized_ = true;
FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged());
}
void ExtensionToolbarModel::Populate(
const ExtensionIdList& positions,
ExtensionService* service) {
ExtensionList sorted;
sorted.resize(positions.size(), NULL);
ExtensionList unsorted;
ExtensionActionManager* extension_action_manager =
ExtensionActionManager::Get(profile_);
int hidden = 0;
for (extensions::ExtensionSet::const_iterator it =
service->extensions()->begin();
it != service->extensions()->end(); ++it) {
const Extension* extension = it->get();
if (!extension_action_manager->GetBrowserAction(*extension))
continue;
if (!ExtensionActionAPI::GetBrowserActionVisibility(
extension_prefs_, extension->id())) {
++hidden;
continue;
}
ExtensionIdList::const_iterator pos =
std::find(positions.begin(), positions.end(), extension->id());
if (pos != positions.end())
sorted[pos - positions.begin()] = extension;
else
unsorted.push_back(make_scoped_refptr(extension));
}
for (size_t i = 0; i < toolbar_items_.size(); i++) {
FOR_EACH_OBSERVER(
Observer, observers_, BrowserActionRemoved(toolbar_items_[i].get()));
}
toolbar_items_.clear();
toolbar_items_.reserve(sorted.size() + unsorted.size());
for (ExtensionList::const_iterator iter = sorted.begin();
iter != sorted.end(); ++iter) {
if (iter->get() != NULL)
toolbar_items_.push_back(*iter);
}
toolbar_items_.insert(toolbar_items_.end(), unsorted.begin(),
unsorted.end());
UMA_HISTOGRAM_COUNTS_100(
"ExtensionToolbarModel.BrowserActionsPermanentlyHidden", hidden);
UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsCount",
toolbar_items_.size());
if (!toolbar_items_.empty()) {
UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsVisible",
visible_icon_count_ == -1 ?
base::HistogramBase::kSampleType_MAX :
visible_icon_count_);
}
for (size_t i = 0; i < toolbar_items_.size(); i++) {
FOR_EACH_OBSERVER(
Observer, observers_, BrowserActionAdded(toolbar_items_[i].get(), i));
}
}
void ExtensionToolbarModel::UpdatePrefs() {
if (!extension_prefs_)
return;
pref_change_registrar_.Remove(pref_names::kToolbar);
extension_prefs_->SetToolbarOrder(last_known_positions_);
pref_change_registrar_.Add(pref_names::kToolbar, pref_change_callback_);
}
int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) {
int original_index = 0, i = 0;
for (ExtensionList::iterator iter = toolbar_items_.begin();
iter != toolbar_items_.end();
++iter, ++original_index) {
if (util::IsIncognitoEnabled((*iter)->id(), profile_)) {
if (incognito_index == i)
break;
++i;
}
}
return original_index;
}
int ExtensionToolbarModel::OriginalIndexToIncognito(int original_index) {
int incognito_index = 0, i = 0;
for (ExtensionList::iterator iter = toolbar_items_.begin();
iter != toolbar_items_.end();
++iter, ++i) {
if (original_index == i)
break;
if (util::IsIncognitoEnabled((*iter)->id(), profile_))
++incognito_index;
}
return incognito_index;
}
void ExtensionToolbarModel::OnExtensionToolbarPrefChange() {
if (!extensions_initialized_)
return;
ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder();
size_t pref_position_size = pref_positions.size();
for (size_t i = 0; i < last_known_positions_.size(); ++i) {
if (std::find(pref_positions.begin(), pref_positions.end(),
last_known_positions_[i]) == pref_positions.end()) {
pref_positions.push_back(last_known_positions_[i]);
}
}
last_known_positions_.swap(pref_positions);
Populate(last_known_positions_,
ExtensionSystem::Get(profile_)->extension_service());
if (last_known_positions_.size() > pref_position_size) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ExtensionToolbarModel::UpdatePrefs,
weak_ptr_factory_.GetWeakPtr()));
}
}
bool ExtensionToolbarModel::ShowBrowserActionPopup(const Extension* extension) {
ObserverListBase<Observer>::Iterator it(observers_);
Observer* obs = NULL;
while ((obs = it.GetNext()) != NULL) {
if (obs->BrowserActionShowPopup(extension))
return true;
}
return false;
}
void ExtensionToolbarModel::EnsureVisibility(
const ExtensionIdList& extension_ids) {
if (visible_icon_count_ == -1)
return;
if (visible_icon_count_ < static_cast<int>(extension_ids.size())) {
SetVisibleIconCount(extension_ids.size());
FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged());
}
if (visible_icon_count_ == -1)
return;
for (ExtensionIdList::const_iterator it = extension_ids.begin();
it != extension_ids.end(); ++it) {
for (ExtensionList::const_iterator extension = toolbar_items_.begin();
extension != toolbar_items_.end(); ++extension) {
if ((*extension)->id() == (*it)) {
if (extension - toolbar_items_.begin() >= visible_icon_count_)
MoveBrowserAction(*extension, 0);
break;
}
}
}
}
bool ExtensionToolbarModel::HighlightExtensions(
const ExtensionIdList& extension_ids) {
highlighted_items_.clear();
for (ExtensionIdList::const_iterator id = extension_ids.begin();
id != extension_ids.end();
++id) {
for (ExtensionList::const_iterator extension = toolbar_items_.begin();
extension != toolbar_items_.end();
++extension) {
if (*id == (*extension)->id())
highlighted_items_.push_back(*extension);
}
}
if (highlighted_items_.size()) {
old_visible_icon_count_ = visible_icon_count_;
is_highlighting_ = true;
if (visible_icon_count_ != -1 &&
visible_icon_count_ < static_cast<int>(extension_ids.size())) {
SetVisibleIconCount(extension_ids.size());
FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged());
}
FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(true));
return true;
}
if (is_highlighting_)
StopHighlighting();
return false;
}
void ExtensionToolbarModel::StopHighlighting() {
if (is_highlighting_) {
highlighted_items_.clear();
is_highlighting_ = false;
if (old_visible_icon_count_ != visible_icon_count_) {
SetVisibleIconCount(old_visible_icon_count_);
FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged());
}
FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(false));
}
};
}