Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(523)

Side by Side Diff: chrome/browser/tab_contents/menu_category_base.cc

Issue 169093009: Separate out logic to handle different category/group of context menu items from RVContextMenu class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@work-mmm-refactor1
Patch Set: fix incorrect dcheck + platform app logic Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/tab_contents/menu_category_base.h"
6
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_io_data.h"
10 #include "chrome/common/url_constants.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/common/url_constants.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/browser/process_manager.h"
15 #include "extensions/common/extension.h"
16 #include "third_party/WebKit/public/web/WebContextMenuData.h"
17
18
19 using blink::WebContextMenuData;
20 using content::RenderFrameHost;
21 using content::WebContents;
22 using extensions::Extension;
23
24 namespace {
25
26 // static
27 bool IsDevToolsURL(const GURL& url) {
28 return url.SchemeIs(content::kChromeDevToolsScheme);
29 }
30
31 // static
32 bool IsInternalResourcesURL(const GURL& url) {
33 if (!url.SchemeIs(content::kChromeUIScheme))
34 return false;
35 return url.host() == chrome::kChromeUISyncResourcesHost;
36 }
37
38 } // namespace
39
40 MenuCategoryBase::MenuCategoryBase(RenderFrameHost* render_frame_host,
41 const content::ContextMenuParams& params)
42 : params_(params),
43 source_web_contents_(
44 WebContents::FromRenderFrameHost(render_frame_host)),
45 profile_(Profile::FromBrowserContext(
46 source_web_contents_->GetBrowserContext())) {
47 }
48
49 MenuCategoryBase::~MenuCategoryBase() {
50 }
51
52 const Extension* MenuCategoryBase::GetExtension() const {
53 extensions::ExtensionSystem* system =
54 extensions::ExtensionSystem::Get(profile_);
55 // There is no process manager in some tests.
56 if (!system->process_manager())
57 return NULL;
58
59 return system->process_manager()->GetExtensionForRenderViewHost(
60 source_web_contents_->GetRenderViewHost());
61 }
62
63 bool MenuCategoryBase::HasCustomItems(
64 const std::vector<content::MenuItem>& items) const {
65 for (size_t i = 0; i < items.size(); ++i) {
66 if (IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action >=
67 IDC_CONTENT_CONTEXT_CUSTOM_LAST) {
68 return false;
69 }
70 return true;
71 }
72 return false;
73 }
74
75 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()
76 int items = 0;
Fady Samuel 2014/02/24 19:07:29 supported_categories
lazyboy 2014/02/24 23:45:00 Done.
77
78 // For menus with custom items, we bail out early.
79 if (HasCustomItems(params_.custom_items)) {
80 items = ITEM_CATEGORY_CUSTOM;
81 const bool has_selection = !params_.selection_text.empty();
82 if (!has_selection && !params_.custom_context.is_pepper_menu)
83 items |= ITEM_CATEGORY_DEVELOPER;
84 return items;
85 }
86
87 for (int i = 0; i <= ITEM_CATEGORY_LAST; ++i) {
88 if (IsCategoryEnabled(i))
89 items |= 1 << i;
90 }
91
92 // Image menu item also implies "Search web for image" and printing.
93 if (items & (1 << ITEM_CATEGORY_MEDIA_IMAGE)) {
94 items |= ITEM_CATEGORY_SEARCHWEBFORIMAGE;
95 items |= ITEM_CATEGORY_PRINT;
96 }
97
98 return items;
99 }
100
101 bool MenuCategoryBase::IsCategoryEnabled(int category) {
Fady Samuel 2014/02/24 19:07:29 IsCategorySupported
lazyboy 2014/02/24 23:45:00 Done.
102 const bool has_link = !params_.unfiltered_link_url.is_empty();
103 const bool has_selection = !params_.selection_text.empty();
104 switch (category) {
105 case ITEM_CATEGORY_CUSTOM:
106 // Doesn't matter, we manually add this section.
107 return true;
108 case ITEM_CATEGORY_PAGE: {
109 bool is_candidate =
110 params_.media_type == WebContextMenuData::MediaTypeNone &&
111 !has_link && !params_.is_editable && !has_selection;
112
113 if (!is_candidate && params_.page_url.is_empty())
114 DCHECK(params_.frame_url.is_empty());
115
116 return is_candidate && !params_.page_url.is_empty() &&
117 !IsDevToolsURL(params_.page_url) &&
118 !IsInternalResourcesURL(params_.page_url);
119 }
120 case ITEM_CATEGORY_FRAME: {
121 bool page_category = IsCategoryEnabled(ITEM_CATEGORY_PAGE);
122 return page_category && !params_.frame_url.is_empty() &&
123 !IsDevToolsURL(params_.frame_url) &&
124 !IsInternalResourcesURL(params_.page_url);
125 }
126 case ITEM_CATEGORY_LINK:
127 return has_link;
128 case ITEM_CATEGORY_MEDIA_IMAGE:
129 case ITEM_CATEGORY_SEARCHWEBFORIMAGE:
130 return params_.media_type == WebContextMenuData::MediaTypeImage;
131 case ITEM_CATEGORY_MEDIA_VIDEO:
132 return params_.media_type == WebContextMenuData::MediaTypeVideo;
133 case ITEM_CATEGORY_MEDIA_AUDIO:
134 return params_.media_type == WebContextMenuData::MediaTypeAudio;
135 case ITEM_CATEGORY_MEDIA_PLUGIN:
136 return params_.media_type == WebContextMenuData::MediaTypePlugin;
137 case ITEM_CATEGORY_MEDIA_FILE:
138 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED
139 return params_.media_type == WebContextMenuData::MediaTypeFile;
140 #endif
141 return false;
142 case ITEM_CATEGORY_EDITABLE:
143 return params_.is_editable;
144 case ITEM_CATEGORY_COPY:
145 return !params_.is_editable && has_selection;
146 case ITEM_CATEGORY_SEARCH_PROVIDER:
147 return has_selection;
148 case ITEM_CATEGORY_PRINT: {
149 bool enable = has_selection && !IsDevToolsURL(params_.page_url);
150 return enable || IsCategoryEnabled(ITEM_CATEGORY_MEDIA_IMAGE);
151 }
152 case ITEM_CATEGORY_ALL_EXTENSION:
153 return GetExtension() != NULL && !IsDevToolsURL(params_.page_url);
154 case ITEM_CATEGORY_CURRENT_EXTENSION:
155 return false;
156 case ITEM_CATEGORY_DEVELOPER:
157 return true;
158 case ITEM_CATEGORY_DEVTOOLS_UNPACKED_EXT:
159 return false;
160 case ITEM_CATEGORY_PRINT_PREVIEW:
161 #if defined(ENABLE_FULL_PRINTING)
162 return true;
163 #else
164 return false
165 #endif
166 }
167
168 NOTREACHED();
169 return false;
170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698