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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: components/renderer_context_menu/render_view_context_menu_base.cc
diff --git a/components/renderer_context_menu/render_view_context_menu_base.cc b/components/renderer_context_menu/render_view_context_menu_base.cc
index cda6c4667aef1908774a90842753fd62598a7b95..b79b173af4b70560ee476500d25c5a879980e1ec 100644
--- a/components/renderer_context_menu/render_view_context_menu_base.cc
+++ b/components/renderer_context_menu/render_view_context_menu_base.cc
@@ -16,6 +16,8 @@
#include "content/public/browser/web_contents.h"
#include "content/public/common/menu_item.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h"
+#include "ui/gfx/image/image.h"
+#include "ui/gfx/image/image_skia_operations.h"
using blink::WebContextMenuData;
using blink::WebString;
@@ -66,12 +68,34 @@ bool IsCustomItemCheckedInternal(const std::vector<content::MenuItem>& items,
const size_t kMaxCustomMenuDepth = 5;
const size_t kMaxCustomMenuTotalItems = 1000;
+const size_t kMenuItemIconSize = 16;
+
+void OnIconDownloaded(RenderViewContextMenuBase* menu,
+ int command_id,
+ int id,
+ int http_status_code,
+ const GURL& image_url,
+ const std::vector<SkBitmap>& bitmaps,
+ const std::vector<gfx::Size>& original_bitmap_sizes) {
+ if (bitmaps.empty())
+ return;
+ gfx::ImageSkia icon = gfx::ImageSkia::CreateFrom1xBitmap(bitmaps[0]);
+ // Scale icon to a 16X16 size.
+ gfx::ImageSkia scaled_icon =
+ gfx::ImageSkiaOperations::CreateResizedImage(
+ icon,
+ skia::ImageOperations::RESIZE_BEST,
+ gfx::Size(kMenuItemIconSize, kMenuItemIconSize));
+ if (menu)
+ menu->SetIcon(command_id, scaled_icon);
+}
void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items,
size_t depth,
size_t* total_items,
ui::SimpleMenuModel::Delegate* delegate,
- ui::SimpleMenuModel* menu_model) {
+ ui::SimpleMenuModel* menu_model,
+ content::WebContents* web_contents) {
if (depth > kMaxCustomMenuDepth) {
LOG(ERROR) << "Custom menu too deeply nested.";
return;
@@ -79,6 +103,17 @@ void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items,
for (size_t i = 0; i < items.size(); ++i) {
int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId(
items[i].action);
+ if (items[i].icon.is_valid()) {
jochen (gone - plz use gerrit) 2015/02/24 10:26:57 i'm still concerned what happens if you specify fo
+ web_contents->DownloadImage(
+ items[i].icon,
+ true, // is_favicon
+ 0, // unlimited
+ base::Bind(OnIconDownloaded,
+ base::Unretained(
+ static_cast<RenderViewContextMenuBase*>(delegate)),
+ command_id));
+ }
+
if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) {
LOG(ERROR) << "Custom menu action value out of range.";
return;
@@ -111,7 +146,7 @@ void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items,
case content::MenuItem::SUBMENU: {
ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate);
AddCustomItemsToMenu(items[i].submenu, depth + 1, total_items, delegate,
- submenu);
+ submenu, web_contents);
menu_model->AddSubMenu(
RenderViewContextMenuBase::ConvertToContentCustomCommandId(
items[i].action),
@@ -215,6 +250,12 @@ void RenderViewContextMenuBase::AddSubMenu(int command_id,
menu_model_.AddSubMenu(command_id, label, model);
}
+void RenderViewContextMenuBase::SetIcon(int command_id,
+ const gfx::ImageSkia& icon) {
+ if (toolkit_delegate_)
+ toolkit_delegate_->SetIcon(command_id, icon);
+}
+
void RenderViewContextMenuBase::UpdateMenuItem(int command_id,
bool enabled,
bool hidden,
@@ -242,7 +283,7 @@ BrowserContext* RenderViewContextMenuBase::GetBrowserContext() const {
bool RenderViewContextMenuBase::AppendCustomItems() {
size_t total_items = 0;
AddCustomItemsToMenu(params_.custom_items, 0, &total_items, this,
- &menu_model_);
+ &menu_model_, source_web_contents_);
return total_items > 0;
}

Powered by Google App Engine
This is Rietveld 408576698