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" | |
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 Loading... | |
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 content::MenuItem item = icon_map_.find(id)->second; | |
82 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( | |
83 item.action); | |
84 gfx::Image* icon = new gfx::Image(gfx::Image::CreateFrom1xBitmap(bitmaps[0])); | |
lazyboy
2015/02/05 01:09:55
|icon| is leaked after this function returns, righ
pals
2015/02/05 09:01:02
Done.
| |
85 UpdateMenuItem(command_id, item.enabled, false, item.label, icon); | |
lazyboy
2015/02/05 01:09:55
The .enabled and .label can be retrieved from the
| |
86 } | |
87 | |
88 void RenderViewContextMenuBase::AddCustomItemsToMenu( | |
89 const std::vector<content::MenuItem>& items, | |
90 size_t depth, | |
91 size_t* total_items, | |
92 ui::SimpleMenuModel::Delegate* delegate, | |
93 ui::SimpleMenuModel* menu_model) { | |
75 if (depth > kMaxCustomMenuDepth) { | 94 if (depth > kMaxCustomMenuDepth) { |
76 LOG(ERROR) << "Custom menu too deeply nested."; | 95 LOG(ERROR) << "Custom menu too deeply nested."; |
77 return; | 96 return; |
78 } | 97 } |
79 for (size_t i = 0; i < items.size(); ++i) { | 98 for (size_t i = 0; i < items.size(); ++i) { |
80 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( | 99 int command_id = RenderViewContextMenuBase::ConvertToContentCustomCommandId( |
81 items[i].action); | 100 items[i].action); |
101 if (!items[i].icon.empty()) { | |
102 int download_id = source_web_contents_->DownloadImage( | |
103 GURL(items[i].icon), | |
lazyboy
2015/02/05 01:09:55
this URL is coming from the renderer/ process, so
pals
2015/02/05 09:01:02
I think we should put a max size limitation on the
| |
104 false, // is_favicon | |
105 0, // no max size | |
106 base::Bind(&RenderViewContextMenuBase::OnIconDownloaded, | |
107 weak_ptr_factory_.GetWeakPtr())); | |
108 icon_map_[download_id] = items[i]; | |
109 } | |
110 | |
82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { | 111 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id)) { |
83 LOG(ERROR) << "Custom menu action value out of range."; | 112 LOG(ERROR) << "Custom menu action value out of range."; |
84 return; | 113 return; |
85 } | 114 } |
86 if (*total_items >= kMaxCustomMenuTotalItems) { | 115 if (*total_items >= kMaxCustomMenuTotalItems) { |
87 LOG(ERROR) << "Custom menu too large (too many items)."; | 116 LOG(ERROR) << "Custom menu too large (too many items)."; |
88 return; | 117 return; |
89 } | 118 } |
90 (*total_items)++; | 119 (*total_items)++; |
91 switch (items[i].type) { | 120 switch (items[i].type) { |
(...skipping 27 matching lines...) Expand all Loading... | |
119 submenu); | 148 submenu); |
120 break; | 149 break; |
121 } | 150 } |
122 default: | 151 default: |
123 NOTREACHED(); | 152 NOTREACHED(); |
124 break; | 153 break; |
125 } | 154 } |
126 } | 155 } |
127 } | 156 } |
128 | 157 |
129 } // namespace | |
130 | |
131 // static | 158 // static |
132 void RenderViewContextMenuBase::SetContentCustomCommandIdRange( | 159 void RenderViewContextMenuBase::SetContentCustomCommandIdRange( |
133 int first, int last) { | 160 int first, int last) { |
134 // The range is inclusive. | 161 // The range is inclusive. |
135 content_context_custom_first = first; | 162 content_context_custom_first = first; |
136 content_context_custom_last = last; | 163 content_context_custom_last = last; |
137 } | 164 } |
138 | 165 |
139 // static | 166 // static |
140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50; | 167 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength = 50; |
(...skipping 11 matching lines...) Expand all Loading... | |
152 | 179 |
153 RenderViewContextMenuBase::RenderViewContextMenuBase( | 180 RenderViewContextMenuBase::RenderViewContextMenuBase( |
154 content::RenderFrameHost* render_frame_host, | 181 content::RenderFrameHost* render_frame_host, |
155 const content::ContextMenuParams& params) | 182 const content::ContextMenuParams& params) |
156 : params_(params), | 183 : params_(params), |
157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), | 184 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), |
158 browser_context_(source_web_contents_->GetBrowserContext()), | 185 browser_context_(source_web_contents_->GetBrowserContext()), |
159 menu_model_(this), | 186 menu_model_(this), |
160 render_frame_id_(render_frame_host->GetRoutingID()), | 187 render_frame_id_(render_frame_host->GetRoutingID()), |
161 command_executed_(false), | 188 command_executed_(false), |
162 render_process_id_(render_frame_host->GetProcess()->GetID()) { | 189 render_process_id_(render_frame_host->GetProcess()->GetID()), |
190 weak_ptr_factory_(this) { | |
163 } | 191 } |
164 | 192 |
165 RenderViewContextMenuBase::~RenderViewContextMenuBase() { | 193 RenderViewContextMenuBase::~RenderViewContextMenuBase() { |
166 } | 194 } |
167 | 195 |
168 // Menu construction functions ------------------------------------------------- | 196 // Menu construction functions ------------------------------------------------- |
169 | 197 |
170 void RenderViewContextMenuBase::Init() { | 198 void RenderViewContextMenuBase::Init() { |
171 // Command id range must have been already initializerd. | 199 // Command id range must have been already initializerd. |
172 DCHECK_NE(-1, content_context_custom_first); | 200 DCHECK_NE(-1, content_context_custom_first); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 | 239 |
212 void RenderViewContextMenuBase::AddSubMenu(int command_id, | 240 void RenderViewContextMenuBase::AddSubMenu(int command_id, |
213 const base::string16& label, | 241 const base::string16& label, |
214 ui::MenuModel* model) { | 242 ui::MenuModel* model) { |
215 menu_model_.AddSubMenu(command_id, label, model); | 243 menu_model_.AddSubMenu(command_id, label, model); |
216 } | 244 } |
217 | 245 |
218 void RenderViewContextMenuBase::UpdateMenuItem(int command_id, | 246 void RenderViewContextMenuBase::UpdateMenuItem(int command_id, |
219 bool enabled, | 247 bool enabled, |
220 bool hidden, | 248 bool hidden, |
221 const base::string16& label) { | 249 const base::string16& label, |
250 gfx::Image* icon) { | |
222 if (toolkit_delegate_) { | 251 if (toolkit_delegate_) { |
223 toolkit_delegate_->UpdateMenuItem(command_id, | 252 toolkit_delegate_->UpdateMenuItem(command_id, |
224 enabled, | 253 enabled, |
225 hidden, | 254 hidden, |
226 label); | 255 label, |
256 icon); | |
227 } | 257 } |
228 } | 258 } |
229 | 259 |
230 RenderViewHost* RenderViewContextMenuBase::GetRenderViewHost() const { | 260 RenderViewHost* RenderViewContextMenuBase::GetRenderViewHost() const { |
231 return source_web_contents_->GetRenderViewHost(); | 261 return source_web_contents_->GetRenderViewHost(); |
232 } | 262 } |
233 | 263 |
234 WebContents* RenderViewContextMenuBase::GetWebContents() const { | 264 WebContents* RenderViewContextMenuBase::GetWebContents() const { |
235 return source_web_contents_; | 265 return source_web_contents_; |
236 } | 266 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 } | 410 } |
381 | 411 |
382 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const { | 412 bool RenderViewContextMenuBase::IsCustomItemChecked(int id) const { |
383 return IsCustomItemCheckedInternal(params_.custom_items, id); | 413 return IsCustomItemCheckedInternal(params_.custom_items, id); |
384 } | 414 } |
385 | 415 |
386 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const { | 416 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id) const { |
387 return IsCustomItemEnabledInternal(params_.custom_items, id); | 417 return IsCustomItemEnabledInternal(params_.custom_items, id); |
388 } | 418 } |
389 | 419 |
OLD | NEW |