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

Side by Side Diff: components/renderer_context_menu/render_view_context_menu_base.cc

Issue 892953002: Show icons for custom menuitems in contextmenu. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Created 5 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/renderer_context_menu/render_view_context_menu_base.h" 5 #include "components/renderer_context_menu/render_view_context_menu_base.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "content/public/browser/render_frame_host.h" 12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h" 14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h" 15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/menu_item.h" 17 #include "content/public/common/menu_item.h"
18 #include "third_party/WebKit/public/web/WebContextMenuData.h" 18 #include "third_party/WebKit/public/web/WebContextMenuData.h"
19 #include "ui/gfx/image/image.h"
19 20
20 using blink::WebContextMenuData; 21 using blink::WebContextMenuData;
21 using blink::WebString; 22 using blink::WebString;
22 using blink::WebURL; 23 using blink::WebURL;
23 using content::BrowserContext; 24 using content::BrowserContext;
24 using content::OpenURLParams; 25 using content::OpenURLParams;
25 using content::RenderFrameHost; 26 using content::RenderFrameHost;
26 using content::RenderViewHost; 27 using content::RenderViewHost;
27 using content::WebContents; 28 using content::WebContents;
28 29
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 if (IsCustomItemCheckedInternal(items[i].submenu, id)) 61 if (IsCustomItemCheckedInternal(items[i].submenu, id))
61 return true; 62 return true;
62 } 63 }
63 } 64 }
64 return false; 65 return false;
65 } 66 }
66 67
67 const size_t kMaxCustomMenuDepth = 5; 68 const size_t kMaxCustomMenuDepth = 5;
68 const size_t kMaxCustomMenuTotalItems = 1000; 69 const size_t kMaxCustomMenuTotalItems = 1000;
69 70
70 void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items, 71 } // namespace
71 size_t depth, 72
72 size_t* total_items, 73 void RenderViewContextMenuBase::OnIconDownloaded(
73 ui::SimpleMenuModel::Delegate* delegate, 74 int id,
74 ui::SimpleMenuModel* menu_model) { 75 int http_status_code,
76 const GURL& image_url,
77 const std::vector<SkBitmap>& bitmaps,
78 const std::vector<gfx::Size>& original_bitmap_sizes) {
79 if (bitmaps.empty())
80 return;
81 SetIcon(icon_map_.find(id)->second,
82 gfx::Image(gfx::Image::CreateFrom1xBitmap(bitmaps[0])));
83 }
84
85 void RenderViewContextMenuBase::AddCustomItemsToMenu(
86 const std::vector<content::MenuItem>& items,
87 size_t depth,
88 size_t* total_items,
89 ui::SimpleMenuModel::Delegate* delegate,
90 ui::SimpleMenuModel* menu_model) {
75 if (depth > kMaxCustomMenuDepth) { 91 if (depth > kMaxCustomMenuDepth) {
76 LOG(ERROR) << "Custom menu too deeply nested."; 92 LOG(ERROR) << "Custom menu too deeply nested.";
77 return; 93 return;
78 } 94 }
79 for (size_t i = 0; i < items.size(); ++i) { 95 for (size_t i = 0; i < items.size(); ++i) {
80 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( 96 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
81 items[i].action); 97 items[i].action);
98 if (!items[i].icon.empty()) {
99 int download_id = source_web_contents_->DownloadImage(
100 GURL(items[i].icon),
101 false, // is_favicon
102 0, // no max size
103 base::Bind(&RenderViewContextMenuBase::OnIconDownloaded,
104 weak_ptr_factory_.GetWeakPtr()));
105 icon_map_[download_id] = command_id;
lazyboy 2015/02/09 23:45:23 You don't need this map, you can bind |command_id|
pals 2015/02/10 12:19:41 Done.
106 }
107
82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { 108 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) {
83 LOG(ERROR) << "Custom menu action value out of range."; 109 LOG(ERROR) << "Custom menu action value out of range.";
84 return; 110 return;
85 } 111 }
86 if (*total_items >= kMaxCustomMenuTotalItems) { 112 if (*total_items >= kMaxCustomMenuTotalItems) {
87 LOG(ERROR) << "Custom menu too large (too many items)."; 113 LOG(ERROR) << "Custom menu too large (too many items).";
88 return; 114 return;
89 } 115 }
90 (*total_items)++; 116 (*total_items)++;
91 switch (items[i].type) { 117 switch (items[i].type) {
(...skipping 27 matching lines...) Expand all
119 submenu); 145 submenu);
120 break; 146 break;
121 } 147 }
122 default: 148 default:
123 NOTREACHED(); 149 NOTREACHED();
124 break; 150 break;
125 } 151 }
126 } 152 }
127 } 153 }
128 154
129 } // namespace
130
131 // static 155 // static
132 void RenderViewContextMenuBase::SetContentCustomCommandIdRange( 156 void RenderViewContextMenuBase::SetContentCustomCommandIdRange(
133 int first, int last) { 157 int first, int last) {
134 // The range is inclusive. 158 // The range is inclusive.
135 content_context_custom_first = first; 159 content_context_custom_first = first;
136 content_context_custom_last = last; 160 content_context_custom_last = last;
137 } 161 }
138 162
139 // static 163 // static
140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50; 164 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50;
(...skipping 11 matching lines...) Expand all
152 176
153 RenderViewContextMenuBase::RenderViewContextMenuBase( 177 RenderViewContextMenuBase::RenderViewContextMenuBase(
154 content::RenderFrameHost* render_frame_host, 178 content::RenderFrameHost* render_frame_host,
155 const content::ContextMenuParams& params) 179 const content::ContextMenuParams& params)
156 : params_(params), 180 : params_(params),
157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), 181 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
158 browser_context_(source_web_contents_->GetBrowserContext()), 182 browser_context_(source_web_contents_->GetBrowserContext()),
159 menu_model_(this), 183 menu_model_(this),
160 render_frame_id_(render_frame_host->GetRoutingID()), 184 render_frame_id_(render_frame_host->GetRoutingID()),
161 command_executed_(false), 185 command_executed_(false),
162 render_process_id_(render_frame_host->GetProcess()->GetID()) { 186 render_process_id_(render_frame_host->GetProcess()->GetID()),
187 weak_ptr_factory_(this) {
163 } 188 }
164 189
165 RenderViewContextMenuBase::~RenderViewContextMenuBase() { 190 RenderViewContextMenuBase::~RenderViewContextMenuBase() {
166 } 191 }
167 192
168 // Menu construction functions ------------------------------------------------- 193 // Menu construction functions -------------------------------------------------
169 194
170 void RenderViewContextMenuBase::Init() { 195 void RenderViewContextMenuBase::Init() {
171 // Command id range must have been already initializerd. 196 // Command id range must have been already initializerd.
172 DCHECK_NE(-1, content_context_custom_first); 197 DCHECK_NE(-1, content_context_custom_first);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 void RenderViewContextMenuBase::AddSeparator() { 233 void RenderViewContextMenuBase::AddSeparator() {
209 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); 234 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
210 } 235 }
211 236
212 void RenderViewContextMenuBase::AddSubMenu(int command_id, 237 void RenderViewContextMenuBase::AddSubMenu(int command_id,
213 const base::string16& label, 238 const base::string16& label,
214 ui::MenuModel* model) { 239 ui::MenuModel* model) {
215 menu_model_.AddSubMenu(command_id, label, model); 240 menu_model_.AddSubMenu(command_id, label, model);
216 } 241 }
217 242
243 void RenderViewContextMenuBase::SetIcon(int command_id,
244 const gfx::Image& icon) {
245 if (toolkit_delegate_) {
lazyboy 2015/02/09 23:45:23 nit: no need for {} if it's a one liner.
pals 2015/02/10 12:19:41 Done.
246 toolkit_delegate_->SetIcon(command_id, icon);
247 }
248 }
249
218 void RenderViewContextMenuBase::UpdateMenuItem(int command_id, 250 void RenderViewContextMenuBase::UpdateMenuItem(int command_id,
219 bool enabled, 251 bool enabled,
220 bool hidden, 252 bool hidden,
221 const base::string16& label) { 253 const base::string16& label) {
222 if (toolkit_delegate_) { 254 if (toolkit_delegate_) {
223 toolkit_delegate_->UpdateMenuItem(command_id, 255 toolkit_delegate_->UpdateMenuItem(command_id,
224 enabled, 256 enabled,
225 hidden, 257 hidden,
226 label); 258 label);
227 } 259 }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 } 412 }
381 413
382 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const { 414 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const {
383 return IsCustomItemCheckedInternal(params_.custom_items, id); 415 return IsCustomItemCheckedInternal(params_.custom_items, id);
384 } 416 }
385 417
386 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const { 418 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const {
387 return IsCustomItemEnabledInternal(params_.custom_items, id); 419 return IsCustomItemEnabledInternal(params_.custom_items, id);
388 } 420 }
389 421
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698