OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/tab_contents/render_view_context_menu.h" | 5 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
6 | 6 |
7 #include "app/clipboard/clipboard.h" | 7 #include "app/clipboard/clipboard.h" |
8 #include "app/clipboard/scoped_clipboard_writer.h" | 8 #include "app/clipboard/scoped_clipboard_writer.h" |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "chrome/common/pref_service.h" | 29 #include "chrome/common/pref_service.h" |
30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
31 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
32 #include "net/base/escape.h" | 32 #include "net/base/escape.h" |
33 #include "webkit/api/public/WebMediaPlayerAction.h" | 33 #include "webkit/api/public/WebMediaPlayerAction.h" |
34 #include "webkit/api/public/WebContextMenuData.h" | 34 #include "webkit/api/public/WebContextMenuData.h" |
35 | 35 |
36 using WebKit::WebContextMenuData; | 36 using WebKit::WebContextMenuData; |
37 using WebKit::WebMediaPlayerAction; | 37 using WebKit::WebMediaPlayerAction; |
38 | 38 |
39 namespace { | |
40 | |
41 string16 EscapeAmpersands(const string16& text) { | |
42 string16 ret; | |
43 ret.reserve(text.length() * 2); | |
44 for (string16::const_iterator i = text.begin(); | |
45 i != text.end(); ++i) { | |
46 // The escape for an ampersand is two ampersands. | |
47 if ('&' == *i) | |
48 ret.push_back(*i); | |
49 | |
50 ret.push_back(*i); | |
51 } | |
52 | |
53 return ret; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 RenderViewContextMenu::RenderViewContextMenu( | 39 RenderViewContextMenu::RenderViewContextMenu( |
59 TabContents* tab_contents, | 40 TabContents* tab_contents, |
60 const ContextMenuParams& params) | 41 const ContextMenuParams& params) |
61 : params_(params), | 42 : params_(params), |
62 source_tab_contents_(tab_contents), | 43 source_tab_contents_(tab_contents), |
63 profile_(tab_contents->profile()) { | 44 profile_(tab_contents->profile()) { |
64 } | 45 } |
65 | 46 |
66 RenderViewContextMenu::~RenderViewContextMenu() { | 47 RenderViewContextMenu::~RenderViewContextMenu() { |
67 } | 48 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 AppendMenuItem(IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE); | 192 AppendMenuItem(IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE); |
212 AppendMenuItem(IDS_CONTENT_CONTEXT_VIEWFRAMEINFO); | 193 AppendMenuItem(IDS_CONTENT_CONTEXT_VIEWFRAMEINFO); |
213 } | 194 } |
214 | 195 |
215 void RenderViewContextMenu::AppendCopyItem() { | 196 void RenderViewContextMenu::AppendCopyItem() { |
216 AppendMenuItem(IDS_CONTENT_CONTEXT_COPY); | 197 AppendMenuItem(IDS_CONTENT_CONTEXT_COPY); |
217 } | 198 } |
218 | 199 |
219 void RenderViewContextMenu::AppendSearchProvider() { | 200 void RenderViewContextMenu::AppendSearchProvider() { |
220 DCHECK(profile_); | 201 DCHECK(profile_); |
221 const TemplateURL* const default_provider = | |
222 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); | |
223 if (!default_provider) | |
224 return; | |
225 | 202 |
226 string16 selection_text = EscapeAmpersands(WideToUTF16( | 203 if (params_.selection_text.empty()) |
227 l10n_util::TruncateString(params_.selection_text, 50))); | |
228 if (selection_text.empty()) | |
229 return; | 204 return; |
230 | 205 |
231 bool is_search; | 206 bool is_search; |
232 profile_->GetSearchVersusNavigateClassifier()->Classify( | 207 profile_->GetSearchVersusNavigateClassifier()->Classify( |
233 UTF16ToWide(selection_text), std::wstring(), &is_search, | 208 params_.selection_text, std::wstring(), &is_search, |
234 &selection_navigation_url_, &transition_, NULL, NULL); | 209 &selection_navigation_url_, &transition_, NULL, NULL); |
235 if (!selection_navigation_url_.is_valid()) | 210 if (!selection_navigation_url_.is_valid()) |
236 return; | 211 return; |
237 | 212 |
| 213 string16 printable_selection_text( |
| 214 WideToUTF16(l10n_util::TruncateString(params_.selection_text, 50))); |
| 215 // Escape "&" as "&&". |
| 216 for (size_t i = printable_selection_text.find('&'); i != string16::npos; |
| 217 i = printable_selection_text.find('&', i + 2)) |
| 218 printable_selection_text.insert(i, 1, '&'); |
| 219 |
238 if (is_search) { | 220 if (is_search) { |
239 string16 label(l10n_util::GetStringFUTF16( | 221 const TemplateURL* const default_provider = |
240 IDS_CONTENT_CONTEXT_SEARCHWEBFOR, | 222 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); |
241 WideToUTF16(default_provider->short_name()), | 223 if (!default_provider) |
242 selection_text)); | 224 return; |
243 AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); | 225 AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 226 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 227 WideToUTF16(default_provider->short_name()), |
| 228 printable_selection_text)); |
244 } else { | 229 } else { |
245 string16 label(l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, | 230 AppendMenuItem(IDS_CONTENT_CONTEXT_GOTOURL, |
246 selection_text)); | 231 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, |
247 AppendMenuItem(IDS_CONTENT_CONTEXT_GOTOURL, label); | 232 printable_selection_text)); |
248 } | 233 } |
249 } | 234 } |
250 | 235 |
251 void RenderViewContextMenu::AppendEditableItems() { | 236 void RenderViewContextMenu::AppendEditableItems() { |
252 // Append Dictionary spell check suggestions. | 237 // Append Dictionary spell check suggestions. |
253 for (size_t i = 0; i < params_.dictionary_suggestions.size() && | 238 for (size_t i = 0; i < params_.dictionary_suggestions.size() && |
254 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; | 239 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; |
255 ++i) { | 240 ++i) { |
256 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), | 241 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), |
257 params_.dictionary_suggestions[i]); | 242 params_.dictionary_suggestions[i]); |
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 810 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
826 g_browser_process->clipboard()); | 811 g_browser_process->clipboard()); |
827 } | 812 } |
828 | 813 |
829 void RenderViewContextMenu::MediaPlayerActionAt( | 814 void RenderViewContextMenu::MediaPlayerActionAt( |
830 const gfx::Point& location, | 815 const gfx::Point& location, |
831 const WebMediaPlayerAction& action) { | 816 const WebMediaPlayerAction& action) { |
832 source_tab_contents_->render_view_host()->MediaPlayerActionAt( | 817 source_tab_contents_->render_view_host()->MediaPlayerActionAt( |
833 location, action); | 818 location, action); |
834 } | 819 } |
OLD | NEW |