Chromium Code Reviews| Index: chrome/browser/tab_contents/menu_category_base.cc |
| diff --git a/chrome/browser/tab_contents/menu_category_base.cc b/chrome/browser/tab_contents/menu_category_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa6981b2d9ec8f5d56d671eeece712d8b9fad423 |
| --- /dev/null |
| +++ b/chrome/browser/tab_contents/menu_category_base.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/tab_contents/menu_category_base.h" |
| + |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_io_data.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/url_constants.h" |
| +#include "extensions/browser/extension_system.h" |
| +#include "extensions/browser/process_manager.h" |
| +#include "extensions/common/extension.h" |
| +#include "third_party/WebKit/public/web/WebContextMenuData.h" |
| + |
| + |
| +using blink::WebContextMenuData; |
| +using content::RenderFrameHost; |
| +using content::WebContents; |
| +using extensions::Extension; |
| + |
| +namespace { |
| + |
| +// static |
| +bool IsDevToolsURL(const GURL& url) { |
| + return url.SchemeIs(content::kChromeDevToolsScheme); |
| +} |
| + |
| +// static |
| +bool IsInternalResourcesURL(const GURL& url) { |
| + if (!url.SchemeIs(content::kChromeUIScheme)) |
| + return false; |
| + return url.host() == chrome::kChromeUISyncResourcesHost; |
| +} |
| + |
| +} // namespace |
| + |
| +MenuCategoryBase::MenuCategoryBase(RenderFrameHost* render_frame_host, |
| + const content::ContextMenuParams& params) |
| + : params_(params), |
| + source_web_contents_( |
| + WebContents::FromRenderFrameHost(render_frame_host)), |
| + profile_(Profile::FromBrowserContext( |
| + source_web_contents_->GetBrowserContext())) { |
| +} |
| + |
| +MenuCategoryBase::~MenuCategoryBase() { |
| +} |
| + |
| +const Extension* MenuCategoryBase::GetExtension() const { |
| + extensions::ExtensionSystem* system = |
| + extensions::ExtensionSystem::Get(profile_); |
| + // There is no process manager in some tests. |
| + if (!system->process_manager()) |
| + return NULL; |
| + |
| + return system->process_manager()->GetExtensionForRenderViewHost( |
| + source_web_contents_->GetRenderViewHost()); |
| +} |
| + |
| +bool MenuCategoryBase::HasCustomItems( |
| + const std::vector<content::MenuItem>& items) const { |
| + for (size_t i = 0; i < items.size(); ++i) { |
| + if (IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action >= |
| + IDC_CONTENT_CONTEXT_CUSTOM_LAST) { |
| + return false; |
| + } |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +int MenuCategoryBase::GetCategories() { |
|
Fady Samuel
2014/02/24 19:07:29
GetSupportedCategories or better yet, can we get r
lazyboy
2014/02/24 23:45:00
Removed GetCategories()
|
| + int items = 0; |
|
Fady Samuel
2014/02/24 19:07:29
supported_categories
lazyboy
2014/02/24 23:45:00
Done.
|
| + |
| + // For menus with custom items, we bail out early. |
| + if (HasCustomItems(params_.custom_items)) { |
| + items = ITEM_CATEGORY_CUSTOM; |
| + const bool has_selection = !params_.selection_text.empty(); |
| + if (!has_selection && !params_.custom_context.is_pepper_menu) |
| + items |= ITEM_CATEGORY_DEVELOPER; |
| + return items; |
| + } |
| + |
| + for (int i = 0; i <= ITEM_CATEGORY_LAST; ++i) { |
| + if (IsCategoryEnabled(i)) |
| + items |= 1 << i; |
| + } |
| + |
| + // Image menu item also implies "Search web for image" and printing. |
| + if (items & (1 << ITEM_CATEGORY_MEDIA_IMAGE)) { |
| + items |= ITEM_CATEGORY_SEARCHWEBFORIMAGE; |
| + items |= ITEM_CATEGORY_PRINT; |
| + } |
| + |
| + return items; |
| +} |
| + |
| +bool MenuCategoryBase::IsCategoryEnabled(int category) { |
|
Fady Samuel
2014/02/24 19:07:29
IsCategorySupported
lazyboy
2014/02/24 23:45:00
Done.
|
| + const bool has_link = !params_.unfiltered_link_url.is_empty(); |
| + const bool has_selection = !params_.selection_text.empty(); |
| + switch (category) { |
| + case ITEM_CATEGORY_CUSTOM: |
| + // Doesn't matter, we manually add this section. |
| + return true; |
| + case ITEM_CATEGORY_PAGE: { |
| + bool is_candidate = |
| + params_.media_type == WebContextMenuData::MediaTypeNone && |
| + !has_link && !params_.is_editable && !has_selection; |
| + |
| + if (!is_candidate && params_.page_url.is_empty()) |
| + DCHECK(params_.frame_url.is_empty()); |
| + |
| + return is_candidate && !params_.page_url.is_empty() && |
| + !IsDevToolsURL(params_.page_url) && |
| + !IsInternalResourcesURL(params_.page_url); |
| + } |
| + case ITEM_CATEGORY_FRAME: { |
| + bool page_category = IsCategoryEnabled(ITEM_CATEGORY_PAGE); |
| + return page_category && !params_.frame_url.is_empty() && |
| + !IsDevToolsURL(params_.frame_url) && |
| + !IsInternalResourcesURL(params_.page_url); |
| + } |
| + case ITEM_CATEGORY_LINK: |
| + return has_link; |
| + case ITEM_CATEGORY_MEDIA_IMAGE: |
| + case ITEM_CATEGORY_SEARCHWEBFORIMAGE: |
| + return params_.media_type == WebContextMenuData::MediaTypeImage; |
| + case ITEM_CATEGORY_MEDIA_VIDEO: |
| + return params_.media_type == WebContextMenuData::MediaTypeVideo; |
| + case ITEM_CATEGORY_MEDIA_AUDIO: |
| + return params_.media_type == WebContextMenuData::MediaTypeAudio; |
| + case ITEM_CATEGORY_MEDIA_PLUGIN: |
| + return params_.media_type == WebContextMenuData::MediaTypePlugin; |
| + case ITEM_CATEGORY_MEDIA_FILE: |
| +#ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED |
| + return params_.media_type == WebContextMenuData::MediaTypeFile; |
| +#endif |
| + return false; |
| + case ITEM_CATEGORY_EDITABLE: |
| + return params_.is_editable; |
| + case ITEM_CATEGORY_COPY: |
| + return !params_.is_editable && has_selection; |
| + case ITEM_CATEGORY_SEARCH_PROVIDER: |
| + return has_selection; |
| + case ITEM_CATEGORY_PRINT: { |
| + bool enable = has_selection && !IsDevToolsURL(params_.page_url); |
| + return enable || IsCategoryEnabled(ITEM_CATEGORY_MEDIA_IMAGE); |
| + } |
| + case ITEM_CATEGORY_ALL_EXTENSION: |
| + return GetExtension() != NULL && !IsDevToolsURL(params_.page_url); |
| + case ITEM_CATEGORY_CURRENT_EXTENSION: |
| + return false; |
| + case ITEM_CATEGORY_DEVELOPER: |
| + return true; |
| + case ITEM_CATEGORY_DEVTOOLS_UNPACKED_EXT: |
| + return false; |
| + case ITEM_CATEGORY_PRINT_PREVIEW: |
| +#if defined(ENABLE_FULL_PRINTING) |
| + return true; |
| +#else |
| + return false |
| +#endif |
| + } |
| + |
| + NOTREACHED(); |
| + return false; |
| +} |