Chromium Code Reviews| Index: chrome/browser/extensions/extension_menu_manager.cc |
| =================================================================== |
| --- chrome/browser/extensions/extension_menu_manager.cc (revision 116264) |
| +++ chrome/browser/extensions/extension_menu_manager.cc (working copy) |
| @@ -139,8 +139,13 @@ |
| context_items_[extension_id].push_back(item); |
| items_by_id_[item->id()] = item; |
| - if (item->type() == ExtensionMenuItem::RADIO && item->checked()) |
| - RadioItemSelected(item); |
| + if (item->type() == ExtensionMenuItem::RADIO) { |
| + if (item->checked()) { |
| + RadioItemSelected(item); |
| + } else { |
| + SanitizeRadioList(context_items_[extension_id]); |
| + } |
| + } |
|
asargent_no_longer_on_chrome
2012/01/06 21:17:42
nit: other code uses the optional style practice o
|
| // If this is the first item for this extension, start loading its icon. |
| if (first_item) |
| @@ -158,6 +163,11 @@ |
| return false; |
| parent->AddChild(child); |
| items_by_id_[child->id()] = child; |
| + |
| + if (child->type() == ExtensionMenuItem::RADIO) { |
| + SanitizeRadioList(parent->children()); |
| + } |
| + |
| return true; |
| } |
| @@ -201,6 +211,7 @@ |
| ExtensionMenuItem* taken = |
| old_parent->ReleaseChild(child_id, false /* non-recursive search*/); |
| DCHECK(taken == child); |
| + SanitizeRadioList(old_parent->children()); |
| } else { |
| // This is a top-level item, so we need to pull it out of our list of |
| // top-level items. |
| @@ -217,13 +228,16 @@ |
| return false; |
| } |
| list.erase(j); |
| + SanitizeRadioList(list); |
| } |
| if (new_parent) { |
| new_parent->AddChild(child); |
| + SanitizeRadioList(new_parent->children()); |
| } else { |
| context_items_[child->extension_id()].push_back(child); |
| child->parent_id_.reset(NULL); |
| + SanitizeRadioList(context_items_[child->extension_id()]); |
| } |
| return true; |
| } |
| @@ -254,6 +268,7 @@ |
| delete *j; |
| list.erase(j); |
| result = true; |
| + SanitizeRadioList(list); |
| break; |
| } else { |
| // See if the item to remove was found as a descendant of the current |
| @@ -262,6 +277,7 @@ |
| if (child) { |
| items_removed = child->RemoveAllDescendants(); |
| items_removed.insert(id); |
| + SanitizeRadioList(GetItemById(*child->parent_id())->children()); |
| delete child; |
| result = true; |
| break; |
| @@ -449,6 +465,64 @@ |
| item->extension_id(), event_name, json_args, profile, GURL()); |
| } |
| +void ExtensionMenuManager::SanitizeRadioList( |
| + const ExtensionMenuItem::List& item_list) { |
| + ExtensionMenuItem::List::const_iterator i = item_list.begin(); |
| + while (i != item_list.end()) { |
| + if ((*i)->type() == ExtensionMenuItem::RADIO) { |
|
asargent_no_longer_on_chrome
2012/01/06 21:17:42
nit: you could improve readability slightly by mak
|
| + // If there are multiple items selected, the last one will override the |
| + // others. |
| + ExtensionMenuItem::List::const_iterator last_checked = item_list.end(); |
| + ExtensionMenuItem::List::const_iterator radio_run_iter; |
| + for (radio_run_iter = i; radio_run_iter != item_list.end(); |
| + ++radio_run_iter) { |
| + if ((*radio_run_iter)->type() != ExtensionMenuItem::RADIO) { |
| + break; |
| + } |
| + |
| + if ((*radio_run_iter)->checked()) { |
| + last_checked = radio_run_iter; |
| + (*radio_run_iter)->SetChecked(false); |
| + } |
| + } |
| + |
| + // If radio items were found in this run, |
| + // check the first radio item in the list (i). |
|
asargent_no_longer_on_chrome
2012/01/06 21:17:42
nit: this comment isn't quite right. It could be s
|
| + if (last_checked != item_list.end()) { |
| + (*last_checked)->SetChecked(true); |
| + } else { |
| + (*i)->SetChecked(true); |
| + } |
| + |
| + i = radio_run_iter; |
| + } else { |
| + ++i; |
| + } |
| + } |
| +} |
| + |
| +bool ExtensionMenuManager::ItemUpdated(const ExtensionMenuItem::Id& id) { |
| + if (!ContainsKey(items_by_id_, id)) |
| + return false; |
| + |
| + ExtensionMenuItem* menu_item = GetItemById(id); |
| + DCHECK(menu_item); |
| + |
| + if (menu_item->parent_id()) { |
| + SanitizeRadioList(GetItemById(*menu_item->parent_id())->children()); |
| + } else { |
| + std::string extension_id = menu_item->extension_id(); |
| + MenuItemMap::iterator i = context_items_.find(extension_id); |
| + if (i == context_items_.end()) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + SanitizeRadioList(i->second); |
| + } |
| + |
| + return true; |
| +} |
| + |
| void ExtensionMenuManager::Observe( |
| int type, |
| const content::NotificationSource& source, |