Chromium Code Reviews| 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..e502237051989d34c33c4b2adc1dccd80c364ef9 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,35 @@ bool IsCustomItemCheckedInternal(const std::vector<content::MenuItem>& items, |
| const size_t kMaxCustomMenuDepth = 5; |
| const size_t kMaxCustomMenuTotalItems = 1000; |
| +const size_t kMenuItemIconSize = 16; |
| -void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items, |
| - size_t depth, |
| - size_t* total_items, |
| - ui::SimpleMenuModel::Delegate* delegate, |
| - ui::SimpleMenuModel* menu_model) { |
| +} // namespace |
| + |
| +void RenderViewContextMenuBase::OnIconDownloaded( |
| + 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::Image icon = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); |
| + // Scale icon to a 16X16 size. |
| + gfx::ImageSkia scaled_icon = |
| + gfx::ImageSkiaOperations::CreateResizedImage( |
| + *icon.ToImageSkia(), |
| + skia::ImageOperations::RESIZE_BEST, |
| + gfx::Size(kMenuItemIconSize, kMenuItemIconSize)); |
| + SetIcon(command_id, gfx::Image(scaled_icon)); |
|
dcheng
2015/02/18 17:03:27
Why do we go from ImageSkia to Image and then back
pals
2015/02/23 10:23:15
Done.
|
| +} |
| + |
| +void RenderViewContextMenuBase::AddCustomItemsToMenu( |
| + const std::vector<content::MenuItem>& items, |
| + size_t depth, |
| + size_t* total_items, |
| + ui::SimpleMenuModel::Delegate* delegate, |
| + ui::SimpleMenuModel* menu_model) { |
| if (depth > kMaxCustomMenuDepth) { |
| LOG(ERROR) << "Custom menu too deeply nested."; |
| return; |
| @@ -79,6 +104,16 @@ 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.empty()) { |
| + source_web_contents_->DownloadImage( |
| + GURL(items[i].icon), |
|
dcheng
2015/02/18 17:03:27
Why don't we change context::MenuItem to pass this
pals
2015/02/23 10:23:15
Done.
|
| + true, // is_favicon |
| + 0, // unlimited |
| + base::Bind(&RenderViewContextMenuBase::OnIconDownloaded, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + command_id)); |
| + } |
| + |
| if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { |
| LOG(ERROR) << "Custom menu action value out of range."; |
| return; |
| @@ -126,8 +161,6 @@ void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items, |
| } |
| } |
| -} // namespace |
| - |
| // static |
| void RenderViewContextMenuBase::SetContentCustomCommandIdRange( |
| int first, int last) { |
| @@ -159,7 +192,8 @@ RenderViewContextMenuBase::RenderViewContextMenuBase( |
| menu_model_(this), |
| render_frame_id_(render_frame_host->GetRoutingID()), |
| command_executed_(false), |
| - render_process_id_(render_frame_host->GetProcess()->GetID()) { |
| + render_process_id_(render_frame_host->GetProcess()->GetID()), |
| + weak_ptr_factory_(this) { |
| } |
| RenderViewContextMenuBase::~RenderViewContextMenuBase() { |
| @@ -215,6 +249,12 @@ void RenderViewContextMenuBase::AddSubMenu(int command_id, |
| menu_model_.AddSubMenu(command_id, label, model); |
| } |
| +void RenderViewContextMenuBase::SetIcon(int command_id, |
| + const gfx::Image& icon) { |
| + if (toolkit_delegate_) |
| + toolkit_delegate_->SetIcon(command_id, icon); |
| +} |
| + |
| void RenderViewContextMenuBase::UpdateMenuItem(int command_id, |
| bool enabled, |
| bool hidden, |