Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 20 #include "ui/gfx/image/image_skia_operations.h" | |
| 19 | 21 |
| 20 using blink::WebContextMenuData; | 22 using blink::WebContextMenuData; |
| 21 using blink::WebString; | 23 using blink::WebString; |
| 22 using blink::WebURL; | 24 using blink::WebURL; |
| 23 using content::BrowserContext; | 25 using content::BrowserContext; |
| 24 using content::OpenURLParams; | 26 using content::OpenURLParams; |
| 25 using content::RenderFrameHost; | 27 using content::RenderFrameHost; |
| 26 using content::RenderViewHost; | 28 using content::RenderViewHost; |
| 27 using content::WebContents; | 29 using content::WebContents; |
| 28 | 30 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 59 if (items[i].type == content::MenuItem::SUBMENU) { | 61 if (items[i].type == content::MenuItem::SUBMENU) { |
| 60 if (IsCustomItemCheckedInternal(items[i].submenu, id)) | 62 if (IsCustomItemCheckedInternal(items[i].submenu, id)) |
| 61 return true; | 63 return true; |
| 62 } | 64 } |
| 63 } | 65 } |
| 64 return false; | 66 return false; |
| 65 } | 67 } |
| 66 | 68 |
| 67 const size_t kMaxCustomMenuDepth = 5; | 69 const size_t kMaxCustomMenuDepth = 5; |
| 68 const size_t kMaxCustomMenuTotalItems = 1000; | 70 const size_t kMaxCustomMenuTotalItems = 1000; |
| 71 const size_t kMenuItemIconSize = 16; | |
| 69 | 72 |
| 70 void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items, | 73 } // namespace |
| 71 size_t depth, | 74 |
| 72 size_t* total_items, | 75 void RenderViewContextMenuBase::OnIconDownloaded( |
| 73 ui::SimpleMenuModel::Delegate* delegate, | 76 int command_id, |
| 74 ui::SimpleMenuModel* menu_model) { | 77 int id, |
| 78 int http_status_code, | |
| 79 const GURL& image_url, | |
| 80 const std::vector<SkBitmap>& bitmaps, | |
| 81 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 82 if (bitmaps.empty()) | |
| 83 return; | |
| 84 gfx::Image icon = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); | |
| 85 // Scale icon to a 16X16 size. | |
| 86 gfx::ImageSkia scaled_icon = | |
| 87 gfx::ImageSkiaOperations::CreateResizedImage( | |
| 88 *icon.ToImageSkia(), | |
| 89 skia::ImageOperations::RESIZE_BEST, | |
| 90 gfx::Size(kMenuItemIconSize, kMenuItemIconSize)); | |
| 91 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.
| |
| 92 } | |
| 93 | |
| 94 void RenderViewContextMenuBase::AddCustomItemsToMenu( | |
| 95 const std::vector<content::MenuItem>& items, | |
| 96 size_t depth, | |
| 97 size_t* total_items, | |
| 98 ui::SimpleMenuModel::Delegate* delegate, | |
| 99 ui::SimpleMenuModel* menu_model) { | |
| 75 if (depth > kMaxCustomMenuDepth) { | 100 if (depth > kMaxCustomMenuDepth) { |
| 76 LOG(ERROR) << "Custom menu too deeply nested."; | 101 LOG(ERROR) << "Custom menu too deeply nested."; |
| 77 return; | 102 return; |
| 78 } | 103 } |
| 79 for (size_t i = 0; i < items.size(); ++i) { | 104 for (size_t i = 0; i < items.size(); ++i) { |
| 80 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( | 105 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( |
| 81 items[i].action); | 106 items[i].action); |
| 107 if (!items[i].icon.empty()) { | |
| 108 source_web_contents_->DownloadImage( | |
| 109 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.
| |
| 110 true, // is_favicon | |
| 111 0, // unlimited | |
| 112 base::Bind(&RenderViewContextMenuBase::OnIconDownloaded, | |
| 113 weak_ptr_factory_.GetWeakPtr(), | |
| 114 command_id)); | |
| 115 } | |
| 116 | |
| 82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { | 117 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { |
| 83 LOG(ERROR) << "Custom menu action value out of range."; | 118 LOG(ERROR) << "Custom menu action value out of range."; |
| 84 return; | 119 return; |
| 85 } | 120 } |
| 86 if (*total_items >= kMaxCustomMenuTotalItems) { | 121 if (*total_items >= kMaxCustomMenuTotalItems) { |
| 87 LOG(ERROR) << "Custom menu too large (too many items)."; | 122 LOG(ERROR) << "Custom menu too large (too many items)."; |
| 88 return; | 123 return; |
| 89 } | 124 } |
| 90 (*total_items)++; | 125 (*total_items)++; |
| 91 switch (items[i].type) { | 126 switch (items[i].type) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 119 submenu); | 154 submenu); |
| 120 break; | 155 break; |
| 121 } | 156 } |
| 122 default: | 157 default: |
| 123 NOTREACHED(); | 158 NOTREACHED(); |
| 124 break; | 159 break; |
| 125 } | 160 } |
| 126 } | 161 } |
| 127 } | 162 } |
| 128 | 163 |
| 129 } // namespace | |
| 130 | |
| 131 // static | 164 // static |
| 132 void RenderViewContextMenuBase::SetContentCustomCommandIdRange( | 165 void RenderViewContextMenuBase::SetContentCustomCommandIdRange( |
| 133 int first, int last) { | 166 int first, int last) { |
| 134 // The range is inclusive. | 167 // The range is inclusive. |
| 135 content_context_custom_first = first; | 168 content_context_custom_first = first; |
| 136 content_context_custom_last = last; | 169 content_context_custom_last = last; |
| 137 } | 170 } |
| 138 | 171 |
| 139 // static | 172 // static |
| 140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50; | 173 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 152 | 185 |
| 153 RenderViewContextMenuBase::RenderViewContextMenuBase( | 186 RenderViewContextMenuBase::RenderViewContextMenuBase( |
| 154 content::RenderFrameHost* render_frame_host, | 187 content::RenderFrameHost* render_frame_host, |
| 155 const content::ContextMenuParams& params) | 188 const content::ContextMenuParams& params) |
| 156 : params_(params), | 189 : params_(params), |
| 157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), | 190 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), |
| 158 browser_context_(source_web_contents_->GetBrowserContext()), | 191 browser_context_(source_web_contents_->GetBrowserContext()), |
| 159 menu_model_(this), | 192 menu_model_(this), |
| 160 render_frame_id_(render_frame_host->GetRoutingID()), | 193 render_frame_id_(render_frame_host->GetRoutingID()), |
| 161 command_executed_(false), | 194 command_executed_(false), |
| 162 render_process_id_(render_frame_host->GetProcess()->GetID()) { | 195 render_process_id_(render_frame_host->GetProcess()->GetID()), |
| 196 weak_ptr_factory_(this) { | |
| 163 } | 197 } |
| 164 | 198 |
| 165 RenderViewContextMenuBase::~RenderViewContextMenuBase() { | 199 RenderViewContextMenuBase::~RenderViewContextMenuBase() { |
| 166 } | 200 } |
| 167 | 201 |
| 168 // Menu construction functions ------------------------------------------------- | 202 // Menu construction functions ------------------------------------------------- |
| 169 | 203 |
| 170 void RenderViewContextMenuBase::Init() { | 204 void RenderViewContextMenuBase::Init() { |
| 171 // Command id range must have been already initializerd. | 205 // Command id range must have been already initializerd. |
| 172 DCHECK_NE(-1, content_context_custom_first); | 206 DCHECK_NE(-1, content_context_custom_first); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 void RenderViewContextMenuBase::AddSeparator() { | 242 void RenderViewContextMenuBase::AddSeparator() { |
| 209 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 243 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 210 } | 244 } |
| 211 | 245 |
| 212 void RenderViewContextMenuBase::AddSubMenu(int command_id, | 246 void RenderViewContextMenuBase::AddSubMenu(int command_id, |
| 213 const base::string16& label, | 247 const base::string16& label, |
| 214 ui::MenuModel* model) { | 248 ui::MenuModel* model) { |
| 215 menu_model_.AddSubMenu(command_id, label, model); | 249 menu_model_.AddSubMenu(command_id, label, model); |
| 216 } | 250 } |
| 217 | 251 |
| 252 void RenderViewContextMenuBase::SetIcon(int command_id, | |
| 253 const gfx::Image& icon) { | |
| 254 if (toolkit_delegate_) | |
| 255 toolkit_delegate_->SetIcon(command_id, icon); | |
| 256 } | |
| 257 | |
| 218 void RenderViewContextMenuBase::UpdateMenuItem(int command_id, | 258 void RenderViewContextMenuBase::UpdateMenuItem(int command_id, |
| 219 bool enabled, | 259 bool enabled, |
| 220 bool hidden, | 260 bool hidden, |
| 221 const base::string16& label) { | 261 const base::string16& label) { |
| 222 if (toolkit_delegate_) { | 262 if (toolkit_delegate_) { |
| 223 toolkit_delegate_->UpdateMenuItem(command_id, | 263 toolkit_delegate_->UpdateMenuItem(command_id, |
| 224 enabled, | 264 enabled, |
| 225 hidden, | 265 hidden, |
| 226 label); | 266 label); |
| 227 } | 267 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 } | 420 } |
| 381 | 421 |
| 382 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const { | 422 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const { |
| 383 return IsCustomItemCheckedInternal(params_.custom_items, id); | 423 return IsCustomItemCheckedInternal(params_.custom_items, id); |
| 384 } | 424 } |
| 385 | 425 |
| 386 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const { | 426 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const { |
| 387 return IsCustomItemEnabledInternal(params_.custom_items, id); | 427 return IsCustomItemEnabledInternal(params_.custom_items, id); |
| 388 } | 428 } |
| 389 | 429 |
| OLD | NEW |