Commit 8e7c1213 authored by Catherine Mullings's avatar Catherine Mullings Committed by Commit Bot

Extensions: Refactor use of range check of custom extension menu ids

Refactor checks of whether a menu item id is an extension custom id or
not. The static ContextMenuMatcher::IsExtensionsCustomCommandId()
function is used instead.

Bug: 765829
Change-Id: I2143e9449beb82a2e6580d82e86055b83c6ca41c
Reviewed-on: https://chromium-review.googlesource.com/695718Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: catmullings <catmullings@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505844}
parent bade1a64
......@@ -74,8 +74,7 @@ class ExtensionContextMenuApiTest : public ExtensionApiTest {
int num_found = 0;
for (int i = 0; i < top_level_model_->GetItemCount(); i++) {
int command_id = top_level_model_->GetCommandIdAt(i);
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST &&
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id) &&
top_level_model_->GetTypeAt(i) == type) {
num_found++;
}
......
......@@ -178,8 +178,7 @@ bool ContextMenuMatcher::IsCommandIdVisible(int command_id) const {
// top-level menu item is not added to the context menu, so checking its
// visibility is a special case handled below. This top-level menu item should
// always be displayed.
if (!item && command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
if (!item && ContextMenuMatcher::IsExtensionsCustomCommandId(command_id)) {
return true;
} else if (item) {
return item->visible();
......
......@@ -161,8 +161,7 @@ bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
if (!extension)
return false;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id))
return extension_items_->IsCommandIdChecked(command_id);
if (command_id == PAGE_ACCESS_RUN_ON_CLICK ||
......@@ -180,8 +179,7 @@ bool ExtensionContextMenuModel::IsCommandIdVisible(int command_id) const {
const Extension* extension = GetExtension();
if (!extension)
return false;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id)) {
return extension_items_->IsCommandIdVisible(command_id);
}
......@@ -194,10 +192,8 @@ bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
if (!extension)
return false;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id))
return extension_items_->IsCommandIdEnabled(command_id);
}
switch (command_id) {
case NAME:
......@@ -237,8 +233,7 @@ void ExtensionContextMenuModel::ExecuteCommand(int command_id,
if (!extension)
return;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id)) {
DCHECK(extension_items_);
extension_items_->ExecuteCommand(command_id, GetActiveWebContents(),
nullptr, content::ContextMenuParams());
......
......@@ -11,6 +11,7 @@
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include "chrome/browser/extensions/context_menu_matcher.h"
#include "chrome/browser/extensions/extension_action_runner.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_service.h"
......@@ -136,10 +137,8 @@ int CountExtensionItems(const ExtensionContextMenuModel& model) {
if (base::StartsWith(actual_label, expected_label,
base::CompareCase::SENSITIVE))
++num_items_found;
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id))
++num_custom_found;
}
}
}
// The only custom extension items present on the menu should be those we
......@@ -155,8 +154,7 @@ void VerifyItems(const ExtensionContextMenuModel& model,
size_t j = 0;
for (int i = 0; i < model.GetItemCount(); i++) {
int command_id = model.GetCommandIdAt(i);
if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST &&
if (ContextMenuMatcher::IsExtensionsCustomCommandId(command_id) &&
model.IsCommandIdVisible(command_id)) {
ASSERT_LT(j, item_number.size());
EXPECT_EQ(base::ASCIIToUTF16(item_label() + item_number[j]),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment